Skip to content

Commit 3098340

Browse files
authored
Merge pull request #48 from sintefore/tf/eachindex
Add eachindex method for AbstractSparseArray and update tests
2 parents 3590ebd + b2a942e commit 3098340

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

src/sparsearray.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ function Base.show(io::IO, ::MIME"text/plain", sa::AbstractSparseArray)
6363
end
6464
Base.show(io::IO, sa::AbstractSparseArray) = show(io, _data(sa))
6565

66+
Base.eachindex(sa::AbstractSparseArray) = keys(_data(sa))
67+
6668
function Base.firstindex(sa::AbstractSparseArray, d)
6769
return minimum(x -> x[d], _data(sa).indices)
6870
end
@@ -89,7 +91,7 @@ end
8991
"""
9092
SparseArray{T,N, K <: NTuple{N,Any} }
9193
Implementation of an AbstractSparseArray where data is stored
92-
in a dictionary.
94+
in a dictionary.
9395
"""
9496
struct SparseArray{T,N,K<:NTuple{N,Any}} <: AbstractSparseArray{T,N}
9597
data::Dictionary{K,T}

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ end
102102
@test typeof(SparseArray{Int,3,NTuple{3,String}}()) ==
103103
SparseArray{Int,3,Tuple{String,String,String}}
104104
@test length(SparseArray{Int,3,Tuple{String,String,String}}()) == 0
105+
106+
# indexing
107+
indices = eachindex(car_cost)
108+
@test length(indices) == 5
109+
@test car_cost[first(indices)] == 200
105110
end
106111

107112
@testset "Repurposed from SparseVarArray" begin

0 commit comments

Comments
 (0)