Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Dec 16, 2024
1 parent 6484a11 commit c576ee1
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ using BlockArrays:
using ..BlockSparseArrays:
BlockSparseArrays,
AbstractBlockSparseArray,
AbstractBlockSparseArrayInterface,
AbstractBlockSparseMatrix,
BlockSparseArray,
BlockSparseArrayInterface,
BlockSparseMatrix,
BlockSparseVector,
block_merge
using Derive: @interface
using GradedUnitRanges:
GradedUnitRanges,
AbstractGradedUnitRange,
Expand Down Expand Up @@ -109,7 +112,7 @@ end
# with mixed dual and non-dual axes. This shouldn't be needed once
# GradedUnitRanges is rewritten using BlockArrays v1.
# TODO: Delete this once GradedUnitRanges is rewritten.
function blocksparse_show(
@interface ::AbstractBlockSparseArrayInterface function Base.show(

Check warning on line 115 in ext/BlockSparseArraysGradedUnitRangesExt/src/BlockSparseArraysGradedUnitRangesExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/BlockSparseArraysGradedUnitRangesExt/src/BlockSparseArraysGradedUnitRangesExt.jl#L115

Added line #L115 was not covered by tests
io::IO, mime::MIME"text/plain", a::AbstractArray, axes_a::Tuple; kwargs...
)
println(io, "typeof(axes) = ", typeof(axes_a), "\n")
Expand All @@ -127,7 +130,7 @@ end
function Base.show(io::IO, mime::MIME"text/plain", a::BlockSparseArray; kwargs...)
axes_a = axes(a)
a_nondual = BlockSparseArray(blocks(a), nondual.(axes(a)))
return blocksparse_show(io, mime, a_nondual, axes_a; kwargs...)
return @interface BlockSparseArrayInterface() show(io, mime, a_nondual, axes_a; kwargs...)

Check warning on line 133 in ext/BlockSparseArraysGradedUnitRangesExt/src/BlockSparseArraysGradedUnitRangesExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/BlockSparseArraysGradedUnitRangesExt/src/BlockSparseArraysGradedUnitRangesExt.jl#L133

Added line #L133 was not covered by tests
end

# This is a temporary fix for `show` being broken for BlockSparseArrays
Expand All @@ -139,7 +142,7 @@ function Base.show(
)
axes_a = axes(a)
a_nondual = BlockSparseArray(blocks(a'), dual.(nondual.(axes(a'))))'
return blocksparse_show(io, mime, a_nondual, axes_a; kwargs...)
return @interface BlockSparseArrayInterface() show(io, mime, a_nondual, axes_a; kwargs...)

Check warning on line 145 in ext/BlockSparseArraysGradedUnitRangesExt/src/BlockSparseArraysGradedUnitRangesExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/BlockSparseArraysGradedUnitRangesExt/src/BlockSparseArraysGradedUnitRangesExt.jl#L145

Added line #L145 was not covered by tests
end

# This is a temporary fix for `show` being broken for BlockSparseArrays
Expand All @@ -151,6 +154,6 @@ function Base.show(
)
axes_a = axes(a)
a_nondual = tranpose(BlockSparseArray(transpose(blocks(a)), nondual.(axes(a))))
return blocksparse_show(io, mime, a_nondual, axes_a; kwargs...)
return @interface BlockSparseArrayInterface() show(io, mime, a_nondual, axes_a; kwargs...)

Check warning on line 157 in ext/BlockSparseArraysGradedUnitRangesExt/src/BlockSparseArraysGradedUnitRangesExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/BlockSparseArraysGradedUnitRangesExt/src/BlockSparseArraysGradedUnitRangesExt.jl#L157

Added line #L157 was not covered by tests
end
end
2 changes: 1 addition & 1 deletion src/BlockArraysExtensions/BlockArraysExtensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Base.view(S::BlockIndices, i) = S[i]
# @view b[Block(1, 1)[1:2, 2:2]]
# ```
# This is similar to the definition:
# blocksparse_to_indices(a, inds, I::Tuple{UnitRange{<:Integer},Vararg{Any}})
# @interface BlockSparseArrayInterface() to_indices(a, inds, I::Tuple{UnitRange{<:Integer},Vararg{Any}})
function Base.getindex(
a::NonBlockedVector{<:Integer,<:BlockIndices}, I::UnitRange{<:Integer}
)
Expand Down
10 changes: 5 additions & 5 deletions src/abstractblocksparsearray/abstractblocksparsearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ end

