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