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