Skip to content

Commit

Permalink
Zero dimensional BlockIndex (#431)
Browse files Browse the repository at this point in the history
* Zero dimensional `BlockIndex`

---------

Co-authored-by: Sheehan Olver <[email protected]>
  • Loading branch information
mtfishman and dlfivefifty authored Nov 22, 2024
1 parent 12a6b6c commit bf6f80b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
7 changes: 4 additions & 3 deletions src/blockindices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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,))
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/test_blockarrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/test_blockindices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,))
Expand Down

2 comments on commit bf6f80b

@mtfishman
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/119994

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.2.0 -m "<description of version>" bf6f80b2ae012a387f278cfd4481c96807cf9db7
git push origin v1.2.0

Please sign in to comment.