From 968d36d56f80e9116fa9b0f3e24693025af11f9f Mon Sep 17 00:00:00 2001 From: Eivind Fonn Date: Sat, 5 Jan 2019 13:38:45 +0100 Subject: [PATCH] Allow empty indexing of zero-dimensional MArrays --- src/Scalar.jl | 1 - src/indexing.jl | 14 ++++++++++++++ test/MArray.jl | 7 +++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Scalar.jl b/src/Scalar.jl index 958e2e15..b6faa0e3 100644 --- a/src/Scalar.jl +++ b/src/Scalar.jl @@ -14,7 +14,6 @@ const Scalar{T} = SArray{Tuple{},T,0,1} end @inline convert(::Type{SA}, sa::SA) where {SA <: Scalar} = sa -getindex(v::Scalar) = v.data[1] @propagate_inbounds function getindex(v::Scalar, i::Int) @boundscheck if i != 1 error("Attempt to index Scalar at index $i") diff --git a/src/indexing.jl b/src/indexing.jl index 21edb874..d2254f0d 100644 --- a/src/indexing.jl +++ b/src/indexing.jl @@ -14,6 +14,13 @@ setindex!(a::StaticArray, value, i::Int) = error("setindex!(::$(typeof(a)), valu end @generated function _getindex_scalar(::Size{S}, a::StaticArray, inds::Int...) where S + if length(inds) == 0 + return quote + @_propagate_inbounds_meta + a[1] + end + end + stride = 1 ind_expr = :() for i ∈ 1:length(inds) @@ -36,6 +43,13 @@ end end @generated function _setindex!_scalar(::Size{S}, a::StaticArray, value, inds::Int...) where S + if length(inds) == 0 + return quote + @_propagate_inbounds_meta + a[1] = value + end + end + stride = 1 ind_expr = :() for i ∈ 1:length(inds) diff --git a/test/MArray.jl b/test/MArray.jl index ed65eb64..0e802919 100644 --- a/test/MArray.jl +++ b/test/MArray.jl @@ -157,4 +157,11 @@ @test @inferred(promote_type(MMatrix{2,3,Float32,6}, MMatrix{2,3,Complex{Float64},6})) === MMatrix{2,3,Complex{Float64},6} @test @inferred(promote_type(MArray{Tuple{2, 2, 2, 2},Float32, 4, 16}, MArray{Tuple{2, 2, 2, 2}, Complex{Float64}, 4, 16})) === MArray{Tuple{2, 2, 2, 2}, Complex{Float64}, 4, 16} end + + @testset "zero-dimensional" begin + v = MArray{Tuple{}, Int, 0, 1}(1) + @test v[] == 1 + v[] = 2 + @test v[] == 2 + end end