Skip to content

Commit ac4df2a

Browse files
authored
Remove first/lasts field access by accessor functions (#343)
* Remove first/lasts field access by accessor functions * Test for pop with empty blocks
1 parent e0d9eca commit ac4df2a

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

src/blockaxis.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ julia> blockaxes(B)
157157
(BlockRange(Base.OneTo(2)), BlockRange(Base.OneTo(3)))
158158
```
159159
"""
160-
blockaxes(b::BlockedUnitRange) = _blockaxes(b.lasts)
160+
blockaxes(b::BlockedUnitRange) = _blockaxes(blocklasts(b))
161161
_blockaxes(b::AbstractVector) = (Block.(axes(b,1)),)
162162
_blockaxes(b::Tuple) = (Block.(Base.OneTo(length(b))),)
163163
blockaxes(b) = blockaxes.(axes(b), 1)

src/blockbroadcast.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ julia> import BlockArrays: SubBlockIterator, BlockIndexRange
6767
6868
julia> A = BlockArray(1:6, 1:3);
6969
70-
julia> subblock_lasts = axes(A, 1).lasts;
70+
julia> subblock_lasts = blocklasts(axes(A, 1));
7171
7272
julia> @assert subblock_lasts == [1, 3, 6];
7373

src/blockdeque.jl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ blockappend!(dest::BlockVector, s1, s2, sources...) =
3232

3333
function blockappend!(dest::BlockVector{<:Any,T}, src::BlockVector{<:Any,T}) where {T}
3434
append!(dest.blocks, src.blocks)
35-
offset = last(dest.axes[1]) + 1 - src.axes[1].first
36-
append!(dest.axes[1].lasts, (n + offset for n in src.axes[1].lasts))
35+
offset = last(dest.axes[1]) + 1 - first(src.axes[1])
36+
append!(blocklasts(dest.axes[1]), (n + offset for n in blocklasts(src.axes[1])))
3737
return dest
3838
end
3939

@@ -102,7 +102,7 @@ _newblockfor(dest, block) =
102102

103103
function _blockpush!(dest, block)
104104
push!(dest.blocks, block)
105-
push!(dest.axes[1].lasts, last(dest.axes[1]) + length(block))
105+
push!(blocklasts(dest.axes[1]), last(dest.axes[1]) + length(block))
106106
return dest
107107
end
108108

@@ -144,8 +144,8 @@ blockpushfirst!(dest::BlockVector{<:Any,<:Any}, block) =
144144

145145
function _blockpushfirst!(dest, block)
146146
pushfirst!(dest.blocks, block)
147-
dest.axes[1].lasts .+= length(block) - 1 + dest.axes[1].first
148-
pushfirst!(dest.axes[1].lasts, length(block))
147+
blocklasts(dest.axes[1]) .+= length(block) - 1 + first(dest.axes[1])
148+
pushfirst!(blocklasts(dest.axes[1]), length(block))
149149
return dest
150150
end
151151

@@ -172,7 +172,7 @@ julia> A
172172
"""
173173
function blockpop!(A::BlockVector)
174174
block = pop!(A.blocks)
175-
pop!(A.axes[1].lasts)
175+
pop!(blocklasts(A.axes[1]))
176176
return block
177177
end
178178

@@ -199,8 +199,8 @@ julia> A
199199
"""
200200
function blockpopfirst!(A::BlockVector)
201201
block = popfirst!(A.blocks)
202-
n = popfirst!(A.axes[1].lasts)
203-
A.axes[1].lasts .-= n
202+
n = popfirst!(blocklasts(A.axes[1]))
203+
blocklasts(A.axes[1]) .-= n
204204
return block
205205
end
206206

@@ -245,7 +245,7 @@ function append_itr!(dest::BlockVector, ::Union{Base.HasShape,Base.HasLength}, s
245245
_append_itr_foldfn!(block,i,x)
246246
end
247247
da, = dest.axes
248-
da.lasts[end] += length(src)
248+
blocklasts(da)[end] += length(src)
249249
return dest
250250
end
251251

@@ -257,23 +257,23 @@ function append_itr!(dest::BlockVector, ::Base.SizeUnknown, src)
257257
return n + 1
258258
end
259259
da, = dest.axes
260-
da.lasts[end] += n
260+
blocklasts(da)[end] += n
261261
return dest
262262
end
263263

264264
# remove empty blocks at the end
265265
function _squash_lasts!(A::BlockVector)
266266
while !isempty(A.blocks) && isempty(A.blocks[end])
267267
pop!(A.blocks)
268-
pop!(A.axes[1].lasts)
268+
pop!(blocklasts(A.axes[1]))
269269
end
270270
end
271271

272272
# remove empty blocks at the beginning
273273
function _squash_firsts!(A::BlockVector)
274274
while !isempty(A.blocks) && isempty(A.blocks[1])
275275
popfirst!(A.blocks)
276-
popfirst!(A.axes[1].lasts)
276+
popfirst!(blocklasts(A.axes[1]))
277277
end
278278
end
279279

@@ -287,7 +287,7 @@ function Base.pop!(A::BlockVector)
287287
isempty(A) && throw(Argument("array must be nonempty"))
288288
_squash_lasts!(A)
289289
x = pop!(A.blocks[end])
290-
lasts = A.axes[1].lasts
290+
lasts = blocklasts(A.axes[1])
291291
if isempty(A.blocks[end])
292292
pop!(A.blocks)
293293
pop!(lasts)
@@ -310,9 +310,9 @@ function Base.popfirst!(A::BlockVector)
310310
ax, = A.axes
311311
if isempty(A.blocks[1])
312312
popfirst!(A.blocks)
313-
popfirst!(ax.lasts)
313+
popfirst!(blocklasts(ax))
314314
else
315-
ax.lasts[1] -= 1
315+
blocklasts(ax)[1] -= 1
316316
end
317317
return x
318318
end
@@ -331,6 +331,6 @@ Push items to the beginning of the first block.
331331
"""
332332
function Base.pushfirst!(A::BlockVector, items...)
333333
pushfirst!(A.blocks[1], items...)
334-
A.axes[1].lasts .+= length(items)
334+
blocklasts(A.axes[1]) .+= length(items)
335335
return A
336336
end

test/test_blockdeque.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ using BlockArrays, Test
159159
end
160160
@test A == []
161161
@test B == 5:-1:1
162+
163+
@testset "empty blocks" begin
164+
B = BlockArray([1:6;], [1,2,3,0,0])
165+
@test pop!(B) == 6
166+
@test B == 1:5
167+
@test !any(isempty, blocks(B))
168+
end
162169
end
163170

164171
@testset "popfirst!" begin

0 commit comments

Comments
 (0)