Skip to content

Commit c5c53ee

Browse files
Allow SparseContainers to work with Arrays
1 parent 52c9ff0 commit c5c53ee

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/sparse_containers.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ struct SparseContainer{SIM, T}
2525
function SparseContainer(
2626
compressed_data::T,
2727
sparse_index_map::Tuple
28-
) where {N, ET, T <: NTuple{N, ET}}
28+
) where {T}
2929
@assert all(map(x-> eltype(compressed_data) .== typeof(x), compressed_data))
3030
return new{sparse_index_map, T}(compressed_data)
3131
end
3232
end
3333

3434
Base.parent(sc::SparseContainer) = sc.data
3535
sc_eltype(::Type{NTuple{N, T}}) where {N, T} = T
36+
sc_eltype(::Type{T}) where {ET, T <: AbstractArray{ET}} = ET
3637
sc_eltype(::SparseContainer{SIM, T}) where {SIM, T} = sc_eltype(T)
3738
@inline function Base.getindex(sc::SparseContainer, i::Int)
3839
return _getindex_sparse(sc, Val(i))::sc_eltype(sc)

test/sparse_containers.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,24 @@ using Test
1919

2020
@test_throws ErrorException("No index 2 found in sparse index map (1, 3, 5, 7)") v[2]
2121
@test_throws ErrorException("No index 8 found in sparse index map (1, 3, 5, 7)") v[8]
22+
@inferred v[7]
23+
24+
a1 = ones(3) .* 1
25+
a2 = ones(3) .* 2
26+
a3 = ones(3) .* 3
27+
a4 = ones(3) .* 4
28+
v = SparseContainer([a1,a2,a3,a4], (1,3,5,7))
29+
@test v[1] == ones(3) .* 1
30+
@test v[3] == ones(3) .* 2
31+
@test v[5] == ones(3) .* 3
32+
@test v[7] == ones(3) .* 4
33+
34+
@test parent(v)[1] == ones(3) .* 1
35+
@test parent(v)[2] == ones(3) .* 2
36+
@test parent(v)[3] == ones(3) .* 3
37+
@test parent(v)[4] == ones(3) .* 4
38+
39+
@test_throws ErrorException("No index 2 found in sparse index map (1, 3, 5, 7)") v[2]
40+
@test_throws ErrorException("No index 8 found in sparse index map (1, 3, 5, 7)") v[8]
41+
@inferred v[7]
2242
end

0 commit comments

Comments
 (0)