Skip to content

Commit 1fef222

Browse files
committed
Add block iterator for DiagonalTensorMap
1 parent 3c7b830 commit 1fef222

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/spaces/homspace.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,18 @@ function fusionblockstructure(W::HomSpace, ::GlobalLRUCache)
349349
end
350350
return structure
351351
end
352+
353+
# Diagonal ranges
354+
#----------------
355+
# TODO: is this something we want to cache?
356+
function diagonalblockstructure(W::HomSpace)
357+
structure = SectorDict{sectortype(W),UnitRange{Int}}() # range
358+
offset = 0
359+
dom = domain(W)[1]
360+
for c in blocksectors(W)
361+
d = dim(dom, c)
362+
structure[c] = offset .+ (1:d)
363+
offset + d
364+
end
365+
return structure
366+
end

src/tensors/diagonal.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,21 @@ function block(d::DiagonalTensorMap, s::Sector)
110110
return Diagonal(view(d.data, 1:0))
111111
end
112112

113-
# TODO: is relying on generic AbstractTensorMap blocks sufficient?
113+
blocks(t::DiagonalTensorMap) = BlockIterator(t, diagonalblockstructure(space(t)))
114+
blocktype(::Type{TT}) where {TT<:DiagonalTensorMap} = Diagonal{eltype(TT),storagetype(TT)}
115+
116+
function Base.iterate(iter::BlockIterator{<:DiagonalTensorMap}, state...)
117+
next = iterate(iter.structure, state...)
118+
isnothing(next) && return next
119+
(c, r), newstate = next
120+
return c => Diagonal(view(iter.t.data, r)), newstate
121+
end
122+
123+
function Base.getindex(iter::BlockIterator{<:DiagonalTensorMap}, c::Sector)
124+
sectortype(iter.t) === typeof(c) || throw(SectorMismatch())
125+
r = get(iter.structure, c, 1:0)
126+
return Diagonal(view(iter.t.data, r))
127+
end
114128

115129
# Indexing and getting and setting the data at the subblock level
116130
#-----------------------------------------------------------------

0 commit comments

Comments
 (0)