95 }; |
95 }; |
96 |
96 |
97 |
97 |
98 |
98 |
99 ListDigraphBase() |
99 ListDigraphBase() |
100 : nodes(), first_node(-1), |
100 : _nodes(), first_node(-1), |
101 first_free_node(-1), arcs(), first_free_arc(-1) {} |
101 first_free_node(-1), _arcs(), first_free_arc(-1) {} |
102 |
102 |
103 |
103 |
104 int maxNodeId() const { return nodes.size()-1; } |
104 int maxNodeId() const { return _nodes.size()-1; } |
105 int maxArcId() const { return arcs.size()-1; } |
105 int maxArcId() const { return _arcs.size()-1; } |
106 |
106 |
107 Node source(Arc e) const { return Node(arcs[e.id].source); } |
107 Node source(Arc e) const { return Node(_arcs[e.id].source); } |
108 Node target(Arc e) const { return Node(arcs[e.id].target); } |
108 Node target(Arc e) const { return Node(_arcs[e.id].target); } |
109 |
109 |
110 |
110 |
111 void first(Node& node) const { |
111 void first(Node& node) const { |
112 node.id = first_node; |
112 node.id = first_node; |
113 } |
113 } |
114 |
114 |
115 void next(Node& node) const { |
115 void next(Node& node) const { |
116 node.id = nodes[node.id].next; |
116 node.id = _nodes[node.id].next; |
117 } |
117 } |
118 |
118 |
119 |
119 |
120 void first(Arc& arc) const { |
120 void first(Arc& arc) const { |
121 int n; |
121 int n; |
122 for(n = first_node; |
122 for(n = first_node; |
123 n != -1 && nodes[n].first_out == -1; |
123 n != -1 && _nodes[n].first_out == -1; |
124 n = nodes[n].next) {} |
124 n = _nodes[n].next) {} |
125 arc.id = (n == -1) ? -1 : nodes[n].first_out; |
125 arc.id = (n == -1) ? -1 : _nodes[n].first_out; |
126 } |
126 } |
127 |
127 |
128 void next(Arc& arc) const { |
128 void next(Arc& arc) const { |
129 if (arcs[arc.id].next_out != -1) { |
129 if (_arcs[arc.id].next_out != -1) { |
130 arc.id = arcs[arc.id].next_out; |
130 arc.id = _arcs[arc.id].next_out; |
131 } else { |
131 } else { |
132 int n; |
132 int n; |
133 for(n = nodes[arcs[arc.id].source].next; |
133 for(n = _nodes[_arcs[arc.id].source].next; |
134 n != -1 && nodes[n].first_out == -1; |
134 n != -1 && _nodes[n].first_out == -1; |
135 n = nodes[n].next) {} |
135 n = _nodes[n].next) {} |
136 arc.id = (n == -1) ? -1 : nodes[n].first_out; |
136 arc.id = (n == -1) ? -1 : _nodes[n].first_out; |
137 } |
137 } |
138 } |
138 } |
139 |
139 |
140 void firstOut(Arc &e, const Node& v) const { |
140 void firstOut(Arc &e, const Node& v) const { |
141 e.id = nodes[v.id].first_out; |
141 e.id = _nodes[v.id].first_out; |
142 } |
142 } |
143 void nextOut(Arc &e) const { |
143 void nextOut(Arc &e) const { |
144 e.id=arcs[e.id].next_out; |
144 e.id=_arcs[e.id].next_out; |
145 } |
145 } |
146 |
146 |
147 void firstIn(Arc &e, const Node& v) const { |
147 void firstIn(Arc &e, const Node& v) const { |
148 e.id = nodes[v.id].first_in; |
148 e.id = _nodes[v.id].first_in; |
149 } |
149 } |
150 void nextIn(Arc &e) const { |
150 void nextIn(Arc &e) const { |
151 e.id=arcs[e.id].next_in; |
151 e.id=_arcs[e.id].next_in; |
152 } |
152 } |
153 |
153 |
154 |
154 |
155 static int id(Node v) { return v.id; } |
155 static int id(Node v) { return v.id; } |
156 static int id(Arc e) { return e.id; } |
156 static int id(Arc e) { return e.id; } |
157 |
157 |
158 static Node nodeFromId(int id) { return Node(id);} |
158 static Node nodeFromId(int id) { return Node(id);} |
159 static Arc arcFromId(int id) { return Arc(id);} |
159 static Arc arcFromId(int id) { return Arc(id);} |
160 |
160 |
161 bool valid(Node n) const { |
161 bool valid(Node n) const { |
162 return n.id >= 0 && n.id < static_cast<int>(nodes.size()) && |
162 return n.id >= 0 && n.id < static_cast<int>(_nodes.size()) && |
163 nodes[n.id].prev != -2; |
163 _nodes[n.id].prev != -2; |
164 } |
164 } |
165 |
165 |
166 bool valid(Arc a) const { |
166 bool valid(Arc a) const { |
167 return a.id >= 0 && a.id < static_cast<int>(arcs.size()) && |
167 return a.id >= 0 && a.id < static_cast<int>(_arcs.size()) && |
168 arcs[a.id].prev_in != -2; |
168 _arcs[a.id].prev_in != -2; |
169 } |
169 } |
170 |
170 |
171 Node addNode() { |
171 Node addNode() { |
172 int n; |
172 int n; |
173 |
173 |
174 if(first_free_node==-1) { |
174 if(first_free_node==-1) { |
175 n = nodes.size(); |
175 n = _nodes.size(); |
176 nodes.push_back(NodeT()); |
176 _nodes.push_back(NodeT()); |
177 } else { |
177 } else { |
178 n = first_free_node; |
178 n = first_free_node; |
179 first_free_node = nodes[n].next; |
179 first_free_node = _nodes[n].next; |
180 } |
180 } |
181 |
181 |
182 nodes[n].next = first_node; |
182 _nodes[n].next = first_node; |
183 if(first_node != -1) nodes[first_node].prev = n; |
183 if(first_node != -1) _nodes[first_node].prev = n; |
184 first_node = n; |
184 first_node = n; |
185 nodes[n].prev = -1; |
185 _nodes[n].prev = -1; |
186 |
186 |
187 nodes[n].first_in = nodes[n].first_out = -1; |
187 _nodes[n].first_in = _nodes[n].first_out = -1; |
188 |
188 |
189 return Node(n); |
189 return Node(n); |
190 } |
190 } |
191 |
191 |
192 Arc addArc(Node u, Node v) { |
192 Arc addArc(Node u, Node v) { |
193 int n; |
193 int n; |
194 |
194 |
195 if (first_free_arc == -1) { |
195 if (first_free_arc == -1) { |
196 n = arcs.size(); |
196 n = _arcs.size(); |
197 arcs.push_back(ArcT()); |
197 _arcs.push_back(ArcT()); |
198 } else { |
198 } else { |
199 n = first_free_arc; |
199 n = first_free_arc; |
200 first_free_arc = arcs[n].next_in; |
200 first_free_arc = _arcs[n].next_in; |
201 } |
201 } |
202 |
202 |
203 arcs[n].source = u.id; |
203 _arcs[n].source = u.id; |
204 arcs[n].target = v.id; |
204 _arcs[n].target = v.id; |
205 |
205 |
206 arcs[n].next_out = nodes[u.id].first_out; |
206 _arcs[n].next_out = _nodes[u.id].first_out; |
207 if(nodes[u.id].first_out != -1) { |
207 if(_nodes[u.id].first_out != -1) { |
208 arcs[nodes[u.id].first_out].prev_out = n; |
208 _arcs[_nodes[u.id].first_out].prev_out = n; |
209 } |
209 } |
210 |
210 |
211 arcs[n].next_in = nodes[v.id].first_in; |
211 _arcs[n].next_in = _nodes[v.id].first_in; |
212 if(nodes[v.id].first_in != -1) { |
212 if(_nodes[v.id].first_in != -1) { |
213 arcs[nodes[v.id].first_in].prev_in = n; |
213 _arcs[_nodes[v.id].first_in].prev_in = n; |
214 } |
214 } |
215 |
215 |
216 arcs[n].prev_in = arcs[n].prev_out = -1; |
216 _arcs[n].prev_in = _arcs[n].prev_out = -1; |
217 |
217 |
218 nodes[u.id].first_out = nodes[v.id].first_in = n; |
218 _nodes[u.id].first_out = _nodes[v.id].first_in = n; |
219 |
219 |
220 return Arc(n); |
220 return Arc(n); |
221 } |
221 } |
222 |
222 |
223 void erase(const Node& node) { |
223 void erase(const Node& node) { |
224 int n = node.id; |
224 int n = node.id; |
225 |
225 |
226 if(nodes[n].next != -1) { |
226 if(_nodes[n].next != -1) { |
227 nodes[nodes[n].next].prev = nodes[n].prev; |
227 _nodes[_nodes[n].next].prev = _nodes[n].prev; |
228 } |
228 } |
229 |
229 |
230 if(nodes[n].prev != -1) { |
230 if(_nodes[n].prev != -1) { |
231 nodes[nodes[n].prev].next = nodes[n].next; |
231 _nodes[_nodes[n].prev].next = _nodes[n].next; |
232 } else { |
232 } else { |
233 first_node = nodes[n].next; |
233 first_node = _nodes[n].next; |
234 } |
234 } |
235 |
235 |
236 nodes[n].next = first_free_node; |
236 _nodes[n].next = first_free_node; |
237 first_free_node = n; |
237 first_free_node = n; |
238 nodes[n].prev = -2; |
238 _nodes[n].prev = -2; |
239 |
239 |
240 } |
240 } |
241 |
241 |
242 void erase(const Arc& arc) { |
242 void erase(const Arc& arc) { |
243 int n = arc.id; |
243 int n = arc.id; |
244 |
244 |
245 if(arcs[n].next_in!=-1) { |
245 if(_arcs[n].next_in!=-1) { |
246 arcs[arcs[n].next_in].prev_in = arcs[n].prev_in; |
246 _arcs[_arcs[n].next_in].prev_in = _arcs[n].prev_in; |
247 } |
247 } |
248 |
248 |
249 if(arcs[n].prev_in!=-1) { |
249 if(_arcs[n].prev_in!=-1) { |
250 arcs[arcs[n].prev_in].next_in = arcs[n].next_in; |
250 _arcs[_arcs[n].prev_in].next_in = _arcs[n].next_in; |
251 } else { |
251 } else { |
252 nodes[arcs[n].target].first_in = arcs[n].next_in; |
252 _nodes[_arcs[n].target].first_in = _arcs[n].next_in; |
253 } |
253 } |
254 |
254 |
255 |
255 |
256 if(arcs[n].next_out!=-1) { |
256 if(_arcs[n].next_out!=-1) { |
257 arcs[arcs[n].next_out].prev_out = arcs[n].prev_out; |
257 _arcs[_arcs[n].next_out].prev_out = _arcs[n].prev_out; |
258 } |
258 } |
259 |
259 |
260 if(arcs[n].prev_out!=-1) { |
260 if(_arcs[n].prev_out!=-1) { |
261 arcs[arcs[n].prev_out].next_out = arcs[n].next_out; |
261 _arcs[_arcs[n].prev_out].next_out = _arcs[n].next_out; |
262 } else { |
262 } else { |
263 nodes[arcs[n].source].first_out = arcs[n].next_out; |
263 _nodes[_arcs[n].source].first_out = _arcs[n].next_out; |
264 } |
264 } |
265 |
265 |
266 arcs[n].next_in = first_free_arc; |
266 _arcs[n].next_in = first_free_arc; |
267 first_free_arc = n; |
267 first_free_arc = n; |
268 arcs[n].prev_in = -2; |
268 _arcs[n].prev_in = -2; |
269 } |
269 } |
270 |
270 |
271 void clear() { |
271 void clear() { |
272 arcs.clear(); |
272 _arcs.clear(); |
273 nodes.clear(); |
273 _nodes.clear(); |
274 first_node = first_free_node = first_free_arc = -1; |
274 first_node = first_free_node = first_free_arc = -1; |
275 } |
275 } |
276 |
276 |
277 protected: |
277 protected: |
278 void changeTarget(Arc e, Node n) |
278 void changeTarget(Arc e, Node n) |
279 { |
279 { |
280 if(arcs[e.id].next_in != -1) |
280 if(_arcs[e.id].next_in != -1) |
281 arcs[arcs[e.id].next_in].prev_in = arcs[e.id].prev_in; |
281 _arcs[_arcs[e.id].next_in].prev_in = _arcs[e.id].prev_in; |
282 if(arcs[e.id].prev_in != -1) |
282 if(_arcs[e.id].prev_in != -1) |
283 arcs[arcs[e.id].prev_in].next_in = arcs[e.id].next_in; |
283 _arcs[_arcs[e.id].prev_in].next_in = _arcs[e.id].next_in; |
284 else nodes[arcs[e.id].target].first_in = arcs[e.id].next_in; |
284 else _nodes[_arcs[e.id].target].first_in = _arcs[e.id].next_in; |
285 if (nodes[n.id].first_in != -1) { |
285 if (_nodes[n.id].first_in != -1) { |
286 arcs[nodes[n.id].first_in].prev_in = e.id; |
286 _arcs[_nodes[n.id].first_in].prev_in = e.id; |
287 } |
287 } |
288 arcs[e.id].target = n.id; |
288 _arcs[e.id].target = n.id; |
289 arcs[e.id].prev_in = -1; |
289 _arcs[e.id].prev_in = -1; |
290 arcs[e.id].next_in = nodes[n.id].first_in; |
290 _arcs[e.id].next_in = _nodes[n.id].first_in; |
291 nodes[n.id].first_in = e.id; |
291 _nodes[n.id].first_in = e.id; |
292 } |
292 } |
293 void changeSource(Arc e, Node n) |
293 void changeSource(Arc e, Node n) |
294 { |
294 { |
295 if(arcs[e.id].next_out != -1) |
295 if(_arcs[e.id].next_out != -1) |
296 arcs[arcs[e.id].next_out].prev_out = arcs[e.id].prev_out; |
296 _arcs[_arcs[e.id].next_out].prev_out = _arcs[e.id].prev_out; |
297 if(arcs[e.id].prev_out != -1) |
297 if(_arcs[e.id].prev_out != -1) |
298 arcs[arcs[e.id].prev_out].next_out = arcs[e.id].next_out; |
298 _arcs[_arcs[e.id].prev_out].next_out = _arcs[e.id].next_out; |
299 else nodes[arcs[e.id].source].first_out = arcs[e.id].next_out; |
299 else _nodes[_arcs[e.id].source].first_out = _arcs[e.id].next_out; |
300 if (nodes[n.id].first_out != -1) { |
300 if (_nodes[n.id].first_out != -1) { |
301 arcs[nodes[n.id].first_out].prev_out = e.id; |
301 _arcs[_nodes[n.id].first_out].prev_out = e.id; |
302 } |
302 } |
303 arcs[e.id].source = n.id; |
303 _arcs[e.id].source = n.id; |
304 arcs[e.id].prev_out = -1; |
304 _arcs[e.id].prev_out = -1; |
305 arcs[e.id].next_out = nodes[n.id].first_out; |
305 _arcs[e.id].next_out = _nodes[n.id].first_out; |
306 nodes[n.id].first_out = e.id; |
306 _nodes[n.id].first_out = e.id; |
307 } |
307 } |
308 |
308 |
309 }; |
309 }; |
310 |
310 |
311 typedef DigraphExtender<ListDigraphBase> ExtendedListDigraphBase; |
311 typedef DigraphExtender<ListDigraphBase> ExtendedListDigraphBase; |
892 void first(Node& node) const { |
892 void first(Node& node) const { |
893 node.id = first_node; |
893 node.id = first_node; |
894 } |
894 } |
895 |
895 |
896 void next(Node& node) const { |
896 void next(Node& node) const { |
897 node.id = nodes[node.id].next; |
897 node.id = _nodes[node.id].next; |
898 } |
898 } |
899 |
899 |
900 void first(Arc& e) const { |
900 void first(Arc& e) const { |
901 int n = first_node; |
901 int n = first_node; |
902 while (n != -1 && nodes[n].first_out == -1) { |
902 while (n != -1 && _nodes[n].first_out == -1) { |
903 n = nodes[n].next; |
903 n = _nodes[n].next; |
904 } |
904 } |
905 e.id = (n == -1) ? -1 : nodes[n].first_out; |
905 e.id = (n == -1) ? -1 : _nodes[n].first_out; |
906 } |
906 } |
907 |
907 |
908 void next(Arc& e) const { |
908 void next(Arc& e) const { |
909 if (arcs[e.id].next_out != -1) { |
909 if (_arcs[e.id].next_out != -1) { |
910 e.id = arcs[e.id].next_out; |
910 e.id = _arcs[e.id].next_out; |
911 } else { |
911 } else { |
912 int n = nodes[arcs[e.id ^ 1].target].next; |
912 int n = _nodes[_arcs[e.id ^ 1].target].next; |
913 while(n != -1 && nodes[n].first_out == -1) { |
913 while(n != -1 && _nodes[n].first_out == -1) { |
914 n = nodes[n].next; |
914 n = _nodes[n].next; |
915 } |
915 } |
916 e.id = (n == -1) ? -1 : nodes[n].first_out; |
916 e.id = (n == -1) ? -1 : _nodes[n].first_out; |
917 } |
917 } |
918 } |
918 } |
919 |
919 |
920 void first(Edge& e) const { |
920 void first(Edge& e) const { |
921 int n = first_node; |
921 int n = first_node; |
922 while (n != -1) { |
922 while (n != -1) { |
923 e.id = nodes[n].first_out; |
923 e.id = _nodes[n].first_out; |
924 while ((e.id & 1) != 1) { |
924 while ((e.id & 1) != 1) { |
925 e.id = arcs[e.id].next_out; |
925 e.id = _arcs[e.id].next_out; |
926 } |
926 } |
927 if (e.id != -1) { |
927 if (e.id != -1) { |
928 e.id /= 2; |
928 e.id /= 2; |
929 return; |
929 return; |
930 } |
930 } |
931 n = nodes[n].next; |
931 n = _nodes[n].next; |
932 } |
932 } |
933 e.id = -1; |
933 e.id = -1; |
934 } |
934 } |
935 |
935 |
936 void next(Edge& e) const { |
936 void next(Edge& e) const { |
937 int n = arcs[e.id * 2].target; |
937 int n = _arcs[e.id * 2].target; |
938 e.id = arcs[(e.id * 2) | 1].next_out; |
938 e.id = _arcs[(e.id * 2) | 1].next_out; |
939 while ((e.id & 1) != 1) { |
939 while ((e.id & 1) != 1) { |
940 e.id = arcs[e.id].next_out; |
940 e.id = _arcs[e.id].next_out; |
941 } |
941 } |
942 if (e.id != -1) { |
942 if (e.id != -1) { |
943 e.id /= 2; |
943 e.id /= 2; |
944 return; |
944 return; |
945 } |
945 } |
946 n = nodes[n].next; |
946 n = _nodes[n].next; |
947 while (n != -1) { |
947 while (n != -1) { |
948 e.id = nodes[n].first_out; |
948 e.id = _nodes[n].first_out; |
949 while ((e.id & 1) != 1) { |
949 while ((e.id & 1) != 1) { |
950 e.id = arcs[e.id].next_out; |
950 e.id = _arcs[e.id].next_out; |
951 } |
951 } |
952 if (e.id != -1) { |
952 if (e.id != -1) { |
953 e.id /= 2; |
953 e.id /= 2; |
954 return; |
954 return; |
955 } |
955 } |
956 n = nodes[n].next; |
956 n = _nodes[n].next; |
957 } |
957 } |
958 e.id = -1; |
958 e.id = -1; |
959 } |
959 } |
960 |
960 |
961 void firstOut(Arc &e, const Node& v) const { |
961 void firstOut(Arc &e, const Node& v) const { |
962 e.id = nodes[v.id].first_out; |
962 e.id = _nodes[v.id].first_out; |
963 } |
963 } |
964 void nextOut(Arc &e) const { |
964 void nextOut(Arc &e) const { |
965 e.id = arcs[e.id].next_out; |
965 e.id = _arcs[e.id].next_out; |
966 } |
966 } |
967 |
967 |
968 void firstIn(Arc &e, const Node& v) const { |
968 void firstIn(Arc &e, const Node& v) const { |
969 e.id = ((nodes[v.id].first_out) ^ 1); |
969 e.id = ((_nodes[v.id].first_out) ^ 1); |
970 if (e.id == -2) e.id = -1; |
970 if (e.id == -2) e.id = -1; |
971 } |
971 } |
972 void nextIn(Arc &e) const { |
972 void nextIn(Arc &e) const { |
973 e.id = ((arcs[e.id ^ 1].next_out) ^ 1); |
973 e.id = ((_arcs[e.id ^ 1].next_out) ^ 1); |
974 if (e.id == -2) e.id = -1; |
974 if (e.id == -2) e.id = -1; |
975 } |
975 } |
976 |
976 |
977 void firstInc(Edge &e, bool& d, const Node& v) const { |
977 void firstInc(Edge &e, bool& d, const Node& v) const { |
978 int a = nodes[v.id].first_out; |
978 int a = _nodes[v.id].first_out; |
979 if (a != -1 ) { |
979 if (a != -1 ) { |
980 e.id = a / 2; |
980 e.id = a / 2; |
981 d = ((a & 1) == 1); |
981 d = ((a & 1) == 1); |
982 } else { |
982 } else { |
983 e.id = -1; |
983 e.id = -1; |
984 d = true; |
984 d = true; |
985 } |
985 } |
986 } |
986 } |
987 void nextInc(Edge &e, bool& d) const { |
987 void nextInc(Edge &e, bool& d) const { |
988 int a = (arcs[(e.id * 2) | (d ? 1 : 0)].next_out); |
988 int a = (_arcs[(e.id * 2) | (d ? 1 : 0)].next_out); |
989 if (a != -1 ) { |
989 if (a != -1 ) { |
990 e.id = a / 2; |
990 e.id = a / 2; |
991 d = ((a & 1) == 1); |
991 d = ((a & 1) == 1); |
992 } else { |
992 } else { |
993 e.id = -1; |
993 e.id = -1; |
1002 static Node nodeFromId(int id) { return Node(id);} |
1002 static Node nodeFromId(int id) { return Node(id);} |
1003 static Arc arcFromId(int id) { return Arc(id);} |
1003 static Arc arcFromId(int id) { return Arc(id);} |
1004 static Edge edgeFromId(int id) { return Edge(id);} |
1004 static Edge edgeFromId(int id) { return Edge(id);} |
1005 |
1005 |
1006 bool valid(Node n) const { |
1006 bool valid(Node n) const { |
1007 return n.id >= 0 && n.id < static_cast<int>(nodes.size()) && |
1007 return n.id >= 0 && n.id < static_cast<int>(_nodes.size()) && |
1008 nodes[n.id].prev != -2; |
1008 _nodes[n.id].prev != -2; |
1009 } |
1009 } |
1010 |
1010 |
1011 bool valid(Arc a) const { |
1011 bool valid(Arc a) const { |
1012 return a.id >= 0 && a.id < static_cast<int>(arcs.size()) && |
1012 return a.id >= 0 && a.id < static_cast<int>(_arcs.size()) && |
1013 arcs[a.id].prev_out != -2; |
1013 _arcs[a.id].prev_out != -2; |
1014 } |
1014 } |
1015 |
1015 |
1016 bool valid(Edge e) const { |
1016 bool valid(Edge e) const { |
1017 return e.id >= 0 && 2 * e.id < static_cast<int>(arcs.size()) && |
1017 return e.id >= 0 && 2 * e.id < static_cast<int>(_arcs.size()) && |
1018 arcs[2 * e.id].prev_out != -2; |
1018 _arcs[2 * e.id].prev_out != -2; |
1019 } |
1019 } |
1020 |
1020 |
1021 Node addNode() { |
1021 Node addNode() { |
1022 int n; |
1022 int n; |
1023 |
1023 |
1024 if(first_free_node==-1) { |
1024 if(first_free_node==-1) { |
1025 n = nodes.size(); |
1025 n = _nodes.size(); |
1026 nodes.push_back(NodeT()); |
1026 _nodes.push_back(NodeT()); |
1027 } else { |
1027 } else { |
1028 n = first_free_node; |
1028 n = first_free_node; |
1029 first_free_node = nodes[n].next; |
1029 first_free_node = _nodes[n].next; |
1030 } |
1030 } |
1031 |
1031 |
1032 nodes[n].next = first_node; |
1032 _nodes[n].next = first_node; |
1033 if (first_node != -1) nodes[first_node].prev = n; |
1033 if (first_node != -1) _nodes[first_node].prev = n; |
1034 first_node = n; |
1034 first_node = n; |
1035 nodes[n].prev = -1; |
1035 _nodes[n].prev = -1; |
1036 |
1036 |
1037 nodes[n].first_out = -1; |
1037 _nodes[n].first_out = -1; |
1038 |
1038 |
1039 return Node(n); |
1039 return Node(n); |
1040 } |
1040 } |
1041 |
1041 |
1042 Edge addEdge(Node u, Node v) { |
1042 Edge addEdge(Node u, Node v) { |
1043 int n; |
1043 int n; |
1044 |
1044 |
1045 if (first_free_arc == -1) { |
1045 if (first_free_arc == -1) { |
1046 n = arcs.size(); |
1046 n = _arcs.size(); |
1047 arcs.push_back(ArcT()); |
1047 _arcs.push_back(ArcT()); |
1048 arcs.push_back(ArcT()); |
1048 _arcs.push_back(ArcT()); |
1049 } else { |
1049 } else { |
1050 n = first_free_arc; |
1050 n = first_free_arc; |
1051 first_free_arc = arcs[n].next_out; |
1051 first_free_arc = _arcs[n].next_out; |
1052 } |
1052 } |
1053 |
1053 |
1054 arcs[n].target = u.id; |
1054 _arcs[n].target = u.id; |
1055 arcs[n | 1].target = v.id; |
1055 _arcs[n | 1].target = v.id; |
1056 |
1056 |
1057 arcs[n].next_out = nodes[v.id].first_out; |
1057 _arcs[n].next_out = _nodes[v.id].first_out; |
1058 if (nodes[v.id].first_out != -1) { |
1058 if (_nodes[v.id].first_out != -1) { |
1059 arcs[nodes[v.id].first_out].prev_out = n; |
1059 _arcs[_nodes[v.id].first_out].prev_out = n; |
1060 } |
1060 } |
1061 arcs[n].prev_out = -1; |
1061 _arcs[n].prev_out = -1; |
1062 nodes[v.id].first_out = n; |
1062 _nodes[v.id].first_out = n; |
1063 |
1063 |
1064 arcs[n | 1].next_out = nodes[u.id].first_out; |
1064 _arcs[n | 1].next_out = _nodes[u.id].first_out; |
1065 if (nodes[u.id].first_out != -1) { |
1065 if (_nodes[u.id].first_out != -1) { |
1066 arcs[nodes[u.id].first_out].prev_out = (n | 1); |
1066 _arcs[_nodes[u.id].first_out].prev_out = (n | 1); |
1067 } |
1067 } |
1068 arcs[n | 1].prev_out = -1; |
1068 _arcs[n | 1].prev_out = -1; |
1069 nodes[u.id].first_out = (n | 1); |
1069 _nodes[u.id].first_out = (n | 1); |
1070 |
1070 |
1071 return Edge(n / 2); |
1071 return Edge(n / 2); |
1072 } |
1072 } |
1073 |
1073 |
1074 void erase(const Node& node) { |
1074 void erase(const Node& node) { |
1075 int n = node.id; |
1075 int n = node.id; |
1076 |
1076 |
1077 if(nodes[n].next != -1) { |
1077 if(_nodes[n].next != -1) { |
1078 nodes[nodes[n].next].prev = nodes[n].prev; |
1078 _nodes[_nodes[n].next].prev = _nodes[n].prev; |
1079 } |
1079 } |
1080 |
1080 |
1081 if(nodes[n].prev != -1) { |
1081 if(_nodes[n].prev != -1) { |
1082 nodes[nodes[n].prev].next = nodes[n].next; |
1082 _nodes[_nodes[n].prev].next = _nodes[n].next; |
1083 } else { |
1083 } else { |
1084 first_node = nodes[n].next; |
1084 first_node = _nodes[n].next; |
1085 } |
1085 } |
1086 |
1086 |
1087 nodes[n].next = first_free_node; |
1087 _nodes[n].next = first_free_node; |
1088 first_free_node = n; |
1088 first_free_node = n; |
1089 nodes[n].prev = -2; |
1089 _nodes[n].prev = -2; |
1090 } |
1090 } |
1091 |
1091 |
1092 void erase(const Edge& edge) { |
1092 void erase(const Edge& edge) { |
1093 int n = edge.id * 2; |
1093 int n = edge.id * 2; |
1094 |
1094 |
1095 if (arcs[n].next_out != -1) { |
1095 if (_arcs[n].next_out != -1) { |
1096 arcs[arcs[n].next_out].prev_out = arcs[n].prev_out; |
1096 _arcs[_arcs[n].next_out].prev_out = _arcs[n].prev_out; |
1097 } |
1097 } |
1098 |
1098 |
1099 if (arcs[n].prev_out != -1) { |
1099 if (_arcs[n].prev_out != -1) { |
1100 arcs[arcs[n].prev_out].next_out = arcs[n].next_out; |
1100 _arcs[_arcs[n].prev_out].next_out = _arcs[n].next_out; |
1101 } else { |
1101 } else { |
1102 nodes[arcs[n | 1].target].first_out = arcs[n].next_out; |
1102 _nodes[_arcs[n | 1].target].first_out = _arcs[n].next_out; |
1103 } |
1103 } |
1104 |
1104 |
1105 if (arcs[n | 1].next_out != -1) { |
1105 if (_arcs[n | 1].next_out != -1) { |
1106 arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out; |
1106 _arcs[_arcs[n | 1].next_out].prev_out = _arcs[n | 1].prev_out; |
1107 } |
1107 } |
1108 |
1108 |
1109 if (arcs[n | 1].prev_out != -1) { |
1109 if (_arcs[n | 1].prev_out != -1) { |
1110 arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out; |
1110 _arcs[_arcs[n | 1].prev_out].next_out = _arcs[n | 1].next_out; |
1111 } else { |
1111 } else { |
1112 nodes[arcs[n].target].first_out = arcs[n | 1].next_out; |
1112 _nodes[_arcs[n].target].first_out = _arcs[n | 1].next_out; |
1113 } |
1113 } |
1114 |
1114 |
1115 arcs[n].next_out = first_free_arc; |
1115 _arcs[n].next_out = first_free_arc; |
1116 first_free_arc = n; |
1116 first_free_arc = n; |
1117 arcs[n].prev_out = -2; |
1117 _arcs[n].prev_out = -2; |
1118 arcs[n | 1].prev_out = -2; |
1118 _arcs[n | 1].prev_out = -2; |
1119 |
1119 |
1120 } |
1120 } |
1121 |
1121 |
1122 void clear() { |
1122 void clear() { |
1123 arcs.clear(); |
1123 _arcs.clear(); |
1124 nodes.clear(); |
1124 _nodes.clear(); |
1125 first_node = first_free_node = first_free_arc = -1; |
1125 first_node = first_free_node = first_free_arc = -1; |
1126 } |
1126 } |
1127 |
1127 |
1128 protected: |
1128 protected: |
1129 |
1129 |
1130 void changeV(Edge e, Node n) { |
1130 void changeV(Edge e, Node n) { |
1131 if(arcs[2 * e.id].next_out != -1) { |
1131 if(_arcs[2 * e.id].next_out != -1) { |
1132 arcs[arcs[2 * e.id].next_out].prev_out = arcs[2 * e.id].prev_out; |
1132 _arcs[_arcs[2 * e.id].next_out].prev_out = _arcs[2 * e.id].prev_out; |
1133 } |
1133 } |
1134 if(arcs[2 * e.id].prev_out != -1) { |
1134 if(_arcs[2 * e.id].prev_out != -1) { |
1135 arcs[arcs[2 * e.id].prev_out].next_out = |
1135 _arcs[_arcs[2 * e.id].prev_out].next_out = |
1136 arcs[2 * e.id].next_out; |
1136 _arcs[2 * e.id].next_out; |
1137 } else { |
1137 } else { |
1138 nodes[arcs[(2 * e.id) | 1].target].first_out = |
1138 _nodes[_arcs[(2 * e.id) | 1].target].first_out = |
1139 arcs[2 * e.id].next_out; |
1139 _arcs[2 * e.id].next_out; |
1140 } |
1140 } |
1141 |
1141 |
1142 if (nodes[n.id].first_out != -1) { |
1142 if (_nodes[n.id].first_out != -1) { |
1143 arcs[nodes[n.id].first_out].prev_out = 2 * e.id; |
1143 _arcs[_nodes[n.id].first_out].prev_out = 2 * e.id; |
1144 } |
1144 } |
1145 arcs[(2 * e.id) | 1].target = n.id; |
1145 _arcs[(2 * e.id) | 1].target = n.id; |
1146 arcs[2 * e.id].prev_out = -1; |
1146 _arcs[2 * e.id].prev_out = -1; |
1147 arcs[2 * e.id].next_out = nodes[n.id].first_out; |
1147 _arcs[2 * e.id].next_out = _nodes[n.id].first_out; |
1148 nodes[n.id].first_out = 2 * e.id; |
1148 _nodes[n.id].first_out = 2 * e.id; |
1149 } |
1149 } |
1150 |
1150 |
1151 void changeU(Edge e, Node n) { |
1151 void changeU(Edge e, Node n) { |
1152 if(arcs[(2 * e.id) | 1].next_out != -1) { |
1152 if(_arcs[(2 * e.id) | 1].next_out != -1) { |
1153 arcs[arcs[(2 * e.id) | 1].next_out].prev_out = |
1153 _arcs[_arcs[(2 * e.id) | 1].next_out].prev_out = |
1154 arcs[(2 * e.id) | 1].prev_out; |
1154 _arcs[(2 * e.id) | 1].prev_out; |
1155 } |
1155 } |
1156 if(arcs[(2 * e.id) | 1].prev_out != -1) { |
1156 if(_arcs[(2 * e.id) | 1].prev_out != -1) { |
1157 arcs[arcs[(2 * e.id) | 1].prev_out].next_out = |
1157 _arcs[_arcs[(2 * e.id) | 1].prev_out].next_out = |
1158 arcs[(2 * e.id) | 1].next_out; |
1158 _arcs[(2 * e.id) | 1].next_out; |
1159 } else { |
1159 } else { |
1160 nodes[arcs[2 * e.id].target].first_out = |
1160 _nodes[_arcs[2 * e.id].target].first_out = |
1161 arcs[(2 * e.id) | 1].next_out; |
1161 _arcs[(2 * e.id) | 1].next_out; |
1162 } |
1162 } |
1163 |
1163 |
1164 if (nodes[n.id].first_out != -1) { |
1164 if (_nodes[n.id].first_out != -1) { |
1165 arcs[nodes[n.id].first_out].prev_out = ((2 * e.id) | 1); |
1165 _arcs[_nodes[n.id].first_out].prev_out = ((2 * e.id) | 1); |
1166 } |
1166 } |
1167 arcs[2 * e.id].target = n.id; |
1167 _arcs[2 * e.id].target = n.id; |
1168 arcs[(2 * e.id) | 1].prev_out = -1; |
1168 _arcs[(2 * e.id) | 1].prev_out = -1; |
1169 arcs[(2 * e.id) | 1].next_out = nodes[n.id].first_out; |
1169 _arcs[(2 * e.id) | 1].next_out = _nodes[n.id].first_out; |
1170 nodes[n.id].first_out = ((2 * e.id) | 1); |
1170 _nodes[n.id].first_out = ((2 * e.id) | 1); |
1171 } |
1171 } |
1172 |
1172 |
1173 }; |
1173 }; |
1174 |
1174 |
1175 typedef GraphExtender<ListGraphBase> ExtendedListGraphBase; |
1175 typedef GraphExtender<ListGraphBase> ExtendedListGraphBase; |
1746 void first(Node& node) const { |
1746 void first(Node& node) const { |
1747 node.id = first_node; |
1747 node.id = first_node; |
1748 } |
1748 } |
1749 |
1749 |
1750 void next(Node& node) const { |
1750 void next(Node& node) const { |
1751 node.id = nodes[node.id].next; |
1751 node.id = _nodes[node.id].next; |
1752 } |
1752 } |
1753 |
1753 |
1754 void first(RedNode& node) const { |
1754 void first(RedNode& node) const { |
1755 node.id = first_red; |
1755 node.id = first_red; |
1756 } |
1756 } |
1757 |
1757 |
1758 void next(RedNode& node) const { |
1758 void next(RedNode& node) const { |
1759 node.id = nodes[node.id].partition_next; |
1759 node.id = _nodes[node.id].partition_next; |
1760 } |
1760 } |
1761 |
1761 |
1762 void first(BlueNode& node) const { |
1762 void first(BlueNode& node) const { |
1763 node.id = first_blue; |
1763 node.id = first_blue; |
1764 } |
1764 } |
1765 |
1765 |
1766 void next(BlueNode& node) const { |
1766 void next(BlueNode& node) const { |
1767 node.id = nodes[node.id].partition_next; |
1767 node.id = _nodes[node.id].partition_next; |
1768 } |
1768 } |
1769 |
1769 |
1770 void first(Arc& e) const { |
1770 void first(Arc& e) const { |
1771 int n = first_node; |
1771 int n = first_node; |
1772 while (n != -1 && nodes[n].first_out == -1) { |
1772 while (n != -1 && _nodes[n].first_out == -1) { |
1773 n = nodes[n].next; |
1773 n = _nodes[n].next; |
1774 } |
1774 } |
1775 e.id = (n == -1) ? -1 : nodes[n].first_out; |
1775 e.id = (n == -1) ? -1 : _nodes[n].first_out; |
1776 } |
1776 } |
1777 |
1777 |
1778 void next(Arc& e) const { |
1778 void next(Arc& e) const { |
1779 if (arcs[e.id].next_out != -1) { |
1779 if (_arcs[e.id].next_out != -1) { |
1780 e.id = arcs[e.id].next_out; |
1780 e.id = _arcs[e.id].next_out; |
1781 } else { |
1781 } else { |
1782 int n = nodes[arcs[e.id ^ 1].target].next; |
1782 int n = _nodes[_arcs[e.id ^ 1].target].next; |
1783 while(n != -1 && nodes[n].first_out == -1) { |
1783 while(n != -1 && _nodes[n].first_out == -1) { |
1784 n = nodes[n].next; |
1784 n = _nodes[n].next; |
1785 } |
1785 } |
1786 e.id = (n == -1) ? -1 : nodes[n].first_out; |
1786 e.id = (n == -1) ? -1 : _nodes[n].first_out; |
1787 } |
1787 } |
1788 } |
1788 } |
1789 |
1789 |
1790 void first(Edge& e) const { |
1790 void first(Edge& e) const { |
1791 int n = first_node; |
1791 int n = first_node; |
1792 while (n != -1) { |
1792 while (n != -1) { |
1793 e.id = nodes[n].first_out; |
1793 e.id = _nodes[n].first_out; |
1794 while ((e.id & 1) != 1) { |
1794 while ((e.id & 1) != 1) { |
1795 e.id = arcs[e.id].next_out; |
1795 e.id = _arcs[e.id].next_out; |
1796 } |
1796 } |
1797 if (e.id != -1) { |
1797 if (e.id != -1) { |
1798 e.id /= 2; |
1798 e.id /= 2; |
1799 return; |
1799 return; |
1800 } |
1800 } |
1801 n = nodes[n].next; |
1801 n = _nodes[n].next; |
1802 } |
1802 } |
1803 e.id = -1; |
1803 e.id = -1; |
1804 } |
1804 } |
1805 |
1805 |
1806 void next(Edge& e) const { |
1806 void next(Edge& e) const { |
1807 int n = arcs[e.id * 2].target; |
1807 int n = _arcs[e.id * 2].target; |
1808 e.id = arcs[(e.id * 2) | 1].next_out; |
1808 e.id = _arcs[(e.id * 2) | 1].next_out; |
1809 while ((e.id & 1) != 1) { |
1809 while ((e.id & 1) != 1) { |
1810 e.id = arcs[e.id].next_out; |
1810 e.id = _arcs[e.id].next_out; |
1811 } |
1811 } |
1812 if (e.id != -1) { |
1812 if (e.id != -1) { |
1813 e.id /= 2; |
1813 e.id /= 2; |
1814 return; |
1814 return; |
1815 } |
1815 } |
1816 n = nodes[n].next; |
1816 n = _nodes[n].next; |
1817 while (n != -1) { |
1817 while (n != -1) { |
1818 e.id = nodes[n].first_out; |
1818 e.id = _nodes[n].first_out; |
1819 while ((e.id & 1) != 1) { |
1819 while ((e.id & 1) != 1) { |
1820 e.id = arcs[e.id].next_out; |
1820 e.id = _arcs[e.id].next_out; |
1821 } |
1821 } |
1822 if (e.id != -1) { |
1822 if (e.id != -1) { |
1823 e.id /= 2; |
1823 e.id /= 2; |
1824 return; |
1824 return; |
1825 } |
1825 } |
1826 n = nodes[n].next; |
1826 n = _nodes[n].next; |
1827 } |
1827 } |
1828 e.id = -1; |
1828 e.id = -1; |
1829 } |
1829 } |
1830 |
1830 |
1831 void firstOut(Arc &e, const Node& v) const { |
1831 void firstOut(Arc &e, const Node& v) const { |
1832 e.id = nodes[v.id].first_out; |
1832 e.id = _nodes[v.id].first_out; |
1833 } |
1833 } |
1834 void nextOut(Arc &e) const { |
1834 void nextOut(Arc &e) const { |
1835 e.id = arcs[e.id].next_out; |
1835 e.id = _arcs[e.id].next_out; |
1836 } |
1836 } |
1837 |
1837 |
1838 void firstIn(Arc &e, const Node& v) const { |
1838 void firstIn(Arc &e, const Node& v) const { |
1839 e.id = ((nodes[v.id].first_out) ^ 1); |
1839 e.id = ((_nodes[v.id].first_out) ^ 1); |
1840 if (e.id == -2) e.id = -1; |
1840 if (e.id == -2) e.id = -1; |
1841 } |
1841 } |
1842 void nextIn(Arc &e) const { |
1842 void nextIn(Arc &e) const { |
1843 e.id = ((arcs[e.id ^ 1].next_out) ^ 1); |
1843 e.id = ((_arcs[e.id ^ 1].next_out) ^ 1); |
1844 if (e.id == -2) e.id = -1; |
1844 if (e.id == -2) e.id = -1; |
1845 } |
1845 } |
1846 |
1846 |
1847 void firstInc(Edge &e, bool& d, const Node& v) const { |
1847 void firstInc(Edge &e, bool& d, const Node& v) const { |
1848 int a = nodes[v.id].first_out; |
1848 int a = _nodes[v.id].first_out; |
1849 if (a != -1 ) { |
1849 if (a != -1 ) { |
1850 e.id = a / 2; |
1850 e.id = a / 2; |
1851 d = ((a & 1) == 1); |
1851 d = ((a & 1) == 1); |
1852 } else { |
1852 } else { |
1853 e.id = -1; |
1853 e.id = -1; |
1854 d = true; |
1854 d = true; |
1855 } |
1855 } |
1856 } |
1856 } |
1857 void nextInc(Edge &e, bool& d) const { |
1857 void nextInc(Edge &e, bool& d) const { |
1858 int a = (arcs[(e.id * 2) | (d ? 1 : 0)].next_out); |
1858 int a = (_arcs[(e.id * 2) | (d ? 1 : 0)].next_out); |
1859 if (a != -1 ) { |
1859 if (a != -1 ) { |
1860 e.id = a / 2; |
1860 e.id = a / 2; |
1861 d = ((a & 1) == 1); |
1861 d = ((a & 1) == 1); |
1862 } else { |
1862 } else { |
1863 e.id = -1; |
1863 e.id = -1; |
1864 d = true; |
1864 d = true; |
1865 } |
1865 } |
1866 } |
1866 } |
1867 |
1867 |
1868 static int id(Node v) { return v.id; } |
1868 static int id(Node v) { return v.id; } |
1869 int id(RedNode v) const { return nodes[v.id].partition_index; } |
1869 int id(RedNode v) const { return _nodes[v.id].partition_index; } |
1870 int id(BlueNode v) const { return nodes[v.id].partition_index; } |
1870 int id(BlueNode v) const { return _nodes[v.id].partition_index; } |
1871 static int id(Arc e) { return e.id; } |
1871 static int id(Arc e) { return e.id; } |
1872 static int id(Edge e) { return e.id; } |
1872 static int id(Edge e) { return e.id; } |
1873 |
1873 |
1874 static Node nodeFromId(int id) { return Node(id);} |
1874 static Node nodeFromId(int id) { return Node(id);} |
1875 static Arc arcFromId(int id) { return Arc(id);} |
1875 static Arc arcFromId(int id) { return Arc(id);} |
1876 static Edge edgeFromId(int id) { return Edge(id);} |
1876 static Edge edgeFromId(int id) { return Edge(id);} |
1877 |
1877 |
1878 bool valid(Node n) const { |
1878 bool valid(Node n) const { |
1879 return n.id >= 0 && n.id < static_cast<int>(nodes.size()) && |
1879 return n.id >= 0 && n.id < static_cast<int>(_nodes.size()) && |
1880 nodes[n.id].prev != -2; |
1880 _nodes[n.id].prev != -2; |
1881 } |
1881 } |
1882 |
1882 |
1883 bool valid(Arc a) const { |
1883 bool valid(Arc a) const { |
1884 return a.id >= 0 && a.id < static_cast<int>(arcs.size()) && |
1884 return a.id >= 0 && a.id < static_cast<int>(_arcs.size()) && |
1885 arcs[a.id].prev_out != -2; |
1885 _arcs[a.id].prev_out != -2; |
1886 } |
1886 } |
1887 |
1887 |
1888 bool valid(Edge e) const { |
1888 bool valid(Edge e) const { |
1889 return e.id >= 0 && 2 * e.id < static_cast<int>(arcs.size()) && |
1889 return e.id >= 0 && 2 * e.id < static_cast<int>(_arcs.size()) && |
1890 arcs[2 * e.id].prev_out != -2; |
1890 _arcs[2 * e.id].prev_out != -2; |
1891 } |
1891 } |
1892 |
1892 |
1893 RedNode addRedNode() { |
1893 RedNode addRedNode() { |
1894 int n; |
1894 int n; |
1895 |
1895 |
1896 if(first_free_red==-1) { |
1896 if(first_free_red==-1) { |
1897 n = nodes.size(); |
1897 n = _nodes.size(); |
1898 nodes.push_back(NodeT()); |
1898 _nodes.push_back(NodeT()); |
1899 nodes[n].partition_index = ++max_red; |
1899 _nodes[n].partition_index = ++max_red; |
1900 nodes[n].red = true; |
1900 _nodes[n].red = true; |
1901 } else { |
1901 } else { |
1902 n = first_free_red; |
1902 n = first_free_red; |
1903 first_free_red = nodes[n].next; |
1903 first_free_red = _nodes[n].next; |
1904 } |
1904 } |
1905 |
1905 |
1906 nodes[n].next = first_node; |
1906 _nodes[n].next = first_node; |
1907 if (first_node != -1) nodes[first_node].prev = n; |
1907 if (first_node != -1) _nodes[first_node].prev = n; |
1908 first_node = n; |
1908 first_node = n; |
1909 nodes[n].prev = -1; |
1909 _nodes[n].prev = -1; |
1910 |
1910 |
1911 nodes[n].partition_next = first_red; |
1911 _nodes[n].partition_next = first_red; |
1912 if (first_red != -1) nodes[first_red].partition_prev = n; |
1912 if (first_red != -1) _nodes[first_red].partition_prev = n; |
1913 first_red = n; |
1913 first_red = n; |
1914 nodes[n].partition_prev = -1; |
1914 _nodes[n].partition_prev = -1; |
1915 |
1915 |
1916 nodes[n].first_out = -1; |
1916 _nodes[n].first_out = -1; |
1917 |
1917 |
1918 return RedNode(n); |
1918 return RedNode(n); |
1919 } |
1919 } |
1920 |
1920 |
1921 BlueNode addBlueNode() { |
1921 BlueNode addBlueNode() { |
1922 int n; |
1922 int n; |
1923 |
1923 |
1924 if(first_free_blue==-1) { |
1924 if(first_free_blue==-1) { |
1925 n = nodes.size(); |
1925 n = _nodes.size(); |
1926 nodes.push_back(NodeT()); |
1926 _nodes.push_back(NodeT()); |
1927 nodes[n].partition_index = ++max_blue; |
1927 _nodes[n].partition_index = ++max_blue; |
1928 nodes[n].red = false; |
1928 _nodes[n].red = false; |
1929 } else { |
1929 } else { |
1930 n = first_free_blue; |
1930 n = first_free_blue; |
1931 first_free_blue = nodes[n].next; |
1931 first_free_blue = _nodes[n].next; |
1932 } |
1932 } |
1933 |
1933 |
1934 nodes[n].next = first_node; |
1934 _nodes[n].next = first_node; |
1935 if (first_node != -1) nodes[first_node].prev = n; |
1935 if (first_node != -1) _nodes[first_node].prev = n; |
1936 first_node = n; |
1936 first_node = n; |
1937 nodes[n].prev = -1; |
1937 _nodes[n].prev = -1; |
1938 |
1938 |
1939 nodes[n].partition_next = first_blue; |
1939 _nodes[n].partition_next = first_blue; |
1940 if (first_blue != -1) nodes[first_blue].partition_prev = n; |
1940 if (first_blue != -1) _nodes[first_blue].partition_prev = n; |
1941 first_blue = n; |
1941 first_blue = n; |
1942 nodes[n].partition_prev = -1; |
1942 _nodes[n].partition_prev = -1; |
1943 |
1943 |
1944 nodes[n].first_out = -1; |
1944 _nodes[n].first_out = -1; |
1945 |
1945 |
1946 return BlueNode(n); |
1946 return BlueNode(n); |
1947 } |
1947 } |
1948 |
1948 |
1949 Edge addEdge(Node u, Node v) { |
1949 Edge addEdge(Node u, Node v) { |
1950 int n; |
1950 int n; |
1951 |
1951 |
1952 if (first_free_arc == -1) { |
1952 if (first_free_arc == -1) { |
1953 n = arcs.size(); |
1953 n = _arcs.size(); |
1954 arcs.push_back(ArcT()); |
1954 _arcs.push_back(ArcT()); |
1955 arcs.push_back(ArcT()); |
1955 _arcs.push_back(ArcT()); |
1956 } else { |
1956 } else { |
1957 n = first_free_arc; |
1957 n = first_free_arc; |
1958 first_free_arc = arcs[n].next_out; |
1958 first_free_arc = _arcs[n].next_out; |
1959 } |
1959 } |
1960 |
1960 |
1961 arcs[n].target = u.id; |
1961 _arcs[n].target = u.id; |
1962 arcs[n | 1].target = v.id; |
1962 _arcs[n | 1].target = v.id; |
1963 |
1963 |
1964 arcs[n].next_out = nodes[v.id].first_out; |
1964 _arcs[n].next_out = _nodes[v.id].first_out; |
1965 if (nodes[v.id].first_out != -1) { |
1965 if (_nodes[v.id].first_out != -1) { |
1966 arcs[nodes[v.id].first_out].prev_out = n; |
1966 _arcs[_nodes[v.id].first_out].prev_out = n; |
1967 } |
1967 } |
1968 arcs[n].prev_out = -1; |
1968 _arcs[n].prev_out = -1; |
1969 nodes[v.id].first_out = n; |
1969 _nodes[v.id].first_out = n; |
1970 |
1970 |
1971 arcs[n | 1].next_out = nodes[u.id].first_out; |
1971 _arcs[n | 1].next_out = _nodes[u.id].first_out; |
1972 if (nodes[u.id].first_out != -1) { |
1972 if (_nodes[u.id].first_out != -1) { |
1973 arcs[nodes[u.id].first_out].prev_out = (n | 1); |
1973 _arcs[_nodes[u.id].first_out].prev_out = (n | 1); |
1974 } |
1974 } |
1975 arcs[n | 1].prev_out = -1; |
1975 _arcs[n | 1].prev_out = -1; |
1976 nodes[u.id].first_out = (n | 1); |
1976 _nodes[u.id].first_out = (n | 1); |
1977 |
1977 |
1978 return Edge(n / 2); |
1978 return Edge(n / 2); |
1979 } |
1979 } |
1980 |
1980 |
1981 void erase(const Node& node) { |
1981 void erase(const Node& node) { |
1982 int n = node.id; |
1982 int n = node.id; |
1983 |
1983 |
1984 if(nodes[n].next != -1) { |
1984 if(_nodes[n].next != -1) { |
1985 nodes[nodes[n].next].prev = nodes[n].prev; |
1985 _nodes[_nodes[n].next].prev = _nodes[n].prev; |
1986 } |
1986 } |
1987 |
1987 |
1988 if(nodes[n].prev != -1) { |
1988 if(_nodes[n].prev != -1) { |
1989 nodes[nodes[n].prev].next = nodes[n].next; |
1989 _nodes[_nodes[n].prev].next = _nodes[n].next; |
1990 } else { |
1990 } else { |
1991 first_node = nodes[n].next; |
1991 first_node = _nodes[n].next; |
1992 } |
1992 } |
1993 |
1993 |
1994 if (nodes[n].partition_next != -1) { |
1994 if (_nodes[n].partition_next != -1) { |
1995 nodes[nodes[n].partition_next].partition_prev = nodes[n].partition_prev; |
1995 _nodes[_nodes[n].partition_next].partition_prev = _nodes[n].partition_prev; |
1996 } |
1996 } |
1997 |
1997 |
1998 if (nodes[n].partition_prev != -1) { |
1998 if (_nodes[n].partition_prev != -1) { |
1999 nodes[nodes[n].partition_prev].partition_next = nodes[n].partition_next; |
1999 _nodes[_nodes[n].partition_prev].partition_next = _nodes[n].partition_next; |
2000 } else { |
2000 } else { |
2001 if (nodes[n].red) { |
2001 if (_nodes[n].red) { |
2002 first_red = nodes[n].partition_next; |
2002 first_red = _nodes[n].partition_next; |
2003 } else { |
2003 } else { |
2004 first_blue = nodes[n].partition_next; |
2004 first_blue = _nodes[n].partition_next; |
2005 } |
2005 } |
2006 } |
2006 } |
2007 |
2007 |
2008 if (nodes[n].red) { |
2008 if (_nodes[n].red) { |
2009 nodes[n].next = first_free_red; |
2009 _nodes[n].next = first_free_red; |
2010 first_free_red = n; |
2010 first_free_red = n; |
2011 } else { |
2011 } else { |
2012 nodes[n].next = first_free_blue; |
2012 _nodes[n].next = first_free_blue; |
2013 first_free_blue = n; |
2013 first_free_blue = n; |
2014 } |
2014 } |
2015 nodes[n].prev = -2; |
2015 _nodes[n].prev = -2; |
2016 } |
2016 } |
2017 |
2017 |
2018 void erase(const Edge& edge) { |
2018 void erase(const Edge& edge) { |
2019 int n = edge.id * 2; |
2019 int n = edge.id * 2; |
2020 |
2020 |
2021 if (arcs[n].next_out != -1) { |
2021 if (_arcs[n].next_out != -1) { |
2022 arcs[arcs[n].next_out].prev_out = arcs[n].prev_out; |
2022 _arcs[_arcs[n].next_out].prev_out = _arcs[n].prev_out; |
2023 } |
2023 } |
2024 |
2024 |
2025 if (arcs[n].prev_out != -1) { |
2025 if (_arcs[n].prev_out != -1) { |
2026 arcs[arcs[n].prev_out].next_out = arcs[n].next_out; |
2026 _arcs[_arcs[n].prev_out].next_out = _arcs[n].next_out; |
2027 } else { |
2027 } else { |
2028 nodes[arcs[n | 1].target].first_out = arcs[n].next_out; |
2028 _nodes[_arcs[n | 1].target].first_out = _arcs[n].next_out; |
2029 } |
2029 } |
2030 |
2030 |
2031 if (arcs[n | 1].next_out != -1) { |
2031 if (_arcs[n | 1].next_out != -1) { |
2032 arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out; |
2032 _arcs[_arcs[n | 1].next_out].prev_out = _arcs[n | 1].prev_out; |
2033 } |
2033 } |
2034 |
2034 |
2035 if (arcs[n | 1].prev_out != -1) { |
2035 if (_arcs[n | 1].prev_out != -1) { |
2036 arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out; |
2036 _arcs[_arcs[n | 1].prev_out].next_out = _arcs[n | 1].next_out; |
2037 } else { |
2037 } else { |
2038 nodes[arcs[n].target].first_out = arcs[n | 1].next_out; |
2038 _nodes[_arcs[n].target].first_out = _arcs[n | 1].next_out; |
2039 } |
2039 } |
2040 |
2040 |
2041 arcs[n].next_out = first_free_arc; |
2041 _arcs[n].next_out = first_free_arc; |
2042 first_free_arc = n; |
2042 first_free_arc = n; |
2043 arcs[n].prev_out = -2; |
2043 _arcs[n].prev_out = -2; |
2044 arcs[n | 1].prev_out = -2; |
2044 _arcs[n | 1].prev_out = -2; |
2045 |
2045 |
2046 } |
2046 } |
2047 |
2047 |
2048 void clear() { |
2048 void clear() { |
2049 arcs.clear(); |
2049 _arcs.clear(); |
2050 nodes.clear(); |
2050 _nodes.clear(); |
2051 first_node = first_free_arc = first_red = first_blue = |
2051 first_node = first_free_arc = first_red = first_blue = |
2052 max_red = max_blue = first_free_red = first_free_blue = -1; |
2052 max_red = max_blue = first_free_red = first_free_blue = -1; |
2053 } |
2053 } |
2054 |
2054 |
2055 protected: |
2055 protected: |
2056 |
2056 |
2057 void changeRed(Edge e, RedNode n) { |
2057 void changeRed(Edge e, RedNode n) { |
2058 if(arcs[(2 * e.id) | 1].next_out != -1) { |
2058 if(_arcs[(2 * e.id) | 1].next_out != -1) { |
2059 arcs[arcs[(2 * e.id) | 1].next_out].prev_out = |
2059 _arcs[_arcs[(2 * e.id) | 1].next_out].prev_out = |
2060 arcs[(2 * e.id) | 1].prev_out; |
2060 _arcs[(2 * e.id) | 1].prev_out; |
2061 } |
2061 } |
2062 if(arcs[(2 * e.id) | 1].prev_out != -1) { |
2062 if(_arcs[(2 * e.id) | 1].prev_out != -1) { |
2063 arcs[arcs[(2 * e.id) | 1].prev_out].next_out = |
2063 _arcs[_arcs[(2 * e.id) | 1].prev_out].next_out = |
2064 arcs[(2 * e.id) | 1].next_out; |
2064 _arcs[(2 * e.id) | 1].next_out; |
2065 } else { |
2065 } else { |
2066 nodes[arcs[2 * e.id].target].first_out = |
2066 _nodes[_arcs[2 * e.id].target].first_out = |
2067 arcs[(2 * e.id) | 1].next_out; |
2067 _arcs[(2 * e.id) | 1].next_out; |
2068 } |
2068 } |
2069 |
2069 |
2070 if (nodes[n.id].first_out != -1) { |
2070 if (_nodes[n.id].first_out != -1) { |
2071 arcs[nodes[n.id].first_out].prev_out = ((2 * e.id) | 1); |
2071 _arcs[_nodes[n.id].first_out].prev_out = ((2 * e.id) | 1); |
2072 } |
2072 } |
2073 arcs[2 * e.id].target = n.id; |
2073 _arcs[2 * e.id].target = n.id; |
2074 arcs[(2 * e.id) | 1].prev_out = -1; |
2074 _arcs[(2 * e.id) | 1].prev_out = -1; |
2075 arcs[(2 * e.id) | 1].next_out = nodes[n.id].first_out; |
2075 _arcs[(2 * e.id) | 1].next_out = _nodes[n.id].first_out; |
2076 nodes[n.id].first_out = ((2 * e.id) | 1); |
2076 _nodes[n.id].first_out = ((2 * e.id) | 1); |
2077 } |
2077 } |
2078 |
2078 |
2079 void changeBlue(Edge e, BlueNode n) { |
2079 void changeBlue(Edge e, BlueNode n) { |
2080 if(arcs[2 * e.id].next_out != -1) { |
2080 if(_arcs[2 * e.id].next_out != -1) { |
2081 arcs[arcs[2 * e.id].next_out].prev_out = arcs[2 * e.id].prev_out; |
2081 _arcs[_arcs[2 * e.id].next_out].prev_out = _arcs[2 * e.id].prev_out; |
2082 } |
2082 } |
2083 if(arcs[2 * e.id].prev_out != -1) { |
2083 if(_arcs[2 * e.id].prev_out != -1) { |
2084 arcs[arcs[2 * e.id].prev_out].next_out = |
2084 _arcs[_arcs[2 * e.id].prev_out].next_out = |
2085 arcs[2 * e.id].next_out; |
2085 _arcs[2 * e.id].next_out; |
2086 } else { |
2086 } else { |
2087 nodes[arcs[(2 * e.id) | 1].target].first_out = |
2087 _nodes[_arcs[(2 * e.id) | 1].target].first_out = |
2088 arcs[2 * e.id].next_out; |
2088 _arcs[2 * e.id].next_out; |
2089 } |
2089 } |
2090 |
2090 |
2091 if (nodes[n.id].first_out != -1) { |
2091 if (_nodes[n.id].first_out != -1) { |
2092 arcs[nodes[n.id].first_out].prev_out = 2 * e.id; |
2092 _arcs[_nodes[n.id].first_out].prev_out = 2 * e.id; |
2093 } |
2093 } |
2094 arcs[(2 * e.id) | 1].target = n.id; |
2094 _arcs[(2 * e.id) | 1].target = n.id; |
2095 arcs[2 * e.id].prev_out = -1; |
2095 _arcs[2 * e.id].prev_out = -1; |
2096 arcs[2 * e.id].next_out = nodes[n.id].first_out; |
2096 _arcs[2 * e.id].next_out = _nodes[n.id].first_out; |
2097 nodes[n.id].first_out = 2 * e.id; |
2097 _nodes[n.id].first_out = 2 * e.id; |
2098 } |
2098 } |
2099 |
2099 |
2100 }; |
2100 }; |
2101 |
2101 |
2102 typedef BpGraphExtender<ListBpGraphBase> ExtendedListBpGraphBase; |
2102 typedef BpGraphExtender<ListBpGraphBase> ExtendedListBpGraphBase; |