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