Skip to content

Commit

Permalink
Preserve indexing of Blocks in BlockSlice
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed Mar 26, 2024
1 parent 129f8c8 commit 20caf08
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/blockindices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,13 @@ _indices(B) = B

@propagate_inbounds getindex(S::BlockSlice, i::Integer) = getindex(S.indices, i)
@propagate_inbounds getindex(S::BlockSlice{<:Block{1}}, k::AbstractUnitRange{Int}) =
BlockSlice(S.block, S.indices[_indices(k)])
BlockSlice(S.block[_indices(k)], S.indices[_indices(k)])
@propagate_inbounds getindex(S::BlockSlice{<:BlockIndexRange{1}}, k::AbstractUnitRange{Int}) =
BlockSlice(S.block, S.indices[_indices(k)])
BlockSlice(S.block[_indices(k)], S.indices[_indices(k)])
show(io::IO, r::BlockSlice) = print(io, "BlockSlice(", r.block, ",", r.indices, ")")

# Avoid creating a SubArray wrapper in certain non-allocating cases
Base.@propagate_inbounds Base.view(C::CartesianIndices{N}, bs::Vararg{BlockSlice,N}) where {N} = C[bs...]
Base.@propagate_inbounds Base.view(C::CartesianIndices{N}, bs::Vararg{BlockSlice,N}) where {N} = view(C, map(x->x.indices, bs)...)

Block(bs::BlockSlice{<:BlockIndexRange}) = Block(bs.block)

Expand Down
22 changes: 13 additions & 9 deletions test/test_blockindices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,24 @@ import BlockArrays: BlockIndex, BlockIndexRange, BlockSlice
@test view(r, Block(1)) === r
@test_throws BlockBoundsError view(r, Block(2))
C = CartesianIndices((2:10, 2:10))
@test view(C, Block(1,1)) === C
=== VERSION >= v"1.10" ? (===) : (==)

@test view(C, Block(1,1)) ==ᵥ C
@test view(C, Block(1), Block(1)) == C
@test_throws BlockBoundsError view(C, Block(1), Block(2))
@test_throws BlockBoundsError view(C, Block(1,2))

B = BlockArray([1:3;], [2,1])
Cb = CartesianIndices(B)
@test view(Cb, Block(1)) === Cb[Block(1)] === CartesianIndices((1:2,))
@test view(Cb, Block(2)) === Cb[Block(2)] === CartesianIndices((3:3,))
@test view(Cb, Block(1)) == Cb[Block(1)] == CartesianIndices((1:2,))
@test view(Cb, Block(2)) == Cb[Block(2)] == CartesianIndices((3:3,))

B = BlockArray(reshape([1:9;],3,3), [2,1], [2,1])
Cb = CartesianIndices(B)
@test view(Cb, Block(1,1)) === Cb[Block(1,1)] === CartesianIndices((1:2,1:2))
@test view(Cb, Block(1,2)) === Cb[Block(1,2)] === CartesianIndices((1:2, 3:3))
@test view(Cb, Block(2,1)) === Cb[Block(2,1)] === CartesianIndices((3:3,1:2))
@test view(Cb, Block(2,2)) === Cb[Block(2,2)] === CartesianIndices((3:3, 3:3))
@test view(Cb, Block(1,1)) == Cb[Block(1,1)] == CartesianIndices((1:2,1:2))
@test view(Cb, Block(1,2)) == Cb[Block(1,2)] == CartesianIndices((1:2, 3:3))
@test view(Cb, Block(2,1)) == Cb[Block(2,1)] == CartesianIndices((3:3,1:2))
@test view(Cb, Block(2,2)) == Cb[Block(2,2)] == CartesianIndices((3:3, 3:3))
for i in 1:2, j in 1:2
@test view(Cb, Block(j), Block(i)) == view(Cb, Block(j, i)) == Cb[Block(j, i)]
end
Expand Down Expand Up @@ -431,9 +433,9 @@ end

@testset "BlockSlice" begin
b = BlockSlice(Block(5),1:3)
@test b[b] === b
@test b[b] == b
@test b[Base.Slice(1:3)] b
@test b[1:2] b[1:2][1:2] BlockSlice(Block(5),1:2)
@test b[1:2] b[1:2][1:2] BlockSlice(Block(5)[1:2],1:2)
@test Block(b) Block(5)

@testset "OneTo converts" begin
Expand All @@ -448,6 +450,8 @@ end
b = BlockSlice(Block(1), r)
@test view(C, b) === view(C, r)
@test view(1:10, b) === view(1:10, r)
C = CartesianIndices((1:3, 1:3))
@test view(C, b, b) === view(C, r, r)
end
end

Expand Down

0 comments on commit 20caf08

Please sign in to comment.