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