@@ -46,6 +46,14 @@ Base.@pure tuple_prod(T::Tuple) = prod(T)
46
46
Base. @pure tuple_minimum (T:: Type{<:Tuple} ) = length (T. parameters) == 0 ? 0 : minimum (tuple (T. parameters... ))
47
47
Base. @pure tuple_minimum (T:: Tuple ) = minimum (T)
48
48
49
+ """
50
+ size_to_tuple(::Type{S}) where S<:Tuple
51
+
52
+ Converts a size given by `Tuple{N, M, ...}` into a tuple `(N, M, ...)`.
53
+ """
54
+ Base. @pure function size_to_tuple (:: Type{S} ) where S<: Tuple
55
+ return tuple (S. parameters... )
56
+ end
49
57
50
58
# Something doesn't match up type wise
51
59
function check_array_parameters (Size, T, N, L)
102
110
103
111
@inline SArray {S,T,N} (x:: Tuple ) where {S<: Tuple ,T,N} = SArray {S,T,N,tuple_prod(S)} (x)
104
112
113
+ """
114
+ SVector{S, T}(x::NTuple{S, T})
115
+ SVector{S, T}(x1, x2, x3, ...)
116
+
117
+ Construct a statically-sized vector `SVector`. Since this type is immutable,
118
+ the data must be provided upon construction and cannot be mutated later.
119
+ Constructors may drop the `T` and `S` parameters if they are inferrable from the
120
+ input (e.g. `SVector(1,2,3)` constructs an `SVector{3, Int}`).
121
+
122
+ SVector{S}(vec::Vector)
123
+
124
+ Construct a statically-sized vector of length `S` using the data from `vec`.
125
+ The parameter `S` is mandatory since the length of `vec` is unknown to the
126
+ compiler (the element type may optionally also be specified).
127
+ """
128
+ const SVector{S, T} = SArray{Tuple{S}, T, 1 , S}
129
+
130
+ """
131
+ SMatrix{S1, S2, T, L}(x::NTuple{L, T})
132
+ SMatrix{S1, S2, T, L}(x1, x2, x3, ...)
133
+
134
+ Construct a statically-sized matrix `SMatrix`. Since this type is immutable,
135
+ the data must be provided upon construction and cannot be mutated later. The
136
+ `L` parameter is the `length` of the array and is always equal to `S1 * S2`.
137
+ Constructors may drop the `L`, `T` and even `S2` parameters if they are inferrable
138
+ from the input (e.g. `L` is always inferrable from `S1` and `S2`).
139
+
140
+ SMatrix{S1, S2}(mat::Matrix)
141
+
142
+ Construct a statically-sized matrix of dimensions `S1 × S2` using the data from
143
+ `mat`. The parameters `S1` and `S2` are mandatory since the size of `mat` is
144
+ unknown to the compiler (the element type may optionally also be specified).
145
+ """
146
+ const SMatrix{S1, S2, T, L} = SArray{Tuple{S1, S2}, T, 2 , L}
147
+
148
+ # MArray
105
149
106
150
"""
107
151
MArray{S, T, N, L}(undef)
157
201
end
158
202
end
159
203
204
+ """
205
+ MVector{S,T}(undef)
206
+ MVector{S,T}(x::NTuple{S, T})
207
+ MVector{S,T}(x1, x2, x3, ...)
208
+
209
+ Construct a statically-sized, mutable vector `MVector`. Data may optionally be
210
+ provided upon construction, and can be mutated later. Constructors may drop the
211
+ `T` and `S` parameters if they are inferrable from the input (e.g.
212
+ `MVector(1,2,3)` constructs an `MVector{3, Int}`).
213
+
214
+ MVector{S}(vec::Vector)
215
+
216
+ Construct a statically-sized, mutable vector of length `S` using the data from
217
+ `vec`. The parameter `S` is mandatory since the length of `vec` is unknown to the
218
+ compiler (the element type may optionally also be specified).
219
+ """
220
+ const MVector{S, T} = MArray{Tuple{S}, T, 1 , S}
221
+
222
+ """
223
+ MMatrix{S1, S2, T, L}(undef)
224
+ MMatrix{S1, S2, T, L}(x::NTuple{L, T})
225
+ MMatrix{S1, S2, T, L}(x1, x2, x3, ...)
226
+
227
+ Construct a statically-sized, mutable matrix `MMatrix`. The data may optionally
228
+ be provided upon construction and can be mutated later. The `L` parameter is the
229
+ `length` of the array and is always equal to `S1 * S2`. Constructors may drop
230
+ the `L`, `T` and even `S2` parameters if they are inferrable from the input
231
+ (e.g. `L` is always inferrable from `S1` and `S2`).
232
+
233
+ MMatrix{S1, S2}(mat::Matrix)
234
+
235
+ Construct a statically-sized, mutable matrix of dimensions `S1 × S2` using the data from
236
+ `mat`. The parameters `S1` and `S2` are mandatory since the size of `mat` is
237
+ unknown to the compiler (the element type may optionally also be specified).
238
+ """
239
+ const MMatrix{S1, S2, T, L} = MArray{Tuple{S1, S2}, T, 2 , L}
240
+
241
+
242
+ # SizedArray
243
+
244
+ require_one_based_indexing (A... ) = ! Base. has_offset_axes (A... ) ||
245
+ throw (ArgumentError (" offset arrays are not supported but got an array with index other than 1" ))
246
+
247
+ """
248
+ SizedArray{Tuple{dims...}}(array)
249
+
250
+ Wraps an `AbstractArray` with a static size, so to take advantage of the (faster)
251
+ methods defined by the static array package. The size is checked once upon
252
+ construction to determine if the number of elements (`length`) match, but the
253
+ array may be reshaped.
254
+
255
+ The aliases `SizedVector{N}` and `SizedMatrix{N,M}` are provided as more
256
+ convenient names for one and two dimensional `SizedArray`s. For example, to
257
+ wrap a 2x3 array `a` in a `SizedArray`, use `SizedMatrix{2,3}(a)`.
258
+ """
259
+ struct SizedArray{S<: Tuple ,T,N,M,TData<: AbstractArray{T,M} } <: StaticArray{S,T,N}
260
+ data:: TData
261
+
262
+ function SizedArray {S,T,N,M,TData} (a:: TData ) where {S<: Tuple ,T,N,M,TData<: AbstractArray{T,M} }
263
+ require_one_based_indexing (a)
264
+ if size (a) != size_to_tuple (S) && size (a) != (tuple_prod (S),)
265
+ throw (DimensionMismatch (" Dimensions $(size (a)) don't match static size $S " ))
266
+ end
267
+ return new {S,T,N,M,TData} (a)
268
+ end
269
+
270
+ function SizedArray {S,T,N,1,TData} (:: UndefInitializer ) where {S<: Tuple ,T,N,TData<: AbstractArray{T,1} }
271
+ return new {S,T,N,1,TData} (TData (undef, tuple_prod (S)))
272
+ end
273
+ function SizedArray {S,T,N,N,TData} (:: UndefInitializer ) where {S<: Tuple ,T,N,TData<: AbstractArray{T,N} }
274
+ return new {S,T,N,N,TData} (TData (undef, size_to_tuple (S)... ))
275
+ end
276
+ end
277
+
278
+ @inline function SizedArray {S,T,N,M} (a:: AbstractArray ) where {S<: Tuple ,T,N,M}
279
+ if eltype (a) == T && (M == 1 || M == ndims (a))
280
+ a′ = M == 1 ? vec (a) : a
281
+ return SizedArray {S,T,N,M,typeof(a′)} (a′)
282
+ end
283
+ return convert (SizedArray{S,T,N,M}, a)
284
+ end
285
+
286
+ @inline function SizedArray {S,T,N} (a:: AbstractArray ) where {S<: Tuple ,T,N}
287
+ M = ndims (a) == N ? N : 1
288
+ return SizedArray {S,T,N,M} (a)
289
+ end
290
+
291
+ const SizedVector{S,T} = SizedArray{Tuple{S},T,1 ,1 }
160
292
293
+ const SizedMatrix{S1,S2,T} = SizedArray{Tuple{S1,S2},T,2 }
161
294
162
295
end # module
0 commit comments