Skip to content

Commit 0a9ab4a

Browse files
authored
setindex! returns the array instead of the value (#124)
1 parent 60aa7b9 commit 0a9ab4a

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/OffsetArrays.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,18 @@ end
157157
@boundscheck checkbounds(A, I...)
158158
J = @inbounds map(parentindex, axes(A), I)
159159
@inbounds parent(A)[J...] = val
160-
val
160+
A
161161
end
162162

163163
@propagate_inbounds function Base.setindex!(A::OffsetVector, val, i::Int)
164164
@boundscheck checkbounds(A, i)
165165
@inbounds parent(A)[parentindex(Base.axes1(A), i)] = val
166-
val
166+
A
167167
end
168168
@propagate_inbounds function Base.setindex!(A::OffsetArray, val, i::Int)
169169
@boundscheck checkbounds(A, i)
170170
@inbounds parent(A)[i] = val
171-
val
171+
A
172172
end
173173

174174
# For fast broadcasting: ref https://discourse.julialang.org/t/why-is-there-a-performance-hit-on-broadcasting-with-offsetarrays/32194

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,18 @@ end
200200
y[-1,-7,-128,-5,-1,-3,-2,-1] = 14
201201
y[-1,-7,-128,-5,-1,-3,-2,-1] += 5
202202
@test y[-1,-7,-128,-5,-1,-3,-2,-1] == 19
203+
204+
@testset "setindex!" begin
205+
A = OffsetArray(ones(2,2), 1:2, 1:2)
206+
@test setindex!(A, 2, 1, 1) === A
207+
@test A[1,1] == 2
208+
@test setindex!(A, 2, 1) === A
209+
@test A[1] == 2
210+
211+
v = OffsetArray(ones(3), 4:6)
212+
@test setindex!(A, 2, 4) === A
213+
@test A[4] == 2
214+
end
203215
end
204216

205217
@testset "Vector indexing" begin

0 commit comments

Comments
 (0)