src/work/list_graph.h
author alpar
Mon, 26 Apr 2004 09:00:12 +0000
changeset 406 e8377ac921b6
parent 368 0beed7a49063
child 409 7ab7f083760a
permissions -rw-r--r--
Docs are now divided into modules.
     1 // -*- c++ -*-
     2 #ifndef HUGO_LIST_GRAPH_H
     3 #define HUGO_LIST_GRAPH_H
     4 
     5 #include <iostream>
     6 #include <vector>
     7 
     8 #include <invalid.h>
     9 
    10 namespace hugo {
    11 
    12   template <typename It>
    13   int count(It it) { 
    14     int i=0;
    15     for( ; it.valid(); ++it) { ++i; } 
    16     return i;
    17   }
    18 
    19   class ListGraph {
    20     class node_item;
    21     class edge_item;
    22   public:
    23     class Node;
    24     class NodeIt;
    25     class Edge;
    26     class EdgeIt;
    27     class OutEdgeIt;
    28     class InEdgeIt;
    29     class SymEdgeIt;
    30     template <typename T> class NodeMap;
    31     template <typename T> class EdgeMap;
    32 //  private:
    33     template <typename T> friend class NodeMap;
    34     template <typename T> friend class EdgeMap;
    35  
    36     template <typename T>
    37     class NodeMap {
    38       const ListGraph& G; 
    39       std::vector<T> container;
    40     public:
    41       typedef T ValueType;
    42       typedef Node KeyType;
    43       NodeMap(const ListGraph& _G) : G(_G), container(G.node_id) { }
    44       NodeMap(const ListGraph& _G, T a) : 
    45 	G(_G), container(G.node_id, a) { }
    46       void set(Node n, T a) { container[/*G.id(n)*/n.node->id]=a; }
    47 //      T get(Node n) const { return container[/*G.id(n)*/n.node->id]; }
    48       typename std::vector<T>::reference operator[](Node n) { 
    49 	return container[/*G.id(n)*/n.node->id]; }
    50       typename std::vector<T>::const_reference operator[](Node n) const { 
    51 	return container[/*G.id(n)*/n.node->id]; 
    52       }
    53       void update() { container.resize(G.node_id); }
    54       void update(T a) { container.resize(G.node_id, a); }
    55     };
    56 
    57     template <typename T>
    58     class EdgeMap {
    59       const ListGraph& G; 
    60       std::vector<T> container;
    61     public:
    62       typedef T ValueType;
    63       typedef Edge KeyType;
    64       EdgeMap(const ListGraph& _G) : G(_G), container(G.edge_id) { }
    65       EdgeMap(const ListGraph& _G, T a) : 
    66 	G(_G), container(G.edge_id, a) { }
    67       void set(Edge e, T a) { container[/*G.id(e)*/e.edge->id]=a; }
    68 //      T get(Edge e) const { return container[/*G.id(e)*/e.edge->id]; }
    69       typename std::vector<T>::reference operator[](Edge e) { 
    70 	return container[/*G.id(e)*/e.edge->id]; } 
    71       typename std::vector<T>::const_reference operator[](Edge e) const { 
    72 	return container[/*G.id(e)*/e.edge->id]; 
    73       } 
    74       void update() { container.resize(G.edge_id); }
    75       void update(T a) { container.resize(G.edge_id, a); }
    76     };
    77 
    78   private:
    79     int node_id;
    80     int edge_id;
    81     int _node_num;
    82     int _edge_num;
    83 
    84     node_item* _first_node;
    85     node_item* _last_node;
    86 
    87     class node_item {
    88       friend class ListGraph;
    89       template <typename T> friend class NodeMap;
    90       
    91       friend class Node;
    92       friend class NodeIt;
    93       friend class Edge;
    94       friend class EdgeIt;
    95       friend class OutEdgeIt;
    96       friend class InEdgeIt;
    97       friend class SymEdgeIt;
    98       friend std::ostream& operator<<(std::ostream& os, const Node& i);
    99       friend std::ostream& operator<<(std::ostream& os, const Edge& i);
   100       //ListGraph* G;
   101       int id;
   102       edge_item* _first_out_edge;
   103       edge_item* _last_out_edge;
   104       edge_item* _first_in_edge;
   105       edge_item* _last_in_edge;
   106       node_item* _next_node;
   107       node_item* _prev_node;
   108     public:
   109       node_item() { }
   110     };
   111 
   112     class edge_item {
   113       friend class ListGraph;
   114       template <typename T> friend class EdgeMap;
   115 
   116       friend class Node;
   117       friend class NodeIt;
   118       friend class Edge;
   119       friend class EdgeIt;
   120       friend class OutEdgeIt;
   121       friend class InEdgeIt;
   122       friend class SymEdgeIt;
   123       friend std::ostream& operator<<(std::ostream& os, const Edge& i);
   124       //ListGraph* G;
   125       int id;
   126       node_item* _tail;
   127       node_item* _head;
   128       edge_item* _next_out;
   129       edge_item* _prev_out;
   130       edge_item* _next_in;
   131       edge_item* _prev_in;
   132     public:
   133       edge_item() { }
   134     };
   135 
   136     node_item* _add_node() { 
   137       node_item* p=new node_item;
   138       p->id=node_id++;
   139       p->_first_out_edge=0;
   140       p->_last_out_edge=0;
   141       p->_first_in_edge=0;
   142       p->_last_in_edge=0;
   143       p->_prev_node=_last_node;
   144       p->_next_node=0;
   145       if (_last_node) _last_node->_next_node=p;
   146       _last_node=p;
   147       if (!_first_node) _first_node=p;
   148 
   149       ++_node_num;
   150       return p;
   151     }
   152 
   153     edge_item* _add_edge(node_item* _tail, node_item* _head) {
   154       edge_item* e=new edge_item;
   155       e->id=edge_id++;
   156       e->_tail=_tail;
   157       e->_head=_head;
   158       
   159       e->_prev_out=_tail->_last_out_edge;
   160       if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
   161       _tail->_last_out_edge=e;
   162       if (!_tail->_first_out_edge) _tail->_first_out_edge=e; 
   163       e->_next_out=0;
   164  
   165       e->_prev_in=_head->_last_in_edge;
   166       if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
   167       _head->_last_in_edge=e;
   168       if (!_head->_first_in_edge) { _head->_first_in_edge=e; } 
   169       e->_next_in=0;
   170 
   171       ++_edge_num;
   172       return e;
   173     }
   174 
   175     //deletes a node which has no out edge and no in edge
   176     void _delete_node(node_item* v) {
   177       if (v->_next_node) (v->_next_node)->_prev_node=v->_prev_node; else 
   178 	_last_node=v->_prev_node;
   179       if (v->_prev_node) (v->_prev_node)->_next_node=v->_next_node; else 
   180 	_first_node=v->_next_node;
   181 
   182       delete v;
   183       --_node_num;
   184     }
   185 
   186     void _delete_edge(edge_item* e) {
   187       if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else 
   188 	(e->_tail)->_last_out_edge=e->_prev_out;
   189       if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else 
   190 	(e->_tail)->_first_out_edge=e->_next_out;
   191       if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else 
   192 	(e->_head)->_last_in_edge=e->_prev_in;
   193       if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else 
   194 	(e->_head)->_first_in_edge=e->_next_in;
   195 
   196       delete e;
   197       --_edge_num;
   198     }
   199 
   200     void _set_tail(edge_item* e, node_item* _tail) {
   201       if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else 
   202 	(e->_tail)->_last_out_edge=e->_prev_out;
   203       if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else 
   204 	(e->_tail)->_first_out_edge=e->_next_out;
   205       
   206       e->_tail=_tail;
   207       
   208       e->_prev_out=_tail->_last_out_edge;
   209       if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e;
   210       _tail->_last_out_edge=e;
   211       if (!_tail->_first_out_edge) _tail->_first_out_edge=e; 
   212       e->_next_out=0;
   213     }
   214 
   215     void _set_head(edge_item* e, node_item* _head) {
   216       if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else 
   217 	(e->_head)->_last_in_edge=e->_prev_in;
   218       if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else 
   219 	(e->_head)->_first_in_edge=e->_next_in;
   220       
   221       e->_head=_head;
   222       
   223       e->_prev_in=_head->_last_in_edge;
   224       if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e;
   225       _head->_last_in_edge=e;
   226       if (!_head->_first_in_edge) { _head->_first_in_edge=e; } 
   227       e->_next_in=0;
   228     }
   229 
   230   public:
   231 
   232     /* default constructor */
   233 
   234     ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0) { }
   235     
   236     ~ListGraph() { 
   237       NodeIt n;
   238       while (this->valid(first(n))) erase(n);
   239       //while (first<NodeIt>().valid()) erase(first<NodeIt>());
   240     }
   241 
   242     int nodeNum() const { return _node_num; }
   243     int edgeNum() const { return _edge_num; }
   244 
   245     /* functions to construct iterators from the graph, or from each other */
   246 
   247     //NodeIt firstNode() const { return NodeIt(*this); }
   248     //EdgeIt firstEdge() const { return EdgeIt(*this); }
   249     
   250     //OutEdgeIt firstOutEdge(const Node v) const { return OutEdgeIt(v); }
   251     //InEdgeIt firstInEdge(const Node v) const { return InEdgeIt(v); }
   252     //SymEdgeIt firstSymEdge(const Node v) const { return SymEdgeIt(v); }
   253     Node tail(Edge e) const { return e.tailNode(); }
   254     Node head(Edge e) const { return e.headNode(); }
   255 
   256     Node aNode(const OutEdgeIt& e) const { return e.aNode(); }
   257     Node aNode(const InEdgeIt& e) const { return e.aNode(); }
   258     Node aNode(const SymEdgeIt& e) const { return e.aNode(); }
   259 
   260     Node bNode(const OutEdgeIt& e) const { return e.bNode(); }
   261     Node bNode(const InEdgeIt& e) const { return e.bNode(); }
   262     Node bNode(const SymEdgeIt& e) const { return e.bNode(); }
   263 
   264     //Node invalid_node() { return Node(); }
   265     //Edge invalid_edge() { return Edge(); }
   266     //OutEdgeIt invalid_out_edge() { return OutEdgeIt(); }
   267     //InEdgeIt invalid_in_edge() { return InEdgeIt(); }
   268     //SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); }
   269 
   270     /* same methods in other style */
   271     /* for experimental purpose */
   272 
   273     NodeIt& first(NodeIt& v) const { 
   274       v=NodeIt(*this); return v; }
   275     EdgeIt& first(EdgeIt& e) const { 
   276       e=EdgeIt(*this); return e; }
   277     OutEdgeIt& first(OutEdgeIt& e, Node v) const { 
   278       e=OutEdgeIt(*this, v); return e; }
   279     InEdgeIt& first(InEdgeIt& e, Node v) const { 
   280       e=InEdgeIt(*this, v); return e; }
   281     SymEdgeIt& first(SymEdgeIt& e, Node v) const { 
   282       e=SymEdgeIt(*this, v); return e; }
   283     //void getTail(Node& n, const Edge& e) const { n=tail(e); }
   284     //void getHead(Node& n, const Edge& e) const { n=head(e); }
   285 
   286     //void getANode(Node& n, const OutEdgeIt& e) const { n=e.aNode(); }
   287     //void getANode(Node& n, const InEdgeIt& e) const { n=e.aNode(); }
   288     //void getANode(Node& n, const SymEdgeIt& e) const { n=e.aNode(); }
   289     //void getBNode(Node& n, const OutEdgeIt& e) const { n=e.bNode(); }
   290     //void getBNode(Node& n, const InEdgeIt& e) const { n=e.bNode(); }
   291     //void getBNode(Node& n, const SymEdgeIt& e) const { n=e.bNode(); }
   292     //void get_invalid(Node& n) { n=Node(); }
   293     //void get_invalid(Edge& e) { e=Edge(); }
   294     //void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); }
   295     //void get_invalid(InEdgeIt& e) { e=InEdgeIt(); }
   296     //void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); }
   297 
   298 //     template< typename It >
   299 //     It first() const { 
   300 //       It e;
   301 //       first(e);
   302 //       return e; 
   303 //     }
   304 
   305 //     template< typename It >
   306 //     It first(Node v) const { 
   307 //       It e;
   308 //       first(e, v);
   309 //       return e; 
   310 //     }
   311 
   312     bool valid(Node n) const { return n.valid(); }
   313     bool valid(Edge e) const { return e.valid(); }
   314     
   315 //    template <typename It> It getNext(It it) const { 
   316 //      It tmp(it); next(tmp); return tmp; }
   317 //     NodeIt& next(NodeIt& it) const { return ++it; }
   318 //     EdgeIt& next(EdgeIt& it) const { return ++it; }
   319 //     OutEdgeIt& next(OutEdgeIt& it) const { return ++it; }
   320 //     InEdgeIt& next(InEdgeIt& it) const { return ++it; }
   321 //     SymEdgeIt& next(SymEdgeIt& it) const { return ++it; }
   322 //    template <typename It> It& next(It& it) const { return ++it; }
   323     template <typename It> It& next(It& it) const { ++it; return it; }
   324    
   325 
   326     /* for getting id's of graph objects */
   327     /* these are important for the implementation of property vectors */
   328 
   329     int id(Node v) const { return v.node->id; }
   330     int id(Edge e) const { return e.edge->id; }
   331 
   332     /* adding nodes and edges */
   333 
   334     Node addNode() { return Node(_add_node()); }
   335     Edge addEdge(Node u, Node v) {
   336       return Edge(_add_edge(u.node, v.node)); 
   337     }
   338 
   339     void erase(Node i) { 
   340       { 
   341 	OutEdgeIt e;
   342 	while (this->valid(first(e, i))) erase(e);
   343       }
   344       {
   345 	InEdgeIt e;
   346 	while (this->valid(first(e, i))) erase(e);
   347       }
   348       //while (first<OutEdgeIt>(i).valid()) erase(first<OutEdgeIt>(i));
   349       //while (first<InEdgeIt>(i).valid()) erase(first<InEdgeIt>(i));
   350       _delete_node(i.node); 
   351     }
   352   
   353     void erase(Edge e) { _delete_edge(e.edge); }
   354 
   355     void clear() { 
   356       NodeIt e;
   357       while (this->valid(first(e))) erase(e); 
   358       //while (first<NodeIt>().valid()) erase(first<NodeIt>());
   359     }
   360 
   361     void setTail(Edge e, Node tail) {
   362       _set_tail(e.edge, tail.node); 
   363     }
   364 
   365     void setHead(Edge e, Node head) {
   366       _set_head(e.edge, head.node); 
   367     }
   368 
   369     /* stream operations, for testing purpose */
   370 
   371     friend std::ostream& operator<<(std::ostream& os, const Node& i) { 
   372       os << i.node->id; return os; 
   373     }
   374     friend std::ostream& operator<<(std::ostream& os, const Edge& i) { 
   375       os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")"; 
   376       return os; 
   377     }
   378 
   379     class Node {
   380       friend class ListGraph;
   381       template <typename T> friend class NodeMap;
   382 
   383       friend class Edge;
   384       friend class OutEdgeIt;
   385       friend class InEdgeIt;
   386       friend class SymEdgeIt;
   387       //public:  //FIXME: It is required by op= of NodeIt
   388     protected:
   389       node_item* node;
   390     protected:
   391       friend int ListGraph::id(Node v) const; 
   392     public:
   393       Node() /*: node(0)*/ { }
   394       Node(const Invalid&) : node(0) { }
   395     protected:
   396       Node(node_item* _node) : node(_node) { }
   397       bool valid() const { return (node); }
   398     public:
   399       //void makeInvalid() { node=0; }
   400       friend bool operator==(Node u, Node v) { return v.node==u.node; } 
   401       friend bool operator!=(Node u, Node v) { return v.node!=u.node; } 
   402       friend std::ostream& operator<<(std::ostream& os, const Node& i);
   403     };
   404     
   405     class NodeIt : public Node {
   406       friend class ListGraph;
   407       //protected:
   408     public: //for everybody but marci
   409       NodeIt(const ListGraph& G) : Node(G._first_node) { }
   410     public:
   411       NodeIt() : Node() { }
   412       NodeIt(const Invalid& i) : Node(i) { }
   413     protected:
   414       NodeIt(node_item* v) : Node(v) { }
   415       NodeIt& operator++() { node=node->_next_node; return *this; }
   416       //FIXME::
   417       //      NodeIt& operator=(const Node& e)
   418       //      { node=e.node; return *this; }
   419     };
   420 
   421     class Edge {
   422       friend class ListGraph;
   423       template <typename T> friend class EdgeMap;
   424       
   425       friend class Node;
   426       friend class NodeIt;
   427     protected:
   428       edge_item* edge;
   429       friend int ListGraph::id(Edge e) const;
   430     public:
   431       Edge() /*: edge(0)*/ { }
   432       Edge(const Invalid&) : edge(0) { }
   433       //Edge() { }
   434     protected:
   435       Edge(edge_item* _edge) : edge(_edge) { }
   436       bool valid() const { return (edge); }
   437     public:
   438       //void makeInvalid() { edge=0; }
   439       friend bool operator==(Edge u, Edge v) { return v.edge==u.edge; } 
   440       friend bool operator!=(Edge u, Edge v) { return v.edge!=u.edge; } 
   441     protected:
   442       Node tailNode() const { return Node(edge->_tail); }
   443       Node headNode() const { return Node(edge->_head); }
   444     public:
   445       friend std::ostream& operator<<(std::ostream& os, const Edge& i);
   446     };
   447     
   448     class EdgeIt : public Edge {
   449       friend class ListGraph;
   450       //protected: 
   451     public: //for alpar
   452       EdgeIt(const ListGraph& G) {
   453 	node_item* v=G._first_node;
   454 	if (v) edge=v->_first_out_edge; else edge=0;
   455 	while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
   456       }
   457     public:
   458       EdgeIt() : Edge() { }
   459       EdgeIt(const Invalid& i) : Edge(i) { }
   460     protected:
   461       EdgeIt(edge_item* _e) : Edge(_e) { }
   462       EdgeIt& operator++() { 
   463 	node_item* v=edge->_tail;
   464 	edge=edge->_next_out; 
   465 	while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
   466 	return *this;
   467       }
   468     };
   469     
   470     class OutEdgeIt : public Edge {
   471       friend class ListGraph;
   472       //node_item* v;
   473       //protected: 
   474     protected: //for alpar
   475       OutEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
   476     public:
   477       OutEdgeIt() : Edge()/*, v(0)*/ { }
   478       OutEdgeIt(const Invalid& i) : Edge(i) { }
   479       OutEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
   480     protected:
   481       OutEdgeIt& operator++() { edge=edge->_next_out; return *this; }
   482     protected:
   483       Node aNode() const { return Node(edge->_tail); }
   484       Node bNode() const { return Node(edge->_head); }
   485     };
   486     
   487     class InEdgeIt : public Edge {
   488       friend class ListGraph;
   489       //node_item* v;
   490       //protected:
   491     protected: //for alpar
   492       InEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
   493     public:
   494       InEdgeIt() : Edge()/*, v(0)*/ { }
   495       InEdgeIt(const Invalid& i) : Edge(i) { }
   496       InEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
   497     protected:
   498       InEdgeIt& operator++() { edge=edge->_next_in; return *this; }
   499     protected:
   500       Node aNode() const { return Node(edge->_head); }
   501       Node bNode() const { return Node(edge->_tail); }
   502     };
   503 
   504     class SymEdgeIt : public Edge {
   505       friend class ListGraph;
   506       bool out_or_in; //1 iff out, 0 iff in
   507       //node_item* v;
   508       //protected:
   509     protected: //for alpar
   510       SymEdgeIt(const Node& _v) /*: v(_v.node)*/ { 
   511 	out_or_in=1;
   512 	edge=_v.node->_first_out_edge; 
   513 	if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
   514       }
   515     public:
   516       SymEdgeIt() : Edge() /*, v(0)*/ { }
   517       SymEdgeIt(const Invalid& i) : Edge(i) { }
   518       SymEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { 
   519 	out_or_in=1;
   520 	edge=_v.node->_first_out_edge; 
   521 	if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
   522       }
   523     protected:
   524       SymEdgeIt& operator++() { 
   525 	if (out_or_in) { 
   526 	  node_item* v=edge->_tail;
   527 	  edge=edge->_next_out; 
   528 	  if (!edge) { out_or_in=0; edge=v->_first_in_edge; }
   529 	} else {
   530 	  edge=edge->_next_in; 
   531 	}
   532 	return *this;
   533       }
   534     protected:
   535       Node aNode() const { 
   536 	return (out_or_in) ? Node(edge->_tail) : Node(edge->_head); }
   537       Node bNode() const { 
   538 	return (out_or_in) ? Node(edge->_head) : Node(edge->_tail); }
   539     };
   540   };
   541 
   542   class UndirListGraph : public ListGraph {
   543   public:
   544     typedef SymEdgeIt OutEdgeIt;
   545     typedef SymEdgeIt InEdgeIt;
   546   };
   547 
   548 } //namespace hugo
   549 
   550 #endif //HUGO_LIST_GRAPH_H