Skip to content
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
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
name = "SparseArraysBase"
uuid = "0d5efcca-f356-4864-8770-e1ed8d78f208"
authors = ["ITensor developers <[email protected]> and contributors"]
version = "0.2.3"
version = "0.2.4"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
DerivableInterfaces = "6c5e35bf-e59e-4898-b73c-732dcc4ba65f"
Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MapBroadcast = "ebd9b9da-f48d-417c-9660-449667d60261"

[compat]
Accessors = "0.1.41"
Aqua = "0.8.9"
ArrayLayouts = "1.11.0"
DerivableInterfaces = "0.3.7"
Expand Down
25 changes: 25 additions & 0 deletions src/abstractsparsearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,28 @@
# which is where matrix multiplication and factorizations
# should go.
@derive AnyAbstractSparseArray AbstractArrayOps
const MIMEtextplain = MIME"text/plain"
@derive (T=AnyAbstractSparseArray,) begin
Base.show(::IO, ::MIMEtextplain, ::T)
end

# Wraps a sparse array but replaces the unstored values.
# This is used in printing in order to customize printing
# of zero/unstored values.
struct ReplacedUnstoredSparseArray{T,N,F,Parent<:AbstractArray{T,N}} <:
AbstractSparseArray{T,N}
parent::Parent
getunstoredindex::F
end
Base.parent(a::ReplacedUnstoredSparseArray) = a.parent
Base.size(a::ReplacedUnstoredSparseArray) = size(parent(a))
function isstored(a::ReplacedUnstoredSparseArray, I::Int...)
return isstored(parent(a), I...)
end
function getstoredindex(a::ReplacedUnstoredSparseArray, I::Int...)
return getstoredindex(parent(a), I...)

Check warning on line 46 in src/abstractsparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractsparsearray.jl#L45-L46

Added lines #L45 - L46 were not covered by tests
end
function getunstoredindex(a::ReplacedUnstoredSparseArray, I::Int...)
return a.getunstoredindex(a, I...)
end
@derive ReplacedUnstoredSparseArray AbstractArrayOps
27 changes: 27 additions & 0 deletions src/abstractsparsearrayinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ function storedlength end
function storedpairs end
function storedvalues end

# Replace the function for accessing
# unstored values.
function set_getunstoredindex end

# Generic functionality for converting to a
# dense array, trying to preserve information
# about the array (such as which device it is on).
Expand Down Expand Up @@ -376,3 +380,26 @@ struct SparseLayout <: AbstractSparseLayout end
@interface ::AbstractSparseArrayInterface function ArrayLayouts.MemoryLayout(type::Type)
return SparseLayout()
end

# Like `Char` but prints without quotes.
struct UnquotedChar <: AbstractChar
char::Char
end
Base.show(io::IO, c::UnquotedChar) = print(io, c.char)
Base.show(io::IO, ::MIME"text/plain", c::UnquotedChar) = show(io, c)

function show_getunstoredindex(a::AbstractArray, I::Int...)
return UnquotedChar('⋅')
end

@interface ::AbstractSparseArrayInterface function Base.show(
io::IO, mime::MIME"text/plain", a::AbstractArray
)
summary(io, a)
isempty(a) && return nothing
print(io, ":")
println(io)
a′ = ReplacedUnstoredSparseArray(a, show_getunstoredindex)
Base.print_array(io, a′)
return nothing
end
6 changes: 6 additions & 0 deletions src/sparsearraydok.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Accessors: @set
using Dictionaries: Dictionary, IndexError, set!

function default_getunstoredindex(a::AbstractArray, I::Int...)
Expand All @@ -10,6 +11,11 @@
getunstoredindex::F
end

function set_getunstoredindex(a::SparseArrayDOK, f)
@set a.getunstoredindex = f
return a

Check warning on line 16 in src/sparsearraydok.jl

View check run for this annotation

Codecov / codecov/patch

src/sparsearraydok.jl#L14-L16

Added lines #L14 - L16 were not covered by tests
end

using DerivableInterfaces: DerivableInterfaces
# This defines the destination type of various operations in DerivableInterfaces.jl.
DerivableInterfaces.arraytype(::AbstractSparseArrayInterface, T::Type) = SparseArrayDOK{T}
Expand Down
7 changes: 7 additions & 0 deletions test/basics/test_sparsearraydok.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,11 @@ arrayts = (Array,)
@test storedlength(b) == 2
@test b[1, 2] == 12
@test b[4, 3] == 21

# Printing
# Not testing other element types since they change the
# spacing so it isn't easy to make the test general.
a = SparseArrayDOK{Float64}(2, 2)
a[1, 2] = 12
@test sprint(show, "text/plain", a) == "$(summary(a)):\n ⋅ $(eltype(a)(12))\n ⋅ ⋅"
end
Loading