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