Skip to content

Commit f1465c4

Browse files
committed
moving more types
1 parent 3ba8063 commit f1465c4

File tree

3 files changed

+152
-1
lines changed

3 files changed

+152
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22

33
Interface package for [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl).
44

5+
Contains definitions for the following types:
56

7+
* immutable: `SArray`, `SVector` and `SMatrix`,
8+
* mutable: `MArray`, `MVector` and `MMatrix`,
9+
* wrapper: `SizedArray`, `SizedVector` and `SizedMatrix`.

src/StaticArraysCore.jl

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ Base.@pure tuple_prod(T::Tuple) = prod(T)
4646
Base.@pure tuple_minimum(T::Type{<:Tuple}) = length(T.parameters) == 0 ? 0 : minimum(tuple(T.parameters...))
4747
Base.@pure tuple_minimum(T::Tuple) = minimum(T)
4848

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
4957

5058
# Something doesn't match up type wise
5159
function check_array_parameters(Size, T, N, L)
@@ -102,6 +110,42 @@ end
102110

103111
@inline SArray{S,T,N}(x::Tuple) where {S<:Tuple,T,N} = SArray{S,T,N,tuple_prod(S)}(x)
104112

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
105149

106150
"""
107151
MArray{S, T, N, L}(undef)
@@ -157,6 +201,95 @@ end
157201
end
158202
end
159203

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}
160292

293+
const SizedMatrix{S1,S2,T} = SizedArray{Tuple{S1,S2},T,2}
161294

162295
end # module

test/runtests.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
using StaticArraysCore, Test
22

3+
using StaticArraysCore: SArray, SVector, SMatrix
4+
using StaticArraysCore: MArray, MVector, MMatrix
5+
using StaticArraysCore: SizedArray, SizedVector, SizedMatrix
6+
37
@testset "types" begin
4-
@test StaticArraysCore.SArray{Tuple{2},Int,1}((1, 2)) isa StaticArraysCore.SArray
8+
@test SArray{Tuple{2},Int,1}((1, 2)) isa SArray
9+
@test SVector{2,Int}((1, 2)) isa SVector
10+
@test SMatrix{1,2,Int}((1, 2)) isa SMatrix
11+
12+
@test MArray{Tuple{2},Int,1}((1, 2)) isa MArray
13+
@test MVector{2,Int}((1, 2)) isa MVector
14+
@test MMatrix{1,2,Int}((1, 2)) isa MMatrix
15+
16+
@test SizedArray{Tuple{2},Int,1}([1, 2]) isa SizedArray
17+
@test SizedVector{2,Int}([1, 2]) isa SizedVector
18+
@test SizedMatrix{1,2,Int}(fill(0, 1, 2)) isa SizedMatrix
519
end
620

0 commit comments

Comments
 (0)