Skip to content

Commit

Permalink
Remove first/lasts field access by accessor functions (#343)
Browse files Browse the repository at this point in the history
* Remove first/lasts field access by accessor functions

* Test for pop with empty blocks
  • Loading branch information
jishnub authored Mar 21, 2024
1 parent e0d9eca commit ac4df2a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/blockaxis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/blockbroadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
32 changes: 16 additions & 16 deletions src/blockdeque.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -257,23 +257,23 @@ 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

# remove empty blocks at the 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

# remove empty blocks at the beginning
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

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

0 comments on commit ac4df2a

Please sign in to comment.