Skip to content

Commit

Permalink
Fix shift direction of circshift! for vectors (#46759)
Browse files Browse the repository at this point in the history
This patch fixes the shifting direction for
circshift!(x::AbstractVector, n::Integer), which was opposite the
direction of circshift(x, n) and circshift!(y, x, n). In addition, this
patch fixes the method to also work correctly with offset arrays.
Fixes #46533.

(cherry picked from commit f1c4d54)
  • Loading branch information
fredrikekre authored and KristofferC committed Oct 28, 2022
1 parent 2666073 commit 5d30157
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 3 additions & 2 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3187,8 +3187,9 @@ function circshift!(a::AbstractVector, shift::Integer)
n == 0 && return
shift = mod(shift, n)
shift == 0 && return
reverse!(a, 1, shift)
reverse!(a, shift+1, length(a))
l = lastindex(a)
reverse!(a, firstindex(a), l-shift)
reverse!(a, l-shift+1, lastindex(a))
reverse!(a)
return a
end
12 changes: 12 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,18 @@ end
@test circshift(src, 1) == src
src = zeros(Bool, (4,0))
@test circshift(src, 1) == src

# 1d circshift! (https://github.com/JuliaLang/julia/issues/46533)
a = [1:5;]
@test circshift!(a, 1) === a
@test a == circshift([1:5;], 1) == [5, 1, 2, 3, 4]
a = [1:5;]
@test circshift!(a, -2) === a
@test a == circshift([1:5;], -2) == [3, 4, 5, 1, 2]
a = [1:5;]
oa = OffsetVector(copy(a), -1)
@test circshift!(oa, 1) === oa
@test oa == circshift(OffsetVector(a, -1), 1)
end

@testset "circcopy" begin
Expand Down

0 comments on commit 5d30157

Please sign in to comment.