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