Skip to content

Add pushfirst! and popfirst! methods for OffsetVectors #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
23 changes: 23 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down