From bf6f80b2ae012a387f278cfd4481c96807cf9db7 Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Fri, 22 Nov 2024 11:59:49 -0500 Subject: [PATCH] Zero dimensional `BlockIndex` (#431) * Zero dimensional `BlockIndex` --------- Co-authored-by: Sheehan Olver --- Project.toml | 2 +- src/blockindices.jl | 7 ++++--- test/test_blockarrays.jl | 4 ++-- test/test_blockindices.jl | 1 + 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Project.toml b/Project.toml index 48f106a1..acbb824d 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "BlockArrays" uuid = "8e7c35d0-a365-5155-bbbb-fb81a777f24e" -version = "1.1.1" +version = "1.2.0" [deps] ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a" diff --git a/src/blockindices.jl b/src/blockindices.jl index f00f54a0..a5b5119e 100644 --- a/src/blockindices.jl +++ b/src/blockindices.jl @@ -52,11 +52,10 @@ last(b::Block) = b iterate(x::Block) = (x, nothing) iterate(x::Block, ::Any) = nothing isempty(x::Block) = false -broadcastable(x::Block) = x +broadcastable(x::Block) = Ref(x) ndims(::Type{<:Block}) = 0 ndims(::Block) = 0 eltype(::Type{B}) where B<:Block = B -getindex(B::Block, ::CartesianIndex{0}) = B # The following code is taken from CartesianIndex @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}}} end @inline BlockIndex(a::NTuple{N,Block{1}}, b::Tuple) where N = BlockIndex(Int.(a), b) +@inline BlockIndex(::Tuple{}, b::Tuple{}) = BlockIndex{0,Tuple{},Tuple{}}((), ()) @inline BlockIndex(a::Integer, b::Integer) = BlockIndex((a,), (b,)) @inline BlockIndex(a::Tuple, b::Integer) = BlockIndex(a, (b,)) @inline BlockIndex(a::Integer, b::Tuple) = BlockIndex((a,), b) +@inline BlockIndex() = BlockIndex((), ()) @inline BlockIndex(a::Block, b::Tuple) = BlockIndex(a.n, b) @inline BlockIndex(a::Block, b::Integer) = BlockIndex(a, (b,)) @@ -202,7 +203,7 @@ BlockIndexRange(block::Block{N}, inds::Vararg{AbstractUnitRange{<:Integer},N}) w block(R::BlockIndexRange) = R.block -getindex(::Block{0}) = Block() +getindex(::Block{0}) = BlockIndex() getindex(B::Block{N}, inds::Vararg{Integer,N}) where N = BlockIndex(B,inds) getindex(B::Block{N}, inds::Vararg{AbstractUnitRange{<:Integer},N}) where N = BlockIndexRange(B,inds) getindex(B::Block{1}, inds::Colon) = B diff --git a/test/test_blockarrays.jl b/test/test_blockarrays.jl index f6ff21a4..d24fb52e 100644 --- a/test/test_blockarrays.jl +++ b/test/test_blockarrays.jl @@ -333,7 +333,7 @@ end @test size(ret) == () @test all(iszero, ret) @test ret[Block()] == zeros() - @test ret[Block()[]] == zeros() + @test ret[Block()[]] == 0 @test ret[] == 0 @test view(ret, Block()) == zeros() @test Array(ret) == zeros() @@ -360,7 +360,7 @@ end @test all(iszero, ret) @test ret[] == 0 @test ret[Block()] == zeros() - @test ret[Block()[]] == zeros() + @test ret[Block()[]] == 0 @test Array(ret) == zeros() ret[] = 1 @test ret[] == 1 diff --git a/test/test_blockindices.jl b/test/test_blockindices.jl index d14ce4d2..d6e4b67b 100644 --- a/test/test_blockindices.jl +++ b/test/test_blockindices.jl @@ -84,6 +84,7 @@ import BlockArrays: BlockIndex, BlockIndexRange, BlockSlice end @testset "BlockIndex" begin + @test Block()[] == BlockIndex() @test Block(1)[1] == BlockIndex((1,),(1,)) @test Block(1)[1:2] == BlockIndexRange(Block(1),(1:2,)) @test Block(1,1)[1,1] == BlockIndex((1,1),(1,1)) == BlockIndex((1,1),(1,))