Skip to content

Commit 95af0c2

Browse files
author
Andy Ferris
committed
Make accumulate! compatible with offset indices
Plus add relevant tests
1 parent d85d03e commit 95af0c2

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

base/accumulate.jl

+5-2
Original file line numberDiff line numberDiff line change
@@ -383,12 +383,15 @@ function _accumulate1!(op, B, v1, A::AbstractVector, dim::Integer)
383383
inds = LinearIndices(A)
384384
inds == LinearIndices(B) || throw(DimensionMismatch("LinearIndices of A and B don't match"))
385385
dim > 1 && return copyto!(B, A)
386-
i1 = inds[1]
386+
(i1, state) = iterate(inds) # We checked earlier that A isn't empty
387387
cur_val = v1
388388
B[i1] = cur_val
389-
@inbounds for i in inds[2:end]
389+
next = iterate(inds, state)
390+
@inbounds while next !== nothing
391+
(i, state) = next
390392
cur_val = op(cur_val, A[i])
391393
B[i] = cur_val
394+
next = iterate(inds, state)
392395
end
393396
return B
394397
end

stdlib/Random/test/runtests.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ end
518518
# test PRNG jump
519519

520520
function randjumpvec(m, steps, len) # old version of randjump
521-
mts = accumulate(Future.randjump, m, fill(steps, len-1))
521+
mts = accumulate(Future.randjump, fill(steps, len-1); init=m)
522522
pushfirst!(mts, m)
523523
mts
524524
end

test/arrayops.jl

+4
Original file line numberDiff line numberDiff line change
@@ -2283,6 +2283,9 @@ end
22832283
@test accumulate(min, [1 0; 0 1], dims=1) == [1 0; 0 0]
22842284
@test accumulate(min, [1 0; 0 1], dims=2) == [1 0; 0 0]
22852285

2286+
@test accumulate(min, [3 2 1; 3 2 1], dims=2) == [3 2 1; 3 2 1]
2287+
@test accumulate(min, [3 2 1; 3 2 1], dims=2, init=2) == [2 2 1; 2 2 1]
2288+
22862289
@test isa(accumulate(+, Int[]), Vector{Int})
22872290
@test isa(accumulate(+, Int[]; init=1.), Vector{Float64})
22882291
@test accumulate(+, [1,2]; init=1) == [2, 4]
@@ -2311,6 +2314,7 @@ end
23112314
arr = randn(4)
23122315
oarr = OffsetArray(arr, (-3,))
23132316
@test accumulate(+, oarr).parent == accumulate(+, arr)
2317+
@test accumulate(+, oarr, init = 10).parent == accumulate(+, arr; init = 10)
23142318

23152319
@inferred accumulate(+, randn(3))
23162320
@inferred accumulate(+, randn(3); init=1)

0 commit comments

Comments
 (0)