Skip to content

Commit a1f2273

Browse files
authored
Fix axes and size of BlockIndices (#490)
The current definition of `size(iter::BlockIndices)` was assuming the indices were ranges (resulting from #483 where `BlockIndexRange` was generalized to `BlockIndices` but the definition if `size` wasn't updated accordingly), so for (non-contiguous) non-ranges it was giving the wrong result, this PR fixes that. Additionally, previously only `size` was defined which meant that BlockIndices where the indices had non-trivial axes (like blocked axes) lost information, which is now fixed and tested.
1 parent d482cd9 commit a1f2273

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
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.9"
3+
version = "1.9.1"
44

55

66
[deps]

src/blockindices.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ BlockIndex(indcs::Tuple{Vararg{BlockIndex{1},N}}) where N = BlockIndex(block.(in
202202
bl = block(I)
203203
checkbounds(Bool, A, bl) || return false
204204
# TODO: Replace with `eachblockaxes(A)[bl]` once that is defined.
205-
binds = map(Base.axes1 getindex, axes(A), Tuple(bl))
205+
binds = map(axes1 getindex, axes(A), Tuple(bl))
206206
Base.checkbounds_indices(Bool, binds, (blockindex(I),))
207207
end
208208
checkbounds(::Type{Bool}, A::AbstractArray{<:Any,N}, I::AbstractArray{<:BlockIndex{N}}) where N =
@@ -374,8 +374,8 @@ end
374374
nextstate, nextstate
375375
end
376376

377-
size(iter::BlockIndices) = map(dimlength, first(iter).α, last(iter).α)
378-
length(iter::BlockIndices) = prod(size(iter))
377+
axes(iter::BlockIndices) = map(axes1, iter.indices)
378+
size(iter::BlockIndices) = map(length, iter.indices)
379379

380380

381381
Block(bs::BlockIndices) = bs.block
@@ -388,7 +388,7 @@ function checkbounds(::Type{Bool}, A::AbstractArray{<:Any,N}, I::BlockIndices{N}
388388
bl = block(I)
389389
checkbounds(Bool, A, bl) || return false
390390
# TODO: Replace with `eachblockaxes(A)[bl]` once that is defined.
391-
binds = map(Base.axes1 getindex, axes(A), Tuple(bl))
391+
binds = map(axes1 getindex, axes(A), Tuple(bl))
392392
Base.checkbounds_indices(Bool, binds, I.indices)
393393
end
394394

test/test_blockindices.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ import BlockArrays: split_index, merge_indices
119119
@test eltype(BlockIndices{3}) BlockIndex{3}
120120
@test Base.IteratorSize(BlockIndices{3}) Base.HasShape{1}()
121121
@test isnothing(iterate(Block(3,3)[[1,3],[3,1]]))
122+
123+
I1 = BlockVector([2,4,7,2,3], (blockedrange([2,3]),))
124+
I2 = blockedrange(2, [4,3])
125+
b = Block(1,2)[I1,I2]
126+
@test blockisequal(axes(b), (blockedrange([2,3]),blockedrange([4,3])))
127+
@test size(b) == (5,7)
128+
@test length(b) == 35
122129
end
123130

124131
@testset "BlockRange" begin

0 commit comments

Comments
 (0)