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