Skip to content

Commit 76f578a

Browse files
committed
fix zero-dim similar
1 parent b55c7a4 commit 76f578a

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/blockarray.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,10 @@ end
453453

454454
@inline Base.similar(B::BlockArray, ::Type{T}) where {T} = _BlockArray(similar.(blocks(B), T), axes(B))
455455

456+
# specific 0-dim
457+
@inline Base.similar(B::BlockArray{<:Any,0}, ::Type{T}) where {T} = BlockArray(similar(only(blocks(B)), T), ())
458+
@inline Base.similar(::BlockArray, ::Type{T}, ::Tuple{}) where {T} = BlockArray(Array{T}(undef))
459+
456460
const OffsetAxis = Union{Integer, UnitRange, Base.OneTo, Base.IdentityUnitRange}
457461

458462
# avoid ambiguities

src/blockedarray.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ function Base.similar(block_array::BlockedArray{T,N}, ::Type{T2}) where {T,N,T2}
203203
BlockedArray(similar(block_array.blocks, T2), axes(block_array))
204204
end
205205

206+
# specific zero dim
207+
Base.similar(::BlockedArray{<:Any,0}, ::Type{T}, ::Tuple{}) where {T} = BlockedArray(Array{T}(undef))
208+
206209
to_axes(r::AbstractUnitRange) = r
207210
to_axes(n::Integer) = Base.oneto(n)
208211

test/test_blockarrays.jl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,13 +343,18 @@ end
343343
view(ret)[] = 0
344344
@test ret[] == 0
345345

346-
ret = BlockArrays.BlockArray(zeros())
346+
ret = BlockArray(zeros())
347347
@test ret isa BlockArray{Float64, 0}
348348
@test size(ret) == ()
349349
@test all(iszero, ret)
350350
@test ret[Block()] == zeros()
351351

352-
ret = BlockArrays.BlockArray(zeros(1,1))
352+
@test similar(ret) isa BlockArray{Float64, 0}
353+
@test similar(ret, Float32) isa BlockArray{Float32, 0}
354+
@test similar(ret, Float32, ()) isa BlockArray{Float32, 0}
355+
@test similar(ret, Float32, (blockedrange([1]),)) isa BlockVector{Float32}
356+
357+
ret = BlockArray(zeros(1,1))
353358
@test reshape(ret, ()) isa AbstractBlockArray{Float64, 0} # may be BlockedArray
354359
@test size(reshape(ret, ())) == ()
355360

@@ -373,7 +378,12 @@ end
373378
@test all(iszero, ret)
374379
@test ret[Block()] == zeros()
375380

376-
ret = BlockArrays.BlockedArray(zeros(1,1))
381+
@test similar(ret) isa BlockedArray{Float64, 0}
382+
@test similar(ret, Float32) isa BlockedArray{Float32, 0}
383+
@test similar(ret, Float32, ()) isa BlockedArray{Float32, 0}
384+
@test similar(ret, Float32, (blockedrange([1]),)) isa BlockedVector{Float32}
385+
386+
ret = BlockedArray(zeros(1,1))
377387
@test reshape(ret, ()) isa BlockedArray{Float64, 0}
378388
@test size(reshape(ret, ())) == ()
379389
end

0 commit comments

Comments
 (0)