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