New version of XML reader/writer.
Now, there are only a single XmlIo class both for reading and writing.
2 * lemon/list_graph.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef LEMON_LIST_GRAPH_H
18 #define LEMON_LIST_GRAPH_H
22 ///\brief ListGraph, UndirListGraph classes.
24 #include <lemon/bits/erasable_graph_extender.h>
25 #include <lemon/bits/clearable_graph_extender.h>
26 #include <lemon/bits/extendable_graph_extender.h>
27 #include <lemon/bits/iterable_graph_extender.h>
28 #include <lemon/bits/alteration_notifier.h>
29 #include <lemon/bits/default_map.h>
31 #include <lemon/bits/undir_graph_extender.h>
41 int first_in, first_out;
47 int prev_in, prev_out;
48 int next_in, next_out;
51 std::vector<NodeT> nodes;
57 std::vector<EdgeT> edges;
63 typedef ListGraphBase Graph;
66 friend class ListGraphBase;
70 Node(int pid) { id = pid;}
74 Node (Invalid) { id = -1; }
75 bool operator==(const Node& node) const {return id == node.id;}
76 bool operator!=(const Node& node) const {return id != node.id;}
77 bool operator<(const Node& node) const {return id < node.id;}
81 friend class ListGraphBase;
85 Edge(int pid) { id = pid;}
89 Edge (Invalid) { id = -1; }
90 bool operator==(const Edge& edge) const {return id == edge.id;}
91 bool operator!=(const Edge& edge) const {return id != edge.id;}
92 bool operator<(const Edge& edge) const {return id < edge.id;}
98 : nodes(), first_node(-1),
99 first_free_node(-1), edges(), first_free_edge(-1) {}
106 int maxId(Node = INVALID) const { return nodes.size()-1; }
112 int maxId(Edge = INVALID) const { return edges.size()-1; }
114 Node source(Edge e) const { return edges[e.id].source; }
115 Node target(Edge e) const { return edges[e.id].target; }
118 void first(Node& node) const {
119 node.id = first_node;
122 void next(Node& node) const {
123 node.id = nodes[node.id].next;
127 void first(Edge& e) const {
130 n!=-1 && nodes[n].first_in == -1;
132 e.id = (n == -1) ? -1 : nodes[n].first_in;
135 void next(Edge& edge) const {
136 if (edges[edge.id].next_in != -1) {
137 edge.id = edges[edge.id].next_in;
140 for(n = nodes[edges[edge.id].target].next;
141 n!=-1 && nodes[n].first_in == -1;
143 edge.id = (n == -1) ? -1 : nodes[n].first_in;
147 void firstOut(Edge &e, const Node& v) const {
148 e.id = nodes[v.id].first_out;
150 void nextOut(Edge &e) const {
151 e.id=edges[e.id].next_out;
154 void firstIn(Edge &e, const Node& v) const {
155 e.id = nodes[v.id].first_in;
157 void nextIn(Edge &e) const {
158 e.id=edges[e.id].next_in;
162 static int id(Node v) { return v.id; }
163 static int id(Edge e) { return e.id; }
165 static Node fromId(int id, Node) { return Node(id);}
166 static Edge fromId(int id, Edge) { return Edge(id);}
168 /// Adds a new node to the graph.
170 /// \warning It adds the new node to the front of the list.
171 /// (i.e. the lastly added node becomes the first.)
175 if(first_free_node==-1) {
177 nodes.push_back(NodeT());
180 first_free_node = nodes[n].next;
183 nodes[n].next = first_node;
184 if(first_node != -1) nodes[first_node].prev = n;
188 nodes[n].first_in = nodes[n].first_out = -1;
193 Edge addEdge(Node u, Node v) {
196 if (first_free_edge == -1) {
198 edges.push_back(EdgeT());
201 first_free_edge = edges[n].next_in;
204 edges[n].source = u.id;
205 edges[n].target = v.id;
207 edges[n].next_out = nodes[u.id].first_out;
208 if(nodes[u.id].first_out != -1) {
209 edges[nodes[u.id].first_out].prev_out = n;
212 edges[n].next_in = nodes[v.id].first_in;
213 if(nodes[v.id].first_in != -1) {
214 edges[nodes[v.id].first_in].prev_in = n;
217 edges[n].prev_in = edges[n].prev_out = -1;
219 nodes[u.id].first_out = nodes[v.id].first_in = n;
224 void erase(const Node& node) {
227 if(nodes[n].next != -1) {
228 nodes[nodes[n].next].prev = nodes[n].prev;
231 if(nodes[n].prev != -1) {
232 nodes[nodes[n].prev].next = nodes[n].next;
234 first_node = nodes[n].next;
237 nodes[n].next = first_free_node;
242 void erase(const Edge& edge) {
245 if(edges[n].next_in!=-1) {
246 edges[edges[n].next_in].prev_in = edges[n].prev_in;
249 if(edges[n].prev_in!=-1) {
250 edges[edges[n].prev_in].next_in = edges[n].next_in;
252 nodes[edges[n].target].first_in = edges[n].next_in;
256 if(edges[n].next_out!=-1) {
257 edges[edges[n].next_out].prev_out = edges[n].prev_out;
260 if(edges[n].prev_out!=-1) {
261 edges[edges[n].prev_out].next_out = edges[n].next_out;
263 nodes[edges[n].source].first_out = edges[n].next_out;
266 edges[n].next_in = first_free_edge;
274 first_node = first_free_node = first_free_edge = -1;
278 void _changeTarget(Edge e, Node n)
280 if(edges[e.id].next_in != -1)
281 edges[edges[e.id].next_in].prev_in = edges[e.id].prev_in;
282 if(edges[e.id].prev_in != -1)
283 edges[edges[e.id].prev_in].next_in = edges[e.id].next_in;
284 else nodes[edges[e.id].target].first_in = edges[e.id].next_in;
285 if (nodes[n.id].first_in != -1) {
286 edges[nodes[n.id].first_in].prev_in = e.id;
288 edges[e.id].target = n.id;
289 edges[e.id].prev_in = -1;
290 edges[e.id].next_in = nodes[n.id].first_in;
291 nodes[n.id].first_in = e.id;
293 void _changeSource(Edge e, Node n)
295 if(edges[e.id].next_out != -1)
296 edges[edges[e.id].next_out].prev_out = edges[e.id].prev_out;
297 if(edges[e.id].prev_out != -1)
298 edges[edges[e.id].prev_out].next_out = edges[e.id].next_out;
299 else nodes[edges[e.id].source].first_out = edges[e.id].next_out;
300 if (nodes[n.id].first_out != -1) {
301 edges[nodes[n.id].first_out].prev_out = e.id;
303 edges[e.id].source = n.id;
304 edges[e.id].prev_out = -1;
305 edges[e.id].next_out = nodes[n.id].first_out;
306 nodes[n.id].first_out = e.id;
311 typedef ErasableGraphExtender<
312 ClearableGraphExtender<
313 ExtendableGraphExtender<
314 MappableGraphExtender<
315 IterableGraphExtender<
316 AlterableGraphExtender<ListGraphBase> > > > > > ExtendedListGraphBase;
318 /// \addtogroup graphs
321 ///A list graph class.
323 ///This is a simple and fast erasable graph implementation.
325 ///It addition that it conforms to the
326 ///\ref concept::ErasableGraph "ErasableGraph" concept,
327 ///it also provides several additional useful extra functionalities.
328 ///\sa concept::ErasableGraph.
330 class ListGraph : public ExtendedListGraphBase
333 /// Changes the target of \c e to \c n
335 /// Changes the target of \c e to \c n
337 ///\note The <tt>Edge</tt>'s and <tt>OutEdge</tt>'s
338 ///referencing the changed edge remain
339 ///valid. However <tt>InEdge</tt>'s are invalidated.
340 void changeTarget(Edge e, Node n) {
343 /// Changes the source of \c e to \c n
345 /// Changes the source of \c e to \c n
347 ///\note The <tt>Edge</tt>'s and <tt>InEdge</tt>'s
348 ///referencing the changed edge remain
349 ///valid. However <tt>OutEdge</tt>'s are invalidated.
350 void changeSource(Edge e, Node n) {
354 /// Invert the direction of an edge.
356 ///\note The <tt>Edge</tt>'s
357 ///referencing the changed edge remain
358 ///valid. However <tt>OutEdge</tt>'s and <tt>InEdge</tt>'s are invalidated.
359 void reverseEdge(Edge e) {
361 _changeTarget(e,source(e));
365 ///Using this it possible to avoid the superfluous memory allocation.
367 ///Using this it possible to avoid the superfluous memory allocation.
368 ///\todo more docs...
369 void reserveEdge(int n) { edges.reserve(n); };
371 ///Contract two nodes.
373 ///This function contracts two nodes.
375 ///Node \p b will be removed but instead of deleting
376 ///its neighboring edges, they will be joined to \p a.
377 ///The last parameter \p r controls whether to remove loops. \c true
378 ///means that loops will be removed.
380 ///\note The <tt>Edge</tt>s
381 ///referencing a moved edge remain
382 ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
383 ///may be invalidated.
384 void contract(Node a, Node b, bool r = true)
386 for(OutEdgeIt e(*this,b);e!=INVALID;) {
389 if(r && target(e)==a) erase(e);
390 else changeSource(e,a);
393 for(InEdgeIt e(*this,b);e!=INVALID;) {
396 if(r && source(e)==a) erase(e);
397 else changeTarget(e,a);
405 ///This function splits a node. First a new node is added to the graph,
406 ///then the source of each outgoing edge of \c n is moved to this new node.
407 ///If \c connect is \c true (this is the default value), then a new edge
408 ///from \c n to the newly created node is also added.
409 ///\return The newly created node.
411 ///\note The <tt>Edge</tt>s
412 ///referencing a moved edge remain
413 ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
414 ///may be invalidated.
415 ///\warning This functionality cannot be used together with the SnapShot
417 ///\todo It could be implemented in a bit faster way.
418 Node split(Node n, bool connect = true)
421 for(OutEdgeIt e(*this,n);e!=INVALID;) {
427 if(connect) addEdge(n,b);
431 ///Class to make a snapshot of the graph and to restrore to it later.
433 ///Class to make a snapshot of the graph and to restrore to it later.
435 ///The newly added nodes and edges can be removed using the
436 ///restore() function.
438 ///\warning Edge and node deletions cannot be restored.
439 ///\warning SnapShots cannot be nested.
440 ///\todo \c SnapShot or \c Snapshot?
441 class SnapShot : protected AlterationNotifier<Node>::ObserverBase,
442 protected AlterationNotifier<Edge>::ObserverBase
447 std::list<Node> added_nodes;
448 std::list<Edge> added_edges;
451 virtual void add(const Node& n) {
452 added_nodes.push_back(n);
456 virtual void erase(const Node&)
460 virtual void add(const Edge& n) {
461 added_edges.push_back(n);
465 virtual void erase(const Edge&)
470 ///\bug What is this used for?
472 virtual void build() {}
473 ///\bug What is this used for?
475 virtual void clear() {}
477 void regist(ListGraph &_g) {
479 AlterationNotifier<Node>::ObserverBase::
480 attach(g->getNotifier(Node()));
481 AlterationNotifier<Edge>::ObserverBase::
482 attach(g->getNotifier(Edge()));
486 AlterationNotifier<Node>::ObserverBase::
488 AlterationNotifier<Edge>::ObserverBase::
494 ///Default constructur.
496 ///Default constructur.
497 ///To actually make a snapshot you must call save().
500 ///Constructor that immediately makes a snapshot.
502 ///This constructor immediately makes a snapshot of the graph.
503 ///\param _g The graph we make a snapshot of.
504 SnapShot(ListGraph &_g) {
507 ///\bug Is it necessary?
516 ///Make a snapshot of the graph.
518 ///This function can be called more than once. In case of a repeated
519 ///call, the previous snapshot gets lost.
520 ///\param _g The graph we make the snapshot of.
521 void save(ListGraph &_g)
531 ///Undo the changes until the last snapshot.
533 ///Undo the changes until last snapshot created by save().
535 ///\todo This function might be called undo().
539 while(!added_edges.empty()) {
540 old_g.erase(added_edges.front());
541 added_edges.pop_front();
543 while(!added_nodes.empty()) {
544 old_g.erase(added_nodes.front());
545 added_nodes.pop_front();
554 /**************** Undirected List Graph ****************/
556 typedef ErasableUndirGraphExtender<
557 ClearableUndirGraphExtender<
558 ExtendableUndirGraphExtender<
559 MappableUndirGraphExtender<
560 IterableUndirGraphExtender<
561 AlterableUndirGraphExtender<
562 UndirGraphExtender<ListGraphBase> > > > > > > ExtendedUndirListGraphBase;
564 /// \addtogroup graphs
567 ///An undirected list graph class.
569 ///This is a simple and fast erasable undirected graph implementation.
571 ///It conforms to the
572 ///\ref concept::UndirGraph "UndirGraph" concept.
574 ///\sa concept::UndirGraph.
576 ///\todo SnapShot, reverseEdge(), changeTarget(), changeSource(), contract()
577 ///haven't been implemented yet.
579 class UndirListGraph : public ExtendedUndirListGraphBase {
581 typedef ExtendedUndirListGraphBase Parent;
582 /// \brief Changes the target of \c e to \c n
584 /// Changes the target of \c e to \c n
586 /// \note The <tt>Edge</tt>'s and <tt>OutEdge</tt>'s
587 /// referencing the changed edge remain
588 /// valid. However <tt>InEdge</tt>'s are invalidated.
589 void changeTarget(UndirEdge e, Node n) {
592 /// Changes the source of \c e to \c n
594 /// Changes the source of \c e to \c n
596 ///\note The <tt>Edge</tt>'s and <tt>InEdge</tt>'s
597 ///referencing the changed edge remain
598 ///valid. However <tt>OutEdge</tt>'s are invalidated.
599 void changeSource(UndirEdge e, Node n) {
602 /// \brief Contract two nodes.
604 /// This function contracts two nodes.
606 /// Node \p b will be removed but instead of deleting
607 /// its neighboring edges, they will be joined to \p a.
608 /// The last parameter \p r controls whether to remove loops. \c true
609 /// means that loops will be removed.
611 /// \note The <tt>Edge</tt>s
612 /// referencing a moved edge remain
614 void contract(Node a, Node b, bool r = true) {
615 for(IncEdgeIt e(*this, b); e!=INVALID;) {
616 IncEdgeIt f = e; ++f;
617 if (r && runningNode(e) == a) {
619 } else if (source(e) == b) {