# Specialized in order to fix ambiguity error with `BlockArrays`.
function Base.getindex(a::AbstractBlockSparseArray{<:Any,N}, I::Vararg{Int,N}) where {N}
return blocksparse_getindex(a, I...)
return @interface BlockSparseArrayInterface() getindex(a, I...)
end

# Specialized in order to fix ambiguity error with `BlockArrays`.
function Base.getindex(a::AbstractBlockSparseArray{<:Any,0})
return blocksparse_getindex(a)
return @interface BlockSparseArrayInterface() getindex(a)

Check warning on line 27 in src/abstractblocksparsearray/abstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/abstractblocksparsearray.jl#L27

Added line #L27 was not covered by tests
end

## # Fix ambiguity error with `BlockArrays`.
Expand All @@ -39,21 +39,21 @@ end
##
## # Fix ambiguity error with `BlockArrays`.
## function Base.getindex(a::AbstractBlockSparseArray, I::Vararg{AbstractVector})
## ## return blocksparse_getindex(a, I...)
## ## return @interface BlockSparseArrayInterface() getindex(a, I...)
## return ArrayLayouts.layout_getindex(a, I...)
## end

# Specialized in order to fix ambiguity error with `BlockArrays`.
function Base.setindex!(
a::AbstractBlockSparseArray{<:Any,N}, value, I::Vararg{Int,N}
) where {N}
blocksparse_setindex!(a, value, I...)
@interface BlockSparseArrayInterface() setindex!(a, value, I...)

Check warning on line 50 in src/abstractblocksparsearray/abstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/abstractblocksparsearray.jl#L50

Added line #L50 was not covered by tests
return a
end

# Fix ambiguity error.
function Base.setindex!(a::AbstractBlockSparseArray{<:Any,0}, value)
blocksparse_setindex!(a, value)
@interface BlockSparseArrayInterface() setindex!(a, value)

Check warning on line 56 in src/abstractblocksparsearray/abstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/abstractblocksparsearray.jl#L56

Added line #L56 was not covered by tests
return a
end

Expand Down
18 changes: 9 additions & 9 deletions src/abstractblocksparsearray/views.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ function Base.view(
},
I::Block{N},
) where {N}
return blocksparse_view(a, I)
return @interface BlockSparseArrayInterface() view(a, I)
end
function Base.view(
a::SubArray{
<:Any,N,<:AnyAbstractBlockSparseArray,<:Tuple{Vararg{BlockSlice{<:BlockRange{1}},N}}
},
I::Vararg{Block{1},N},
) where {N}
return blocksparse_view(a, I...)
return @interface BlockSparseArrayInterface() view(a, I...)

Check warning on line 50 in src/abstractblocksparsearray/views.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/views.jl#L50

Added line #L50 was not covered by tests
end
function Base.view(
V::SubArray{<:Any,1,<:AnyAbstractBlockSparseArray,<:Tuple{BlockSlice{<:BlockRange{1}}}},
I::Block{1},
)
return blocksparse_view(a, I)
return @interface BlockSparseArrayInterface() view(a, I)

Check warning on line 56 in src/abstractblocksparsearray/views.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/views.jl#L56

Added line #L56 was not covered by tests
end

