diff --git a/src/MArray.jl b/src/MArray.jl index 80d94163..50a0f828 100644 --- a/src/MArray.jl +++ b/src/MArray.jl @@ -106,17 +106,20 @@ end ## MArray methods ## #################### -function getindex(v::MArray, i::Int) - Base.@_inline_meta +@propagate_inbounds function getindex(v::MArray, i::Int) + @boundscheck checkbounds(v,i) + T = eltype(v) + + if isbitstype(T) + return unsafe_load(Base.unsafe_convert(Ptr{T}, pointer_from_objref(v)), i) + end v.data[i] end @inline function setindex!(v::MArray, val, i::Int) - @boundscheck if i < 1 || i > length(v) - throw(BoundsError()) - end - + @boundscheck checkbounds(v,i) T = eltype(v) + if isbitstype(T) unsafe_store!(Base.unsafe_convert(Ptr{T}, pointer_from_objref(v)), convert(T, val), i) else diff --git a/src/MMatrix.jl b/src/MMatrix.jl index eb3e3b20..74f036d7 100644 --- a/src/MMatrix.jl +++ b/src/MMatrix.jl @@ -75,38 +75,6 @@ end ## MMatrix methods ## ##################### -@propagate_inbounds function getindex(m::MMatrix{S1,S2,T}, i::Int) where {S1,S2,T} - #@boundscheck if i < 1 || i > length(m) - # throw(BoundsError(m,i)) - #end - - # This is nasty... but it turns out Julia will literally copy the whole tuple to the stack otherwise! - if isbitstype(T) - unsafe_load(Base.unsafe_convert(Ptr{T}, pointer_from_objref(m)), i) - else - # Not sure about this... slow option for now... - m.data[i] - #unsafe_load(Base.unsafe_convert(Ptr{Ptr{Nothing}}, pointer_from_objref(m.data)), i) - end -end - -@propagate_inbounds setindex!(m::MMatrix{S1,S2,T}, val, i::Int) where {S1,S2,T} = setindex!(m, convert(T, val), i) -@propagate_inbounds function setindex!(m::MMatrix{S1,S2,T}, val::T, i::Int) where {S1,S2,T} - #@boundscheck if i < 1 || i > length(m) - # throw(BoundsError(m,i)) - #end - - if isbitstype(T) - unsafe_store!(Base.unsafe_convert(Ptr{T}, pointer_from_objref(m)), val, i) - else # TODO check that this isn't crazy. Also, check it doesn't cause problems with GC... - # This one is unsafe (#27) - # unsafe_store!(Base.unsafe_convert(Ptr{Ptr{Nothing}}, pointer_from_objref(m.data)), pointer_from_objref(val), i) - error("setindex!() with non-isbitstype eltype is not supported by StaticArrays. Consider using SizedArray.") - end - - return val -end - macro MMatrix(ex) if !isa(ex, Expr) error("Bad input for @MMatrix") diff --git a/src/MVector.jl b/src/MVector.jl index b601e8e8..d7e281d9 100644 --- a/src/MVector.jl +++ b/src/MVector.jl @@ -31,28 +31,6 @@ const MVector{S, T} = MArray{Tuple{S}, T, 1, S} ## MVector methods ## ##################### -@propagate_inbounds function getindex(v::MVector, i::Int) - v.data[i] -end - -# Mutating setindex! -@propagate_inbounds setindex!(v::MVector{S,T}, val, i::Int) where {S,T} = setindex!(v, convert(T, val), i) -@inline function setindex!(v::MVector{S,T}, val::T, i::Int) where {S,T} - @boundscheck if i < 1 || i > length(v) - throw(BoundsError()) - end - - if isbitstype(T) - unsafe_store!(Base.unsafe_convert(Ptr{T}, pointer_from_objref(v)), val, i) - else - # This one is unsafe (#27) - #unsafe_store!(Base.unsafe_convert(Ptr{Ptr{Nothing}}, pointer_from_objref(v.data)), pointer_from_objref(val), i) - error("setindex!() with non-isbitstype eltype is not supported by StaticArrays. Consider using SizedArray.") - end - - return val -end - macro MVector(ex) if isa(ex, Expr) && ex.head == :vect return esc(Expr(:call, MVector{length(ex.args)}, Expr(:tuple, ex.args...))) diff --git a/src/indexing.jl b/src/indexing.jl index d429bc50..ca4e1b6f 100644 --- a/src/indexing.jl +++ b/src/indexing.jl @@ -9,6 +9,7 @@ setindex!(a::StaticArray, value, i::Int) = error("setindex!(::$(typeof(a)), valu # Note: all indexing behavior defaults to dense, linear indexing @propagate_inbounds function getindex(a::StaticArray, inds::Int...) + @boundscheck checkbounds(a, inds...) _getindex_scalar(Size(a), a, inds...) end @@ -30,6 +31,7 @@ end end @propagate_inbounds function setindex!(a::StaticArray, value, inds::Int...) + @boundscheck checkbounds(a, inds...) _setindex!_scalar(Size(a), a, value, inds...) end