|
1 | 1 | using Accessors: @set |
2 | 2 | using Dictionaries: Dictionary, set! |
| 3 | +using MacroTools: @capture |
3 | 4 | using ..SparseArrayInterface: |
4 | 5 | SparseArrayInterface, AbstractSparseArray, getindex_zero_function |
5 | 6 |
|
6 | 7 | # TODO: Parametrize by `data`? |
7 | 8 | struct SparseArrayDOK{T,N,Zero} <: AbstractSparseArray{T,N} |
8 | 9 | data::Dictionary{CartesianIndex{N},T} |
9 | | - dims::NTuple{N,Int} |
| 10 | + dims::Ref{NTuple{N,Int}} |
10 | 11 | zero::Zero |
| 12 | + function SparseArrayDOK{T,N,Zero}(data, dims::NTuple{N,Int}, zero) where {T,N,Zero} |
| 13 | + return new{T,N,Zero}(data, Ref(dims), zero) |
| 14 | + end |
11 | 15 | end |
12 | 16 |
|
13 | 17 | # Constructors |
| 18 | +function SparseArrayDOK(data, dims::Tuple{Vararg{Int}}, zero) |
| 19 | + return SparseArrayDOK{eltype(data),length(dims),typeof(zero)}(data, dims, zero) |
| 20 | +end |
| 21 | + |
14 | 22 | function SparseArrayDOK{T,N,Zero}(dims::Tuple{Vararg{Int}}, zero) where {T,N,Zero} |
15 | 23 | return SparseArrayDOK{T,N,Zero}(default_data(T, N), dims, zero) |
16 | 24 | end |
@@ -72,7 +80,7 @@ function SparseArrayDOK{T}(::UndefInitializer, dims::Tuple{Vararg{Int}}, zero) w |
72 | 80 | end |
73 | 81 |
|
74 | 82 | # Base `AbstractArray` interface |
75 | | -Base.size(a::SparseArrayDOK) = a.dims |
| 83 | +Base.size(a::SparseArrayDOK) = a.dims[] |
76 | 84 |
|
77 | 85 | SparseArrayInterface.getindex_zero_function(a::SparseArrayDOK) = a.zero |
78 | 86 | function SparseArrayInterface.set_getindex_zero_function(a::SparseArrayDOK, f) |
@@ -104,3 +112,37 @@ SparseArrayDOK{T}(a::AbstractArray) where {T} = SparseArrayDOK{T,ndims(a)}(a) |
104 | 112 | function SparseArrayDOK{T,N}(a::AbstractArray) where {T,N} |
105 | 113 | return SparseArrayInterface.sparse_convert(SparseArrayDOK{T,N}, a) |
106 | 114 | end |
| 115 | + |
| 116 | +function Base.resize!(a::SparseArrayDOK{<:Any,N}, new_size::NTuple{N,Integer}) where {N} |
| 117 | + a.dims[] = new_size |
| 118 | + return a |
| 119 | +end |
| 120 | + |
| 121 | +function setindex_maybe_grow!(a::SparseArrayDOK{<:Any,N}, value, I::Vararg{Int,N}) where {N} |
| 122 | + if any(I .> size(a)) |
| 123 | + resize!(a, max.(I, size(a))) |
| 124 | + end |
| 125 | + a[I...] = value |
| 126 | + return a |
| 127 | +end |
| 128 | + |
| 129 | +function is_setindex!_expr(expr::Expr) |
| 130 | + return is_assignment_expr(expr) && is_getindex_expr(first(expr.args)) |
| 131 | +end |
| 132 | +is_setindex!_expr(x) = false |
| 133 | + |
| 134 | +is_getindex_expr(expr::Expr) = (expr.head === :ref) |
| 135 | +is_getindex_expr(x) = false |
| 136 | + |
| 137 | +is_assignment_expr(expr::Expr) = (expr.head === :(=)) |
| 138 | +is_assignment_expr(expr) = false |
| 139 | + |
| 140 | +macro maybe_grow(expr) |
| 141 | + if !is_setindex!_expr(expr) |
| 142 | + error( |
| 143 | + "@maybe_grow must be used with setindex! syntax (as @maybe_grow a[i,j,...] = value)" |
| 144 | + ) |
| 145 | + end |
| 146 | + @capture(expr, array_[indices__] = value_) |
| 147 | + return :(setindex_maybe_grow!($(esc(array)), $value, $indices...)) |
| 148 | +end |
0 commit comments