# Specialized code for getting the view of a block.
Expand All @@ -63,7 +63,7 @@ function BlockArrays.viewblock(
return viewblock(a, Tuple(block)...)
end

# TODO: Define `blocksparse_viewblock`.
# TODO: Define `@interface BlockSparseArrayInterface() viewblock`.
function BlockArrays.viewblock(
a::AbstractBlockSparseArray{<:Any,N}, block::Vararg{Block{1},N}
) where {N}
Expand Down Expand Up @@ -177,9 +177,9 @@ end

# XXX: TODO: Distinguish if a sub-view of the block needs to be taken!
# Define a new `SubBlockSlice` which is used in:
# `blocksparse_to_indices(a, inds, I::Tuple{UnitRange{<:Integer},Vararg{Any}})`
# `@interface BlockSparseArrayInterface() to_indices(a, inds, I::Tuple{UnitRange{<:Integer},Vararg{Any}})`
# in `blocksparsearrayinterface/blocksparsearrayinterface.jl`.
# TODO: Define `blocksparse_viewblock`.
# TODO: Define `@interface BlockSparseArrayInterface() viewblock`.
function BlockArrays.viewblock(
a::SubArray{T,N,<:AbstractBlockSparseArray{T,N},<:Tuple{Vararg{BlockSliceCollection,N}}},
block::Vararg{Block{1},N},
Expand Down Expand Up @@ -220,7 +220,7 @@ function BlockArrays.viewblock(
return @view parent(a)[brs...]
end

# TODO: Define `blocksparse_viewblock`.
# TODO: Define `@interface BlockSparseArrayInterface() viewblock`.
function BlockArrays.viewblock(
a::SubArray{
T,
Expand Down Expand Up @@ -257,7 +257,7 @@ function BlockArrays.viewblock(
) where {T,N}
return viewblock(a, to_tuple(block)...)
end
# TODO: Define `blocksparse_viewblock`.
# TODO: Define `@interface BlockSparseArrayInterface() viewblock`.
function BlockArrays.viewblock(
a::SubArray{T,N,<:AbstractBlockSparseArray{T,N},<:Tuple{Vararg{BlockedSlice,N}}},
I::Vararg{Block{1},N},
Expand All @@ -271,7 +271,7 @@ function BlockArrays.viewblock(
end
return @view parent(a)[brs...]
end
# TODO: Define `blocksparse_viewblock`.
# TODO: Define `@interface BlockSparseArrayInterface() viewblock`.
function BlockArrays.viewblock(
a::SubArray{T,N,<:AbstractBlockSparseArray{T,N},<:Tuple{Vararg{BlockedSlice,N}}},
block::Vararg{BlockIndexRange{1},N},
Expand Down
65 changes: 43 additions & 22 deletions src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using BlockArrays:
blockedrange,
mortar,
unblock
using Derive: Derive
using Derive: Derive, @interface
using SplitApplyCombine: groupcount
using TypeParameterAccessors: similartype

Expand All @@ -28,14 +28,14 @@ Derive.interface(::Type{<:AnyAbstractBlockSparseArray}) = BlockSparseArrayInterf
function Base.to_indices(
a::AnyAbstractBlockSparseArray, inds, I::Tuple{UnitRange{<:Integer},Vararg{Any}}
)
return blocksparse_to_indices(a, inds, I)
return @interface BlockSparseArrayInterface() to_indices(a, inds, I)

Check warning on line 31 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L31

Added line #L31 was not covered by tests
end

# a[[Block(2), Block(1)], [Block(2), Block(1)]]
function Base.to_indices(
a::AnyAbstractBlockSparseArray, inds, I::Tuple{Vector{<:Block{1}},Vararg{Any}}
)
return blocksparse_to_indices(a, inds, I)
return @interface BlockSparseArrayInterface() to_indices(a, inds, I)

Check warning on line 38 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L38

Added line #L38 was not covered by tests
end

# a[BlockVector([Block(2), Block(1)], [2]), BlockVector([Block(2), Block(1)], [2])]
Expand All @@ -45,7 +45,7 @@ function Base.to_indices(
inds,
I::Tuple{AbstractBlockVector{<:Block{1}},Vararg{Any}},
)
return blocksparse_to_indices(a, inds, I)
return @interface BlockSparseArrayInterface() to_indices(a, inds, I)

Check warning on line 48 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L48

Added line #L48 was not covered by tests
end

# a[mortar([Block(1)[1:2], Block(2)[1:3]])]
Expand All @@ -54,7 +54,7 @@ function Base.to_indices(
inds,
I::Tuple{BlockVector{<:BlockIndex{1},<:Vector{<:BlockIndexRange{1}}},Vararg{Any}},
)
return blocksparse_to_indices(a, inds, I)
return @interface BlockSparseArrayInterface() to_indices(a, inds, I)

Check warning on line 57 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L57

Added line #L57 was not covered by tests
end

# a[[Block(1)[1:2], Block(2)[1:2]], [Block(1)[1:2], Block(2)[1:2]]]
Expand All @@ -65,14 +65,16 @@ function Base.to_indices(
end

# BlockArrays `AbstractBlockArray` interface
BlockArrays.blocks(a::AnyAbstractBlockSparseArray) = blocksparse_blocks(a)
function BlockArrays.blocks(a::AnyAbstractBlockSparseArray)
@interface BlockSparseArrayInterface() blocks(a)
end

# Fix ambiguity error with `BlockArrays`
using BlockArrays: BlockSlice
function BlockArrays.blocks(
a::SubArray{<:Any,<:Any,<:AbstractBlockSparseArray,<:Tuple{Vararg{BlockSlice}}}
)
return blocksparse_blocks(a)
return @interface BlockSparseArrayInterface() blocks(a)

Check warning on line 77 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L77

Added line #L77 was not covered by tests
end

using TypeParameterAccessors: parenttype
Expand Down Expand Up @@ -105,7 +107,7 @@ function Base.getindex(a::AnyAbstractBlockSparseArray{<:Any,0})
return ArrayLayouts.layout_getindex(a)
end

# TODO: Define `blocksparse_isassigned`.
# TODO: Define `@interface BlockSparseArrayInterface() isassigned`.
function Base.isassigned(
a::AnyAbstractBlockSparseArray{<:Any,N}, index::Vararg{Block{1},N}
) where {N}
Expand All @@ -121,7 +123,7 @@ function Base.isassigned(a::AnyAbstractBlockSparseArray{<:Any,N}, index::Block{N
return isassigned(a, Tuple(index)...)
end

# TODO: Define `blocksparse_isassigned`.
# TODO: Define `@interface BlockSparseArrayInterface() isassigned`.
function Base.isassigned(
a::AnyAbstractBlockSparseArray{<:Any,N}, index::Vararg{BlockIndex{1},N}
) where {N}
Expand All @@ -133,13 +135,13 @@ function Base.setindex!(
a::AnyAbstractBlockSparseArray{<:Any,N}, value, I::BlockIndex{N}
) where {N}
# TODO: Use `@interface interface(a) setindex!(...)`.
blocksparse_setindex!(a, value, I)
@interface BlockSparseArrayInterface() setindex!(a, value, I)

Check warning on line 138 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L138

Added line #L138 was not covered by tests
return a
end
# Fixes ambiguity error with BlockArrays.jl
function Base.setindex!(a::AnyAbstractBlockSparseArray{<:Any,1}, value, I::BlockIndex{1})
# TODO: Use `@interface interface(a) setindex!(...)`.
blocksparse_setindex!(a, value, I)
@interface BlockSparseArrayInterface() setindex!(a, value, I)

Check warning on line 144 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L144

Added line #L144 was not covered by tests
return a
end

Expand Down Expand Up @@ -212,32 +214,51 @@ function blocksparse_similar(a, elt::Type, axes::Tuple)
undef, axes
)
end
@interface ::AbstractBlockSparseArrayInterface function Base.similar(

Check warning on line 217 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L217

Added line #L217 was not covered by tests
a::AbstractArray, elt::Type, axes::Tuple{Vararg{Int}}
)
return blocksparse_similar(a, elt, axes)

Check warning on line 220 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L220

Added line #L220 was not covered by tests
end
@interface ::AbstractBlockSparseArrayInterface function Base.similar(
a::AbstractArray, elt::Type, axes::Tuple
)
return blocksparse_similar(a, elt, axes)
end
@interface ::AbstractBlockSparseArrayInterface function Base.similar(

Check warning on line 227 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L227

Added line #L227 was not covered by tests
a::Type{<:AbstractArray}, elt::Type, axes::Tuple{Vararg{Int}}
)
return blocksparse_similar(a, elt, axes)

Check warning on line 230 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L230

Added line #L230 was not covered by tests
end
@interface ::AbstractBlockSparseArrayInterface function Base.similar(
a::Type{<:AbstractArray}, elt::Type, axes::Tuple
)
return blocksparse_similar(a, elt, axes)
end

# Needed by `BlockArrays` matrix multiplication interface
# TODO: Define a `blocksparse_similar` function.
# TODO: Define a `@interface BlockSparseArrayInterface() similar` function.
function Base.similar(
arraytype::Type{<:AnyAbstractBlockSparseArray},
elt::Type,
axes::Tuple{Vararg{AbstractUnitRange{<:Integer}}},
)
# TODO: Use `@interface interface(arraytype) similar(...)`.
return blocksparse_similar(arraytype, elt, axes)
return @interface BlockSparseArrayInterface() similar(arraytype, elt, axes)
end

# TODO: Define a `blocksparse_similar` function.
# TODO: Define a `@interface BlockSparseArrayInterface() similar` function.
function Base.similar(
a::AnyAbstractBlockSparseArray,
elt::Type,
axes::Tuple{Vararg{AbstractUnitRange{<:Integer}}},
)
# TODO: Use `@interface interface(a) similar(...)`.
return blocksparse_similar(a, elt, axes)
return @interface BlockSparseArrayInterface() similar(a, elt, axes)

Check warning on line 255 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L255

Added line #L255 was not covered by tests
end

# Fixes ambiguity error.
function Base.similar(a::AnyAbstractBlockSparseArray, elt::Type, axes::Tuple{})
# TODO: Use `@interface interface(a) similar(...)`.
return blocksparse_similar(a, elt, axes)
return @interface BlockSparseArrayInterface() similar(a, elt, axes)

Check warning on line 261 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L261

Added line #L261 was not covered by tests
end

# Fixes ambiguity error with `BlockArrays`.
Expand All @@ -249,7 +270,7 @@ function Base.similar(
},
)
# TODO: Use `@interface interface(a) similar(...)`.
return blocksparse_similar(a, elt, axes)
return @interface BlockSparseArrayInterface() similar(a, elt, axes)
end

# Fixes ambiguity error with `OffsetArrays`.
Expand All @@ -259,7 +280,7 @@ function Base.similar(
axes::Tuple{AbstractUnitRange{<:Integer},Vararg{AbstractUnitRange{<:Integer}}},
)
# TODO: Use `@interface interface(a) similar(...)`.
return blocksparse_similar(a, elt, axes)
return @interface BlockSparseArrayInterface() similar(a, elt, axes)

Check warning on line 283 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L283

Added line #L283 was not covered by tests
end

# Fixes ambiguity error with `BlockArrays`.
Expand All @@ -269,7 +290,7 @@ function Base.similar(
axes::Tuple{AbstractBlockedUnitRange{<:Integer},Vararg{AbstractUnitRange{<:Integer}}},
)
# TODO: Use `@interface interface(a) similar(...)`.
return blocksparse_similar(a, elt, axes)
return @interface BlockSparseArrayInterface() similar(a, elt, axes)

Check warning on line 293 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L293

Added line #L293 was not covered by tests
end

# Fixes ambiguity errors with BlockArrays.
Expand All @@ -283,15 +304,15 @@ function Base.similar(
},
)
# TODO: Use `@interface interface(a) similar(...)`.
return blocksparse_similar(a, elt, axes)
return @interface BlockSparseArrayInterface() similar(a, elt, axes)

Check warning on line 307 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L307

Added line #L307 was not covered by tests
end

# Fixes ambiguity error with `StaticArrays`.
function Base.similar(
a::AnyAbstractBlockSparseArray, elt::Type, axes::Tuple{Base.OneTo,Vararg{Base.OneTo}}
)
# TODO: Use `@interface interface(a) similar(...)`.
return blocksparse_similar(a, elt, axes)
return @interface BlockSparseArrayInterface() similar(a, elt, axes)

Check warning on line 315 in src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl#L315

Added line #L315 was not covered by tests
end

# TODO: Implement this in a more generic way using a smarter `copyto!`,
Expand Down
Loading

0 comments on commit c576ee1

Please sign in to comment.