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