Skip to content

Commit 13d0478

Browse files
Add type assert to SparseContainers for inference
1 parent e9e237c commit 13d0478

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/sparse_containers.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
SparseContainer(compressed_data, sparse_index_map)
44
55
A compact container that allows dense-like
6-
indexing into a sparse container.
6+
indexing into a sparse, uniform, container.
77
88
# Example
99
@@ -17,18 +17,22 @@ v = SparseContainer((a1,a2,a3,a4), (1,3,5,7))
1717
@test v[1] == ones(3) .* 1
1818
@test v[3] == ones(3) .* 2
1919
@test v[5] == ones(3) .* 3
20-
@test v[7] == ones(3) .* 4```
20+
@test v[7] == ones(3) .* 4
21+
```
2122
"""
22-
struct SparseContainer{T,SIM}
23+
struct SparseContainer{ET, SIM, T}
2324
data::T
2425
function SparseContainer(compressed_data::T, sparse_index_map::Tuple) where {T}
25-
return new{T, sparse_index_map}(compressed_data)
26+
@assert all(map(x-> eltype(compressed_data) .== typeof(x), compressed_data))
27+
return new{eltype(compressed_data), sparse_index_map, T}(compressed_data)
2628
end
2729
end
2830

2931
Base.parent(sc::SparseContainer) = sc.data
30-
@inline Base.getindex(sc::SparseContainer, i::Int) = _getindex_sparse(sc, Val(i))
31-
@generated function _getindex_sparse(sc::SparseContainer{T,SIM}, ::Val{i}) where {T, SIM, i}
32+
@inline function Base.getindex(sc::SparseContainer{ET}, i::Int) where {ET}
33+
return _getindex_sparse(sc, Val(i))::ET
34+
end
35+
@generated function _getindex_sparse(sc::SparseContainer{ET,SIM}, ::Val{i})::ET where {ET, SIM, i}
3236
j = findfirst(k -> k == i, SIM)
3337
j == nothing && error("No index $i found in sparse index map $(SIM)")
3438
return :(sc.data[$j])

0 commit comments

Comments
 (0)