Skip to content

Overload DerivableInterfaces.zero! #48

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
Mar 11, 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 = "SparseArraysBase"
uuid = "0d5efcca-f356-4864-8770-e1ed8d78f208"
authors = ["ITensor developers <[email protected]> and contributors"]
version = "0.5.1"
version = "0.5.2"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand Down
11 changes: 4 additions & 7 deletions src/abstractsparsearrayinterface.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Base: @_propagate_inbounds_meta
using DerivableInterfaces: DerivableInterfaces, @derive, @interface, AbstractArrayInterface

# This is to bring `ArrayLayouts.zero!` into the namespace
# since it is considered part of the sparse array interface.
using ArrayLayouts: zero!
using DerivableInterfaces:
DerivableInterfaces, @derive, @interface, AbstractArrayInterface, zero!

function eachstoredindex end
function getstoredindex end
Expand Down Expand Up @@ -256,13 +253,13 @@ function map_all! end
return a_dest
end

using ArrayLayouts: ArrayLayouts, zero!
using DerivableInterfaces: DerivableInterfaces, zero!

# `zero!` isn't defined in `Base`, but it is defined in `ArrayLayouts`
# and is useful for sparse array logic, since it can be used to empty
# the sparse array storage.
# We use a single function definition to minimize method ambiguities.
@interface interface::AbstractSparseArrayInterface function ArrayLayouts.zero!(
@interface interface::AbstractSparseArrayInterface function DerivableInterfaces.zero!(
a::AbstractArray
)
# More generally, this codepath could be taking if `zero(eltype(a))`
Expand Down
8 changes: 6 additions & 2 deletions src/sparsearraydok.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Accessors: @set
using DerivableInterfaces: @interface, interface
using DerivableInterfaces: DerivableInterfaces, @interface, interface, zero!
using Dictionaries: Dictionary, IndexError, set!

function getzero(a::AbstractArray{<:Any,N}, I::Vararg{Int,N}) where {N}
Expand Down Expand Up @@ -102,7 +102,11 @@ end
# Optional, but faster than the default.
storedpairs(a::SparseArrayDOK) = pairs(storage(a))

function ArrayLayouts.zero!(a::SparseArrayDOK)
# TODO: Also handle wrappers.
function DerivableInterfaces.zero!(a::SparseArrayDOK)
empty!(storage(a))
return a
end
function ArrayLayouts.zero!(a::SparseArrayDOK)
return zero!(a)
end
Loading