diff --git a/src/blockaxis.jl b/src/blockaxis.jl index 79b86b23..de144392 100644 --- a/src/blockaxis.jl +++ b/src/blockaxis.jl @@ -157,7 +157,7 @@ julia> blockaxes(B) (BlockRange(Base.OneTo(2)), BlockRange(Base.OneTo(3))) ``` """ -blockaxes(b::BlockedUnitRange) = _blockaxes(b.lasts) +blockaxes(b::BlockedUnitRange) = _blockaxes(blocklasts(b)) _blockaxes(b::AbstractVector) = (Block.(axes(b,1)),) _blockaxes(b::Tuple) = (Block.(Base.OneTo(length(b))),) blockaxes(b) = blockaxes.(axes(b), 1) diff --git a/src/blockbroadcast.jl b/src/blockbroadcast.jl index f8e7e89d..7cfaf02b 100644 --- a/src/blockbroadcast.jl +++ b/src/blockbroadcast.jl @@ -67,7 +67,7 @@ julia> import BlockArrays: SubBlockIterator, BlockIndexRange julia> A = BlockArray(1:6, 1:3); -julia> subblock_lasts = axes(A, 1).lasts; +julia> subblock_lasts = blocklasts(axes(A, 1)); julia> @assert subblock_lasts == [1, 3, 6]; diff --git a/src/blockdeque.jl b/src/blockdeque.jl index 54564ec1..748ef861 100644 --- a/src/blockdeque.jl +++ b/src/blockdeque.jl @@ -32,8 +32,8 @@ blockappend!(dest::BlockVector, s1, s2, sources...) = function blockappend!(dest::BlockVector{<:Any,T}, src::BlockVector{<:Any,T}) where {T} append!(dest.blocks, src.blocks) - offset = last(dest.axes[1]) + 1 - src.axes[1].first - append!(dest.axes[1].lasts, (n + offset for n in src.axes[1].lasts)) + offset = last(dest.axes[1]) + 1 - first(src.axes[1]) + append!(blocklasts(dest.axes[1]), (n + offset for n in blocklasts(src.axes[1]))) return dest end @@ -102,7 +102,7 @@ _newblockfor(dest, block) = function _blockpush!(dest, block) push!(dest.blocks, block) - push!(dest.axes[1].lasts, last(dest.axes[1]) + length(block)) + push!(blocklasts(dest.axes[1]), last(dest.axes[1]) + length(block)) return dest end @@ -144,8 +144,8 @@ blockpushfirst!(dest::BlockVector{<:Any,<:Any}, block) = function _blockpushfirst!(dest, block) pushfirst!(dest.blocks, block) - dest.axes[1].lasts .+= length(block) - 1 + dest.axes[1].first - pushfirst!(dest.axes[1].lasts, length(block)) + blocklasts(dest.axes[1]) .+= length(block) - 1 + first(dest.axes[1]) + pushfirst!(blocklasts(dest.axes[1]), length(block)) return dest end @@ -172,7 +172,7 @@ julia> A """ function blockpop!(A::BlockVector) block = pop!(A.blocks) - pop!(A.axes[1].lasts) + pop!(blocklasts(A.axes[1])) return block end @@ -199,8 +199,8 @@ julia> A """ function blockpopfirst!(A::BlockVector) block = popfirst!(A.blocks) - n = popfirst!(A.axes[1].lasts) - A.axes[1].lasts .-= n + n = popfirst!(blocklasts(A.axes[1])) + blocklasts(A.axes[1]) .-= n return block end @@ -245,7 +245,7 @@ function append_itr!(dest::BlockVector, ::Union{Base.HasShape,Base.HasLength}, s _append_itr_foldfn!(block,i,x) end da, = dest.axes - da.lasts[end] += length(src) + blocklasts(da)[end] += length(src) return dest end @@ -257,7 +257,7 @@ function append_itr!(dest::BlockVector, ::Base.SizeUnknown, src) return n + 1 end da, = dest.axes - da.lasts[end] += n + blocklasts(da)[end] += n return dest end @@ -265,7 +265,7 @@ end function _squash_lasts!(A::BlockVector) while !isempty(A.blocks) && isempty(A.blocks[end]) pop!(A.blocks) - pop!(A.axes[1].lasts) + pop!(blocklasts(A.axes[1])) end end @@ -273,7 +273,7 @@ end function _squash_firsts!(A::BlockVector) while !isempty(A.blocks) && isempty(A.blocks[1]) popfirst!(A.blocks) - popfirst!(A.axes[1].lasts) + popfirst!(blocklasts(A.axes[1])) end end @@ -287,7 +287,7 @@ function Base.pop!(A::BlockVector) isempty(A) && throw(Argument("array must be nonempty")) _squash_lasts!(A) x = pop!(A.blocks[end]) - lasts = A.axes[1].lasts + lasts = blocklasts(A.axes[1]) if isempty(A.blocks[end]) pop!(A.blocks) pop!(lasts) @@ -310,9 +310,9 @@ function Base.popfirst!(A::BlockVector) ax, = A.axes if isempty(A.blocks[1]) popfirst!(A.blocks) - popfirst!(ax.lasts) + popfirst!(blocklasts(ax)) else - ax.lasts[1] -= 1 + blocklasts(ax)[1] -= 1 end return x end @@ -331,6 +331,6 @@ Push items to the beginning of the first block. """ function Base.pushfirst!(A::BlockVector, items...) pushfirst!(A.blocks[1], items...) - A.axes[1].lasts .+= length(items) + blocklasts(A.axes[1]) .+= length(items) return A end diff --git a/test/test_blockdeque.jl b/test/test_blockdeque.jl index db2e4968..29994e0f 100644 --- a/test/test_blockdeque.jl +++ b/test/test_blockdeque.jl @@ -159,6 +159,13 @@ using BlockArrays, Test end @test A == [] @test B == 5:-1:1 + + @testset "empty blocks" begin + B = BlockArray([1:6;], [1,2,3,0,0]) + @test pop!(B) == 6 + @test B == 1:5 + @test !any(isempty, blocks(B)) + end end @testset "popfirst!" begin