Skip to content

Commit

Permalink
[SparseArrayInterface] [BlockSparseArrays] Rename wrapper type unions
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Nov 14, 2024
1 parent 57994ff commit b2bcdc4
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using BlockArrays: BlockLayout
using ..SparseArrayInterface: SparseLayout
using ..TypeParameterAccessors: parenttype, similartype

function ArrayLayouts.MemoryLayout(arraytype::Type{<:BlockSparseArrayLike})
function ArrayLayouts.MemoryLayout(arraytype::Type{<:AnyAbstractBlockSparseArray})
outer_layout = typeof(MemoryLayout(blockstype(arraytype)))
inner_layout = typeof(MemoryLayout(blocktype(arraytype)))
return BlockLayout{outer_layout,inner_layout}()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using BlockArrays: AbstractBlockedUnitRange, BlockSlice
using Base.Broadcast: Broadcast

function Broadcast.BroadcastStyle(arraytype::Type{<:BlockSparseArrayLike})
function Broadcast.BroadcastStyle(arraytype::Type{<:AnyAbstractBlockSparseArray})
return BlockSparseArrayStyle{ndims(arraytype)}()
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# TODO: Change to `AnyAbstractBlockSparseArray`.
function Base.cat(as::BlockSparseArrayLike...; dims)
function Base.cat(as::AnyAbstractBlockSparseArray...; dims)
# TODO: Use `sparse_cat` instead, currently
# that erroneously allocates too many blocks that are
# zero and shouldn't be stored.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,27 @@ end
# function SparseArrayInterface.sparse_mapreduce(::BlockSparseArrayStyle, f, a_dest::AbstractArray, a_srcs::Vararg{AbstractArray})
# end

function Base.map!(f, a_dest::AbstractArray, a_srcs::Vararg{BlockSparseArrayLike})
function Base.map!(f, a_dest::AbstractArray, a_srcs::Vararg{AnyAbstractBlockSparseArray})
sparse_map!(f, a_dest, a_srcs...)
return a_dest
end

function Base.map(f, as::Vararg{BlockSparseArrayLike})
function Base.map(f, as::Vararg{AnyAbstractBlockSparseArray})
return f.(as...)
end

function Base.copy!(a_dest::AbstractArray, a_src::BlockSparseArrayLike)
function Base.copy!(a_dest::AbstractArray, a_src::AnyAbstractBlockSparseArray)
sparse_copy!(a_dest, a_src)
return a_dest
end

function Base.copyto!(a_dest::AbstractArray, a_src::BlockSparseArrayLike)
function Base.copyto!(a_dest::AbstractArray, a_src::AnyAbstractBlockSparseArray)
sparse_copyto!(a_dest, a_src)
return a_dest
end

# Fix ambiguity error
function Base.copyto!(a_dest::LayoutArray, a_src::BlockSparseArrayLike)
function Base.copyto!(a_dest::LayoutArray, a_src::AnyAbstractBlockSparseArray)
sparse_copyto!(a_dest, a_src)
return a_dest
end
Expand All @@ -131,21 +131,21 @@ function Base.copyto!(
return a_dest
end

function Base.permutedims!(a_dest, a_src::BlockSparseArrayLike, perm)
function Base.permutedims!(a_dest, a_src::AnyAbstractBlockSparseArray, perm)
sparse_permutedims!(a_dest, a_src, perm)
return a_dest
end

function Base.mapreduce(f, op, as::Vararg{BlockSparseArrayLike}; kwargs...)
function Base.mapreduce(f, op, as::Vararg{AnyAbstractBlockSparseArray}; kwargs...)
return sparse_mapreduce(f, op, as...; kwargs...)
end

# TODO: Why isn't this calling `mapreduce` already?
function Base.iszero(a::BlockSparseArrayLike)
function Base.iszero(a::AnyAbstractBlockSparseArray)
return sparse_iszero(blocks(a))
end

# TODO: Why isn't this calling `mapreduce` already?
function Base.isreal(a::BlockSparseArrayLike)
function Base.isreal(a::AnyAbstractBlockSparseArray)
return sparse_isreal(blocks(a))
end
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ function SparseArrayInterface.sparse_storage(a::AbstractBlockSparseArray)
return BlockSparseStorage(a)
end

function SparseArrayInterface.nstored(a::BlockSparseArrayLike)
function SparseArrayInterface.nstored(a::AnyAbstractBlockSparseArray)
return sum(nstored, sparse_storage(blocks(a)); init=zero(Int))
end
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ end

# Override the default definition of `BlockArrays.blocksize`,
# which is incorrect for certain slices.
function BlockArrays.blocksize(a::SubArray{<:Any,<:Any,<:BlockSparseArrayLike})
function BlockArrays.blocksize(a::SubArray{<:Any,<:Any,<:AnyAbstractBlockSparseArray})
return blocklength.(axes(a))
end
function BlockArrays.blocksize(a::SubArray{<:Any,<:Any,<:BlockSparseArrayLike}, i::Int)
function BlockArrays.blocksize(
a::SubArray{<:Any,<:Any,<:AnyAbstractBlockSparseArray}, i::Int
)
# TODO: Maybe use `blocklength(axes(a, i))` which would be a bit faster.
return blocksize(a)[i]
end
Expand All @@ -33,22 +35,22 @@ end
# which don't handle subslices of blocks properly.
function Base.view(
a::SubArray{
<:Any,N,<:BlockSparseArrayLike,<:Tuple{Vararg{BlockSlice{<:BlockRange{1}},N}}
<:Any,N,<:AnyAbstractBlockSparseArray,<:Tuple{Vararg{BlockSlice{<:BlockRange{1}},N}}
},
I::Block{N},
) where {N}
return blocksparse_view(a, I)
end
function Base.view(
a::SubArray{
<:Any,N,<:BlockSparseArrayLike,<:Tuple{Vararg{BlockSlice{<:BlockRange{1}},N}}
<:Any,N,<:AnyAbstractBlockSparseArray,<:Tuple{Vararg{BlockSlice{<:BlockRange{1}},N}}
},
I::Vararg{Block{1},N},
) where {N}
return blocksparse_view(a, I...)
end
function Base.view(
V::SubArray{<:Any,1,<:BlockSparseArrayLike,<:Tuple{BlockSlice{<:BlockRange{1}}}},
V::SubArray{<:Any,1,<:AnyAbstractBlockSparseArray,<:Tuple{BlockSlice{<:BlockRange{1}}}},
I::Block{1},
)
return blocksparse_view(a, I)
Expand Down Expand Up @@ -154,7 +156,7 @@ function BlockArrays.viewblock(
return viewblock(a, to_tuple(block)...)
end

# Fixes ambiguity error with `BlockSparseArrayLike` definition.
# Fixes ambiguity error with `AnyAbstractBlockSparseArray` definition.
function Base.view(
a::SubArray{
T,N,<:AbstractBlockSparseArray{T,N},<:Tuple{Vararg{BlockSlice{<:BlockRange{1}},N}}
Expand All @@ -163,7 +165,7 @@ function Base.view(
) where {T,N}
return viewblock(a, block)
end
# Fixes ambiguity error with `BlockSparseArrayLike` definition.
# Fixes ambiguity error with `AnyAbstractBlockSparseArray` definition.
function Base.view(
a::SubArray{
T,N,<:AbstractBlockSparseArray{T,N},<:Tuple{Vararg{BlockSlice{<:BlockRange{1}},N}}
Expand Down
Loading

0 comments on commit b2bcdc4

Please sign in to comment.