11# # DerivableInterfaces.jl
2- #
2+ #
33# [](https://itensor.github.io/DerivableInterfaces.jl/stable/)
44# [](https://itensor.github.io/DerivableInterfaces.jl/dev/)
55# [](https://github.com/ITensor/DerivableInterfaces.jl/actions/workflows/Tests.yml?query=branch%3Amain)
@@ -60,56 +60,56 @@ julia> Pkg.add("DerivableInterfaces")
6060# ## Examples
6161
6262using DerivableInterfaces:
63- DerivableInterfaces, @array_aliases , @derive , @interface , interface
63+ DerivableInterfaces, @array_aliases , @derive , @interface , interface
6464using Test: @test
6565
6666# Define an interface.
6767struct SparseArrayInterface end
6868
6969# Define interface functions.
7070@interface :: SparseArrayInterface function Base. getindex (a, I:: Int... )
71- checkbounds (a, I... )
72- ! isstored (a, I... ) && return getunstoredindex (a, I... )
73- return getstoredindex (a, I... )
71+ checkbounds (a, I... )
72+ ! isstored (a, I... ) && return getunstoredindex (a, I... )
73+ return getstoredindex (a, I... )
7474end
7575@interface :: SparseArrayInterface function Base. setindex! (a, value, I:: Int... )
76- checkbounds (a, I... )
77- iszero (value) && return a
78- if ! isstored (a, I... )
79- setunstoredindex! (a, value, I... )
76+ checkbounds (a, I... )
77+ iszero (value) && return a
78+ if ! isstored (a, I... )
79+ setunstoredindex! (a, value, I... )
80+ return a
81+ end
82+ setstoredindex! (a, value, I... )
8083 return a
81- end
82- setstoredindex! (a, value, I... )
83- return a
8484end
8585
8686# Define a type that will derive the interface.
87- struct SparseArrayDOK{T,N} <: AbstractArray{T,N}
88- storage:: Dict{CartesianIndex{N},T}
89- size:: NTuple{N,Int}
87+ struct SparseArrayDOK{T, N} <: AbstractArray{T, N}
88+ storage:: Dict{CartesianIndex{N}, T}
89+ size:: NTuple{N, Int}
9090end
9191storage (a:: SparseArrayDOK ) = a. storage
9292Base. size (a:: SparseArrayDOK ) = a. size
9393function SparseArrayDOK {T} (size:: Int... ) where {T}
94- N = length (size)
95- return SparseArrayDOK {T,N} (Dict {CartesianIndex{N},T} (), size)
94+ N = length (size)
95+ return SparseArrayDOK {T, N} (Dict {CartesianIndex{N}, T} (), size)
9696end
9797function isstored (a:: SparseArrayDOK , I:: Int... )
98- return CartesianIndex (I) in keys (storage (a))
98+ return CartesianIndex (I) in keys (storage (a))
9999end
100100function getstoredindex (a:: SparseArrayDOK , I:: Int... )
101- return storage (a)[CartesianIndex (I)]
101+ return storage (a)[CartesianIndex (I)]
102102end
103103function getunstoredindex (a:: SparseArrayDOK , I:: Int... )
104- return zero (eltype (a))
104+ return zero (eltype (a))
105105end
106106function setstoredindex! (a:: SparseArrayDOK , value, I:: Int... )
107- storage (a)[CartesianIndex (I)] = value
108- return a
107+ storage (a)[CartesianIndex (I)] = value
108+ return a
109109end
110110function setunstoredindex! (a:: SparseArrayDOK , value, I:: Int... )
111- storage (a)[CartesianIndex (I)] = value
112- return a
111+ storage (a)[CartesianIndex (I)] = value
112+ return a
113113end
114114
115115# Specify the interface the type adheres to.
@@ -119,9 +119,9 @@ DerivableInterfaces.interface(::Type{<:SparseArrayDOK}) = SparseArrayInterface()
119119@array_aliases SparseArrayDOK
120120
121121# DerivableInterfaces the interface for the type.
122- @derive (T= SparseArrayDOK,) begin
123- Base. getindex (:: T , :: Int... )
124- Base. setindex! (:: T , :: Any , :: Int... )
122+ @derive (T = SparseArrayDOK,) begin
123+ Base. getindex (:: T , :: Int... )
124+ Base. setindex! (:: T , :: Any , :: Int... )
125125end
126126
127127a = SparseArrayDOK {Float64} (2 , 2 )
0 commit comments