Skip to content

Commit ddc8d05

Browse files
committed
Define push!, pop! and empty! for OffsetVector.
1 parent 07f33c3 commit ddc8d05

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/OffsetArrays.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,13 @@ Base.fill(x, inds::Tuple{UnitRange,Vararg{UnitRange}}) =
195195
fill!(OffsetArray{typeof(x)}(undef, inds), x)
196196
@inline Base.fill(x, ind1::UnitRange, inds::UnitRange...) = fill(x, (ind1, inds...))
197197

198+
199+
### Some mutating functions defined only for OffsetVector ###
200+
198201
Base.resize!(A::OffsetVector, nl::Integer) = (resize!(A.parent, nl); A)
202+
Base.push!(A::OffsetVector, x...) = (push!(A.parent, x...); A)
203+
Base.pop!(A::OffsetVector) = pop!(A.parent)
204+
Base.empty!(A::OffsetVector) = (empty!(A.parent); A)
199205

200206
### Low-level utilities ###
201207

test/runtests.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,3 +554,24 @@ end
554554
O2[1, 1] = x + 1 # just a sanity check
555555
@test A[2, 2] == x + 1
556556
end
557+
558+
@testset "mutating functions for OffsetVector" begin
559+
# push!
560+
o = OffsetVector(Int[], -1)
561+
@test push!(o) === o
562+
@test axes(o, 1) == 0:-1
563+
@test push!(o, 1) === o
564+
@test axes(o, 1) == 0:0
565+
@test o[end] == 1
566+
@test push!(o, 2, 3) === o
567+
@test axes(o, 1) == 0:2
568+
@test o[end-1:end] == [2, 3]
569+
# pop!
570+
o = OffsetVector([1, 2, 3], -1)
571+
@test pop!(o) == 3
572+
@test axes(o, 1) == 0:1
573+
# empty!
574+
o = OffsetVector([1, 2, 3], -1)
575+
@test empty!(o) === o
576+
@test axes(o, 1) == 0:-1
577+
end

0 commit comments

Comments
 (0)