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