Skip to content

Commit a1c89dc

Browse files
Improve n-dimensional slicing of BlockIndexRange (#463)
Fixes #355. This is also fixed as a part of #462 but this PR is narrower in scope so could be merged more quickly. --------- Co-authored-by: Sheehan Olver <[email protected]>
1 parent f6c5555 commit a1c89dc

File tree

3 files changed

+5
-1
lines changed

3 files changed

+5
-1
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ name = "BlockArrays"
22
uuid = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
33
version = "1.7.0"
44

5+
56
[deps]
67
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
78
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"

src/blockindices.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ getindex(B::Block{N}, inds::Vararg{AbstractUnitRange{<:Integer},N}) where N = Bl
211211
getindex(B::Block{1}, inds::Colon) = B
212212
getindex(B::Block{1}, inds::Base.Slice) = B
213213

214-
@propagate_inbounds getindex(B::BlockIndexRange{1}, kr::AbstractUnitRange{<:Integer}) = BlockIndexRange(B.block, B.indices[1][kr])
214+
getindex(B::BlockIndexRange{0}) = B.block[]
215+
@propagate_inbounds getindex(B::BlockIndexRange{N}, kr::Vararg{AbstractUnitRange{<:Integer},N}) where {N} = BlockIndexRange(B.block, map(getindex, B.indices, kr))
215216
@propagate_inbounds getindex(B::BlockIndexRange{N}, inds::Vararg{Int,N}) where N = B.block[Base.reindex(B.indices, inds)...]
216217

217218
eltype(R::BlockIndexRange) = eltype(typeof(R))

test/test_blockindices.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ import BlockArrays: BlockIndex, BlockIndexRange, BlockSlice, BlockedSlice
9090
@test Block(1,1)[1,1] == BlockIndex((1,1),(1,1)) == BlockIndex((1,1),(1,))
9191
@test Block(1,1)[1:2,1:2] == BlockIndexRange(Block(1,1),(1:2,1:2))
9292
@test Block(1)[1:3][1:2] == BlockIndexRange(Block(1),1:2)
93+
@test Block(1,1)[2:4,2:4][2:3,2:3] == BlockIndexRange(Block(1,1),(3:4,3:4))
94+
@test BlockIndexRange(Block(),())[] == BlockIndex()
9395
@test BlockIndex((2,2,2),(2,)) == BlockIndex((2,2,2),(2,1,)) == BlockIndex((2,2,2),(2,1,1))
9496
@test BlockIndex(2,(2,)) === BlockIndex((2,),(2,))
9597
@test BlockIndex(UInt(2),(2,)) === BlockIndex((UInt(2),),(2,))

0 commit comments

Comments
 (0)