|
2 | 2 | getindex(a::StaticArray, i::Int) = error("getindex(::$typeof(a), ::Int) is not defined.")
|
3 | 3 | setindex!(a::StaticArray, value, i::Int) = error("setindex!(::$(typeof(a)), value, ::Int) is not defined.")
|
4 | 4 |
|
5 |
| -###################### |
6 |
| -## Scalar Indexing ## |
7 |
| -###################### |
| 5 | +####################################### |
| 6 | +## Multidimensional scalar indexing ## |
| 7 | +####################################### |
8 | 8 |
|
9 | 9 | # Note: all indexing behavior defaults to dense, linear indexing
|
10 | 10 |
|
|
48 | 48 | end
|
49 | 49 | end
|
50 | 50 |
|
51 |
| - |
52 |
| - |
53 | 51 | #########################
|
54 | 52 | ## Indexing utilities ##
|
55 | 53 | #########################
|
@@ -79,11 +77,109 @@ _ind(i::Int, ::Int, ::Type{Int}) = :(inds[$i])
|
79 | 77 | _ind(i::Int, j::Int, ::Type{<:StaticArray}) = :(inds[$i][$j])
|
80 | 78 | _ind(i::Int, j::Int, ::Type{Colon}) = j
|
81 | 79 |
|
| 80 | +################################ |
| 81 | +## Non-scalar linear indexing ## |
| 82 | +################################ |
| 83 | + |
| 84 | +@inline function getindex(a::StaticArray, ::Colon) |
| 85 | + _getindex(a::StaticArray, Length(a), :) |
| 86 | +end |
| 87 | + |
| 88 | +@generated function _getindex(a::StaticArray, ::Length{L}, ::Colon) where {L} |
| 89 | + exprs = [:(a[$i]) for i = 1:L] |
| 90 | + return quote |
| 91 | + @_inline_meta |
| 92 | + @inbounds return similar_type(a, Size(L))(tuple($(exprs...))) |
| 93 | + end |
| 94 | +end |
| 95 | + |
| 96 | +@propagate_inbounds function getindex(a::StaticArray, inds::StaticArray{Int}) |
| 97 | + _getindex(a, Length(inds), inds) |
| 98 | +end |
| 99 | + |
| 100 | +@generated function _getindex(a::StaticArray, ::Length{L}, inds::StaticArray{Int}) where {L} |
| 101 | + exprs = [:(a[inds[$i]]) for i = 1:L] |
| 102 | + return quote |
| 103 | + @_propagate_inbounds_meta |
| 104 | + similar_type(a, Size(L))(tuple($(exprs...))) |
| 105 | + end |
| 106 | +end |
| 107 | + |
| 108 | +@inline function setindex!(a::StaticArray, v, ::Colon) |
| 109 | + _setindex!(a::StaticArray, v, Length(a), :) |
| 110 | + return v |
| 111 | +end |
82 | 112 |
|
| 113 | +@generated function _setindex!(a::StaticArray, v, ::Length{L}, ::Colon) where {L} |
| 114 | + exprs = [:(a[$i] = v) for i = 1:L] |
| 115 | + return quote |
| 116 | + @_inline_meta |
| 117 | + @inbounds $(Expr(:block, exprs...)) |
| 118 | + end |
| 119 | +end |
83 | 120 |
|
84 |
| -##################### |
85 |
| -## Array Indexing ## |
86 |
| -##################### |
| 121 | +@generated function _setindex!(a::StaticArray, v::AbstractArray, ::Length{L}, ::Colon) where {L} |
| 122 | + exprs = [:(a[$i] = v[$i]) for i = 1:L] |
| 123 | + return quote |
| 124 | + @_propagate_inbounds_meta |
| 125 | + if length(v) != L |
| 126 | + throw(DimensionMismatch("tried to assign $(length(v))-element array to length-$L destination")) |
| 127 | + end |
| 128 | + @inbounds $(Expr(:block, exprs...)) |
| 129 | + end |
| 130 | +end |
| 131 | + |
| 132 | +@generated function _setindex!(a::StaticArray, v::StaticArray, ::Length{L}, ::Colon) where {L} |
| 133 | + exprs = [:(a[$i] = v[$i]) for i = 1:L] |
| 134 | + return quote |
| 135 | + @_propagate_inbounds_meta |
| 136 | + if Length(typeof(v)) != L |
| 137 | + throw(DimensionMismatch("tried to assign $(length(v))-element array to length-$L destination")) |
| 138 | + end |
| 139 | + $(Expr(:block, exprs...)) |
| 140 | + end |
| 141 | +end |
| 142 | + |
| 143 | +@propagate_inbounds function setindex!(a::StaticArray, v, inds::StaticArray{Int}) |
| 144 | + _setindex!(a, v, Length(inds), inds) |
| 145 | + return v |
| 146 | +end |
| 147 | + |
| 148 | +@generated function _setindex!(a::StaticArray, v, ::Length{L}, inds::StaticArray{Int}) where {L} |
| 149 | + exprs = [:(a[inds[$i]] = v) for i = 1:L] |
| 150 | + return quote |
| 151 | + @_propagate_inbounds_meta |
| 152 | + similar_type(a, Size(L))(tuple($(exprs...))) |
| 153 | + end |
| 154 | +end |
| 155 | + |
| 156 | +@generated function _setindex!(a::StaticArray, v::AbstractArray, ::Length{L}, inds::StaticArray{Int}) where {L} |
| 157 | + exprs = [:(a[$i] = v[$i]) for i = 1:L] |
| 158 | + return quote |
| 159 | + @_propagate_inbounds_meta |
| 160 | + if length(v) != L |
| 161 | + throw(DimensionMismatch("tried to assign $(length(v))-element array to length-$L destination")) |
| 162 | + end |
| 163 | + $(Expr(:block, exprs...)) |
| 164 | + end |
| 165 | +end |
| 166 | + |
| 167 | +@generated function _setindex!(a::StaticArray, v::StaticArray, ::Length{L}, inds::StaticArray{Int}) where {L} |
| 168 | + exprs = [:(a[$i] = v[$i]) for i = 1:L] |
| 169 | + return quote |
| 170 | + @_propagate_inbounds_meta |
| 171 | + if Length(typeof(v)) != L |
| 172 | + throw(DimensionMismatch("tried to assign $(length(v))-element array to length-$L destination")) |
| 173 | + end |
| 174 | + $(Expr(:block, exprs...)) |
| 175 | + end |
| 176 | +end |
| 177 | + |
| 178 | +########################################### |
| 179 | +## Multidimensional non-scalar indexing ## |
| 180 | +########################################### |
| 181 | + |
| 182 | +# getindex |
87 | 183 |
|
88 | 184 | @propagate_inbounds function getindex(a::StaticArray, inds::Union{Int, StaticArray{Int}, Colon}...)
|
89 | 185 | _getindex(a, index_sizes(Size(a), inds...), inds)
|
|
124 | 220 | _getindex(a, index_sizes(i1, i2, i3, i4, inds...), (i1, i2, i3, i4, inds...))
|
125 | 221 | end
|
126 | 222 |
|
127 |
| - |
128 | 223 | @generated function _getindex(a::AbstractArray, ind_sizes::Tuple{Vararg{Size}}, inds)
|
129 | 224 | newsize = out_index_size(ind_sizes.parameters...)
|
130 | 225 | linearsizes = linear_index_size(ind_sizes.parameters...)
|
|
161 | 256 | end
|
162 | 257 | end
|
163 | 258 |
|
164 |
| - |
| 259 | +# setindex! |
165 | 260 |
|
166 | 261 | @propagate_inbounds function setindex!(a::StaticArray, value, inds::Union{Int, StaticArray{Int}, Colon}...)
|
167 | 262 | _setindex!(a, value, index_sizes(Size(a), inds...), inds)
|
|
264 | 359 | end
|
265 | 360 | end
|
266 | 361 |
|
267 |
| - |
268 | 362 | # setindex! from an array
|
269 | 363 | @generated function _setindex!(a::AbstractArray, v::AbstractArray, ind_sizes::Tuple{Vararg{Size}}, inds)
|
270 | 364 | linearsizes = linear_index_size(ind_sizes.parameters...)
|
|
0 commit comments