11# SparseBlockTensorMap parent array
22# ---------------------------------
3- struct SparseTensorArray{S,N₁,N₂,T <: AbstractTensorMap{<:Any,S, N₁,N₂} ,N} < :
4- AbstractArray{T,N}
5- data:: Dict{CartesianIndex{N},T}
6- space:: TensorMapSumSpace{S,N₁,N₂}
7- function SparseTensorArray {S,N₁,N₂,T, N} (
8- data:: Dict{CartesianIndex{N},T} , space:: TensorMapSumSpace{S,N₁,N₂}
9- ) where {S,N₁,N₂,T, N}
3+ struct SparseTensorArray{S, N₁, N₂, T <: AbstractTensorMap{<:Any, S, N₁, N₂} , N} < :
4+ AbstractArray{T, N}
5+ data:: Dict{CartesianIndex{N}, T}
6+ space:: TensorMapSumSpace{S, N₁, N₂}
7+ function SparseTensorArray {S, N₁, N₂, T, N} (
8+ data:: Dict{CartesianIndex{N}, T} , space:: TensorMapSumSpace{S, N₁, N₂}
9+ ) where {S, N₁, N₂, T, N}
1010 N₁ + N₂ == N || throw (
1111 TypeError (
1212 :SparseTensorArray ,
13- SparseTensorArray{S,N₁,N₂,T, N₁ + N₂},
14- SparseTensorArray{S,N₁,N₂,T, N},
13+ SparseTensorArray{S, N₁, N₂, T, N₁ + N₂},
14+ SparseTensorArray{S, N₁, N₂, T, N},
1515 ),
1616 )
17- return new {S,N₁,N₂,T, N} (data, space)
17+ return new {S, N₁, N₂, T, N} (data, space)
1818 end
1919end
2020
21- function SparseTensorArray {S,N₁,N₂,T, N} (
22- :: UndefInitializer , space:: TensorMapSumSpace{S,N₁,N₂}
23- ) where {S,N₁,N₂,T <: AbstractTensorMap{<:Any,S, N₁,N₂} ,N}
24- return SparseTensorArray {S,N₁,N₂,T, N} (Dict {CartesianIndex{N},T} (), space)
21+ function SparseTensorArray {S, N₁, N₂, T, N} (
22+ :: UndefInitializer , space:: TensorMapSumSpace{S, N₁, N₂}
23+ ) where {S, N₁, N₂, T <: AbstractTensorMap{<:Any, S, N₁, N₂} , N}
24+ return SparseTensorArray {S, N₁, N₂, T, N} (Dict {CartesianIndex{N}, T} (), space)
2525end
2626
2727function SparseTensorArray (
28- data:: Dict{CartesianIndex{N},T} , space:: TensorMapSumSpace{S,N₁,N₂}
29- ) where {S,N₁,N₂,T, N}
30- return SparseTensorArray {S,N₁,N₂,T, N} (data, space)
28+ data:: Dict{CartesianIndex{N}, T} , space:: TensorMapSumSpace{S, N₁, N₂}
29+ ) where {S, N₁, N₂, T, N}
30+ return SparseTensorArray {S, N₁, N₂, T, N} (data, space)
3131end
3232
3333Base. pairs (A:: SparseTensorArray ) = pairs (A. data)
@@ -41,29 +41,29 @@ TensorKit.space(A::SparseTensorArray) = A.space
4141Base. size (A:: SparseTensorArray ) = ntuple (i -> length (space (A)[i]), ndims (A))
4242
4343@inline function Base. getindex (
44- A:: SparseTensorArray{S,N₁,N₂,T, N} , I:: Vararg{Int,N}
45- ) where {S,N₁,N₂,T, N}
44+ A:: SparseTensorArray{S, N₁, N₂, T, N} , I:: Vararg{Int, N}
45+ ) where {S, N₁, N₂, T, N}
4646 @boundscheck checkbounds (A, I... )
4747 return @inbounds get (A. data, CartesianIndex (I)) do
4848 return fill! (similar (T, eachspace (A)[I... ]), zero (scalartype (T)))
4949 end
5050end
5151@inline function getindex! (
52- A:: SparseTensorArray{S,N₁,N₂,T, N} , I:: CartesianIndex{N}
53- ) where {S,N₁,N₂,T, N}
52+ A:: SparseTensorArray{S, N₁, N₂, T, N} , I:: CartesianIndex{N}
53+ ) where {S, N₁, N₂, T, N}
5454 @boundscheck checkbounds (A, I)
5555 return @inbounds get! (A. data, I) do
5656 return fill! (similar (T, eachspace (A)[I]), zero (scalartype (T)))
5757 end
5858end
5959@inline function getindex! (
60- A:: SparseTensorArray{S,N₁,N₂,T, N} , I:: Vararg{Int,N}
61- ) where {S,N₁,N₂,T, N}
60+ A:: SparseTensorArray{S, N₁, N₂, T, N} , I:: Vararg{Int, N}
61+ ) where {S, N₁, N₂, T, N}
6262 return getindex! (A, CartesianIndex (I))
6363end
6464@inline function Base. setindex! (
65- A:: SparseTensorArray{S,N₁,N₂,T, N} , v, I:: Vararg{Int,N}
66- ) where {S,N₁,N₂,T, N}
65+ A:: SparseTensorArray{S, N₁, N₂, T, N} , v, I:: Vararg{Int, N}
66+ ) where {S, N₁, N₂, T, N}
6767 @boundscheck begin
6868 checkbounds (A, I... )
6969 checkspaces (A, v, I... )
7272 return A
7373end
7474
75- function Base. delete! (A:: SparseTensorArray , I:: Vararg{Int,N} ) where {N}
75+ function Base. delete! (A:: SparseTensorArray , I:: Vararg{Int, N} ) where {N}
7676 return delete! (A. data, CartesianIndex (I))
7777end
7878Base. delete! (A:: SparseTensorArray , I:: CartesianIndex ) = delete! (A. data, I)
7979Base. empty! (A:: SparseTensorArray ) = empty! (A. data)
80- function Base. haskey (A:: SparseTensorArray , I:: Vararg{Int,N} ) where {N}
80+ function Base. haskey (A:: SparseTensorArray , I:: Vararg{Int, N} ) where {N}
8181 return haskey (A. data, CartesianIndex (I))
8282end
8383Base. haskey (A:: SparseTensorArray , I:: CartesianIndex ) = haskey (A. data, I)
8484
8585function Base. similar (
86- :: SparseTensorArray , :: Type{T} , spaces:: TensorMapSumSpace{S,N₁,N₂}
87- ) where {S,N₁,N₂,T <: AbstractTensorMap{<:Any,S, N₁,N₂} }
86+ :: SparseTensorArray , :: Type{T} , spaces:: TensorMapSumSpace{S, N₁, N₂}
87+ ) where {S, N₁, N₂, T <: AbstractTensorMap{<:Any, S, N₁, N₂} }
8888 N = N₁ + N₂
89- return SparseTensorArray {S,N₁,N₂,T, N} (Dict {CartesianIndex{N},T} (), spaces)
89+ return SparseTensorArray {S, N₁, N₂, T, N} (Dict {CartesianIndex{N}, T} (), spaces)
9090end
9191
9292Base. @propagate_inbounds function Base. copyto! (
93- t:: SparseTensorArray , v:: SubArray{T,N, A}
94- ) where {T,N,A <: SparseTensorArray }
93+ t:: SparseTensorArray , v:: SubArray{T, N, A}
94+ ) where {T, N, A <: SparseTensorArray }
9595 undropped_parentindices = map (Base. parentindices (v)) do I
9696 I isa Base. ScalarIndex ? (I: I) : I
9797 end
@@ -108,8 +108,8 @@ Base.@propagate_inbounds function Base.copyto!(
108108end
109109
110110Base. @propagate_inbounds function Base. copyto! (
111- t:: SubArray{T,N, A} , v:: SparseTensorArray
112- ) where {T,N,A <: SparseTensorArray }
111+ t:: SubArray{T, N, A} , v:: SparseTensorArray
112+ ) where {T, N, A <: SparseTensorArray }
113113 undropped_parentindices = map (Base. parentindices (t)) do I
114114 I isa Base. ScalarIndex ? (I: I) : I
115115 end
@@ -126,11 +126,9 @@ Base.@propagate_inbounds function Base.copyto!(
126126end
127127
128128Base. @propagate_inbounds function Base. copyto! (
129- dest:: SparseTensorArray ,
130- Rdest:: CartesianIndices ,
131- src:: SparseTensorArray ,
132- Rsrc:: CartesianIndices ,
133- )
129+ dest:: SparseTensorArray , Rdest:: CartesianIndices ,
130+ src:: SparseTensorArray , Rsrc:: CartesianIndices ,
131+ )
134132 isempty (Rdest) && return dest
135133 if size (Rdest) != size (Rsrc)
136134 throw (
@@ -174,10 +172,9 @@ function _newindices(I::Tuple, indices::Tuple)
174172end
175173
176174function Base. _unsafe_getindex (
177- :: IndexCartesian ,
178- t:: SparseTensorArray{S,N₁,N₂,T,N} ,
179- I:: Vararg{Union{Real,AbstractArray},N} ,
180- ) where {S,N₁,N₂,T,N}
175+ :: IndexCartesian ,
176+ t:: SparseTensorArray{S, N₁, N₂, T, N} , I:: Vararg{Union{Real, AbstractArray}, N} ,
177+ ) where {S, N₁, N₂, T, N}
181178 dest = similar (t, eltype (t), space (eachspace (t)[I... ]))
182179 indices = Base. to_indices (t, I)
183180 for (k, v) in t. data
0 commit comments