From f1c4d54696c2d296a10a8f8d659f6a3e85da9a29 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Tue, 27 Sep 2022 21:15:51 +0200 Subject: [PATCH] Fix shift direction of circshift! for vectors (#46759) 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. --- base/abstractarray.jl | 5 +++-- test/arrayops.jl | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index e0ba09f10c063..815c7256d744c 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -3473,8 +3473,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 diff --git a/test/arrayops.jl b/test/arrayops.jl index 45e7451c22a78..50dc8d45de6e8 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -769,6 +769,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