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