Skip to content

Commit 549f1f4

Browse files
Kenoandyferris
authored andcommitted
Fix indexing with zero-size arrays (#124)
1 parent a2eaeb4 commit 549f1f4

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

src/broadcast.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ end
5353
newsize = tuple(newsize...)
5454

5555
exprs = Array{Expr}(newsize)
56-
more = true
56+
more = newsize[1] != 0
5757
current_ind = ones(Int, length(newsize))
5858

5959
while more
@@ -116,7 +116,7 @@ end
116116

117117
exprs = Array{Expr}(newsize)
118118
j = 1
119-
more = true
119+
more = newsize[1] != 0
120120
current_ind = ones(Int, max(length(newsize), length.(sizes)...))
121121
while more
122122
exprs_vals = [(a[i] <: Number ? :(a[$i]) : :(a[$i][$(broadcasted_index(sizes[i], current_ind))])) for i = 1:length(sizes)]

src/indexing.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ end
133133
# Iterate over input indices
134134
ind_types = inds.parameters
135135
current_ind = ones(Int,length(linearsizes))
136-
more = true
136+
more = linearsizes[1] != 0
137137
while more
138138
exprs_tmp = [_ind(i, current_ind[i], ind_types[i]) for i = 1:length(linearsizes)]
139139
exprs[current_ind...] = :(getindex(a, $(exprs_tmp...)))
@@ -203,6 +203,14 @@ end
203203
end
204204

205205
# disambiguities from Base
206+
@propagate_inbounds function setindex!(a::Array, value, i1::StaticVector{Int})
207+
_setindex!(a, value, index_sizes(i1), (i1,))
208+
end
209+
210+
@propagate_inbounds function setindex!(a::Array, value::AbstractArray, i1::StaticVector{Int})
211+
_setindex!(a, value, index_sizes(i1), (i1,))
212+
end
213+
206214
@propagate_inbounds function setindex!(a::Array, value::AbstractArray, i1::StaticArray{Int}, inds::Union{Int, StaticArray{Int}}...)
207215
_setindex!(a, value, index_sizes(i1, inds...), (i1, inds...))
208216
end
@@ -227,7 +235,7 @@ end
227235
# Iterate over input indices
228236
ind_types = inds.parameters
229237
current_ind = ones(Int,length(ind_types))
230-
more = true
238+
more = linearsizes[1] != 0
231239
while more
232240
exprs_tmp = [_ind(i, current_ind[i], ind_types[i]) for i = 1:length(ind_types)]
233241
exprs[current_ind...] = :(setindex!(a, value, $(exprs_tmp...)))

test/indexing.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,12 @@
9393
@test (ma[2,1,1,1] = 48; ma[2,1,1,1] === 48)
9494
end
9595

96+
@testset "Indexing with empty vectors" begin
97+
a = randn(2,2)
98+
@test a[SVector{0,Int}()] == SVector{0,Float64}(())
99+
@test a[SVector{0,Int}(),SVector{0,Int}()] == SMatrix{0,0,Float64,0}(())
100+
b = copy(a)
101+
a[SVector{0,Int}()] = 5.0
102+
@test b == a
103+
end
96104
end

test/mapreduce.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,7 @@
7474
@test mm == @MMatrix [3 4; 7 8; 11 12; 15 16]
7575
# issue #103
7676
@test map(+, M, M) == [2 4; 6 8; 10 12; 14 16]
77+
78+
@test ((@SVector Int64[]) + (@SVector Int64[])) == (@SVector Int64[])
7779
end
7880
end

0 commit comments

Comments
 (0)