@@ -421,28 +421,35 @@ end
421421
422422# Getting and setting the data at the block level
423423# -------------------------------------------------
424- function block (t:: TensorMap , s:: Sector )
425- sectortype (t) == typeof (s) || throw (SectorMismatch ())
426- structure = fusionblockstructure (t). blockstructure
427- (d₁, d₂), r = get (structure, s) do
424+ block (t:: TensorMap , c:: Sector ) = blocks (t)[c]
425+
426+ blocks (t:: TensorMap ) = BlockIterator (t, fusionblockstructure (t). blockstructure)
427+
428+ function blocktype (:: Type{TT} ) where {TT<: TensorMap }
429+ A = storagetype (TT)
430+ T = eltype (A)
431+ return Base. ReshapedArray{T,2 ,SubArray{T,1 ,A,Tuple{UnitRange{Int}},true },Tuple{}}
432+ end
433+
434+ function Base. iterate (iter:: BlockIterator{<:TensorMap} , state... )
435+ next = iterate (iter. structure, state... )
436+ isnothing (next) && return next
437+ (c, (sz, r)), newstate = next
438+ return c => reshape (view (iter. t. data, r), sz), newstate
439+ end
440+
441+ function Base. getindex (iter:: BlockIterator{<:TensorMap} , c:: Sector )
442+ sectortype (iter. t) === typeof (c) || throw (SectorMismatch ())
443+ (d₁, d₂), r = get (iter. structure, c) do
428444 # is s is not a key, at least one of the two dimensions will be zero:
429445 # it then does not matter where exactly we construct a view in `t.data`,
430446 # as it will have length zero anyway
431- d₁′ = blockdim (codomain (t), s )
432- d₂′ = blockdim (domain (t), s )
447+ d₁′ = blockdim (codomain (iter . t), c )
448+ d₂′ = blockdim (domain (iter . t), c )
433449 l = d₁′ * d₂′
434450 return (d₁′, d₂′), 1 : l
435451 end
436- return reshape (view (t. data, r), (d₁, d₂))
437- end
438-
439- function blocks (t:: TensorMap )
440- structure = fusionblockstructure (t). blockstructure
441- iter = Base. Iterators. map (structure) do (c, ((d₁, d₂), r))
442- b = reshape (view (t. data, r), (d₁, d₂))
443- return c => b
444- end
445- return iter
452+ return reshape (view (iter. t. data, r), (d₁, d₂))
446453end
447454
448455# Indexing and getting and setting the data at the subblock level
0 commit comments