Skip to content

Commit 3fce692

Browse files
committed
Determine size automatically in sparse
1 parent d1123fb commit 3fce692

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

src/abstractsparsearray.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ end
4141
4242
Construct an `N`-dimensional [`SparseArrayDOK`](@ref) containing elements of type `T`. Both
4343
`T` and `N` can either be supplied explicitly or be determined by the `storage` and the
44-
length or number of `dims`.
44+
length or number of `dims`. If `dims` aren't specified, the size will be determined automatically
45+
from the input indices.
4546
4647
This constructor does not take ownership of the supplied storage, and will result in an
4748
independent container.
@@ -60,6 +61,14 @@ end
6061
function sparse(storage::AbstractDictOrDictionary, dims::Int...; kwargs...)
6162
return sparse(storage, dims; kwargs...)
6263
end
64+
# Determine the size automatically.
65+
function sparse(storage::AbstractDictOrDictionary; kwargs...)
66+
dims = ntuple(Returns(0), length(keytype(storage)))
67+
for I in keys(storage)
68+
dims = map(max, dims, Tuple(I))
69+
end
70+
return sparse(storage, dims; kwargs...)
71+
end
6372

6473
using Random: Random, AbstractRNG, default_rng
6574

test/test_sparsearraydok.jl

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Adapt: adapt
22
using ArrayLayouts: zero!
3+
using Dictionaries: Dictionary
34
using JLArrays: JLArray, @allowscalar
45
using SparseArraysBase:
56
SparseArraysBase,
@@ -222,22 +223,29 @@ arrayts = (Array,)
222223
@test a == [11 12; 12 22]
223224
@test storedlength(a) == 4
224225

225-
d = Dict([CartesianIndex(1, 2) => elt(12), CartesianIndex(2, 1) => elt(21)])
226-
for a in (
227-
sparse(d, 2, 2),
228-
sparse(d, 2, 2; getunstored=Returns(zero(elt))),
229-
sparse(d, (2, 2)),
230-
sparse(d, (2, 2); getunstored=Returns(zero(elt))),
226+
for d in (
227+
Dict([CartesianIndex(1, 2) => elt(12), CartesianIndex(2, 1) => elt(21)]),
228+
Dictionary([CartesianIndex(1, 2), CartesianIndex(2, 1)], [elt(12), elt(21)]),
231229
)
232-
@test !iszero(a)
233-
@test iszero(a[1, 1])
234-
@test a[2, 1] == elt(21)
235-
@test a[1, 2] == elt(12)
236-
@test iszero(a[2, 2])
237-
@test size(a) == (2, 2)
238-
@test storedlength(a) == 2
239-
@test eltype(a) === elt
240-
@test a isa SparseMatrixDOK{elt}
230+
for a in (
231+
sparse(d, 2, 2),
232+
sparse(d, 2, 2; getunstored=Returns(zero(elt))),
233+
sparse(d, (2, 2)),
234+
sparse(d, (2, 2); getunstored=Returns(zero(elt))),
235+
# Determine the size automatically.
236+
sparse(d),
237+
sparse(d; getunstored=Returns(zero(elt))),
238+
)
239+
@test !iszero(a)
240+
@test iszero(a[1, 1])
241+
@test a[2, 1] == elt(21)
242+
@test a[1, 2] == elt(12)
243+
@test iszero(a[2, 2])
244+
@test size(a) == (2, 2)
245+
@test storedlength(a) == 2
246+
@test eltype(a) === elt
247+
@test a isa SparseMatrixDOK{elt}
248+
end
241249
end
242250

243251
for (a, elt′) in (

0 commit comments

Comments
 (0)