EXAMPLE_PATH is set to the demo dir.
3 #ifndef HUGO_LIST_GRAPH_H
4 #define HUGO_LIST_GRAPH_H
8 ///\brief ListGraph, SymListGraph, NodeSet and EdgeSet classes.
13 #include <hugo/invalid.h>
15 #include <hugo/map_registry.h>
16 #include <hugo/default_map.h>
18 #include <hugo/sym_map.h>
20 #include <hugo/map_defines.h>
25 /// \addtogroup graphs
28 ///A list graph class.
30 ///This is a simple and fast erasable graph implementation.
33 ///\ref skeleton::ErasableGraph "ErasableGraph" concept.
34 ///\sa skeleton::ErasableGraph.
37 //Nodes are double linked.
38 //The free nodes are only single linked using the "next" field.
41 int first_in,first_out;
44 //Edges are double linked.
45 //The free edges are only single linked using the "next_in" field.
49 int prev_in, prev_out;
50 int next_in, next_out;
53 std::vector<NodeT> nodes;
58 std::vector<EdgeT> edges;
64 typedef ListGraph Graph;
77 /// Creating map registries.
78 CREATE_MAP_REGISTRIES;
79 /// Creating node and edge maps.
82 /// It apears in the documentation as if it were a function definition.
83 CREATE_MAPS(DefaultMap);
88 : nodes(), first_node(-1),
89 first_free_node(-1), edges(), first_free_edge(-1) {}
91 ListGraph(const ListGraph &_g)
92 : nodes(_g.nodes), first_node(_g.first_node),
93 first_free_node(_g.first_free_node), edges(_g.edges),
94 first_free_edge(_g.first_free_edge) {}
97 int nodeNum() const { return nodes.size(); }
99 int edgeNum() const { return edges.size(); }
101 ///Set the expected maximum number of edges.
103 ///With this function, it is possible to set the expected number of edges.
104 ///The use of this fasten the building of the graph and makes
105 ///it possible to avoid the superfluous memory allocation.
106 void reserveEdge(int n) { edges.reserve(n); };
112 int maxNodeId() const { return nodes.size()-1; }
117 int maxEdgeId() const { return edges.size()-1; }
119 Node tail(Edge e) const { return edges[e.n].tail; }
120 Node head(Edge e) const { return edges[e.n].head; }
122 NodeIt& first(NodeIt& v) const {
123 v=NodeIt(*this); return v; }
124 EdgeIt& first(EdgeIt& e) const {
125 e=EdgeIt(*this); return e; }
126 OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
127 e=OutEdgeIt(*this,v); return e; }
128 InEdgeIt& first(InEdgeIt& e, const Node v) const {
129 e=InEdgeIt(*this,v); return e; }
133 /// The ID of a valid Node is a nonnegative integer not greater than
134 /// \ref maxNodeId(). The range of the ID's is not surely continuous
135 /// and the greatest node ID can be actually less then \ref maxNodeId().
137 /// The ID of the \ref INVALID node is -1.
138 ///\return The ID of the node \c v.
139 static int id(Node v) { return v.n; }
142 /// The ID of a valid Edge is a nonnegative integer not greater than
143 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
144 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
146 /// The ID of the \ref INVALID edge is -1.
147 ///\return The ID of the edge \c e.
148 static int id(Edge e) { return e.n; }
150 /// Adds a new node to the graph.
152 /// \warning It adds the new node to the front of the list.
153 /// (i.e. the lastly added node becomes the first.)
157 if(first_free_node==-1)
160 nodes.push_back(NodeT());
164 first_free_node = nodes[n].next;
167 nodes[n].next = first_node;
168 if(first_node != -1) nodes[first_node].prev = n;
172 nodes[n].first_in = nodes[n].first_out = -1;
176 //Update dynamic maps
182 Edge addEdge(Node u, Node v) {
185 if(first_free_edge==-1)
188 edges.push_back(EdgeT());
192 first_free_edge = edges[n].next_in;
195 edges[n].tail = u.n; edges[n].head = v.n;
197 edges[n].next_out = nodes[u.n].first_out;
198 if(nodes[u.n].first_out != -1) edges[nodes[u.n].first_out].prev_out = n;
199 edges[n].next_in = nodes[v.n].first_in;
200 if(nodes[v.n].first_in != -1) edges[nodes[v.n].first_in].prev_in = n;
201 edges[n].prev_in = edges[n].prev_out = -1;
203 nodes[u.n].first_out = nodes[v.n].first_in = n;
207 //Update dynamic maps
213 /// Finds an edge between two nodes.
215 /// Finds an edge from node \c u to node \c v.
217 /// If \c prev is \ref INVALID (this is the default value), then
218 /// It finds the first edge from \c u to \c v. Otherwise it looks for
219 /// the next edge from \c u to \c v after \c prev.
220 /// \return The found edge or INVALID if there is no such an edge.
221 Edge findEdge(Node u,Node v, Edge prev = INVALID)
223 int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
224 while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out;
230 void eraseEdge(int n) {
232 if(edges[n].next_in!=-1)
233 edges[edges[n].next_in].prev_in = edges[n].prev_in;
234 if(edges[n].prev_in!=-1)
235 edges[edges[n].prev_in].next_in = edges[n].next_in;
236 else nodes[edges[n].head].first_in = edges[n].next_in;
238 if(edges[n].next_out!=-1)
239 edges[edges[n].next_out].prev_out = edges[n].prev_out;
240 if(edges[n].prev_out!=-1)
241 edges[edges[n].prev_out].next_out = edges[n].next_out;
242 else nodes[edges[n].tail].first_out = edges[n].next_out;
244 edges[n].next_in = first_free_edge;
247 //Update dynamic maps
255 void erase(Node nn) {
259 while((m=nodes[n].first_in)!=-1) eraseEdge(m);
260 while((m=nodes[n].first_out)!=-1) eraseEdge(m);
262 if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev;
263 if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next;
264 else first_node = nodes[n].next;
266 nodes[n].next = first_free_node;
269 //Update dynamic maps
274 void erase(Edge e) { eraseEdge(e.n); }
281 first_node=first_free_node=first_free_edge=-1;
285 friend class ListGraph;
286 template <typename T> friend class NodeMap;
289 friend class OutEdgeIt;
290 friend class InEdgeIt;
291 friend class SymEdge;
295 friend int ListGraph::id(Node v);
299 Node (Invalid) { n=-1; }
300 bool operator==(const Node i) const {return n==i.n;}
301 bool operator!=(const Node i) const {return n!=i.n;}
302 bool operator<(const Node i) const {return n<i.n;}
304 // operator bool() { return n!=-1; }
307 class NodeIt : public Node {
309 friend class ListGraph;
311 NodeIt() : Node() { }
312 NodeIt(Invalid i) : Node(i) { }
313 NodeIt(const ListGraph& _G) : Node(_G.first_node), G(&_G) { }
314 NodeIt(const ListGraph& _G,Node n) : Node(n), G(&_G) { }
315 NodeIt &operator++() {
320 // operator bool() { return Node::operator bool(); }
324 friend class ListGraph;
325 template <typename T> friend class EdgeMap;
327 //template <typename T> friend class SymListGraph::SymEdgeMap;
328 //friend Edge SymListGraph::opposite(Edge) const;
334 friend int ListGraph::id(Edge e);
337 /// An Edge with id \c n.
339 /// \bug It should be
340 /// obtained by a member function of the Graph.
344 Edge (Invalid) { n=-1; }
345 bool operator==(const Edge i) const {return n==i.n;}
346 bool operator!=(const Edge i) const {return n!=i.n;}
347 bool operator<(const Edge i) const {return n<i.n;}
348 ///\bug This is a workaround until somebody tells me how to
349 ///make class \c SymListGraph::SymEdgeMap friend of Edge
350 int &idref() {return n;}
351 const int &idref() const {return n;}
353 // operator bool() { return n!=-1; }
356 class EdgeIt : public Edge {
358 friend class ListGraph;
360 EdgeIt(const ListGraph& _G) : Edge(), G(&_G) {
363 m!=-1 && _G.nodes[m].first_in == -1; m = _G.nodes[m].next);
364 n = (m==-1)?-1:_G.nodes[m].first_in;
366 EdgeIt (Invalid i) : Edge(i) { }
367 EdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { }
368 EdgeIt() : Edge() { }
369 ///\bug This is a workaround until somebody tells me how to
370 ///make class \c SymListGraph::SymEdgeMap friend of Edge
371 int &idref() {return n;}
372 EdgeIt &operator++() {
373 if(G->edges[n].next_in!=-1) n=G->edges[n].next_in;
376 for(nn=G->nodes[G->edges[n].head].next;
377 nn!=-1 && G->nodes[nn].first_in == -1;
378 nn = G->nodes[nn].next) ;
379 n = (nn==-1)?-1:G->nodes[nn].first_in;
384 // operator bool() { return Edge::operator bool(); }
387 class OutEdgeIt : public Edge {
389 friend class ListGraph;
391 OutEdgeIt() : Edge() { }
392 OutEdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { }
393 OutEdgeIt (Invalid i) : Edge(i) { }
395 OutEdgeIt(const ListGraph& _G,const Node v)
396 : Edge(_G.nodes[v.n].first_out), G(&_G) {}
397 OutEdgeIt &operator++() { n=G->edges[n].next_out; return *this; }
399 // operator bool() { return Edge::operator bool(); }
402 class InEdgeIt : public Edge {
404 friend class ListGraph;
406 InEdgeIt() : Edge() { }
407 InEdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { }
408 InEdgeIt (Invalid i) : Edge(i) { }
409 InEdgeIt(const ListGraph& _G,Node v)
410 : Edge(_G.nodes[v.n].first_in), G(&_G) { }
411 InEdgeIt &operator++() { n=G->edges[n].next_in; return *this; }
413 // operator bool() { return Edge::operator bool(); }
417 ///Graph for bidirectional edges.
419 ///The purpose of this graph structure is to handle graphs
420 ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair
421 ///of oppositely directed edges.
422 ///There is a new edge map type called
423 ///\ref SymListGraph::SymEdgeMap "SymEdgeMap"
424 ///that complements this
426 ///storing shared values for the edge pairs. The usual
427 ///\ref Graph::EdgeMap "EdgeMap"
431 ///The oppositely directed edge can also be obtained easily
432 ///using \ref opposite.
434 ///Here erase(Edge) deletes a pair of edges.
436 ///\todo this date structure need some reconsiderations. Maybe it
437 ///should be implemented independently from ListGraph.
439 class SymListGraph : public ListGraph
443 typedef SymListGraph Graph;
445 /// Importing maps from the base class ListGraph.
446 KEEP_MAPS(ListGraph, SymListGraph);
448 /// Creating symmetric map registry.
449 CREATE_SYM_EDGE_MAP_REGISTRY;
450 /// Creating symmetric edge map.
451 CREATE_SYM_EDGE_MAP(DefaultMap);
453 SymListGraph() : ListGraph() { }
454 SymListGraph(const ListGraph &_g) : ListGraph(_g) { }
455 ///Adds a pair of oppositely directed edges to the graph.
456 Edge addEdge(Node u, Node v)
458 Edge e = ListGraph::addEdge(u,v);
459 Edge f = ListGraph::addEdge(v,u);
460 sym_edge_maps.add(e);
461 sym_edge_maps.add(f);
466 void erase(Node n) { ListGraph::erase(n);}
467 ///The oppositely directed edge.
469 ///Returns the oppositely directed
470 ///pair of the edge \c e.
471 static Edge opposite(Edge e)
474 f.idref() = e.idref() - 2*(e.idref()%2) + 1;
478 ///Removes a pair of oppositely directed edges to the graph.
480 Edge f = opposite(e);
481 sym_edge_maps.erase(e);
482 sym_edge_maps.erase(f);
489 ///A graph class containing only nodes.
491 ///This class implements a graph structure without edges.
492 ///The most useful application of this class is to be the node set of an
493 ///\ref EdgeSet class.
496 ///the \ref skeleton::ExtendableGraph "ExtendableGraph" concept
497 ///with the exception that you cannot
498 ///add (or delete) edges. The usual edge iterators are exists, but they are
499 ///always \ref INVALID.
500 ///\sa skeleton::ExtendableGraph
504 //Nodes are double linked.
505 //The free nodes are only single linked using the "next" field.
508 int first_in,first_out;
513 std::vector<NodeT> nodes;
516 //The first free node
521 typedef NodeSet Graph;
533 /// Creating node map registry.
534 CREATE_NODE_MAP_REGISTRY;
535 /// Creating node maps.
536 CREATE_NODE_MAP(DefaultMap);
538 /// Creating empty map structure for edges.
539 template <typename Value>
543 EdgeMap(const Graph&) {}
544 EdgeMap(const Graph&, const Value&) {}
546 EdgeMap(const EdgeMap&) {}
547 template <typename CMap> EdgeMap(const CMap&) {}
549 EdgeMap& operator=(const EdgeMap&) {}
550 template <typename CMap> EdgeMap& operator=(const CMap&) {}
552 class ConstIterator {
554 bool operator==(const ConstIterator&) {return true;}
555 bool operator!=(const ConstIterator&) {return false;}
558 typedef ConstIterator Iterator;
560 Iterator begin() { return Iterator();}
561 Iterator end() { return Iterator();}
563 ConstIterator begin() const { return ConstIterator();}
564 ConstIterator end() const { return ConstIterator();}
570 ///Default constructor
572 : nodes(), first_node(-1), first_free_node(-1) {}
574 NodeSet(const NodeSet &_g)
575 : nodes(_g.nodes), first_node(_g.first_node),
576 first_free_node(_g.first_free_node) {}
579 int nodeNum() const { return nodes.size(); }
581 int edgeNum() const { return 0; }
587 int maxNodeId() const { return nodes.size()-1; }
592 int maxEdgeId() const { return 0; }
594 Node tail(Edge e) const { return INVALID; }
595 Node head(Edge e) const { return INVALID; }
597 NodeIt& first(NodeIt& v) const {
598 v=NodeIt(*this); return v; }
599 EdgeIt& first(EdgeIt& e) const {
600 e=EdgeIt(*this); return e; }
601 OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
602 e=OutEdgeIt(*this,v); return e; }
603 InEdgeIt& first(InEdgeIt& e, const Node v) const {
604 e=InEdgeIt(*this,v); return e; }
608 /// The ID of a valid Node is a nonnegative integer not greater than
609 /// \ref maxNodeId(). The range of the ID's is not surely continuous
610 /// and the greatest node ID can be actually less then \ref maxNodeId().
612 /// The ID of the \ref INVALID node is -1.
613 ///\return The ID of the node \c v.
614 int id(Node v) const { return v.n; }
617 /// The ID of a valid Edge is a nonnegative integer not greater than
618 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
619 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
621 /// The ID of the \ref INVALID edge is -1.
622 ///\return The ID of the edge \c e.
623 int id(Edge e) const { return -1; }
625 /// Adds a new node to the graph.
627 /// \warning It adds the new node to the front of the list.
628 /// (i.e. the lastly added node becomes the first.)
632 if(first_free_node==-1)
635 nodes.push_back(NodeT());
639 first_free_node = nodes[n].next;
642 nodes[n].next = first_node;
643 if(first_node != -1) nodes[first_node].prev = n;
647 nodes[n].first_in = nodes[n].first_out = -1;
651 //Update dynamic maps
657 void erase(Node nn) {
660 if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev;
661 if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next;
662 else first_node = nodes[n].next;
664 nodes[n].next = first_free_node;
667 //Update dynamic maps
672 Edge findEdge(Node u,Node v, Edge prev = INVALID)
680 first_node = first_free_node = -1;
684 friend class NodeSet;
685 template <typename T> friend class NodeMap;
688 friend class OutEdgeIt;
689 friend class InEdgeIt;
693 friend int NodeSet::id(Node v) const;
697 Node (Invalid i) { n=-1; }
698 bool operator==(const Node i) const {return n==i.n;}
699 bool operator!=(const Node i) const {return n!=i.n;}
700 bool operator<(const Node i) const {return n<i.n;}
703 class NodeIt : public Node {
705 friend class NodeSet;
707 NodeIt() : Node() { }
708 NodeIt(const NodeSet& _G,Node n) : Node(n), G(&_G) { }
709 NodeIt(Invalid i) : Node(i) { }
710 NodeIt(const NodeSet& _G) : Node(_G.first_node), G(&_G) { }
711 NodeIt &operator++() {
718 //friend class NodeSet;
719 //template <typename T> friend class EdgeMap;
721 //template <typename T> friend class SymNodeSet::SymEdgeMap;
722 //friend Edge SymNodeSet::opposite(Edge) const;
724 // friend class Node;
725 // friend class NodeIt;
727 //friend int NodeSet::id(Edge e) const;
732 bool operator==(const Edge i) const {return true;}
733 bool operator!=(const Edge i) const {return false;}
734 bool operator<(const Edge i) const {return false;}
735 ///\bug This is a workaround until somebody tells me how to
736 ///make class \c SymNodeSet::SymEdgeMap friend of Edge
737 // int idref() {return -1;}
738 // int idref() const {return -1;}
741 class EdgeIt : public Edge {
742 //friend class NodeSet;
744 EdgeIt(const NodeSet& G) : Edge() { }
745 EdgeIt(const NodeSet&, Edge) : Edge() { }
746 EdgeIt (Invalid i) : Edge(i) { }
747 EdgeIt() : Edge() { }
748 ///\bug This is a workaround until somebody tells me how to
749 ///make class \c SymNodeSet::SymEdgeMap friend of Edge
750 // int idref() {return -1;}
751 EdgeIt operator++() { return INVALID; }
754 class OutEdgeIt : public Edge {
755 friend class NodeSet;
757 OutEdgeIt() : Edge() { }
758 OutEdgeIt(const NodeSet&, Edge) : Edge() { }
759 OutEdgeIt (Invalid i) : Edge(i) { }
760 OutEdgeIt(const NodeSet& G,const Node v) : Edge() {}
761 OutEdgeIt operator++() { return INVALID; }
764 class InEdgeIt : public Edge {
765 friend class NodeSet;
767 InEdgeIt() : Edge() { }
768 InEdgeIt(const NodeSet&, Edge) : Edge() { }
769 InEdgeIt (Invalid i) : Edge(i) { }
770 InEdgeIt(const NodeSet& G,Node v) :Edge() {}
771 InEdgeIt operator++() { return INVALID; }
778 ///Graph structure using a node set of another graph.
780 ///This structure can be used to establish another graph over a node set
781 /// of an existing one. The node iterator will go through the nodes of the
782 /// original graph, and the NodeMap's of both graphs will convert to
785 ///\warning Adding or deleting nodes from the graph is not safe if an
786 ///\ref EdgeSet is currently attached to it!
788 ///\todo Make it possible to add/delete edges from the base graph
789 ///(and from \ref EdgeSet, as well)
791 ///\param GG The type of the graph which shares its node set with this class.
792 ///Its interface must conform to the
793 ///\ref skeleton::StaticGraph "StaticGraph" concept.
795 ///It conforms to the
796 ///\ref skeleton::ExtendableGraph "ExtendableGraph" concept.
797 ///\sa skeleton::ExtendableGraph.
799 template<typename GG>
802 typedef GG NodeGraphType;
814 typedef EdgeSet Graph;
816 int id(Node v) const;
818 class Node : public NodeGraphType::Node {
819 friend class EdgeSet;
820 // template <typename T> friend class NodeMap;
823 friend class OutEdgeIt;
824 friend class InEdgeIt;
825 friend class SymEdge;
828 friend int EdgeSet::id(Node v) const;
829 // Node(int nn) {n=nn;}
831 Node() : NodeGraphType::Node() {}
832 Node (Invalid i) : NodeGraphType::Node(i) {}
833 Node(const typename NodeGraphType::Node &n) : NodeGraphType::Node(n) {}
836 class NodeIt : public NodeGraphType::NodeIt {
837 friend class EdgeSet;
839 NodeIt() : NodeGraphType::NodeIt() { }
840 NodeIt(const EdgeSet& _G,Node n) : NodeGraphType::NodeIt(_G.G,n) { }
841 NodeIt (Invalid i) : NodeGraphType::NodeIt(i) {}
842 NodeIt(const EdgeSet& _G) : NodeGraphType::NodeIt(_G.G) { }
843 NodeIt(const typename NodeGraphType::NodeIt &n)
844 : NodeGraphType::NodeIt(n) {}
846 operator Node() { return Node(*this);}
848 { this->NodeGraphType::NodeIt::operator++(); return *this;}
852 //Edges are double linked.
853 //The free edges are only single linked using the "next_in" field.
856 int first_in,first_out;
857 NodeT() : first_in(-1), first_out(-1) { }
863 int prev_in, prev_out;
864 int next_in, next_out;
868 typename NodeGraphType::template NodeMap<NodeT> nodes;
870 std::vector<EdgeT> edges;
871 //The first free edge
885 /// Creates edge map registry.
886 CREATE_EDGE_MAP_REGISTRY;
887 /// Creates edge maps.
888 CREATE_EDGE_MAP(DefaultMap);
890 /// Imports node maps from the NodeGraphType.
891 IMPORT_NODE_MAP(NodeGraphType, graph.G, EdgeSet, graph);
898 ///Construates a new graph based on the nodeset of an existing one.
899 ///\param _G the base graph.
900 explicit EdgeSet(NodeGraphType &_G)
901 : G(_G), nodes(_G), edges(),
902 first_free_edge(-1) {}
905 ///Makes a copy of an EdgeSet.
906 ///It will be based on the same graph.
907 explicit EdgeSet(const EdgeSet &_g)
908 : G(_g.G), nodes(_g.G), edges(_g.edges),
909 first_free_edge(_g.first_free_edge) {}
912 int nodeNum() const { return G.nodeNum(); }
914 int edgeNum() const { return edges.size(); }
920 int maxNodeId() const { return G.maxNodeId(); }
925 int maxEdgeId() const { return edges.size()-1; }
927 Node tail(Edge e) const { return edges[e.n].tail; }
928 Node head(Edge e) const { return edges[e.n].head; }
930 NodeIt& first(NodeIt& v) const {
931 v=NodeIt(*this); return v; }
932 EdgeIt& first(EdgeIt& e) const {
933 e=EdgeIt(*this); return e; }
934 OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
935 e=OutEdgeIt(*this,v); return e; }
936 InEdgeIt& first(InEdgeIt& e, const Node v) const {
937 e=InEdgeIt(*this,v); return e; }
941 /// The ID of a valid Node is a nonnegative integer not greater than
942 /// \ref maxNodeId(). The range of the ID's is not surely continuous
943 /// and the greatest node ID can be actually less then \ref maxNodeId().
945 /// The ID of the \ref INVALID node is -1.
946 ///\return The ID of the node \c v.
947 int id(Node v) { return G.id(v); }
950 /// The ID of a valid Edge is a nonnegative integer not greater than
951 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
952 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
954 /// The ID of the \ref INVALID edge is -1.
955 ///\return The ID of the edge \c e.
956 int id(Edge e) const { return e.n; }
958 /// Adds a new node to the graph.
959 Node addNode() { return G.addNode(); }
961 Edge addEdge(Node u, Node v) {
964 if(first_free_edge==-1)
967 edges.push_back(EdgeT());
971 first_free_edge = edges[n].next_in;
974 edges[n].tail = u; edges[n].head = v;
976 edges[n].next_out = nodes[u].first_out;
977 if(nodes[u].first_out != -1) edges[nodes[u].first_out].prev_out = n;
978 edges[n].next_in = nodes[v].first_in;
979 if(nodes[v].first_in != -1) edges[nodes[v].first_in].prev_in = n;
980 edges[n].prev_in = edges[n].prev_out = -1;
982 nodes[u].first_out = nodes[v].first_in = n;
986 //Update dynamic maps
992 /// Finds an edge between two nodes.
994 /// Finds an edge from node \c u to node \c v.
996 /// If \c prev is \ref INVALID (this is the default value), then
997 /// It finds the first edge from \c u to \c v. Otherwise it looks for
998 /// the next edge from \c u to \c v after \c prev.
999 /// \return The found edge or INVALID if there is no such an edge.
1000 Edge findEdge(Node u,Node v, Edge prev = INVALID)
1002 int e = (prev.n==-1)? nodes[u].first_out : edges[prev.n].next_out;
1003 while(e!=-1 && edges[e].tail!=v) e = edges[e].next_out;
1009 void eraseEdge(int n) {
1011 if(edges[n].next_in!=-1)
1012 edges[edges[n].next_in].prev_in = edges[n].prev_in;
1013 if(edges[n].prev_in!=-1)
1014 edges[edges[n].prev_in].next_in = edges[n].next_in;
1015 else nodes[edges[n].head].first_in = edges[n].next_in;
1017 if(edges[n].next_out!=-1)
1018 edges[edges[n].next_out].prev_out = edges[n].prev_out;
1019 if(edges[n].prev_out!=-1)
1020 edges[edges[n].prev_out].next_out = edges[n].next_out;
1021 else nodes[edges[n].tail].first_out = edges[n].next_out;
1023 edges[n].next_in = first_free_edge;
1024 first_free_edge = -1;
1026 //Update dynamic maps
1033 // void erase(Node nn) {
1036 // while((m=nodes[n].first_in)!=-1) eraseEdge(m);
1037 // while((m=nodes[n].first_out)!=-1) eraseEdge(m);
1040 void erase(Edge e) { eraseEdge(e.n); }
1042 ///Clear all edges. (Doesn't clear the nodes!)
1052 friend class EdgeSet;
1053 template <typename T> friend class EdgeMap;
1056 friend class NodeIt;
1058 ///\bug It should be at least protected
1062 friend int EdgeSet::id(Edge e) const;
1064 Edge(int nn) {n=nn;}
1067 Edge (Invalid) { n=-1; }
1068 bool operator==(const Edge i) const {return n==i.n;}
1069 bool operator!=(const Edge i) const {return n!=i.n;}
1070 bool operator<(const Edge i) const {return n<i.n;}
1071 ///\bug This is a workaround until somebody tells me how to
1072 ///make class \c SymEdgeSet::SymEdgeMap friend of Edge
1073 int &idref() {return n;}
1074 const int &idref() const {return n;}
1077 class EdgeIt : public Edge {
1078 friend class EdgeSet;
1079 template <typename T> friend class EdgeMap;
1083 EdgeIt(const EdgeSet& _G) : Edge(), G(&_G) {
1084 // typename NodeGraphType::Node m;
1087 m!=INVALID && G->nodes[m].first_in == -1; ++m);
1088 ///\bug AJJAJ! This is a non sense!!!!!!!
1089 this->n = m!=INVALID?-1:G->nodes[m].first_in;
1091 EdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { }
1092 EdgeIt (Invalid i) : Edge(i) { }
1093 EdgeIt() : Edge() { }
1096 ///\bug UNIMPLEMENTED!!!!!
1098 EdgeIt &operator++() {
1101 ///\bug This is a workaround until somebody tells me how to
1102 ///make class \c SymEdgeSet::SymEdgeMap friend of Edge
1103 int &idref() {return this->n;}
1106 class OutEdgeIt : public Edge {
1108 friend class EdgeSet;
1110 OutEdgeIt() : Edge() { }
1111 OutEdgeIt (Invalid i) : Edge(i) { }
1112 OutEdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { }
1114 OutEdgeIt(const EdgeSet& _G,const Node v) :
1115 Edge(_G.nodes[v].first_out), G(&_G) { }
1116 OutEdgeIt &operator++() {
1117 Edge::n = G->edges[Edge::n].next_out;
1122 class InEdgeIt : public Edge {
1124 friend class EdgeSet;
1126 InEdgeIt() : Edge() { }
1127 InEdgeIt (Invalid i) : Edge(i) { }
1128 InEdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { }
1129 InEdgeIt(const EdgeSet& _G,Node v)
1130 : Edge(_G.nodes[v].first_in), G(&_G) { }
1131 InEdgeIt &operator++() {
1132 Edge::n = G->edges[Edge::n].next_in;
1139 template<typename GG>
1140 inline int EdgeSet<GG>::id(Node v) const { return G.id(v); }
1146 #endif //HUGO_LIST_GRAPH_H