Skip to content

Commit 9ceba1c

Browse files
committed
Fix some ambiguity errors with BlockArrays.jl
1 parent a7197ad commit 9ceba1c

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

ext/BlockSparseArraysGradedUnitRangesExt/BlockSparseArraysGradedUnitRangesExt.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ function Base.similar(
2929
return similar_blocksparse(a, elt, axes)
3030
end
3131

32+
# Fix ambiguity error with `BlockArrays.jl`.
33+
function Base.similar(
34+
a::StridedArray,
35+
elt::Type,
36+
axes::Tuple{AbstractGradedUnitRange,Vararg{AbstractGradedUnitRange}},
37+
)
38+
return similar_blocksparse(a, elt, axes)
39+
end
40+
3241
# Fix ambiguity error with `BlockArrays.jl`.
3342
function Base.similar(
3443
a::StridedArray,
@@ -63,6 +72,11 @@ function Base.getindex(
6372
return getindex_blocksparse(a, I1, I_rest...)
6473
end
6574

75+
# Fix ambiguity error with Base.
76+
function Base.getindex(a::Vector, I::AbstractGradedUnitRange)
77+
return getindex_blocksparse(a, I)
78+
end
79+
6680
# Fix ambiguity errors.
6781
function Base.getindex(
6882
a::AnyAbstractBlockSparseArray,

test/test_gradedunitrangesext.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
using Test: @test, @testset
33
using BlockArrays:
44
AbstractBlockArray, Block, BlockedOneTo, blockedrange, blocklengths, blocksize
5-
using BlockSparseArrays: BlockSparseArray, BlockSparseMatrix, blockstoredlength
5+
using BlockSparseArrays:
6+
BlockSparseArray, BlockSparseMatrix, BlockSparseVector, blockstoredlength
67
using GradedUnitRanges:
78
GradedUnitRanges,
89
GradedOneTo,
@@ -332,6 +333,20 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
332333
@test iszero(b[Block(1, 2)])
333334
@test b[Block(2, 2)] == a2
334335
@test all(GradedUnitRanges.space_isequal.(axes(b), (r, dual(r))))
336+
337+
# Regression test for Vector, which caused
338+
# an ambiguity error with Base.
339+
r = gradedrange([U1(0) => 2, U1(1) => 3])
340+
a1 = randn(elt, 2)
341+
a2 = zeros(elt, 3)
342+
a = vcat(a1, a2)
343+
b = a[r]
344+
@test eltype(b) === elt
345+
@test b isa BlockSparseVector{elt}
346+
@test blockstoredlength(b) == 1
347+
@test b[Block(1)] == a1
348+
@test iszero(b[Block(2)])
349+
@test all(GradedUnitRanges.space_isequal.(axes(b), (r,)))
335350
end
336351
end
337352
end

0 commit comments

Comments
 (0)