From 34afb332ce7d06d6ab8126682ab1b1615077f2eb Mon Sep 17 00:00:00 2001 From: GregPlowman Date: Tue, 19 Jan 2021 08:52:40 +1100 Subject: [PATCH 1/2] Add methods for pushfirst! and popfirst! --- src/OffsetArrays.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/OffsetArrays.jl b/src/OffsetArrays.jl index 582661dd..f844b8f6 100644 --- a/src/OffsetArrays.jl +++ b/src/OffsetArrays.jl @@ -367,7 +367,9 @@ Base.show(io::IO, ::MIME"text/plain", r::OffsetRange) = show(io, r) Base.resize!(A::OffsetVector, nl::Integer) = (resize!(A.parent, nl); A) Base.push!(A::OffsetVector, x...) = (push!(A.parent, x...); A) +Base.pushfirst!(A::OffsetVector, x...) = (pushfirst!(A.parent, x...); A) Base.pop!(A::OffsetVector) = pop!(A.parent) +Base.popfirst!(A::OffsetVector) = popfirst!(A.parent) Base.append!(A::OffsetVector, items) = (append!(A.parent, items); A) Base.empty!(A::OffsetVector) = (empty!(A.parent); A) From 005e5098d35def6f985d6205d6cab0100bbddfa2 Mon Sep 17 00:00:00 2001 From: GregPlowman Date: Tue, 19 Jan 2021 08:57:57 +1100 Subject: [PATCH 2/2] Tests for pushfirst! and popfirst! --- test/runtests.jl | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index 91571549..f2804391 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1440,14 +1440,37 @@ end @test push!(o, 2, 3) === o @test axes(o, 1) == 0:2 @test o[end-1:end] == [2, 3] + + # pushfirst! + o = OffsetVector(Int[], -1) + @test pushfirst!(o) === o + @test axes(o, 1) == 0:-1 + @test pushfirst!(o, 1) === o + @test axes(o, 1) == 0:0 + @test o[end] == 1 + @test pushfirst!(o, 2, 3) === o + @test axes(o, 1) == 0:2 + if VERSION >= v"1.5" + @test o[begin:begin+1] == [2, 3] + else + @test o[0:1] == [2, 3] + end + # pop! o = OffsetVector([1, 2, 3], -1) @test pop!(o) == 3 @test axes(o, 1) == 0:1 + + # popfirst! + o = OffsetVector([1, 2, 3], -1) + @test popfirst!(o) == 1 + @test axes(o, 1) == 0:1 + # append! o = OffsetVector([1, 2, 3], -1) append!(o, [4, 5]) @test axes(o, 1) == 0:4 + # empty! o = OffsetVector([1, 2, 3], -1) @test empty!(o) === o