src/work/list_graph.h
author marci
Mon, 26 Apr 2004 15:14:23 +0000
changeset 417 4ce3d5f675ea
parent 389 770cc1f4861f
child 436 6d632cb56ea3
permissions -rw-r--r--
Better control of gcc version
     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       if (i.valid())
   373 	os << i.node->id; 
   374       else
   375 	os << "invalid";
   376       return os; 
   377     }
   378     friend std::ostream& operator<<(std::ostream& os, const Edge& i) { 
   379       if (i.valid()) 
   380 	os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")"; 
   381       else 
   382 	os << "invalid";
   383       return os; 
   384     }
   385 
   386     class Node {
   387       friend class ListGraph;
   388       template <typename T> friend class NodeMap;
   389 
   390       friend class Edge;
   391       friend class OutEdgeIt;
   392       friend class InEdgeIt;
   393       friend class SymEdgeIt;
   394       //public:  //FIXME: It is required by op= of NodeIt
   395     protected:
   396       node_item* node;
   397     protected:
   398       friend int ListGraph::id(Node v) const; 
   399     public:
   400       Node() /*: node(0)*/ { }
   401       Node(const Invalid&) : node(0) { }
   402     protected:
   403       Node(node_item* _node) : node(_node) { }
   404       bool valid() const { return (node); }
   405     public:
   406       //void makeInvalid() { node=0; }
   407       friend bool operator==(Node u, Node v) { return v.node==u.node; } 
   408       friend bool operator!=(Node u, Node v) { return v.node!=u.node; } 
   409       friend std::ostream& operator<<(std::ostream& os, const Node& i);
   410     };
   411     
   412     class NodeIt : public Node {
   413       friend class ListGraph;
   414       //protected:
   415     public: //for everybody but marci
   416       NodeIt(const ListGraph& G) : Node(G._first_node) { }
   417     public:
   418       NodeIt() : Node() { }
   419       NodeIt(const Invalid& i) : Node(i) { }
   420     protected:
   421       NodeIt(node_item* v) : Node(v) { }
   422       NodeIt& operator++() { node=node->_next_node; return *this; }
   423       //FIXME::
   424       //      NodeIt& operator=(const Node& e)
   425       //      { node=e.node; return *this; }
   426     };
   427 
   428     class Edge {
   429       friend class ListGraph;
   430       template <typename T> friend class EdgeMap;
   431       
   432       friend class Node;
   433       friend class NodeIt;
   434     protected:
   435       edge_item* edge;
   436       friend int ListGraph::id(Edge e) const;
   437     public:
   438       Edge() /*: edge(0)*/ { }
   439       Edge(const Invalid&) : edge(0) { }
   440       //Edge() { }
   441     protected:
   442       Edge(edge_item* _edge) : edge(_edge) { }
   443       bool valid() const { return (edge); }
   444     public:
   445       //void makeInvalid() { edge=0; }
   446       friend bool operator==(Edge u, Edge v) { return v.edge==u.edge; } 
   447       friend bool operator!=(Edge u, Edge v) { return v.edge!=u.edge; } 
   448     protected:
   449       Node tailNode() const { return Node(edge->_tail); }
   450       Node headNode() const { return Node(edge->_head); }
   451     public:
   452       friend std::ostream& operator<<(std::ostream& os, const Edge& i);
   453     };
   454     
   455     class EdgeIt : public Edge {
   456       friend class ListGraph;
   457       //protected: 
   458     public: //for alpar
   459       EdgeIt(const ListGraph& G) {
   460 	node_item* v=G._first_node;
   461 	if (v) edge=v->_first_out_edge; else edge=0;
   462 	while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
   463       }
   464     public:
   465       EdgeIt() : Edge() { }
   466       EdgeIt(const Invalid& i) : Edge(i) { }
   467     protected:
   468       EdgeIt(edge_item* _e) : Edge(_e) { }
   469       EdgeIt& operator++() { 
   470 	node_item* v=edge->_tail;
   471 	edge=edge->_next_out; 
   472 	while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
   473 	return *this;
   474       }
   475     };
   476     
   477     class OutEdgeIt : public Edge {
   478       friend class ListGraph;
   479       //node_item* v;
   480       //protected: 
   481     protected: //for alpar
   482       OutEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
   483     public:
   484       OutEdgeIt() : Edge()/*, v(0)*/ { }
   485       OutEdgeIt(const Invalid& i) : Edge(i) { }
   486       OutEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; }
   487     protected:
   488       OutEdgeIt& operator++() { edge=edge->_next_out; return *this; }
   489     protected:
   490       Node aNode() const { return Node(edge->_tail); }
   491       Node bNode() const { return Node(edge->_head); }
   492     };
   493     
   494     class InEdgeIt : public Edge {
   495       friend class ListGraph;
   496       //node_item* v;
   497       //protected:
   498     protected: //for alpar
   499       InEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
   500     public:
   501       InEdgeIt() : Edge()/*, v(0)*/ { }
   502       InEdgeIt(const Invalid& i) : Edge(i) { }
   503       InEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; }
   504     protected:
   505       InEdgeIt& operator++() { edge=edge->_next_in; return *this; }
   506     protected:
   507       Node aNode() const { return Node(edge->_head); }
   508       Node bNode() const { return Node(edge->_tail); }
   509     };
   510 
   511     class SymEdgeIt : public Edge {
   512       friend class ListGraph;
   513       bool out_or_in; //1 iff out, 0 iff in
   514       //node_item* v;
   515       //protected:
   516     protected: //for alpar
   517       SymEdgeIt(const Node& _v) /*: v(_v.node)*/ { 
   518 	out_or_in=1;
   519 	edge=_v.node->_first_out_edge; 
   520 	if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
   521       }
   522     public:
   523       SymEdgeIt() : Edge() /*, v(0)*/ { }
   524       SymEdgeIt(const Invalid& i) : Edge(i) { }
   525       SymEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { 
   526 	out_or_in=1;
   527 	edge=_v.node->_first_out_edge; 
   528 	if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
   529       }
   530     protected:
   531       SymEdgeIt& operator++() { 
   532 	if (out_or_in) { 
   533 	  node_item* v=edge->_tail;
   534 	  edge=edge->_next_out; 
   535 	  if (!edge) { out_or_in=0; edge=v->_first_in_edge; }
   536 	} else {
   537 	  edge=edge->_next_in; 
   538 	}
   539 	return *this;
   540       }
   541     protected:
   542       Node aNode() const { 
   543 	return (out_or_in) ? Node(edge->_tail) : Node(edge->_head); }
   544       Node bNode() const { 
   545 	return (out_or_in) ? Node(edge->_head) : Node(edge->_tail); }
   546     };
   547   };
   548 
   549   class UndirListGraph : public ListGraph {
   550   public:
   551     typedef SymEdgeIt OutEdgeIt;
   552     typedef SymEdgeIt InEdgeIt;
   553   };
   554 
   555 } //namespace hugo
   556 
   557 #endif //HUGO_LIST_GRAPH_H