Skip to content

Commit bf6f80b

Browse files
Zero dimensional BlockIndex (#431)
* Zero dimensional `BlockIndex` --------- Co-authored-by: Sheehan Olver <[email protected]>
1 parent 12a6b6c commit bf6f80b

File tree

4 files changed

+8
-6
lines changed

4 files changed

+8
-6
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 = "1.1.1"
3+
version = "1.2.0"
44

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

src/blockindices.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,10 @@ last(b::Block) = b
5252
iterate(x::Block) = (x, nothing)
5353
iterate(x::Block, ::Any) = nothing
5454
isempty(x::Block) = false
55-
broadcastable(x::Block) = x
55+
broadcastable(x::Block) = Ref(x)
5656
ndims(::Type{<:Block}) = 0
5757
ndims(::Block) = 0
5858
eltype(::Type{B}) where B<:Block = B
59-
getindex(B::Block, ::CartesianIndex{0}) = B
6059

6160
# The following code is taken from CartesianIndex
6261
@inline (+)(index::Block{N}) where {N} = Block{N}(map(+, index.n))
@@ -147,10 +146,12 @@ struct BlockIndex{N,TI<:Tuple{Vararg{Integer,N}},Tα<:Tuple{Vararg{Integer,N}}}
147146
end
148147

149148
@inline BlockIndex(a::NTuple{N,Block{1}}, b::Tuple) where N = BlockIndex(Int.(a), b)
149+
@inline BlockIndex(::Tuple{}, b::Tuple{}) = BlockIndex{0,Tuple{},Tuple{}}((), ())
150150

151151
@inline BlockIndex(a::Integer, b::Integer) = BlockIndex((a,), (b,))
152152
@inline BlockIndex(a::Tuple, b::Integer) = BlockIndex(a, (b,))
153153
@inline BlockIndex(a::Integer, b::Tuple) = BlockIndex((a,), b)
154+
@inline BlockIndex() = BlockIndex((), ())
154155

155156
@inline BlockIndex(a::Block, b::Tuple) = BlockIndex(a.n, b)
156157
@inline BlockIndex(a::Block, b::Integer) = BlockIndex(a, (b,))
@@ -202,7 +203,7 @@ BlockIndexRange(block::Block{N}, inds::Vararg{AbstractUnitRange{<:Integer},N}) w
202203

203204
block(R::BlockIndexRange) = R.block
204205

205-
getindex(::Block{0}) = Block()
206+
getindex(::Block{0}) = BlockIndex()
206207
getindex(B::Block{N}, inds::Vararg{Integer,N}) where N = BlockIndex(B,inds)
207208
getindex(B::Block{N}, inds::Vararg{AbstractUnitRange{<:Integer},N}) where N = BlockIndexRange(B,inds)
208209
getindex(B::Block{1}, inds::Colon) = B

test/test_blockarrays.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ end
333333
@test size(ret) == ()
334334
@test all(iszero, ret)
335335
@test ret[Block()] == zeros()
336-
@test ret[Block()[]] == zeros()
336+
@test ret[Block()[]] == 0
337337
@test ret[] == 0
338338
@test view(ret, Block()) == zeros()
339339
@test Array(ret) == zeros()
@@ -360,7 +360,7 @@ end
360360
@test all(iszero, ret)
361361
@test ret[] == 0
362362
@test ret[Block()] == zeros()
363-
@test ret[Block()[]] == zeros()
363+
@test ret[Block()[]] == 0
364364
@test Array(ret) == zeros()
365365
ret[] = 1
366366
@test ret[] == 1

test/test_blockindices.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ import BlockArrays: BlockIndex, BlockIndexRange, BlockSlice
8484
end
8585

8686
@testset "BlockIndex" begin
87+
@test Block()[] == BlockIndex()
8788
@test Block(1)[1] == BlockIndex((1,),(1,))
8889
@test Block(1)[1:2] == BlockIndexRange(Block(1),(1:2,))
8990
@test Block(1,1)[1,1] == BlockIndex((1,1),(1,1)) == BlockIndex((1,1),(1,))

0 commit comments

Comments
 (0)