1+ using Dictionaries: AbstractDictionary
2+
13abstract type AbstractSparseArray{T,N} <: AbstractArray{T,N} end
24
35using DerivableInterfaces: @array_aliases
3335
3436# Special-purpose constructors
3537# ----------------------------
38+
39+ """
40+ sparse(storage::Union{AbstractDict,AbstractDictionary}, dims...[; getunstored])
41+
42+ Construct an `N`-dimensional [`SparseArrayDOK`](@ref) containing elements of type `T`. Both
43+ `T` and `N` can either be supplied explicitly or be determined by the `storage` and the
44+ length or number of `dims`. If `dims` aren't specified, the size will be determined automatically
45+ from the input indices.
46+
47+ This constructor does not take ownership of the supplied storage, and will result in an
48+ independent container.
49+ """
50+ sparse (:: Union{AbstractDict,AbstractDictionary} , dims... ; kwargs... )
51+
52+ const AbstractDictOrDictionary = Union{AbstractDict,AbstractDictionary}
53+ # checked constructor from data: use `setindex!` to validate/convert input
54+ function sparse (storage:: AbstractDictOrDictionary , dims:: Dims ; kwargs... )
55+ A = SparseArrayDOK {valtype(storage)} (undef, dims; kwargs... )
56+ for (i, v) in pairs (storage)
57+ A[i] = v
58+ end
59+ return A
60+ end
61+ function sparse (storage:: AbstractDictOrDictionary , dims:: Int... ; kwargs... )
62+ return sparse (storage, dims; kwargs... )
63+ 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
72+
3673using Random: Random, AbstractRNG, default_rng
3774
3875@doc """
39- sparsezeros([T::Type], dims) -> A::SparseArrayDOK{T}
76+ sparsezeros([T::Type], dims[; getunstored] ) -> A::SparseArrayDOK{T}
4077
4178Create an empty size `dims` sparse array.
4279The optional `T` argument specifies the element type, which defaults to `Float64`.
4380""" sparsezeros
4481
45- sparsezeros (dims:: Dims ) = sparsezeros (Float64, dims)
46- sparsezeros (:: Type{T} , dims:: Dims ) where {T} = SparseArrayDOK {T} (undef, dims)
82+ function sparsezeros (:: Type{T} , dims:: Dims ; kwargs... ) where {T}
83+ return SparseArrayDOK {T} (undef, dims; kwargs... )
84+ end
85+ sparsezeros (:: Type{T} , dims:: Int... ; kwargs... ) where {T} = sparsezeros (T, dims; kwargs... )
86+ sparsezeros (dims:: Dims ; kwargs... ) = sparsezeros (Float64, dims; kwargs... )
87+ sparsezeros (dims:: Int... ; kwargs... ) = sparsezeros (Float64, dims; kwargs... )
4788
4889@doc """
4990 sparserand([rng], [T::Type], dims; density::Real=0.5, randfun::Function=rand) -> A::SparseArrayDOK{T}
@@ -61,15 +102,25 @@ See also [`sparserand!`](@ref).
61102function sparserand (:: Type{T} , dims:: Dims ; kwargs... ) where {T}
62103 return sparserand (default_rng (), T, dims; kwargs... )
63104end
105+ function sparserand (:: Type{T} , dims:: Int... ; kwargs... ) where {T}
106+ return sparserand (T, dims; kwargs... )
107+ end
64108sparserand (dims:: Dims ; kwargs... ) = sparserand (default_rng (), Float64, dims; kwargs... )
109+ sparserand (dims:: Int... ; kwargs... ) = sparserand (dims; kwargs... )
65110function sparserand (rng:: AbstractRNG , dims:: Dims ; kwargs... )
66111 return sparserand (rng, Float64, dims; kwargs... )
67112end
113+ function sparserand (rng:: AbstractRNG , dims:: Int... ; kwargs... )
114+ return sparserand (rng, dims; kwargs... )
115+ end
68116function sparserand (rng:: AbstractRNG , :: Type{T} , dims:: Dims ; kwargs... ) where {T}
69117 A = SparseArrayDOK {T} (undef, dims)
70118 sparserand! (rng, A; kwargs... )
71119 return A
72120end
121+ function sparserand (rng:: AbstractRNG , :: Type{T} , dims:: Int... ; kwargs... ) where {T}
122+ return sparserand (rng, T, dims; kwargs... )
123+ end
73124
74125@doc """
75126 sparserand!([rng], A::AbstractArray; density::Real=0.5, randfun::Function=rand) -> A
0 commit comments