Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
9 changes: 5 additions & 4 deletions src/indexedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 35 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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, :, :]) ==
Expand All @@ -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)

Expand Down
Loading