Skip to content

Commit 88cadf0

Browse files
authored
Merge pull request #60 from JuliaArrays/sizedarray
Add `SizedArray` wrapper for `Array`
2 parents 76fed85 + f30281a commit 88cadf0

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed

src/SizedArray.jl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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)

src/StaticArrays.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export StaticScalar, StaticArray, StaticVector, StaticMatrix
1212
export Scalar, SArray, SVector, SMatrix
1313
export MArray, MVector, MMatrix
1414
export FieldVector, MutableFieldVector
15+
export SizedArray, SizedVector, SizedMatrix
1516

1617
export Size
1718

@@ -32,6 +33,7 @@ include("SArray.jl")
3233
include("MVector.jl")
3334
include("MMatrix.jl")
3435
include("MArray.jl")
36+
include("SizedArray.jl")
3537

3638
include("indexing.jl")
3739
include("abstractarray.jl")

src/abstractarray.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ end
116116
@pure similar_type{SA<:StaticArray,N}(::Union{SA,Type{SA}}, sizes::Tuple{Vararg{Int,N}}) = SArray{sizes, eltype(SA), N, prod(sizes)}
117117

118118
# Some specializations for the mutable case
119-
@pure similar_type{MA<:Union{MVector,MMatrix,MArray}}(::Union{MA,Type{MA}}, size::Int) = MVector{size, eltype(MA)}
120-
@pure similar_type{MA<:Union{MVector,MMatrix,MArray}}(::Union{MA,Type{MA}}, sizes::Tuple{Int}) = MVector{sizes[1], eltype(MA)}
119+
@pure similar_type{MA<:Union{MVector,MMatrix,MArray,SizedArray}}(::Union{MA,Type{MA}}, size::Int) = MVector{size, eltype(MA)}
120+
@pure similar_type{MA<:Union{MVector,MMatrix,MArray,SizedArray}}(::Union{MA,Type{MA}}, sizes::Tuple{Int}) = MVector{sizes[1], eltype(MA)}
121121

122-
@pure similar_type{MA<:Union{MVector,MMatrix,MArray}}(::Union{MA,Type{MA}}, sizes::Tuple{Int,Int}) = MMatrix{sizes[1], sizes[2], eltype(MA), sizes[1]*sizes[2]}
122+
@pure similar_type{MA<:Union{MVector,MMatrix,MArray,SizedArray}}(::Union{MA,Type{MA}}, sizes::Tuple{Int,Int}) = MMatrix{sizes[1], sizes[2], eltype(MA), sizes[1]*sizes[2]}
123123

124-
@pure similar_type{MA<:Union{MVector,MMatrix,MArray},N}(::Union{MA,Type{MA}}, sizes::Tuple{Vararg{Int,N}}) = MArray{sizes, eltype(MA), N, prod(sizes)}
124+
@pure similar_type{MA<:Union{MVector,MMatrix,MArray,SizedArray},N}(::Union{MA,Type{MA}}, sizes::Tuple{Vararg{Int,N}}) = MArray{sizes, eltype(MA), N, prod(sizes)}
125125

126126
# And also similar() returning mutable StaticArrays
127127
@inline similar{SV <: StaticVector}(::SV) = MVector{length(SV),eltype(SV)}()

src/det.jl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
@inline det(A::StaticMatrix) = _det(Size(A), A)
22

3-
"""
4-
det(Size(m,m), mat)
5-
6-
Calculate the matrix determinate using an algorithm specialized on the size of
7-
the `m`×`m` matrix `mat`, which is much faster for small matrices.
8-
"""
93
@inline _det(::Size{(1,1)}, A::AbstractMatrix) = @inbounds return A[1]
104

115
@inline function _det(::Size{(2,2)}, A::AbstractMatrix)

0 commit comments

Comments
 (0)