diff --git a/src/broadcast.jl b/src/broadcast.jl index d4e4e12..a66d5c6 100644 --- a/src/broadcast.jl +++ b/src/broadcast.jl @@ -5,7 +5,7 @@ # ------------------------------------------------------------------------------ """ - SparseBroadcastStyle{N,K} <: Broadcast.BroadcastStyle + SparseBroadcastStyle{K} <: Broadcast.BroadcastStyle Broadcasting style for all `AbstractSparseArray` subtypes. `K` is the key tuple type. All broadcast results are materialised as `SparseArray`. diff --git a/src/indexedarray.jl b/src/indexedarray.jl index edc708d..10ee534 100644 --- a/src/indexedarray.jl +++ b/src/indexedarray.jl @@ -160,12 +160,13 @@ end function _getcache(sa::IndexedVarArray{V,N,T}, pat::P) where {V,N,T,P} t = _get_cache_index(pat) - if isassigned(sa.index_cache, t) - return sa.index_cache[t] + idx = t + 1 + if isassigned(sa.index_cache, idx) + return sa.index_cache[idx] else - sa.index_cache[t] = Dictionary{_decode_nonslices(sa, t),Vector{T}}() + sa.index_cache[idx] = Dictionary{_decode_nonslices(sa, t),Vector{T}}() end - return sa.index_cache[t] + return sa.index_cache[idx] end # Extension for standard JuMP macros diff --git a/test/runtests.jl b/test/runtests.jl index 2873510..fb9ad42 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -222,9 +222,9 @@ end @test length(z3["bmw", :, :, :]) == length(filter(x -> x[1] == "bmw", indices)) - @test length(z3.index_cache[4]) == length(unique(i[2] for i in indices)) + @test length(z3.index_cache[5]) == length(unique(i[2] for i in indices)) SparseVariables.clear_cache!(z3) - @test length(z3.index_cache[4]) == 0 + @test length(z3.index_cache[5]) == 0 # Begin/End @test length(z3[:, begin:2000, :, :]) == @@ -235,6 +235,39 @@ end length(filter(x -> x[2] >= 1990 && x[2] <= 2000, indices)) end +@testset "IndexedVarArray all-colon cache" begin + (; cars, year, car_cost) = testdata1(false) + m = Model() + @variable(m, x[c=cars, y=year]; container = IndexedVarArray) + for k in keys(car_cost) + insertvar!(x, k...) + end + n = length(x) # 4 entries + + old_cutoff = SV._CACHE_CUTOFF + try + SV.set_cache_cutoff!(0) # force the cached path for any size + + @test length(SV.select(x, :, :)) == n + + sl = x[:, :] + @test sl isa SparseArraySlice + @test length(sl) == n + @test sum(sl) isa AffExpr + + sl2 = slice(x, :, :) + @test length(sl2) == n + + @test length(SV.select(x, "ford", :)) == 2 + @test length(SV.select(x, :, 2001)) == 2 + + @test isassigned(x.index_cache, 1) + @test !isempty(x.index_cache[1]) + finally + SV.set_cache_cutoff!(old_cutoff) + end +end + @testset "Tables IndexedVarArray" begin (; cars, year, car_cost) = testdata1(false)