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