From 8b4ae9c71bf634f35b6d5f7a1d494961d2575265 Mon Sep 17 00:00:00 2001 From: Keno Fischer Date: Mon, 3 Apr 2017 23:16:58 +0000 Subject: [PATCH] Define `size` more explicitly, helping the compiler --- src/MArray.jl | 6 ++++++ src/SArray.jl | 6 ++++++ src/SizedArray.jl | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/src/MArray.jl b/src/MArray.jl index 1383fe38..afbe92f5 100644 --- a/src/MArray.jl +++ b/src/MArray.jl @@ -34,6 +34,12 @@ type MArray{Size, T, N, L} <: StaticArray{T, N} new{Size,T,N,L}() end end +@pure size(::Type{<:MArray{S}}) where S = tuple(S.parameters...) +@inline function size(t::Type{<:MArray},d::Int) + S = size(t) + d > length(S) ? 1 : S[d] +end +@inline size(t::MArray, d::Int) = size(typeof(t), d) @generated function (::Type{MArray{Size,T,N}}){Size,T,N}(x::Tuple) return quote diff --git a/src/SArray.jl b/src/SArray.jl index f90ab93d..893abc74 100644 --- a/src/SArray.jl +++ b/src/SArray.jl @@ -28,6 +28,12 @@ immutable SArray{Size, T, N, L} <: StaticArray{T, N} new{Size,T,N,L}(convert_ntuple(T, x)) end end +@pure size(T::Type{SArray{S}}) where S = tuple(S.parameters...) +@inline function size(t::Type{<:SArray},d::Int) + S = size(t) + d > length(S) ? 1 : S[d] +end +@inline size(t::SArray,d::Int) = size(typeof(t), d) @generated function (::Type{SArray{Size,T,N}}){Size <: Tuple,T,N}(x::Tuple) return quote diff --git a/src/SizedArray.jl b/src/SizedArray.jl index 60817d38..ab2933b7 100644 --- a/src/SizedArray.jl +++ b/src/SizedArray.jl @@ -23,6 +23,12 @@ immutable SizedArray{S,T,N,M} <: StaticArray{T,N} new{S,T,N,M}(Array{T,M}(S)) end end +@pure size(t::SizedArray{S}) where S = S +@inline function size(t::Type{<:SizedArray},d::Int) + S = size(t) + d > length(S) ? 1 : S[d] +end +@inline size(t::SizedArray, d::Int) = size(typeof(t), d) @inline (::Type{SizedArray{S,T,N}}){S,T,N,M}(a::Array{T,M}) = SizedArray{S,T,N,M}(a) @inline (::Type{SizedArray{S,T}}){S,T,M}(a::Array{T,M}) = SizedArray{S,T,_ndims(S),M}(a)