|
| 1 | + |
| 2 | +""" |
| 3 | + SizedArray{(dims...)}(array) |
| 4 | +
|
| 5 | +Wraps an `Array` with a static size, so to take advantage of the (faster) |
| 6 | +methods defined by the static array package. The size is checked once upon |
| 7 | +construction. |
| 8 | +
|
| 9 | +(Also, `Size(dims...)(array)` acheives the same thing) |
| 10 | +""" |
| 11 | +immutable SizedArray{S,T,N} <: StaticArray{T,N} |
| 12 | + data::Array{T,N} |
| 13 | + |
| 14 | + function SizedArray(a) |
| 15 | + if size(a) != S |
| 16 | + error("Dimensions $(size(a)) don't match static size $S") |
| 17 | + end |
| 18 | + new(a) |
| 19 | + end |
| 20 | +end |
| 21 | + |
| 22 | +@inline (::Type{SizedArray{S,T}}){S,T,N}(a::Array{T,N}) = SizedArray{S,T,N}(a) |
| 23 | +@inline (::Type{SizedArray{S}}){S,T,N}(a::Array{T,N}) = SizedArray{S,T,N}(a) |
| 24 | + |
| 25 | +@pure size{S}(::Type{SizedArray{S}}) = S |
| 26 | +@pure size{S,T}(::Type{SizedArray{S,T}}) = S |
| 27 | +@pure size{S,T,N}(::Type{SizedArray{S,T,N}}) = S |
| 28 | + |
| 29 | +@propagate_inbounds getindex(a::SizedArray, i::Int) = getindex(a.data, i...) |
| 30 | +@propagate_inbounds setindex!(a::SizedArray, v, i::Int) = setindex!(a.data, v, i...) |
| 31 | + |
| 32 | +typealias SizedVector{S,T} SizedArray{S,T,1} |
| 33 | +@inline (::Type{SizedVector{S}}){S,T}(a::Vector{T}) = SizedArray{S,T,1}(a) |
| 34 | + |
| 35 | +typealias SizedMatrix{S,T} SizedArray{S,T,2} |
| 36 | +@inline (::Type{SizedMatrix{S}}){S,T}(a::Matrix{T}) = SizedArray{S,T,2}(a) |
| 37 | + |
| 38 | + |
| 39 | +""" |
| 40 | + Size(dims)(array) |
| 41 | +
|
| 42 | +Creates a `SizedArray` wrapping `array` with the specified statically-known |
| 43 | +`dims`, so to take advantage of the (faster) methods defined by the static array |
| 44 | +package. |
| 45 | +""" |
| 46 | +(::Size{S}){S}(a::Array) = SizedArray{S}(a) |
0 commit comments