Skip to content

Commit ec26f0a

Browse files
authored
Merge pull request #581 from TheBB/zerodim-marray
Allow empty indexing of zero-dimensional MArrays
2 parents 0759078 + 968d36d commit ec26f0a

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/Scalar.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const Scalar{T} = SArray{Tuple{},T,0,1}
1414
end
1515
@inline convert(::Type{SA}, sa::SA) where {SA <: Scalar} = sa
1616

17-
getindex(v::Scalar) = v.data[1]
1817
@propagate_inbounds function getindex(v::Scalar, i::Int)
1918
@boundscheck if i != 1
2019
error("Attempt to index Scalar at index $i")

src/indexing.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ setindex!(a::StaticArray, value, i::Int) = error("setindex!(::$(typeof(a)), valu
1414
end
1515

1616
@generated function _getindex_scalar(::Size{S}, a::StaticArray, inds::Int...) where S
17+
if length(inds) == 0
18+
return quote
19+
@_propagate_inbounds_meta
20+
a[1]
21+
end
22+
end
23+
1724
stride = 1
1825
ind_expr = :()
1926
for i 1:length(inds)
@@ -36,6 +43,13 @@ end
3643
end
3744

3845
@generated function _setindex!_scalar(::Size{S}, a::StaticArray, value, inds::Int...) where S
46+
if length(inds) == 0
47+
return quote
48+
@_propagate_inbounds_meta
49+
a[1] = value
50+
end
51+
end
52+
3953
stride = 1
4054
ind_expr = :()
4155
for i 1:length(inds)

test/MArray.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,11 @@
153153
@test @inferred(promote_type(MMatrix{2,3,Float32,6}, MMatrix{2,3,Complex{Float64},6})) === MMatrix{2,3,Complex{Float64},6}
154154
@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}
155155
end
156+
157+
@testset "zero-dimensional" begin
158+
v = MArray{Tuple{}, Int, 0, 1}(1)
159+
@test v[] == 1
160+
v[] = 2
161+
@test v[] == 2
162+
end
156163
end

0 commit comments

Comments
 (0)