@@ -22,31 +22,35 @@ struct SectorStructure{I <: Sector, F <: FusionTreePair{I}, T <: AbstractIndices
2222end
2323
2424"""
25- FusionTreePairs{F₁, F₂} <: AbstractIndices{Tuple{F₁, F₂}}
25+ FusionTreePairs{I, F₁, F₂} <: AbstractIndices{Tuple{F₁, F₂}}
2626
2727The fusion tree pairs `(f₁, f₂)` of a `HomSpace`, ordered by coupled sector, then `f₂`, then `f₁`,
28- stored as the splitting trees `f₁` and the fusion trees `f₂` of every coupled sector. The token of
29- a pair is its position.
28+ stored as the coupled sectors and the splitting trees `f₁` and fusion trees `f₂` of every coupled
29+ sector. The token of a pair is `(i₁, i₂)`, the positions of `f₁` and `f₂` among all splitting and
30+ fusion trees; `pairposition` converts it into the position of the pair.
3031"""
31- struct FusionTreePairs{F₁, F₂} <: AbstractIndices{Tuple{F₁, F₂}}
32+ struct FusionTreePairs{I, F₁, F₂} <: AbstractIndices{Tuple{F₁, F₂}}
33+ blocksectors:: Indices{I}
3234 trees₁:: Indices{F₁} # splitting trees, grouped by coupled sector
3335 trees₂:: Indices{F₂} # fusion trees, grouped by coupled sector
34- blocks₁:: Vector{Int} # the coupled sector of every splitting tree
35- blocks₂:: Vector{Int} # the coupled sector of every fusion tree
3636 ranges₁:: Vector{UnitRange{Int}} # the splitting trees of every coupled sector
3737 ranges₂:: Vector{UnitRange{Int}} # the fusion trees of every coupled sector
38+ positions₁:: Vector{Int} # the position of every splitting tree within its coupled sector
39+ positions₂:: Vector{Int} # the number of pairs before the first pair with every fusion tree
3840 offsets:: Vector{Int} # the number of pairs before every coupled sector, and in total
3941end
4042
4143# from the trees of both sides of every coupled sector
42- function FusionTreePairs {F₁, F₂} (sides₁, sides₂) where {F₁, F₂}
44+ function FusionTreePairs {I, F₁, F₂} (blocksectors :: Indices{I} , sides₁, sides₂) where {I, F₁, F₂}
4345 lengths (ss) = map (length, ss)
4446 ranges (ss) = map ((n, l) -> (n - l + 1 ): n, cumsum (lengths (ss)), lengths (ss))
45- blocks (ss) = Int[b for (b, ts) in enumerate (ss) for _ in ts]
4647 offsets = cumsum (vcat (0 , lengths (sides₁) .* lengths (sides₂)))
47- return FusionTreePairs {F₁, F₂} (
48+ positions₁ = Int[j for ts in sides₁ for j in eachindex (ts)]
49+ positions₂ = Int[offsets[b] + (j - 1 ) * length (sides₁[b]) for (b, ts) in enumerate (sides₂) for j in eachindex (ts)]
50+ return FusionTreePairs {I, F₁, F₂} (
51+ blocksectors,
4852 Indices (F₁[f for ts in sides₁ for f in ts]), Indices (F₂[f for ts in sides₂ for f in ts]),
49- blocks (sides₁), blocks (sides₂), ranges (sides₁), ranges (sides₂) , offsets
53+ ranges (sides₁), ranges (sides₂), positions₁, positions₂ , offsets
5054 )
5155end
5256
@@ -55,39 +59,83 @@ _cache_payload_size(t::FusionTreePairs) =
5559
5660Base. length (t:: FusionTreePairs ) = last (t. offsets)
5761Dictionaries. istokenizable (:: FusionTreePairs ) = true
58- Dictionaries. tokentype (:: FusionTreePairs ) = Int
59- Dictionaries. iteratetoken (t:: FusionTreePairs , i:: Int = 0 ) = i < length (t) ? (i + 1 , i + 1 ) : nothing
60- Dictionaries. iteratetoken_reverse (t:: FusionTreePairs , i:: Int = length (t) + 1 ) = i > 1 ? (i - 1 , i - 1 ) : nothing
61- # tokens as returned by `gettoken` look like those of `Indices`, with the position second
62- function Dictionaries. gettoken (t:: FusionTreePairs , (f₁, f₂))
63- found₁, (_, i₁) = gettoken (t. trees₁, f₁)
64- found₂, (_, i₂) = gettoken (t. trees₂, f₂)
65- (found₁ && found₂ && t. blocks₁[i₁] == t. blocks₂[i₂]) || return false , (0 , 0 )
66- b = t. blocks₁[i₁]
67- r₁, r₂ = t. ranges₁[b], t. ranges₂[b]
68- return true , (0 , t. offsets[b] + (i₂ - first (r₂)) * length (r₁) + (i₁ - first (r₁)) + 1 )
62+ Dictionaries. tokentype (:: FusionTreePairs ) = Tuple{Int, Int}
63+
64+ """
65+ pairposition(t::FusionTreePairs, token) -> Int
66+ pairposition(t::FusionTreePairs, (f₁, f₂)) -> Int
67+
68+ The position of a fusion tree pair in the canonical order of `t`, from its token or from the pair
69+ itself, which must be present.
70+ """
71+ pairposition (t:: FusionTreePairs , (i₁, i₂):: Tuple{Int, Int} ) = @inbounds t. positions₂[i₂] + t. positions₁[i₁]
72+ function pairposition (t:: FusionTreePairs , f:: FusionTreePair )
73+ found, token = gettoken (t, f)
74+ found || throw (KeyError (f))
75+ return pairposition (t, token)
6976end
70- Dictionaries . gettokenvalue (t :: FusionTreePairs , (_, i) :: Tuple{Int, Int} ) = gettokenvalue (t, i)
71- function Dictionaries . gettokenvalue (t:: FusionTreePairs , i:: Int )
77+ # the token of the pair at position `i`
78+ function pairtoken (t:: FusionTreePairs , i:: Int )
7279 b = searchsortedlast (t. offsets, i - 1 )
7380 r₁, r₂ = t. ranges₁[b], t. ranges₂[b]
7481 j₂, j₁ = divrem (i - 1 - t. offsets[b], length (r₁))
75- return (gettokenvalue (t . trees₁, first (r₁) + j₁), gettokenvalue (t . trees₂, first (r₂) + j₂) )
82+ return (first (r₁) + j₁, first (r₂) + j₂)
7683end
77- Base. iterate (t:: FusionTreePairs ) = iterate (t, (1 , 0 , 1 ))
78- function Base. iterate (t:: FusionTreePairs , (b, i₁, i₂))
84+
85+ # iterate over the tokens, with the state `(b, i₁, i₂)`: the coupled sector and the tokens of the
86+ # current pair, or those before the first pair (forward) or after the last pair (reverse)
87+ function Dictionaries. iteratetoken (t:: FusionTreePairs , (b, i₁, i₂) = (1 , 0 , 0 ))
7988 while b <= length (t. ranges₁)
8089 r₁, r₂ = t. ranges₁[b], t. ranges₂[b]
81- i₁ += 1
82- i₁ > length (r₁) && (i₁ = 1 ; i₂ += 1 )
83- i₂ <= length (r₂) && ! isempty (r₁) &&
84- return (gettokenvalue (t. trees₁, first (r₁) + i₁ - 1 ), gettokenvalue (t. trees₂, first (r₂) + i₂ - 1 )), (b, i₁, i₂)
85- b, i₁, i₂ = b + 1 , 0 , 1
90+ if ! (isempty (r₁) || isempty (r₂))
91+ if i₁ < first (r₁) # before the first pair of `b`
92+ i₁, i₂ = first (r₁), first (r₂)
93+ return (i₁, i₂), (b, i₁, i₂)
94+ elseif i₁ < last (r₁)
95+ return (i₁ + 1 , i₂), (b, i₁ + 1 , i₂)
96+ elseif i₂ < last (r₂)
97+ return (first (r₁), i₂ + 1 ), (b, first (r₁), i₂ + 1 )
98+ end
99+ end
100+ b, i₁, i₂ = b + 1 , 0 , 0
86101 end
87102 return nothing
88103end
104+ function Dictionaries. iteratetoken_reverse (t:: FusionTreePairs , (b, i₁, i₂) = (length (t. ranges₁), typemax (Int), typemax (Int)))
105+ while b >= 1
106+ r₁, r₂ = t. ranges₁[b], t. ranges₂[b]
107+ if ! (isempty (r₁) || isempty (r₂))
108+ if i₁ > last (r₁) # after the last pair of `b`
109+ i₁, i₂ = last (r₁), last (r₂)
110+ return (i₁, i₂), (b, i₁, i₂)
111+ elseif i₁ > first (r₁)
112+ return (i₁ - 1 , i₂), (b, i₁ - 1 , i₂)
113+ elseif i₂ > first (r₂)
114+ return (last (r₁), i₂ - 1 ), (b, last (r₁), i₂ - 1 )
115+ end
116+ end
117+ b, i₁, i₂ = b - 1 , typemax (Int), typemax (Int)
118+ end
119+ return nothing
120+ end
121+ function Dictionaries. gettoken (t:: FusionTreePairs , (f₁, f₂))
122+ f₁. coupled == f₂. coupled || return false , (0 , 0 )
123+ found₁, (_, i₁) = gettoken (t. trees₁, f₁)
124+ found₂, (_, i₂) = gettoken (t. trees₂, f₂)
125+ return found₁ & found₂, (i₁, i₂)
126+ end
127+ Dictionaries. gettokenvalue (t:: FusionTreePairs , (i₁, i₂):: Tuple{Int, Int} ) =
128+ (gettokenvalue (t. trees₁, i₁), gettokenvalue (t. trees₂, i₂))
129+ # the pair at position `i`
130+ Dictionaries. gettokenvalue (t:: FusionTreePairs , i:: Int ) = gettokenvalue (t, pairtoken (t, i))
131+ function Base. iterate (t:: FusionTreePairs , state... )
132+ next = Dictionaries. iteratetoken (t, state... )
133+ isnothing (next) && return nothing
134+ token, newstate = next
135+ return gettokenvalue (t, token), newstate
136+ end
89137
90- # values for every fusion tree pair, sharing the tokens of `FusionTreePairs`
138+ # values for every fusion tree pair, stored by position
91139struct FusionTreePairDict{K, V, T <: FusionTreePairs } <: AbstractDictionary{K, V}
92140 keys:: T
93141 values:: Vector{V}
@@ -98,15 +146,15 @@ Base.keys(d::FusionTreePairDict) = d.keys
98146Dictionaries. istokenizable (:: FusionTreePairDict ) = true
99147Dictionaries. istokenassigned (:: FusionTreePairDict , _) = true
100148Dictionaries. gettokenvalue (d:: FusionTreePairDict , i:: Int ) = d. values[i]
101- Dictionaries. gettokenvalue (d:: FusionTreePairDict , (_, i) :: Tuple{Int, Int} ) = d. values[i ]
149+ Dictionaries. gettokenvalue (d:: FusionTreePairDict , token :: Tuple{Int, Int} ) = d. values[pairposition (d . keys, token) ]
102150
103151MemoizationKit. cachesize (s:: SectorStructure ) =
104152 sizeof (s) + _cache_payload_size (s. blocksectors) + _cache_payload_size (s. fusiontrees)
105153
106154Base. @assume_effects :foldable function sectorstructuretype (:: S ) where {S <: HomSpace }
107155 I = sectortype (S)
108156 F₁, F₂ = fusiontreetype (I, numout (S)), fusiontreetype (I, numin (S))
109- return SectorStructure{I, Tuple{F₁, F₂}, FusionTreePairs{F₁, F₂}}
157+ return SectorStructure{I, Tuple{F₁, F₂}, FusionTreePairs{I, F₁, F₂}}
110158end
111159
112160"""
@@ -120,11 +168,12 @@ See also [`degeneracystructure`](@ref), [`fusiontrees`](@ref), [`blocksectors`](
120168"""
121169@cached function sectorstructure (W:: S ):: sectorstructuretype (W) where {S <: HomSpace }
122170 # the trees of both sides of every coupled sector; the pairs are not stored
123- bs = vec (collect (_blocksectors (W))) # `vec`: the units of a product sector have a shape
124- F₁, F₂ = fusiontreetype (sectortype (S), numout (S)), fusiontreetype (sectortype (S), numin (S))
125- sides₁ = map (c -> collect (fusiontrees (codomain (W), c)), bs)
126- sides₂ = map (c -> collect (fusiontrees (domain (W), c)), bs)
127- return sectorstructuretype (W)(Indices (bs), FusionTreePairs {F₁, F₂} (sides₁, sides₂))
171+ I = sectortype (S)
172+ bs = Indices (vec (collect (_blocksectors (W)))) # `vec`: the units of a product sector have a shape
173+ F₁, F₂ = fusiontreetype (I, numout (S)), fusiontreetype (I, numin (S))
174+ sides₁ = map (c -> collect (fusiontrees (codomain (W), c)), bs. values)
175+ sides₂ = map (c -> collect (fusiontrees (domain (W), c)), bs. values)
176+ return sectorstructuretype (W)(bs, FusionTreePairs {I, F₁, F₂} (bs, sides₁, sides₂))
128177end
129178
130179MemoizationKit. cachekey (:: typeof (sectorstructure), W:: HomSpace ) = (Hashed (W, sectorhash, sectorequal),)
0 commit comments