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