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