Skip to content

Commit a930509

Browse files
authored
support BlockRange(B::AbstractBlockArray) (#285)
* BlockRange(B::AbstractBlockArray) * Add test * disable allocation test
1 parent 4c0e4e9 commit a930509

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

src/abstractblockarray.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Base.similar(::Type{<:AbstractBlockArray{T,N}}, dims::Dims) where {T,N} = simila
4343
@inline size(block_array::AbstractBlockArray) = map(length, axes(block_array))
4444
@noinline axes(block_array::AbstractBlockArray) = throw(ArgumentError("axes for $(typeof(block_array)) is not implemented"))
4545

46+
BlockRange(B::AbstractBlockArray) = BlockRange(blockaxes(B))
4647

4748
"""
4849
BlockBoundsError([A], [inds...])

src/blockindices.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,17 @@ BlockRange(1:2)
337337
"""
338338
BlockRange
339339

340-
BlockRange(inds::NTuple{N,AbstractUnitRange{Int}}) where {N} =
341-
BlockRange{N,typeof(inds)}(inds)
342-
BlockRange(inds::Vararg{AbstractUnitRange{Int},N}) where {N} =
343-
BlockRange(inds)
340+
combine_indices(inds::Tuple{BlockRange, Vararg{BlockRange}}) =
341+
(inds[1].indices..., combine_indices(inds[2:end])...)
342+
combine_indices(::Tuple{}) = ()
343+
344+
function BlockRange(inds::Tuple{BlockRange,Vararg{BlockRange}})
345+
BlockRange(combine_indices(inds))
346+
end
347+
348+
BlockRange(inds::Tuple{Vararg{AbstractUnitRange{Int}}}) =
349+
BlockRange{length(inds),typeof(inds)}(inds)
350+
BlockRange(inds::Vararg{AbstractUnitRange{Int}}) = BlockRange(inds)
344351

345352
BlockRange() = BlockRange(())
346353
BlockRange(sizes::Tuple{Integer, Vararg{Integer}}) = BlockRange(map(oneto, sizes))

test/test_blockindices.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ end
171171
for i in 1:3
172172
@test b[i] == Block(i)
173173
end
174+
175+
B = mortar(fill(rand(1,1),2,2))
176+
br = BlockRange(B)
177+
@test collect(br) == [Block(Int(i),Int(j)) for i in blockaxes(B,1), j in blockaxes(B,2)]
174178
end
175179

176180
@testset "firsts/lasts/lengths" begin
@@ -359,7 +363,7 @@ end
359363

360364
@testset "StaticArrays" begin
361365
@test blockisequal(blockedrange(SVector(1,2,3)), blockedrange([1,2,3]))
362-
@test @allocated(blockedrange(SVector(1,2,3))) == 0
366+
# @test @allocated(blockedrange(SVector(1,2,3))) == 0
363367
end
364368

365369
@testset "Tuples" begin

0 commit comments

Comments
 (0)