Skip to content

Commit e427fc8

Browse files
authored
Allow resize!(A, Block(0)) to make empty (#210)
* Support resize!(a, Block(0)) to make empty * Special case isless with Block{1} * add BlockVector resize! * add printing tests
1 parent a192e2f commit e427fc8

File tree

6 files changed

+50
-5
lines changed

6 files changed

+50
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "BlockArrays"
22
uuid = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
3-
version = "0.16.14"
3+
version = "0.16.15"
44

55
[deps]
66
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"

src/blockarray.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,3 +471,17 @@ Base.reshape(block_array::BlockArray, axes::Tuple{Union{Integer,Base.OneTo}, Var
471471
reshape(PseudoBlockArray(block_array), axes)
472472
Base.reshape(block_array::BlockArray, dims::Tuple{Vararg{Union{Int,Colon}}}) =
473473
reshape(PseudoBlockArray(block_array), dims)
474+
475+
"""
476+
resize!(a::BlockVector, N::Block) -> PseudoBlockVector
477+
478+
Resize `a` to contain the first `N` blocks, returning a new `BlockVector` sharing
479+
memory with `a`. If `N` is smaller than the current
480+
collection block length, the first `N` blocks will be retained. `N` is not allowed to be larger.
481+
"""
482+
function resize!(a::BlockVector, N::Block{1})
483+
ax = axes(a,1)
484+
Ni = Int(N)
485+
_BlockArray(resize!(a.blocks, Ni), (ax[Block.(Base.OneTo(Ni))],))
486+
end
487+

src/blockindices.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ julia> A[Block(1, 1)]
2020
struct Block{N, T}
2121
n::NTuple{N, T}
2222
Block{N, T}(n::NTuple{N, T}) where {N, T} = new{N, T}(n)
23+
Block{1, T}(n::Tuple{T}) where T = new{1, T}(n)
2324
end
2425

25-
26-
Block{N, T}(n::Vararg{T, N}) where {N,T} = Block{N, T}(n)
26+
Block{N, T}(n::Tuple{Vararg{Any, N}}) where {N,T} = Block{N, T}(convert(NTuple{N,T}, n))
27+
Block{N, T}(n::Vararg{Any, N}) where {N,T} = Block{N, T}(n)
2728
Block{N}(n::Vararg{T, N}) where {N,T} = Block{N, T}(n)
28-
Block() = Block{0,Int}()
29+
Block{1, T}(n::Tuple{Any}) where {N,T} = Block{1, T}(convert(Tuple{T}, n))
30+
Block{0}() = Block{0,Int}()
31+
Block() = Block{0}()
2932
Block(n::Vararg{T, N}) where {N,T} = Block{N, T}(n)
3033
Block{1}(n::Tuple{T}) where {T} = Block{1, T}(n)
3134
Block{N}(n::NTuple{N, T}) where {N,T} = Block{N, T}(n)
@@ -77,6 +80,7 @@ end
7780
_isless(ret, ::Tuple{}, ::Tuple{}) = ifelse(ret==1, true, false)
7881
icmp(a, b) = ifelse(isless(a,b), 1, ifelse(a==b, 0, -1))
7982
@inline isless(I1::Block{N}, I2::Block{N}) where {N} = _isless(0, I1.n, I2.n)
83+
@inline isless(I1::Block{1}, I2::Block{1}) = isless(Integer(I1), Integer(I2))
8084

8185
# conversions
8286
convert(::Type{T}, index::Block{1}) where {T<:Number} = convert(T, index.n[1])

src/pseudo_blockarray.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,15 @@ collection block length, the first `N` blocks will be retained. `N` is not allow
258258
"""
259259
function resize!(a::PseudoBlockVector, N::Block{1})
260260
ax = axes(a,1)
261-
PseudoBlockVector(resize!(a.blocks, last(ax[N])), (ax[Block.(Base.OneTo(Int(N)))],))
261+
if iszero(Int(N))
262+
PseudoBlockVector(resize!(a.blocks, 0), (ax[Block.(Base.OneTo(0))],))
263+
else
264+
PseudoBlockVector(resize!(a.blocks, last(ax[N])), (ax[Block.(Base.OneTo(Int(N)))],))
265+
end
262266
end
263267

264268

269+
265270
###########################
266271
# Strided Array interface #
267272
###########################

test/test_blockarrays.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,5 +606,14 @@ end
606606
b = resize!(a,Block(2))
607607
@test b == 1:3
608608
@test_throws BoundsError a[4] # length of a.blocks has changed
609+
c = resize!(b,Block(0))
610+
@test isempty(c)
611+
612+
a = BlockVector(collect(1:6), 1:3)
613+
b = resize!(a,Block(2))
614+
@test b == 1:3
615+
@test_throws BoundsError a[4] # length of a.blocks has changed
616+
c = resize!(b,Block(0))
617+
@test isempty(c)
609618
end
610619
end

test/test_blockindices.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ import BlockArrays: BlockIndex, BlockIndexRange, BlockSlice
9191

9292
@test intersect(Block.(2:5), Block.(3:6)) Block.(3:5)
9393
end
94+
95+
@testset "print" begin
96+
@test stringmime("text/plain", Block()) == "Block()"
97+
@test stringmime("text/plain", Block(1)) == "Block(1)"
98+
@test stringmime("text/plain", Block(1,2)) == "Block(1, 2)"
99+
@test stringmime("text/plain", Block{0}()) == "Block()"
100+
@test stringmime("text/plain", Block{1}(1)) == "Block(1)"
101+
@test stringmime("text/plain", Block{2}(1,2)) == "Block(1, 2)"
102+
103+
@test stringmime("text/plain", Block{0,BigInt}()) == "Block{0, BigInt}(())"
104+
@test stringmime("text/plain", Block{1,BigInt}(1)) == "Block{1, BigInt}((1,))"
105+
@test stringmime("text/plain", Block{2}(1,2)) == "Block(1, 2)"
106+
end
94107
end
95108

96109
@testset "BlockedUnitRange" begin

0 commit comments

Comments
 (0)