From b2a942ee9f2cada1fdf546bfb3f4864243fe2c63 Mon Sep 17 00:00:00 2001 From: Truls Flatberg Date: Thu, 27 Feb 2025 10:36:47 +0100 Subject: [PATCH] Add eachindex method for AbstractSparseArray and update tests --- src/sparsearray.jl | 4 +++- test/runtests.jl | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/sparsearray.jl b/src/sparsearray.jl index 506fe21..5f6d9a7 100644 --- a/src/sparsearray.jl +++ b/src/sparsearray.jl @@ -63,6 +63,8 @@ function Base.show(io::IO, ::MIME"text/plain", sa::AbstractSparseArray) end Base.show(io::IO, sa::AbstractSparseArray) = show(io, _data(sa)) +Base.eachindex(sa::AbstractSparseArray) = keys(_data(sa)) + function Base.firstindex(sa::AbstractSparseArray, d) return minimum(x -> x[d], _data(sa).indices) end @@ -89,7 +91,7 @@ end """ SparseArray{T,N, K <: NTuple{N,Any} } Implementation of an AbstractSparseArray where data is stored -in a dictionary. +in a dictionary. """ struct SparseArray{T,N,K<:NTuple{N,Any}} <: AbstractSparseArray{T,N} data::Dictionary{K,T} diff --git a/test/runtests.jl b/test/runtests.jl index 9b346a4..742a538 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -102,6 +102,11 @@ end @test typeof(SparseArray{Int,3,NTuple{3,String}}()) == SparseArray{Int,3,Tuple{String,String,String}} @test length(SparseArray{Int,3,Tuple{String,String,String}}()) == 0 + + # indexing + indices = eachindex(car_cost) + @test length(indices) == 5 + @test car_cost[first(indices)] == 200 end @testset "Repurposed from SparseVarArray" begin