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_factory.h>
18 #include <hugo/sym_map_factory.h>
20 #include <hugo/map_defines.h>
25 /// \addtogroup graphs
28 // class SymListGraph;
30 ///A list graph class.
32 ///This is a simple and fast erasable graph implementation.
34 ///It conforms to the graph interface documented under
35 ///the description of \ref GraphSkeleton.
36 ///\sa \ref GraphSkeleton.
39 //Nodes are double linked.
40 //The free nodes are only single linked using the "next" field.
43 int first_in,first_out;
47 //Edges are double linked.
48 //The free edges are only single linked using the "next_in" field.
52 int prev_in, prev_out;
53 int next_in, next_out;
54 //FIXME: is this necessary?
55 // EdgeT() : next_in(-1), next_out(-1) prev_in(-1), prev_out(-1) {}
58 std::vector<NodeT> nodes;
63 std::vector<EdgeT> edges;
69 typedef ListGraph Graph;
82 CREATE_MAP_REGISTRIES;
83 CREATE_MAPS(DefaultMapFactory);
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 ///\todo Undocumented conversion Node -\> NodeIt.
315 NodeIt(const ListGraph& _G,Node n) : Node(n), G(&_G) { }
316 NodeIt &operator++() {
321 // operator bool() { return Node::operator bool(); }
325 friend class ListGraph;
326 template <typename T> friend class EdgeMap;
328 //template <typename T> friend class SymListGraph::SymEdgeMap;
329 //friend Edge SymListGraph::opposite(Edge) const;
335 friend int ListGraph::id(Edge e);
338 /// An Edge with id \c n.
340 /// \bug It should be
341 /// obtained by a member function of the Graph.
345 Edge (Invalid) { n=-1; }
346 bool operator==(const Edge i) const {return n==i.n;}
347 bool operator!=(const Edge i) const {return n!=i.n;}
348 bool operator<(const Edge i) const {return n<i.n;}
349 ///\bug This is a workaround until somebody tells me how to
350 ///make class \c SymListGraph::SymEdgeMap friend of Edge
351 int &idref() {return n;}
352 const int &idref() const {return n;}
354 // operator bool() { return n!=-1; }
357 class EdgeIt : public Edge {
359 friend class ListGraph;
361 EdgeIt(const ListGraph& _G) : Edge(), G(&_G) {
364 m!=-1 && _G.nodes[m].first_in == -1; m = _G.nodes[m].next);
365 n = (m==-1)?-1:_G.nodes[m].first_in;
367 EdgeIt (Invalid i) : Edge(i) { }
368 EdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { }
369 EdgeIt() : Edge() { }
370 ///\bug This is a workaround until somebody tells me how to
371 ///make class \c SymListGraph::SymEdgeMap friend of Edge
372 int &idref() {return n;}
373 EdgeIt &operator++() {
374 if(G->edges[n].next_in!=-1) n=G->edges[n].next_in;
377 for(nn=G->nodes[G->edges[n].head].next;
378 nn!=-1 && G->nodes[nn].first_in == -1;
379 nn = G->nodes[nn].next) ;
380 n = (nn==-1)?-1:G->nodes[nn].first_in;
385 // operator bool() { return Edge::operator bool(); }
388 class OutEdgeIt : public Edge {
390 friend class ListGraph;
392 OutEdgeIt() : Edge() { }
393 OutEdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { }
394 OutEdgeIt (Invalid i) : Edge(i) { }
396 OutEdgeIt(const ListGraph& _G,const Node v)
397 : Edge(_G.nodes[v.n].first_out), G(&_G) {}
398 OutEdgeIt &operator++() { n=G->edges[n].next_out; return *this; }
400 // operator bool() { return Edge::operator bool(); }
403 class InEdgeIt : public Edge {
405 friend class ListGraph;
407 InEdgeIt() : Edge() { }
408 InEdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { }
409 InEdgeIt (Invalid i) : Edge(i) { }
410 InEdgeIt(const ListGraph& _G,Node v)
411 : Edge(_G.nodes[v.n].first_in), G(&_G) { }
412 InEdgeIt &operator++() { n=G->edges[n].next_in; return *this; }
414 // operator bool() { return Edge::operator bool(); }
418 ///Graph for bidirectional edges.
420 ///The purpose of this graph structure is to handle graphs
421 ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair
422 ///of oppositely directed edges.
423 ///There is a new edge map type called
424 ///\ref SymListGraph::SymEdgeMap "SymEdgeMap"
425 ///that complements this
427 ///storing shared values for the edge pairs. The usual
428 ///\ref GraphSkeleton::EdgeMap "EdgeMap"
432 ///The oppositely directed edge can also be obtained easily
433 ///using \ref opposite.
435 ///Here erase(Edge) deletes a pair of edges.
437 ///\todo this date structure need some reconsiderations. Maybe it
438 ///should be implemented independently from ListGraph.
440 class SymListGraph : public ListGraph
444 typedef SymListGraph Graph;
446 KEEP_NODE_MAP(ListGraph);
447 KEEP_EDGE_MAP(ListGraph);
449 CREATE_SYM_EDGE_MAP_REGISTRY;
450 CREATE_SYM_EDGE_MAP_FACTORY(DefaultMapFactory);
451 IMPORT_SYM_EDGE_MAP(SymEdgeMapFactory);
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.
495 ///It conforms to the graph interface documented under
496 ///the description of \ref GraphSkeleton with the exception that you cannot
497 ///add (or delete) edges. The usual edge iterators are exists, but they are
498 ///always \ref INVALID.
499 ///\sa \ref GraphSkeleton
503 //Nodes are double linked.
504 //The free nodes are only single linked using the "next" field.
507 int first_in,first_out;
512 std::vector<NodeT> nodes;
515 //The first free node
520 typedef NodeSet Graph;
532 CREATE_MAP_REGISTRIES;
533 CREATE_MAPS(DefaultMapFactory);
537 ///Default constructor
539 : nodes(), first_node(-1), first_free_node(-1) {}
541 NodeSet(const NodeSet &_g)
542 : nodes(_g.nodes), first_node(_g.first_node),
543 first_free_node(_g.first_free_node) {}
546 int nodeNum() const { return nodes.size(); }
548 int edgeNum() const { return 0; }
554 int maxNodeId() const { return nodes.size()-1; }
559 int maxEdgeId() const { return 0; }
561 Node tail(Edge e) const { return INVALID; }
562 Node head(Edge e) const { return INVALID; }
564 NodeIt& first(NodeIt& v) const {
565 v=NodeIt(*this); return v; }
566 EdgeIt& first(EdgeIt& e) const {
567 e=EdgeIt(*this); return e; }
568 OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
569 e=OutEdgeIt(*this,v); return e; }
570 InEdgeIt& first(InEdgeIt& e, const Node v) const {
571 e=InEdgeIt(*this,v); return e; }
575 /// The ID of a valid Node is a nonnegative integer not greater than
576 /// \ref maxNodeId(). The range of the ID's is not surely continuous
577 /// and the greatest node ID can be actually less then \ref maxNodeId().
579 /// The ID of the \ref INVALID node is -1.
580 ///\return The ID of the node \c v.
581 int id(Node v) const { return v.n; }
584 /// The ID of a valid Edge is a nonnegative integer not greater than
585 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
586 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
588 /// The ID of the \ref INVALID edge is -1.
589 ///\return The ID of the edge \c e.
590 int id(Edge e) const { return -1; }
592 /// Adds a new node to the graph.
594 /// \warning It adds the new node to the front of the list.
595 /// (i.e. the lastly added node becomes the first.)
599 if(first_free_node==-1)
602 nodes.push_back(NodeT());
606 first_free_node = nodes[n].next;
609 nodes[n].next = first_node;
610 if(first_node != -1) nodes[first_node].prev = n;
614 nodes[n].first_in = nodes[n].first_out = -1;
618 //Update dynamic maps
624 void erase(Node nn) {
627 if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev;
628 if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next;
629 else first_node = nodes[n].next;
631 nodes[n].next = first_free_node;
634 //Update dynamic maps
639 Edge findEdge(Node u,Node v, Edge prev = INVALID)
647 first_node = first_free_node = -1;
651 friend class NodeSet;
652 template <typename T> friend class NodeMap;
655 friend class OutEdgeIt;
656 friend class InEdgeIt;
660 friend int NodeSet::id(Node v) const;
664 Node (Invalid i) { n=-1; }
665 bool operator==(const Node i) const {return n==i.n;}
666 bool operator!=(const Node i) const {return n!=i.n;}
667 bool operator<(const Node i) const {return n<i.n;}
670 class NodeIt : public Node {
672 friend class NodeSet;
674 NodeIt() : Node() { }
675 NodeIt(const NodeSet& _G,Node n) : Node(n), G(&_G) { }
676 NodeIt(Invalid i) : Node(i) { }
677 NodeIt(const NodeSet& _G) : Node(_G.first_node), G(&_G) { }
678 NodeIt &operator++() {
685 //friend class NodeSet;
686 //template <typename T> friend class EdgeMap;
688 //template <typename T> friend class SymNodeSet::SymEdgeMap;
689 //friend Edge SymNodeSet::opposite(Edge) const;
691 // friend class Node;
692 // friend class NodeIt;
694 //friend int NodeSet::id(Edge e) const;
699 bool operator==(const Edge i) const {return true;}
700 bool operator!=(const Edge i) const {return false;}
701 bool operator<(const Edge i) const {return false;}
702 ///\bug This is a workaround until somebody tells me how to
703 ///make class \c SymNodeSet::SymEdgeMap friend of Edge
704 // int idref() {return -1;}
705 // int idref() const {return -1;}
708 class EdgeIt : public Edge {
709 //friend class NodeSet;
711 EdgeIt(const NodeSet& G) : Edge() { }
712 EdgeIt(const NodeSet&, Edge) : Edge() { }
713 EdgeIt (Invalid i) : Edge(i) { }
714 EdgeIt() : Edge() { }
715 ///\bug This is a workaround until somebody tells me how to
716 ///make class \c SymNodeSet::SymEdgeMap friend of Edge
717 // int idref() {return -1;}
718 EdgeIt operator++() { return INVALID; }
721 class OutEdgeIt : public Edge {
722 friend class NodeSet;
724 OutEdgeIt() : Edge() { }
725 OutEdgeIt(const NodeSet&, Edge) : Edge() { }
726 OutEdgeIt (Invalid i) : Edge(i) { }
727 OutEdgeIt(const NodeSet& G,const Node v) : Edge() {}
728 OutEdgeIt operator++() { return INVALID; }
731 class InEdgeIt : public Edge {
732 friend class NodeSet;
734 InEdgeIt() : Edge() { }
735 InEdgeIt(const NodeSet&, Edge) : Edge() { }
736 InEdgeIt (Invalid i) : Edge(i) { }
737 InEdgeIt(const NodeSet& G,Node v) :Edge() {}
738 InEdgeIt operator++() { return INVALID; }
745 ///Graph structure using a node set of another graph.
747 ///This structure can be used to establish another graph over a node set
748 /// of an existing one. The node iterator will go through the nodes of the
749 /// original graph, and the NodeMap's of both graphs will convert to
752 ///\warning Adding or deleting nodes from the graph is not safe if an
753 ///\ref EdgeSet is currently attached to it!
755 ///\todo Make it possible to add/delete edges from the base graph
756 ///(and from \ref EdgeSet, as well)
758 ///\param GG The type of the graph which shares its node set with this class.
759 ///Its interface must conform with \ref GraphSkeleton.
761 ///It conforms to the graph interface documented under
762 ///the description of \ref GraphSkeleton.
763 ///\sa \ref GraphSkeleton.
765 template<typename GG>
768 typedef GG NodeGraphType;
780 typedef EdgeSet Graph;
782 int id(Node v) const;
784 class Node : public NodeGraphType::Node {
785 friend class EdgeSet;
786 // template <typename T> friend class NodeMap;
789 friend class OutEdgeIt;
790 friend class InEdgeIt;
791 friend class SymEdge;
794 friend int EdgeSet::id(Node v) const;
795 // Node(int nn) {n=nn;}
797 Node() : NodeGraphType::Node() {}
798 Node (Invalid i) : NodeGraphType::Node(i) {}
799 Node(const typename NodeGraphType::Node &n) : NodeGraphType::Node(n) {}
802 class NodeIt : public NodeGraphType::NodeIt {
803 friend class EdgeSet;
805 NodeIt() : NodeGraphType::NodeIt() { }
806 NodeIt(const EdgeSet& _G,Node n) : NodeGraphType::NodeIt(_G.G,n) { }
807 NodeIt (Invalid i) : NodeGraphType::NodeIt(i) {}
808 NodeIt(const EdgeSet& _G) : NodeGraphType::NodeIt(_G.G) { }
809 NodeIt(const typename NodeGraphType::NodeIt &n)
810 : NodeGraphType::NodeIt(n) {}
812 operator Node() { return Node(*this);}
814 { this->NodeGraphType::NodeIt::operator++(); return *this;}
818 //Edges are double linked.
819 //The free edges are only single linked using the "next_in" field.
822 int first_in,first_out;
823 NodeT() : first_in(-1), first_out(-1) { }
829 int prev_in, prev_out;
830 int next_in, next_out;
834 typename NodeGraphType::template NodeMap<NodeT> nodes;
836 std::vector<EdgeT> edges;
837 //The first free edge
851 CREATE_EDGE_MAP_REGISTRY;
852 CREATE_EDGE_MAP_FACTORY(DefaultMapFactory);
853 IMPORT_EDGE_MAP(EdgeMapFactory);
860 ///Construates a new graph based on the nodeset of an existing one.
861 ///\param _G the base graph.
862 ///\todo It looks like a copy constructor, but it isn't.
863 EdgeSet(NodeGraphType &_G)
864 : G(_G), nodes(_G), edges(),
865 first_free_edge(-1) {}
868 ///Makes a copy of an EdgeSet.
869 ///It will be based on the same graph.
870 EdgeSet(const EdgeSet &_g)
871 : G(_g.G), nodes(_g.G), edges(_g.edges),
872 first_free_edge(_g.first_free_edge) {}
875 int nodeNum() const { return G.nodeNum(); }
877 int edgeNum() const { return edges.size(); }
883 int maxNodeId() const { return G.maxNodeId(); }
888 int maxEdgeId() const { return edges.size()-1; }
890 Node tail(Edge e) const { return edges[e.n].tail; }
891 Node head(Edge e) const { return edges[e.n].head; }
893 NodeIt& first(NodeIt& v) const {
894 v=NodeIt(*this); return v; }
895 EdgeIt& first(EdgeIt& e) const {
896 e=EdgeIt(*this); return e; }
897 OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
898 e=OutEdgeIt(*this,v); return e; }
899 InEdgeIt& first(InEdgeIt& e, const Node v) const {
900 e=InEdgeIt(*this,v); return e; }
904 /// The ID of a valid Node is a nonnegative integer not greater than
905 /// \ref maxNodeId(). The range of the ID's is not surely continuous
906 /// and the greatest node ID can be actually less then \ref maxNodeId().
908 /// The ID of the \ref INVALID node is -1.
909 ///\return The ID of the node \c v.
910 int id(Node v) { return G.id(v); }
913 /// The ID of a valid Edge is a nonnegative integer not greater than
914 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
915 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
917 /// The ID of the \ref INVALID edge is -1.
918 ///\return The ID of the edge \c e.
919 int id(Edge e) const { return e.n; }
921 /// Adds a new node to the graph.
922 Node addNode() { return G.addNode(); }
924 Edge addEdge(Node u, Node v) {
927 if(first_free_edge==-1)
930 edges.push_back(EdgeT());
934 first_free_edge = edges[n].next_in;
937 edges[n].tail = u; edges[n].head = v;
939 edges[n].next_out = nodes[u].first_out;
940 if(nodes[u].first_out != -1) edges[nodes[u].first_out].prev_out = n;
941 edges[n].next_in = nodes[v].first_in;
942 if(nodes[v].first_in != -1) edges[nodes[v].first_in].prev_in = n;
943 edges[n].prev_in = edges[n].prev_out = -1;
945 nodes[u].first_out = nodes[v].first_in = n;
949 //Update dynamic maps
955 /// Finds an edge between two nodes.
957 /// Finds an edge from node \c u to node \c v.
959 /// If \c prev is \ref INVALID (this is the default value), then
960 /// It finds the first edge from \c u to \c v. Otherwise it looks for
961 /// the next edge from \c u to \c v after \c prev.
962 /// \return The found edge or INVALID if there is no such an edge.
963 Edge findEdge(Node u,Node v, Edge prev = INVALID)
965 int e = (prev.n==-1)? nodes[u].first_out : edges[prev.n].next_out;
966 while(e!=-1 && edges[e].tail!=v) e = edges[e].next_out;
972 void eraseEdge(int n) {
974 if(edges[n].next_in!=-1)
975 edges[edges[n].next_in].prev_in = edges[n].prev_in;
976 if(edges[n].prev_in!=-1)
977 edges[edges[n].prev_in].next_in = edges[n].next_in;
978 else nodes[edges[n].head].first_in = edges[n].next_in;
980 if(edges[n].next_out!=-1)
981 edges[edges[n].next_out].prev_out = edges[n].prev_out;
982 if(edges[n].prev_out!=-1)
983 edges[edges[n].prev_out].next_out = edges[n].next_out;
984 else nodes[edges[n].tail].first_out = edges[n].next_out;
986 edges[n].next_in = first_free_edge;
987 first_free_edge = -1;
989 //Update dynamic maps
996 // void erase(Node nn) {
999 // while((m=nodes[n].first_in)!=-1) eraseEdge(m);
1000 // while((m=nodes[n].first_out)!=-1) eraseEdge(m);
1003 void erase(Edge e) { eraseEdge(e.n); }
1005 ///Clear all edges. (Doesn't clear the nodes!)
1015 friend class EdgeSet;
1016 template <typename T> friend class EdgeMap;
1019 friend class NodeIt;
1021 ///\bug It should be at least protected
1025 friend int EdgeSet::id(Edge e) const;
1027 Edge(int nn) {n=nn;}
1030 Edge (Invalid) { n=-1; }
1031 bool operator==(const Edge i) const {return n==i.n;}
1032 bool operator!=(const Edge i) const {return n!=i.n;}
1033 bool operator<(const Edge i) const {return n<i.n;}
1034 ///\bug This is a workaround until somebody tells me how to
1035 ///make class \c SymEdgeSet::SymEdgeMap friend of Edge
1036 int &idref() {return n;}
1037 const int &idref() const {return n;}
1040 class EdgeIt : public Edge {
1041 friend class EdgeSet;
1042 template <typename T> friend class EdgeMap;
1046 EdgeIt(const EdgeSet& _G) : Edge(), G(&_G) {
1047 // typename NodeGraphType::Node m;
1050 m!=INVALID && G->nodes[m].first_in == -1; ++m);
1051 ///\bug AJJAJ! This is a non sense!!!!!!!
1052 this->n = m!=INVALID?-1:G->nodes[m].first_in;
1054 EdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { }
1055 EdgeIt (Invalid i) : Edge(i) { }
1056 EdgeIt() : Edge() { }
1059 ///\bug UNIMPLEMENTED!!!!!
1061 EdgeIt &operator++() {
1064 ///\bug This is a workaround until somebody tells me how to
1065 ///make class \c SymEdgeSet::SymEdgeMap friend of Edge
1066 int &idref() {return this->n;}
1069 class OutEdgeIt : public Edge {
1071 friend class EdgeSet;
1073 OutEdgeIt() : Edge() { }
1074 OutEdgeIt (Invalid i) : Edge(i) { }
1075 OutEdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { }
1077 OutEdgeIt(const EdgeSet& _G,const Node v) :
1078 Edge(_G.nodes[v].first_out), G(&_G) { }
1079 OutEdgeIt &operator++() { n = G->edges[n].next_out; return *this; }
1082 class InEdgeIt : public Edge {
1084 friend class EdgeSet;
1086 InEdgeIt() : Edge() { }
1087 InEdgeIt (Invalid i) : Edge(i) { }
1088 InEdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { }
1089 InEdgeIt(const EdgeSet& _G,Node v)
1090 : Edge(_G.nodes[v].first_in), G(&_G) { }
1091 InEdgeIt &operator++() { n=G->edges[n].next_in; return *this; }
1095 template <typename V> class NodeMap
1096 : public NodeGraphType::template NodeMap<V>
1098 //This is a must, the constructors need it.
1099 typedef typename NodeGraphType::template NodeMap<V> MapImpl;
1102 NodeMap() : MapImpl() {}
1104 NodeMap(const EdgeSet& graph)
1105 : MapImpl(graph.G) { }
1107 NodeMap(const EdgeSet& graph, const Value& value)
1108 : MapImpl(graph.G, value) { }
1110 NodeMap(const NodeMap& copy)
1111 : MapImpl(static_cast<const MapImpl&>(copy)) {}
1113 template<typename CMap>
1114 NodeMap(const CMap& copy)
1117 NodeMap& operator=(const NodeMap& copy) {
1118 MapImpl::operator=(static_cast<const MapImpl&>(copy));
1122 template <typename CMap>
1123 NodeMap& operator=(const CMap& copy) {
1124 MapImpl::operator=(copy);
1131 template<typename GG>
1132 inline int EdgeSet<GG>::id(Node v) const { return G.id(v); }
1138 #endif //HUGO_LIST_GRAPH_H