1+ export WeightedCell, WeightedGadget, WeightedNode
12# TODO :
23# * add path decomposition
34struct WeightedCell{RT} <: AbstractCell
@@ -7,13 +8,10 @@ struct WeightedCell{RT} <: AbstractCell
78 weight:: RT
89end
910
10- abstract type WeightedCrossPattern <: Pattern end
11- abstract type WeightedSimplifyPattern <: Pattern end
1211struct WeightedGadget{GT} <: Pattern
1312 gadget:: GT
14- factor:: Int
1513end
16- const WeightedPattern = Union{WeightedCrossPattern, WeightedSimplifyPattern, WeightedGadget}
14+ const WeightedGadgetTypes = Union{WeightedGadget, RotatedGadget{ <: WeightedGadget }, ReflectedGadget{ <: WeightedGadget } }
1715
1816Base. isempty (cell:: WeightedCell ) = ! cell. occupied
1917Base. empty (:: Type{WeightedCell{RT}} ) where RT = WeightedCell (false , false , false ,0 )
@@ -62,143 +60,76 @@ _weight2(::CopyLine{Weighted}, i, j) = WeightedNode(i, j, 2)
6260_weight1 (:: CopyLine{Weighted} , i, j) = WeightedNode (i, j, 1 )
6361_cell_type (:: Type{<:WeightedNode} ) = WeightedCell{Int}
6462
65- function source_graph (r:: WeightedGadget )
66- locs, g, pins = source_graph (r. gadget)
67- _mul_weight .(locs, r. factor), g, pins
68- end
69- function mapped_graph (r:: WeightedGadget )
70- locs, g, pins = mapped_graph (r. gadget)
71- _mul_weight .(locs, r. factor), g, pins
72- end
73- _mul_weight (node:: SimpleNode , factor) = WeightedNode (node... , factor)
74- mis_overhead (p:: WeightedGadget ) = 2 * mis_overhead (p. gadget)
75-
76- # new gadgets
77- struct WeightedWTurn <: WeightedCrossPattern end
78- # ⋅ ⋅ ⋅ ⋅
79- # ⋅ ⋅ ◯ ●
80- # ⋅ ● ● ⋅
81- # ⋅ ● ⋅ ⋅
82-
83- # ⋅ ⋅ ⋅ ⋅
84- # ⋅ ⋅ ⋅ ●
85- # ⋅ ⋅ ◯ ⋅
86- # ⋅ ● ⋅ ⋅
87-
88- struct WeightedBranchFix <: WeightedCrossPattern end
89- # ⋅ ● ⋅ ⋅
90- # ⋅ ● ◯ ⋅
91- # ⋅ ● ● ⋅
92- # ⋅ ● ⋅ ⋅
93-
94- # ⋅ ● ⋅ ⋅
95- # ⋅ ● ⋅ ⋅
96- # ⋅ ◯ ⋅ ⋅
97- # ⋅ ● ⋅ ⋅
98-
99- struct WeightedTurn <: WeightedCrossPattern end
100- # ⋅ ● ⋅ ⋅
101- # ⋅ ● ⋅ ⋅
102- # ⋅ ● ◯ ●
103- # ⋅ ⋅ ⋅ ⋅
104-
105- # ⋅ ● ⋅ ⋅
106- # ⋅ ⋅ ◯ ⋅
107- # ⋅ ⋅ ⋅ ●
108- # ⋅ ⋅ ⋅ ⋅
109-
110- struct WeightedBranch <: WeightedCrossPattern end
111- # ⋅ ● ⋅ ⋅
112- # ⋅ ● ⋅ ⋅
113- # ⋅ ● ◯ ●
114- # ⋅ ● ● ⋅
115- # ⋅ ● ⋅ ⋅
116-
117-
118- # ⋅ ● ⋅ ⋅ ?
119- # ⋅ ⋅ ◯ ⋅
120- # ⋅ ● ⋅ ●
121- # ⋅ ⋅ ● ⋅
122- # ⋅ ● ⋅ ⋅
123-
124- struct WeightedBranchFixB <: WeightedCrossPattern end
125- # ⋅ ⋅ ⋅ ⋅
126- # ⋅ ⋅ ◯ ⋅
127- # ⋅ ● ● ⋅
128- # ⋅ ● ⋅ ⋅
129-
130- # ⋅ ⋅ ⋅ ⋅
131- # ⋅ ⋅ ⋅ ⋅
132- # ⋅ ◯ ⋅ ⋅
133- # ⋅ ● ⋅ ⋅
134-
135- struct WeightedEndTurn <: WeightedCrossPattern end
136- # ⋅ ● ⋅ ⋅
137- # ⋅ ● ◯ ⋅
138- # ⋅ ⋅ ⋅ ⋅
139-
140- # ⋅ ◯ ⋅ ⋅
141- # ⋅ ⋅ ⋅ ⋅
142- # ⋅ ⋅ ⋅ ⋅
143-
144- for T in [:Cross , :TrivialTurn , :TCon ]
145- @eval weighted (c:: $T ) = WeightedGadget (c, 2 )
146- end
63+ weighted (c:: Pattern ) = WeightedGadget (c)
14764unweighted (w:: WeightedGadget ) = w. gadget
14865weighted (r:: RotatedGadget ) = RotatedGadget (weighted (r. gadget), r. n)
14966weighted (r:: ReflectedGadget ) = ReflectedGadget (weighted (r. gadget), r. mirror)
15067unweighted (r:: RotatedGadget ) = RotatedGadget (unweighted (r. gadget), r. n)
15168unweighted (r:: ReflectedGadget ) = ReflectedGadget (unweighted (r. gadget), r. mirror)
69+ mis_overhead (w:: WeightedGadget ) = mis_overhead (w. gadget) * 2
15270
153- for T in [:Turn , :Branch , :BranchFix , :BranchFixB , :WTurn , :EndTurn ]
154- WT = Symbol (:Weighted , T)
155- @eval weighted (:: $T ) = $ WT ()
156- @eval unweighted (:: $WT ) = $ T ()
157- @eval mis_overhead (:: $WT ) = mis_overhead ($ T ()) * 2
71+ function source_graph (r:: WeightedGadget )
72+ raw = unweighted (r)
73+ locs, g, pins = source_graph (raw)
74+ return map (loc-> _mul_weight (loc, getxy (loc) ∈ source_centers (r) ? 1 : 2 ), locs), g, pins
75+ end
76+ function mapped_graph (r:: WeightedGadget )
77+ raw = unweighted (r)
78+ locs, g, pins = mapped_graph (raw)
79+ return map (loc-> _mul_weight (loc, getxy (loc) ∈ mapped_centers (r) ? 1 : 2 ), locs), g, pins
15880end
81+ _mul_weight (node:: SimpleNode , factor) = WeightedNode (node... , factor)
15982
16083for (T, centerloc) in [(:Turn , (2 , 3 )), (:Branch , (2 , 3 )), (:BranchFix , (3 , 2 )), (:BranchFixB , (3 , 2 )), (:WTurn , (3 , 3 )), (:EndTurn , (1 , 2 ))]
161- WT = Symbol ( :Weighted , T)
162- @eval function source_graph (r :: $WT )
163- raw = unweighted (r)
164- locs, g, pins = source_graph (raw)
165- return map (loc -> _mul_weight (loc, loc == SimpleNode ( cross_location (raw) .+ ( 0 , 1 )) ? 1 : 2 ), locs), g, pins
166- end
167- @eval function mapped_graph (r :: $WT )
168- raw = unweighted (r )
169- locs, g, pins = mapped_graph (raw )
170- return map (loc-> _mul_weight ( loc, loc == SimpleNode ( $ centerloc) ? 1 : 2 ), locs ), g, pins
84+ @eval source_centers ( :: WeightedGadget{<:$T} ) = [ cross_location ( $ T ()) .+ ( 0 , 1 )]
85+ @eval mapped_centers ( :: WeightedGadget{<:$T} ) = [ $ centerloc]
86+ end
87+ # default to having no source center!
88+ source_centers ( :: WeightedGadget ) = Tuple{Int,Int}[]
89+ mapped_centers ( :: WeightedGadget ) = Tuple{Int,Int}[]
90+ for T in [:(RotatedGadget{ <: WeightedGadget }), :(ReflectedGadget{ <: WeightedGadget })]
91+ @eval function source_centers (r :: $T )
92+ cross = cross_location (r . gadget )
93+ return map (loc-> loc .+ _get_offset (r), _apply_transform .( Ref (r ), source_centers (r . gadget ), Ref (cross)))
17194 end
172- @eval function move_center (:: $WT , nodexy)
173- nodexy .+ $ centerloc .- (cross_location ($ WT ()) .+ (0 , 1 ))
95+ @eval function mapped_centers (r:: $T )
96+ cross = cross_location (r. gadget)
97+ return map (loc-> loc .+ _get_offset (r), _apply_transform .(Ref (r), mapped_centers (r. gadget), Ref (cross)))
17498 end
17599end
176- move_center (:: Pattern , nodexy) = nodexy
177100
178- for T in [:WeightedCrossPattern , :WeightedGadget ]
179- @eval Base. size (r:: $T ) = size (unweighted (r))
180- @eval cross_location (r:: $T ) = cross_location (unweighted (r))
181- @eval iscon (r:: $T ) = iscon (unweighted (r))
182- @eval connected_nodes (r:: $T ) = connected_nodes (unweighted (r))
183- @eval vertex_overhead (r:: $T ) = vertex_overhead (unweighted (r))
184- end
101+ Base. size (r:: WeightedGadget ) = size (unweighted (r))
102+ cross_location (r:: WeightedGadget ) = cross_location (unweighted (r))
103+ iscon (r:: WeightedGadget ) = iscon (unweighted (r))
104+ connected_nodes (r:: WeightedGadget ) = connected_nodes (unweighted (r))
105+ vertex_overhead (r:: WeightedGadget ) = vertex_overhead (unweighted (r))
185106
186107const crossing_ruleset_weighted = weighted .(crossing_ruleset)
187108get_ruleset (:: Weighted ) = crossing_ruleset_weighted
188109
189110export get_weights
190111get_weights (ug:: UGrid ) = [ug. content[ci... ]. weight for ci in coordinates (ug)]
191112
113+ # mapping configurations back
192114export trace_centers
115+ function move_center (w:: WeightedGadgetTypes , nodexy, offset)
116+ for (sc, mc) in zip (source_centers (w), mapped_centers (w))
117+ if offset == sc
118+ return nodexy .+ mc .- sc # found
119+ end
120+ end
121+ error (" center not found, source center = $(source_centers (w)) , while offset = $(offset) " )
122+ end
123+
193124trace_centers (r:: MappingResult ) = trace_centers (r. grid_graph, r. mapping_history)
194125function trace_centers (ug:: UGrid , tape)
195126 center_locations = map (x-> center_location (x; padding= ug. padding) .+ (0 , 1 ), ug. lines)
196127 for (gadget, i, j) in tape
197128 m, n = size (gadget)
198129 for (k, centerloc) in enumerate (center_locations)
199- offset = centerloc .- (i,j )
200- if 0 <= offset[1 ] <= m- 1 && 0 <= offset[2 ] <= n- 1
201- center_locations[k] = move_center (gadget, centerloc)
130+ offset = centerloc .- (i- 1 ,j - 1 )
131+ if 1 <= offset[1 ] <= m && 1 <= offset[2 ] <= n
132+ center_locations[k] = move_center (gadget, centerloc, offset )
202133 end
203134 end
204135 end
0 commit comments