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...[; getunstoredfun])
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`.
45+
46+ This constructor does not take ownership of the supplied storage, and will result in an
47+ independent container.
48+ """
49+ sparse (:: Union{AbstractDict,AbstractDictionary} , dims... ; kwargs... )
50+
51+ const AbstractDictOrDictionary = Union{AbstractDict,AbstractDictionary}
52+ # checked constructor from data: use `setindex!` to validate/convert input
53+ function sparse (storage:: AbstractDictOrDictionary , dims:: Dims ; kwargs... )
54+ A = SparseArrayDOK {eltype(storage)} (undef, dims; kwargs... )
55+ for (i, v) in pairs (storage)
56+ A[i] = v
57+ end
58+ return A
59+ end
60+ function sparse (storage:: AbstractDictOrDictionary , dims:: Int... ; kwargs... )
61+ return sparse (storage, dims; kwargs... )
62+ end
63+
3664using Random: Random, AbstractRNG, default_rng
3765
3866@doc """
39- sparsezeros([T::Type], dims) -> A::SparseArrayDOK{T}
67+ sparsezeros([T::Type], dims[; getunstoredfun] ) -> A::SparseArrayDOK{T}
4068
4169Create an empty size `dims` sparse array.
4270The optional `T` argument specifies the element type, which defaults to `Float64`.
4371""" sparsezeros
4472
45- sparsezeros (dims:: Dims ) = sparsezeros (Float64, dims)
46- sparsezeros (:: Type{T} , dims:: Dims ) where {T} = SparseArrayDOK {T} (undef, dims)
73+ function sparsezeros (:: Type{T} , dims:: Dims ; kwargs... ) where {T}
74+ return SparseArrayDOK {T} (undef, dims; kwargs... )
75+ end
76+ sparsezeros (:: Type{T} , dims:: Int... ; kwargs... ) where {T} = sparsezeros (T, dims; kwargs... )
77+ sparsezeros (dims:: Dims ; kwargs... ) = sparsezeros (Float64, dims; kwargs... )
78+ sparsezeros (dims:: Int... ; kwargs... ) = sparsezeros (Float64, dims; kwargs... )
4779
4880@doc """
4981 sparserand([rng], [T::Type], dims; density::Real=0.5, randfun::Function=rand) -> A::SparseArrayDOK{T}
@@ -61,15 +93,25 @@ See also [`sparserand!`](@ref).
6193function sparserand (:: Type{T} , dims:: Dims ; kwargs... ) where {T}
6294 return sparserand (default_rng (), T, dims; kwargs... )
6395end
96+ function sparserand (:: Type{T} , dims:: Int... ; kwargs... ) where {T}
97+ return sparserand (T, dims; kwargs... )
98+ end
6499sparserand (dims:: Dims ; kwargs... ) = sparserand (default_rng (), Float64, dims; kwargs... )
100+ sparserand (dims:: Int... ; kwargs... ) = sparserand (dims; kwargs... )
65101function sparserand (rng:: AbstractRNG , dims:: Dims ; kwargs... )
66102 return sparserand (rng, Float64, dims; kwargs... )
67103end
104+ function sparserand (rng:: AbstractRNG , dims:: Int... ; kwargs... )
105+ return sparserand (rng, dims; kwargs... )
106+ end
68107function sparserand (rng:: AbstractRNG , :: Type{T} , dims:: Dims ; kwargs... ) where {T}
69108 A = SparseArrayDOK {T} (undef, dims)
70109 sparserand! (rng, A; kwargs... )
71110 return A
72111end
112+ function sparserand (rng:: AbstractRNG , :: Type{T} , dims:: Int... ; kwargs... ) where {T}
113+ return sparserand (rng, T, dims; kwargs... )
114+ end
73115
74116@doc """
75117 sparserand!([rng], A::AbstractArray; density::Real=0.5, randfun::Function=rand) -> A
0 commit comments