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