Skip to content

More specific interfaces #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "BlockSparseArrays"
uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
authors = ["ITensor developers <[email protected]> and contributors"]
version = "0.7.7"
version = "0.7.8"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
2 changes: 1 addition & 1 deletion src/BlockArraysExtensions/BlockArraysExtensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Base.view(S::BlockIndices, i) = S[i]
# @view b[Block(1, 1)[1:2, 2:2]]
# ```
# This is similar to the definition:
# @interface BlockSparseArrayInterface() to_indices(a, inds, I::Tuple{UnitRange{<:Integer},Vararg{Any}})
# @interface interface(a) to_indices(a, inds, I::Tuple{UnitRange{<:Integer},Vararg{Any}})
function Base.getindex(
a::NonBlockedVector{<:Integer,<:BlockIndices}, I::UnitRange{<:Integer}
)
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 @interface BlockSparseArrayInterface() view(a, I)
return @interface interface(a) 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 @interface BlockSparseArrayInterface() view(a, I...)
return @interface interface(a) view(a, I...)
end
function Base.view(
V::SubArray{<:Any,1,<:AnyAbstractBlockSparseArray,<:Tuple{BlockSlice{<:BlockRange{1}}}},
I::Block{1},
)
return @interface BlockSparseArrayInterface() view(a, I)
return @interface interface(a) view(a, I)
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 `@interface BlockSparseArrayInterface() viewblock`.
# TODO: Define `@interface interface(a) viewblock`.
function BlockArrays.viewblock(
a::AbstractBlockSparseArray{<:Any,N}, block::Vararg{Block{1},N}
) where {N}
Expand Down Expand Up @@ -192,9 +192,9 @@ end

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

# TODO: Define `@interface BlockSparseArrayInterface() viewblock`.
# TODO: Define `@interface interface() viewblock`.
function BlockArrays.viewblock(
a::SubArray{
T,
Expand Down Expand Up @@ -295,7 +295,7 @@ end
blockedslice_blocks(x::BlockSlice) = x.block
blockedslice_blocks(x::BlockIndices) = x.blocks

# TODO: Define `@interface BlockSparseArrayInterface() viewblock`.
# TODO: Define `@interface interface(a) viewblock`.
function BlockArrays.viewblock(
a::SubArray{
T,N,<:AbstractBlockSparseArray{T,N},<:Tuple{Vararg{SubBlockSliceCollection,N}}
Expand All @@ -311,7 +311,7 @@ function BlockArrays.viewblock(
end
return @view parent(a)[brs...]
end
# TODO: Define `@interface BlockSparseArrayInterface() viewblock`.
# TODO: Define `@interface interface(a) viewblock`.
function BlockArrays.viewblock(
a::SubArray{
T,N,<:AbstractBlockSparseArray{T,N},<:Tuple{Vararg{SubBlockSliceCollection,N}}
Expand Down
44 changes: 22 additions & 22 deletions src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,27 @@ end
function Base.to_indices(
a::AnyAbstractBlockSparseArray, inds, I::Tuple{UnitRange{<:Integer},Vararg{Any}}
)
return @interface BlockSparseArrayInterface() to_indices(a, inds, I)
return @interface interface(a) to_indices(a, inds, I)
end

function Base.to_indices(
a::AnyAbstractBlockSparseArray, inds, I::Tuple{AbstractArray{Bool},Vararg{Any}}
)
return @interface BlockSparseArrayInterface() to_indices(a, inds, I)
return @interface interface(a) to_indices(a, inds, I)
end
# Fix ambiguity error with Base for logical indexing in Julia 1.10.
# TODO: Delete this once we drop support for Julia 1.10.
function Base.to_indices(
a::AnyAbstractBlockSparseArray, inds, I::Union{Tuple{BitArray{N}},Tuple{Array{Bool,N}}}
) where {N}
return @interface BlockSparseArrayInterface() to_indices(a, inds, I)
return @interface interface(a) to_indices(a, inds, I)
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 @interface BlockSparseArrayInterface() to_indices(a, inds, I)
return @interface interface(a) to_indices(a, inds, I)
end

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

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

# a[[Block(1)[1:2], Block(2)[1:2]], [Block(1)[1:2], Block(2)[1:2]]]
Expand All @@ -88,15 +88,15 @@ end

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

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

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

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

# TODO: Define `@interface BlockSparseArrayInterface() isassigned`.
# TODO: Define `@interface interface(a) isassigned`.
function Base.isassigned(
a::AnyAbstractBlockSparseArray{<:Any,N}, index::Vararg{BlockIndex{1},N}
) where {N}
Expand All @@ -157,13 +157,13 @@ function Base.setindex!(
a::AnyAbstractBlockSparseArray{<:Any,N}, value, I::BlockIndex{N}
) where {N}
# TODO: Use `@interface interface(a) setindex!(...)`.
@interface BlockSparseArrayInterface() setindex!(a, value, I)
@interface interface(a) setindex!(a, value, I)
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!(...)`.
@interface BlockSparseArrayInterface() setindex!(a, value, I)
@interface interface(a) setindex!(a, value, I)
return a
end

Expand Down Expand Up @@ -269,29 +269,29 @@ end
end

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

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

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

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

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

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

# Fixes ambiguity errors with BlockArrays.
Expand All @@ -337,15 +337,15 @@ function Base.similar(
},
)
# TODO: Use `@interface interface(a) similar(...)`.
return @interface BlockSparseArrayInterface() similar(a, elt, axes)
return @interface interface(a) similar(a, elt, axes)
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 @interface BlockSparseArrayInterface() similar(a, elt, axes)
return @interface interface(a) similar(a, elt, axes)
end

struct BlockType{T} end
Expand Down
6 changes: 6 additions & 0 deletions src/blocksparsearray/blocksparsearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ Base.axes(a::BlockSparseArray) = a.axes
@interface ::AbstractBlockSparseArrayInterface BlockArrays.blocks(a::BlockSparseArray) =
a.blocks

function blocktype(
arraytype::Type{<:BlockSparseArray{T,N,A}}
) where {T,N,A<:AbstractArray{T,N}}
return A
end

# TODO: Use `TypeParameterAccessors`.
function blockstype(
arraytype::Type{<:BlockSparseArray{T,N,A,Blocks}}
Expand Down
Loading