src/work/alpar/list_graph.h
author alpar
Sun, 25 Apr 2004 20:16:16 +0000
changeset 400 cb377609cf1d
parent 397 b4d7b19b6740
child 401 2d0cccf7cc94
permissions -rw-r--r--
class NodeSet: A graph class with no edges
class EdgeSet: A graph class using the node set of another graph.
It compiles but untested and undocumented.
alpar@395
     1
// -*- mode:C++ -*-
alpar@395
     2
alpar@395
     3
#ifndef HUGO_SMART_GRAPH_H
alpar@395
     4
#define HUGO_SMART_GRAPH_H
alpar@395
     5
alpar@395
     6
///\file
alpar@397
     7
///\brief ListGraph and SymListGraph classes.
alpar@395
     8
alpar@395
     9
#include <vector>
alpar@395
    10
#include <limits.h>
alpar@395
    11
alpar@395
    12
#include "invalid.h"
alpar@395
    13
alpar@395
    14
namespace hugo {
alpar@395
    15
alpar@397
    16
  class SymListGraph;
alpar@395
    17
alpar@395
    18
  ///A smart graph class.
alpar@395
    19
alpar@397
    20
  ///This is a simple and fast erasable graph implementation.
alpar@397
    21
  ///
alpar@395
    22
  ///It conforms to the graph interface documented under
alpar@395
    23
  ///the description of \ref GraphSkeleton.
alpar@395
    24
  ///\sa \ref GraphSkeleton.
alpar@397
    25
  class ListGraph {
alpar@395
    26
alpar@397
    27
    //Nodes are double linked.
alpar@397
    28
    //The free nodes are only single linked using the "next" field.
alpar@395
    29
    struct NodeT 
alpar@395
    30
    {
alpar@397
    31
      int first_in,first_out;
alpar@397
    32
      int prev, next;
alpar@397
    33
      //      NodeT() {}
alpar@395
    34
    };
alpar@397
    35
    //Edges are double linked.
alpar@397
    36
    //The free edges are only single linked using the "next_in" field.
alpar@395
    37
    struct EdgeT 
alpar@395
    38
    {
alpar@397
    39
      int head, tail;
alpar@397
    40
      int prev_in, prev_out;
alpar@397
    41
      int next_in, next_out;
alpar@395
    42
      //FIXME: is this necessary?
alpar@397
    43
      //      EdgeT() : next_in(-1), next_out(-1) prev_in(-1), prev_out(-1) {}  
alpar@395
    44
    };
alpar@395
    45
alpar@395
    46
    std::vector<NodeT> nodes;
alpar@397
    47
    //The first node
alpar@397
    48
    int first_node;
alpar@397
    49
    //The first free node
alpar@397
    50
    int first_free_node;
alpar@395
    51
    std::vector<EdgeT> edges;
alpar@397
    52
    //The first free edge
alpar@397
    53
    int first_free_edge;
alpar@395
    54
    
alpar@397
    55
  protected:
alpar@395
    56
    
alpar@395
    57
    template <typename Key> class DynMapBase
alpar@395
    58
    {
alpar@395
    59
    protected:
alpar@397
    60
      const ListGraph* G; 
alpar@395
    61
    public:
alpar@395
    62
      virtual void add(const Key k) = NULL;
alpar@395
    63
      virtual void erase(const Key k) = NULL;
alpar@397
    64
      DynMapBase(const ListGraph &_G) : G(&_G) {}
alpar@395
    65
      virtual ~DynMapBase() {}
alpar@397
    66
      friend class ListGraph;
alpar@395
    67
    };
alpar@395
    68
    
alpar@395
    69
  public:
alpar@395
    70
    template <typename T> class EdgeMap;
alpar@400
    71
    template <typename T> class NodeMap;
alpar@397
    72
    
alpar@395
    73
    class Node;
alpar@395
    74
    class Edge;
alpar@395
    75
alpar@395
    76
    //  protected:
alpar@395
    77
    // HELPME:
alpar@395
    78
  protected:
alpar@395
    79
    ///\bug It must be public because of SymEdgeMap.
alpar@395
    80
    ///
alpar@395
    81
    mutable std::vector<DynMapBase<Node> * > dyn_node_maps;
alpar@395
    82
    ///\bug It must be public because of SymEdgeMap.
alpar@395
    83
    ///
alpar@395
    84
    mutable std::vector<DynMapBase<Edge> * > dyn_edge_maps;
alpar@395
    85
    
alpar@395
    86
  public:
alpar@395
    87
alpar@395
    88
    class NodeIt;
alpar@395
    89
    class EdgeIt;
alpar@395
    90
    class OutEdgeIt;
alpar@395
    91
    class InEdgeIt;
alpar@395
    92
    
alpar@395
    93
    template <typename T> class NodeMap;
alpar@395
    94
    template <typename T> class EdgeMap;
alpar@395
    95
    
alpar@395
    96
  public:
alpar@395
    97
alpar@397
    98
    ListGraph() : nodes(), first_node(-1),
alpar@397
    99
		  first_free_node(-1), edges(), first_free_edge(-1) {}
alpar@397
   100
    ListGraph(const ListGraph &_g) : nodes(_g.nodes), first_node(_g.first_node),
alpar@397
   101
				     first_free_node(_g.first_free_node),
alpar@397
   102
				     edges(_g.edges),
alpar@397
   103
				     first_free_edge(_g.first_free_edge) {}
alpar@395
   104
    
alpar@397
   105
    ~ListGraph()
alpar@395
   106
    {
alpar@395
   107
      for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
alpar@395
   108
	  i!=dyn_node_maps.end(); ++i) (**i).G=NULL;
alpar@395
   109
      for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin();
alpar@395
   110
	  i!=dyn_edge_maps.end(); ++i) (**i).G=NULL;
alpar@395
   111
    }
alpar@395
   112
alpar@395
   113
    int nodeNum() const { return nodes.size(); }  //FIXME: What is this?
alpar@395
   114
    int edgeNum() const { return edges.size(); }  //FIXME: What is this?
alpar@395
   115
alpar@395
   116
    ///\bug This function does something different than
alpar@395
   117
    ///its name would suggests...
alpar@395
   118
    int maxNodeId() const { return nodes.size(); }  //FIXME: What is this?
alpar@395
   119
    ///\bug This function does something different than
alpar@395
   120
    ///its name would suggests...
alpar@395
   121
    int maxEdgeId() const { return edges.size(); }  //FIXME: What is this?
alpar@395
   122
alpar@395
   123
    Node tail(Edge e) const { return edges[e.n].tail; }
alpar@395
   124
    Node head(Edge e) const { return edges[e.n].head; }
alpar@395
   125
alpar@395
   126
    Node aNode(OutEdgeIt e) const { return edges[e.n].tail; }
alpar@395
   127
    Node aNode(InEdgeIt e) const { return edges[e.n].head; }
alpar@395
   128
alpar@395
   129
    Node bNode(OutEdgeIt e) const { return edges[e.n].head; }
alpar@395
   130
    Node bNode(InEdgeIt e) const { return edges[e.n].tail; }
alpar@395
   131
alpar@395
   132
    NodeIt& first(NodeIt& v) const { 
alpar@395
   133
      v=NodeIt(*this); return v; }
alpar@395
   134
    EdgeIt& first(EdgeIt& e) const { 
alpar@395
   135
      e=EdgeIt(*this); return e; }
alpar@395
   136
    OutEdgeIt& first(OutEdgeIt& e, const Node v) const { 
alpar@395
   137
      e=OutEdgeIt(*this,v); return e; }
alpar@395
   138
    InEdgeIt& first(InEdgeIt& e, const Node v) const { 
alpar@395
   139
      e=InEdgeIt(*this,v); return e; }
alpar@395
   140
alpar@395
   141
//     template< typename It >
alpar@395
   142
//     It first() const { It e; first(e); return e; }
alpar@395
   143
alpar@395
   144
//     template< typename It >
alpar@395
   145
//     It first(Node v) const { It e; first(e,v); return e; }
alpar@395
   146
alpar@395
   147
    bool valid(Edge e) const { return e.n!=-1; }
alpar@395
   148
    bool valid(Node n) const { return n.n!=-1; }
alpar@395
   149
    
alpar@395
   150
    void setInvalid(Edge &e) { e.n=-1; }
alpar@395
   151
    void setInvalid(Node &n) { n.n=-1; }
alpar@395
   152
    
alpar@395
   153
    template <typename It> It getNext(It it) const
alpar@395
   154
    { It tmp(it); return next(tmp); }
alpar@395
   155
alpar@395
   156
    NodeIt& next(NodeIt& it) const { 
alpar@397
   157
      it.n=nodes[it.n].next; 
alpar@395
   158
      return it; 
alpar@395
   159
    }
alpar@395
   160
    OutEdgeIt& next(OutEdgeIt& it) const
alpar@395
   161
    { it.n=edges[it.n].next_out; return it; }
alpar@395
   162
    InEdgeIt& next(InEdgeIt& it) const
alpar@395
   163
    { it.n=edges[it.n].next_in; return it; }
alpar@397
   164
    EdgeIt& next(EdgeIt& it) const {
alpar@397
   165
      if(edges[it.n].next_in!=-1) { 
alpar@397
   166
	it.n=edges[it.n].next_in;
alpar@397
   167
      }
alpar@397
   168
      else {
alpar@397
   169
	int n;
alpar@397
   170
	for(n=nodes[edges[it.n].head].next;
alpar@397
   171
	    n!=-1 && nodes[n].first_in == -1;
alpar@397
   172
	    n = nodes[n].next) ;
alpar@397
   173
	it.n = (n==-1)?-1:nodes[n].first_in;
alpar@397
   174
      }
alpar@397
   175
      return it;
alpar@397
   176
    }
alpar@395
   177
alpar@395
   178
    int id(Node v) const { return v.n; }
alpar@395
   179
    int id(Edge e) const { return e.n; }
alpar@395
   180
alpar@397
   181
    /// Adds a new node to the graph.
alpar@397
   182
alpar@397
   183
    /// \todo It adds the nodes in a reversed order.
alpar@397
   184
    /// (i.e. the lastly added node becomes the first.)
alpar@395
   185
    Node addNode() {
alpar@397
   186
      int n;
alpar@397
   187
      
alpar@397
   188
      if(first_free_node==-1)
alpar@397
   189
	{
alpar@397
   190
	  n = nodes.size();
alpar@397
   191
	  nodes.push_back(NodeT());
alpar@397
   192
	}
alpar@397
   193
      else {
alpar@397
   194
	n = first_free_node;
alpar@397
   195
	first_free_node = nodes[n].next;
alpar@397
   196
      }
alpar@397
   197
      
alpar@397
   198
      nodes[n].next = first_node;
alpar@397
   199
      if(first_node != -1) nodes[first_node].prev = n;
alpar@397
   200
      first_node = n;
alpar@397
   201
      nodes[n].prev = -1;
alpar@397
   202
      
alpar@397
   203
      nodes[n].first_in = nodes[n].first_out = -1;
alpar@397
   204
      
alpar@397
   205
      Node nn; nn.n=n;
alpar@395
   206
alpar@397
   207
      //Update dynamic maps
alpar@395
   208
      for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
alpar@397
   209
	  i!=dyn_node_maps.end(); ++i) (**i).add(nn);
alpar@395
   210
alpar@397
   211
      return nn;
alpar@395
   212
    }
alpar@395
   213
    
alpar@395
   214
    Edge addEdge(Node u, Node v) {
alpar@397
   215
      int n;
alpar@397
   216
      
alpar@397
   217
      if(first_free_edge==-1)
alpar@397
   218
	{
alpar@397
   219
	  n = edges.size();
alpar@397
   220
	  edges.push_back(EdgeT());
alpar@397
   221
	}
alpar@397
   222
      else {
alpar@397
   223
	n = first_free_edge;
alpar@397
   224
	first_free_edge = edges[n].next_in;
alpar@397
   225
      }
alpar@397
   226
      
alpar@397
   227
      edges[n].tail = u.n; edges[n].head = v.n;
alpar@395
   228
alpar@397
   229
      edges[n].next_out = nodes[u.n].first_out;
alpar@397
   230
      if(nodes[u.n].first_out != -1) edges[nodes[u.n].first_out].prev_out = n;
alpar@397
   231
      edges[n].next_in = nodes[v.n].first_in;
alpar@397
   232
      if(nodes[v.n].first_in != -1) edges[nodes[v.n].first_in].prev_in = n;
alpar@397
   233
      edges[n].prev_in = edges[n].prev_out = -1;
alpar@397
   234
	
alpar@397
   235
      nodes[u.n].first_out = nodes[v.n].first_in = n;
alpar@397
   236
alpar@397
   237
      Edge e; e.n=n;
alpar@397
   238
alpar@397
   239
      //Update dynamic maps
alpar@395
   240
      for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin();
alpar@395
   241
	  i!=dyn_edge_maps.end(); ++i) (**i).add(e);
alpar@395
   242
alpar@395
   243
      return e;
alpar@395
   244
    }
alpar@395
   245
alpar@397
   246
  private:
alpar@397
   247
    void eraseEdge(int n) {
alpar@397
   248
      
alpar@397
   249
      if(edges[n].next_in!=-1)
alpar@397
   250
	edges[edges[n].next_in].prev_in = edges[n].prev_in;
alpar@397
   251
      if(edges[n].prev_in!=-1)
alpar@397
   252
	edges[edges[n].prev_in].next_in = edges[n].next_in;
alpar@397
   253
      else nodes[edges[n].head].first_in = edges[n].next_in;
alpar@397
   254
      
alpar@397
   255
      if(edges[n].next_out!=-1)
alpar@397
   256
	edges[edges[n].next_out].prev_out = edges[n].prev_out;
alpar@397
   257
      if(edges[n].prev_out!=-1)
alpar@397
   258
	edges[edges[n].prev_out].next_out = edges[n].next_out;
alpar@397
   259
      else nodes[edges[n].tail].first_out = edges[n].next_out;
alpar@397
   260
      
alpar@397
   261
      edges[n].next_in = first_free_edge;
alpar@397
   262
      first_free_edge = -1;      
alpar@397
   263
alpar@397
   264
      //Update dynamic maps
alpar@397
   265
      Edge e; e.n=n;
alpar@397
   266
      for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin();
alpar@397
   267
	  i!=dyn_edge_maps.end(); ++i) (**i).erase(e);
alpar@397
   268
    }
alpar@397
   269
      
alpar@397
   270
  public:
alpar@397
   271
alpar@397
   272
    void erase(Node nn) {
alpar@397
   273
      int n=nn.n;
alpar@397
   274
      
alpar@397
   275
      int m;
alpar@397
   276
      while((m=nodes[n].first_in)!=-1) eraseEdge(m);
alpar@397
   277
      while((m=nodes[n].first_out)!=-1) eraseEdge(m);
alpar@397
   278
alpar@397
   279
      if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev;
alpar@397
   280
      if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next;
alpar@397
   281
      else first_node = nodes[n].next;
alpar@397
   282
      
alpar@397
   283
      nodes[n].next = first_free_node;
alpar@397
   284
      first_free_node = n;
alpar@397
   285
alpar@397
   286
      //Update dynamic maps
alpar@397
   287
      for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
alpar@397
   288
	  i!=dyn_node_maps.end(); ++i) (**i).erase(nn);
alpar@397
   289
    }
alpar@397
   290
    
alpar@397
   291
    void erase(Edge e) { eraseEdge(e.n); }
alpar@397
   292
alpar@397
   293
    ///\bug Dynamic maps must be updated!
alpar@397
   294
    ///
alpar@397
   295
    void clear() {
alpar@397
   296
      nodes.clear();edges.clear();
alpar@397
   297
      first_node=first_free_node=first_free_edge=-1;
alpar@397
   298
    }
alpar@395
   299
alpar@395
   300
    class Node {
alpar@397
   301
      friend class ListGraph;
alpar@395
   302
      template <typename T> friend class NodeMap;
alpar@400
   303
       
alpar@395
   304
      friend class Edge;
alpar@395
   305
      friend class OutEdgeIt;
alpar@395
   306
      friend class InEdgeIt;
alpar@395
   307
      friend class SymEdge;
alpar@395
   308
alpar@395
   309
    protected:
alpar@395
   310
      int n;
alpar@397
   311
      friend int ListGraph::id(Node v) const; 
alpar@395
   312
      Node(int nn) {n=nn;}
alpar@395
   313
    public:
alpar@395
   314
      Node() {}
alpar@395
   315
      Node (Invalid i) { n=-1; }
alpar@395
   316
      bool operator==(const Node i) const {return n==i.n;}
alpar@395
   317
      bool operator!=(const Node i) const {return n!=i.n;}
alpar@395
   318
      bool operator<(const Node i) const {return n<i.n;}
alpar@395
   319
    };
alpar@395
   320
    
alpar@395
   321
    class NodeIt : public Node {
alpar@397
   322
      friend class ListGraph;
alpar@395
   323
    public:
alpar@400
   324
      NodeIt() : Node() { }
alpar@400
   325
      NodeIt(Invalid i) : Node(i) { }
alpar@397
   326
      NodeIt(const ListGraph& G) : Node(G.first_node) { }
alpar@395
   327
    };
alpar@395
   328
alpar@395
   329
    class Edge {
alpar@397
   330
      friend class ListGraph;
alpar@395
   331
      template <typename T> friend class EdgeMap;
alpar@395
   332
alpar@397
   333
      //template <typename T> friend class SymListGraph::SymEdgeMap;      
alpar@397
   334
      //friend Edge SymListGraph::opposite(Edge) const;
alpar@395
   335
      
alpar@395
   336
      friend class Node;
alpar@395
   337
      friend class NodeIt;
alpar@395
   338
    protected:
alpar@395
   339
      int n;
alpar@397
   340
      friend int ListGraph::id(Edge e) const;
alpar@395
   341
alpar@395
   342
      Edge(int nn) {n=nn;}
alpar@395
   343
    public:
alpar@395
   344
      Edge() { }
alpar@395
   345
      Edge (Invalid) { n=-1; }
alpar@395
   346
      bool operator==(const Edge i) const {return n==i.n;}
alpar@395
   347
      bool operator!=(const Edge i) const {return n!=i.n;}
alpar@395
   348
      bool operator<(const Edge i) const {return n<i.n;}
alpar@395
   349
      ///\bug This is a workaround until somebody tells me how to
alpar@397
   350
      ///make class \c SymListGraph::SymEdgeMap friend of Edge
alpar@395
   351
      int &idref() {return n;}
alpar@395
   352
      const int &idref() const {return n;}
alpar@395
   353
    };
alpar@395
   354
    
alpar@395
   355
    class EdgeIt : public Edge {
alpar@397
   356
      friend class ListGraph;
alpar@395
   357
    public:
alpar@397
   358
      EdgeIt(const ListGraph& G) : Edge() {
alpar@397
   359
      	int m;
alpar@397
   360
	for(m=G.first_node;
alpar@397
   361
	    m!=-1 && G.nodes[m].first_in == -1; m = G.nodes[m].next);
alpar@397
   362
	n = (m==-1)?-1:G.nodes[m].first_in;
alpar@397
   363
      }
alpar@395
   364
      EdgeIt (Invalid i) : Edge(i) { }
alpar@395
   365
      EdgeIt() : Edge() { }
alpar@395
   366
      ///\bug This is a workaround until somebody tells me how to
alpar@397
   367
      ///make class \c SymListGraph::SymEdgeMap friend of Edge
alpar@395
   368
      int &idref() {return n;}
alpar@395
   369
    };
alpar@395
   370
    
alpar@395
   371
    class OutEdgeIt : public Edge {
alpar@397
   372
      friend class ListGraph;
alpar@395
   373
    public: 
alpar@395
   374
      OutEdgeIt() : Edge() { }
alpar@395
   375
      OutEdgeIt (Invalid i) : Edge(i) { }
alpar@395
   376
alpar@397
   377
      OutEdgeIt(const ListGraph& G,const Node v)
alpar@395
   378
	: Edge(G.nodes[v.n].first_out) {}
alpar@395
   379
    };
alpar@395
   380
    
alpar@395
   381
    class InEdgeIt : public Edge {
alpar@397
   382
      friend class ListGraph;
alpar@395
   383
    public: 
alpar@395
   384
      InEdgeIt() : Edge() { }
alpar@395
   385
      InEdgeIt (Invalid i) : Edge(i) { }
alpar@397
   386
      InEdgeIt(const ListGraph& G,Node v) :Edge(G.nodes[v.n].first_in){}
alpar@395
   387
    };
alpar@395
   388
alpar@395
   389
    template <typename T> class NodeMap : public DynMapBase<Node>
alpar@395
   390
    {
alpar@395
   391
      std::vector<T> container;
alpar@395
   392
alpar@395
   393
    public:
alpar@395
   394
      typedef T ValueType;
alpar@395
   395
      typedef Node KeyType;
alpar@395
   396
alpar@397
   397
      NodeMap(const ListGraph &_G) :
alpar@395
   398
	DynMapBase<Node>(_G), container(_G.maxNodeId())
alpar@395
   399
      {
alpar@395
   400
	G->dyn_node_maps.push_back(this);
alpar@395
   401
      }
alpar@397
   402
      NodeMap(const ListGraph &_G,const T &t) :
alpar@395
   403
	DynMapBase<Node>(_G), container(_G.maxNodeId(),t)
alpar@395
   404
      {
alpar@395
   405
	G->dyn_node_maps.push_back(this);
alpar@395
   406
      }
alpar@395
   407
      
alpar@395
   408
      NodeMap(const NodeMap<T> &m) :
alpar@395
   409
 	DynMapBase<Node>(*m.G), container(m.container)
alpar@395
   410
      {
alpar@395
   411
 	G->dyn_node_maps.push_back(this);
alpar@395
   412
      }
alpar@395
   413
alpar@395
   414
      template<typename TT> friend class NodeMap;
alpar@395
   415
 
alpar@395
   416
      ///\todo It can copy between different types.
alpar@395
   417
      ///
alpar@395
   418
      template<typename TT> NodeMap(const NodeMap<TT> &m) :
alpar@395
   419
	DynMapBase<Node>(*m.G)
alpar@395
   420
      {
alpar@395
   421
	G->dyn_node_maps.push_back(this);
alpar@395
   422
	typename std::vector<TT>::const_iterator i;
alpar@395
   423
	for(typename std::vector<TT>::const_iterator i=m.container.begin();
alpar@395
   424
	    i!=m.container.end();
alpar@395
   425
	    i++)
alpar@395
   426
	  container.push_back(*i);
alpar@395
   427
      }
alpar@395
   428
      ~NodeMap()
alpar@395
   429
      {
alpar@395
   430
	if(G) {
alpar@395
   431
	  std::vector<DynMapBase<Node>* >::iterator i;
alpar@395
   432
	  for(i=G->dyn_node_maps.begin();
alpar@395
   433
	      i!=G->dyn_node_maps.end() && *i!=this; ++i) ;
alpar@395
   434
	  //if(*i==this) G->dyn_node_maps.erase(i); //FIXME: Way too slow...
alpar@395
   435
	  //A better way to do that: (Is this really important?)
alpar@395
   436
	  if(*i==this) {
alpar@395
   437
	    *i=G->dyn_node_maps.back();
alpar@395
   438
	    G->dyn_node_maps.pop_back();
alpar@395
   439
	  }
alpar@395
   440
	}
alpar@395
   441
      }
alpar@395
   442
alpar@395
   443
      void add(const Node k) 
alpar@395
   444
      {
alpar@395
   445
	if(k.n>=int(container.size())) container.resize(k.n+1);
alpar@395
   446
      }
alpar@395
   447
alpar@395
   448
      void erase(const Node) { }
alpar@395
   449
      
alpar@395
   450
      void set(Node n, T a) { container[n.n]=a; }
alpar@395
   451
      //'T& operator[](Node n)' would be wrong here
alpar@395
   452
      typename std::vector<T>::reference
alpar@395
   453
      operator[](Node n) { return container[n.n]; }
alpar@395
   454
      //'const T& operator[](Node n)' would be wrong here
alpar@395
   455
      typename std::vector<T>::const_reference 
alpar@395
   456
      operator[](Node n) const { return container[n.n]; }
alpar@395
   457
alpar@395
   458
      ///\warning There is no safety check at all!
alpar@395
   459
      ///Using operator = between maps attached to different graph may
alpar@395
   460
      ///cause serious problem.
alpar@395
   461
      ///\todo Is this really so?
alpar@395
   462
      ///\todo It can copy between different types.
alpar@395
   463
      const NodeMap<T>& operator=(const NodeMap<T> &m)
alpar@395
   464
      {
alpar@395
   465
	container = m.container;
alpar@395
   466
	return *this;
alpar@395
   467
      }
alpar@395
   468
      template<typename TT>
alpar@395
   469
      const NodeMap<T>& operator=(const NodeMap<TT> &m)
alpar@395
   470
      {
alpar@395
   471
	copy(m.container.begin(), m.container.end(), container.begin());
alpar@395
   472
	return *this;
alpar@395
   473
      }
alpar@395
   474
      
alpar@395
   475
      void update() {}    //Useless for Dynamic Maps
alpar@395
   476
      void update(T a) {}  //Useless for Dynamic Maps
alpar@395
   477
    };
alpar@395
   478
    
alpar@395
   479
    template <typename T> class EdgeMap : public DynMapBase<Edge>
alpar@395
   480
    {
alpar@395
   481
      std::vector<T> container;
alpar@395
   482
alpar@395
   483
    public:
alpar@395
   484
      typedef T ValueType;
alpar@395
   485
      typedef Edge KeyType;
alpar@395
   486
alpar@397
   487
      EdgeMap(const ListGraph &_G) :
alpar@395
   488
	DynMapBase<Edge>(_G), container(_G.maxEdgeId())
alpar@395
   489
      {
alpar@395
   490
	//FIXME: What if there are empty Id's?
alpar@395
   491
	//FIXME: Can I use 'this' in a constructor?
alpar@395
   492
	G->dyn_edge_maps.push_back(this);
alpar@395
   493
      }
alpar@397
   494
      EdgeMap(const ListGraph &_G,const T &t) :
alpar@395
   495
	DynMapBase<Edge>(_G), container(_G.maxEdgeId(),t)
alpar@395
   496
      {
alpar@395
   497
	G->dyn_edge_maps.push_back(this);
alpar@395
   498
      } 
alpar@395
   499
      EdgeMap(const EdgeMap<T> &m) :
alpar@395
   500
 	DynMapBase<Edge>(*m.G), container(m.container)
alpar@395
   501
      {
alpar@395
   502
 	G->dyn_node_maps.push_back(this);
alpar@395
   503
      }
alpar@395
   504
alpar@395
   505
      template<typename TT> friend class EdgeMap;
alpar@395
   506
alpar@395
   507
      ///\todo It can copy between different types.
alpar@395
   508
      ///
alpar@395
   509
      template<typename TT> EdgeMap(const EdgeMap<TT> &m) :
alpar@395
   510
	DynMapBase<Edge>(*m.G)
alpar@395
   511
      {
alpar@395
   512
	G->dyn_node_maps.push_back(this);
alpar@395
   513
	typename std::vector<TT>::const_iterator i;
alpar@395
   514
	for(typename std::vector<TT>::const_iterator i=m.container.begin();
alpar@395
   515
	    i!=m.container.end();
alpar@395
   516
	    i++)
alpar@395
   517
	  container.push_back(*i);
alpar@395
   518
      }
alpar@395
   519
      ~EdgeMap()
alpar@395
   520
      {
alpar@395
   521
	if(G) {
alpar@395
   522
	  std::vector<DynMapBase<Edge>* >::iterator i;
alpar@395
   523
	  for(i=G->dyn_edge_maps.begin();
alpar@395
   524
	      i!=G->dyn_edge_maps.end() && *i!=this; ++i) ;
alpar@395
   525
	  //if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow...
alpar@395
   526
	  //A better way to do that: (Is this really important?)
alpar@395
   527
	  if(*i==this) {
alpar@395
   528
	    *i=G->dyn_edge_maps.back();
alpar@395
   529
	    G->dyn_edge_maps.pop_back();
alpar@395
   530
	  }
alpar@395
   531
	}
alpar@395
   532
      }
alpar@395
   533
      
alpar@395
   534
      void add(const Edge k) 
alpar@395
   535
      {
alpar@395
   536
	if(k.n>=int(container.size())) container.resize(k.n+1);
alpar@395
   537
      }
alpar@395
   538
      void erase(const Edge) { }
alpar@395
   539
      
alpar@395
   540
      void set(Edge n, T a) { container[n.n]=a; }
alpar@395
   541
      //T get(Edge n) const { return container[n.n]; }
alpar@395
   542
      typename std::vector<T>::reference
alpar@395
   543
      operator[](Edge n) { return container[n.n]; }
alpar@395
   544
      typename std::vector<T>::const_reference
alpar@395
   545
      operator[](Edge n) const { return container[n.n]; }
alpar@395
   546
alpar@395
   547
      ///\warning There is no safety check at all!
alpar@395
   548
      ///Using operator = between maps attached to different graph may
alpar@395
   549
      ///cause serious problem.
alpar@395
   550
      ///\todo Is this really so?
alpar@395
   551
      ///\todo It can copy between different types.
alpar@395
   552
      const EdgeMap<T>& operator=(const EdgeMap<T> &m)
alpar@395
   553
      {
alpar@395
   554
	container = m.container;
alpar@395
   555
	return *this;
alpar@395
   556
      }
alpar@395
   557
      template<typename TT>
alpar@395
   558
      const EdgeMap<T>& operator=(const EdgeMap<TT> &m)
alpar@395
   559
      {
alpar@395
   560
	copy(m.container.begin(), m.container.end(), container.begin());
alpar@395
   561
	return *this;
alpar@395
   562
      }
alpar@395
   563
      
alpar@395
   564
      void update() {}    //Useless for DynMaps
alpar@395
   565
      void update(T a) {}  //Useless for DynMaps
alpar@395
   566
    };
alpar@395
   567
alpar@395
   568
  };
alpar@395
   569
alpar@395
   570
  ///Graph for bidirectional edges.
alpar@395
   571
alpar@395
   572
  ///The purpose of this graph structure is to handle graphs
alpar@395
   573
  ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair
alpar@395
   574
  ///of oppositely directed edges.
alpar@395
   575
  ///There is a new edge map type called
alpar@397
   576
  ///\ref SymListGraph::SymEdgeMap "SymEdgeMap"
alpar@395
   577
  ///that complements this
alpar@395
   578
  ///feature by
alpar@395
   579
  ///storing shared values for the edge pairs. The usual
alpar@395
   580
  ///\ref GraphSkeleton::EdgeMap "EdgeMap"
alpar@395
   581
  ///can be used
alpar@395
   582
  ///as well.
alpar@395
   583
  ///
alpar@395
   584
  ///The oppositely directed edge can also be obtained easily
alpar@395
   585
  ///using \ref opposite.
alpar@397
   586
  ///
alpar@397
   587
  ///Here erase(Edge) deletes a pair of edges.
alpar@397
   588
  ///
alpar@397
   589
  ///\todo this date structure need some reconsiderations. Maybe it
alpar@397
   590
  ///should be implemented independently from ListGraph.
alpar@395
   591
alpar@397
   592
  class SymListGraph : public ListGraph
alpar@395
   593
  {
alpar@395
   594
  public:
alpar@395
   595
    template<typename T> class SymEdgeMap;
alpar@395
   596
    template<typename T> friend class SymEdgeMap;
alpar@395
   597
alpar@397
   598
    SymListGraph() : ListGraph() { }
alpar@397
   599
    SymListGraph(const ListGraph &_g) : ListGraph(_g) { }
alpar@397
   600
    ///Adds a pair of oppositely directed edges to the graph.
alpar@395
   601
    Edge addEdge(Node u, Node v)
alpar@395
   602
    {
alpar@397
   603
      Edge e = ListGraph::addEdge(u,v);
alpar@397
   604
      ListGraph::addEdge(v,u);
alpar@395
   605
      return e;
alpar@395
   606
    }
alpar@395
   607
alpar@397
   608
    void erase(Node n) { ListGraph::erase(n); }
alpar@395
   609
    ///The oppositely directed edge.
alpar@395
   610
alpar@395
   611
    ///Returns the oppositely directed
alpar@395
   612
    ///pair of the edge \c e.
alpar@395
   613
    Edge opposite(Edge e) const
alpar@395
   614
    {
alpar@395
   615
      Edge f;
alpar@395
   616
      f.idref() = e.idref() - 2*(e.idref()%2) + 1;
alpar@395
   617
      return f;
alpar@395
   618
    }
alpar@395
   619
    
alpar@397
   620
    ///Removes a pair of oppositely directed edges to the graph.
alpar@397
   621
    void erase(Edge e) {
alpar@397
   622
      ListGraph::erase(opposite(e));
alpar@397
   623
      ListGraph::erase(e);
alpar@397
   624
    }
alpar@397
   625
    
alpar@395
   626
    ///Common data storage for the edge pairs.
alpar@395
   627
alpar@395
   628
    ///This map makes it possible to store data shared by the oppositely
alpar@395
   629
    ///directed pairs of edges.
alpar@395
   630
    template <typename T> class SymEdgeMap : public DynMapBase<Edge>
alpar@395
   631
    {
alpar@395
   632
      std::vector<T> container;
alpar@395
   633
      
alpar@395
   634
    public:
alpar@395
   635
      typedef T ValueType;
alpar@395
   636
      typedef Edge KeyType;
alpar@395
   637
alpar@397
   638
      SymEdgeMap(const SymListGraph &_G) :
alpar@395
   639
	DynMapBase<Edge>(_G), container(_G.maxEdgeId()/2)
alpar@395
   640
      {
alpar@397
   641
	static_cast<const SymListGraph*>(G)->dyn_edge_maps.push_back(this);
alpar@395
   642
      }
alpar@397
   643
      SymEdgeMap(const SymListGraph &_G,const T &t) :
alpar@395
   644
	DynMapBase<Edge>(_G), container(_G.maxEdgeId()/2,t)
alpar@395
   645
      {
alpar@395
   646
	G->dyn_edge_maps.push_back(this);
alpar@395
   647
      }
alpar@395
   648
alpar@395
   649
      SymEdgeMap(const SymEdgeMap<T> &m) :
alpar@395
   650
 	DynMapBase<SymEdge>(*m.G), container(m.container)
alpar@395
   651
      {
alpar@395
   652
 	G->dyn_node_maps.push_back(this);
alpar@395
   653
      }
alpar@395
   654
alpar@395
   655
      //      template<typename TT> friend class SymEdgeMap;
alpar@395
   656
alpar@395
   657
      ///\todo It can copy between different types.
alpar@395
   658
      ///
alpar@395
   659
alpar@395
   660
      template<typename TT> SymEdgeMap(const SymEdgeMap<TT> &m) :
alpar@395
   661
	DynMapBase<SymEdge>(*m.G)
alpar@395
   662
      {
alpar@395
   663
	G->dyn_node_maps.push_back(this);
alpar@395
   664
	typename std::vector<TT>::const_iterator i;
alpar@395
   665
	for(typename std::vector<TT>::const_iterator i=m.container.begin();
alpar@395
   666
	    i!=m.container.end();
alpar@395
   667
	    i++)
alpar@395
   668
	  container.push_back(*i);
alpar@395
   669
      }
alpar@395
   670
 
alpar@395
   671
      ~SymEdgeMap()
alpar@395
   672
      {
alpar@395
   673
	if(G) {
alpar@395
   674
	  std::vector<DynMapBase<Edge>* >::iterator i;
alpar@397
   675
	  for(i=static_cast<const SymListGraph*>(G)->dyn_edge_maps.begin();
alpar@397
   676
	      i!=static_cast<const SymListGraph*>(G)->dyn_edge_maps.end()
alpar@395
   677
		&& *i!=this; ++i) ;
alpar@395
   678
	  //if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow...
alpar@395
   679
	  //A better way to do that: (Is this really important?)
alpar@395
   680
	  if(*i==this) {
alpar@397
   681
	    *i=static_cast<const SymListGraph*>(G)->dyn_edge_maps.back();
alpar@397
   682
	    static_cast<const SymListGraph*>(G)->dyn_edge_maps.pop_back();
alpar@395
   683
	  }
alpar@395
   684
	}
alpar@395
   685
      }
alpar@395
   686
      
alpar@395
   687
      void add(const Edge k) 
alpar@395
   688
      {
alpar@395
   689
	if(!k.idref()%2&&k.idref()/2>=int(container.size()))
alpar@395
   690
	  container.resize(k.idref()/2+1);
alpar@395
   691
      }
alpar@395
   692
      void erase(const Edge k) { }
alpar@395
   693
      
alpar@395
   694
      void set(Edge n, T a) { container[n.idref()/2]=a; }
alpar@395
   695
      //T get(Edge n) const { return container[n.idref()/2]; }
alpar@395
   696
      typename std::vector<T>::reference
alpar@395
   697
      operator[](Edge n) { return container[n.idref()/2]; }
alpar@395
   698
      typename std::vector<T>::const_reference
alpar@395
   699
      operator[](Edge n) const { return container[n.idref()/2]; }
alpar@395
   700
alpar@395
   701
      ///\warning There is no safety check at all!
alpar@395
   702
      ///Using operator = between maps attached to different graph may
alpar@395
   703
      ///cause serious problem.
alpar@395
   704
      ///\todo Is this really so?
alpar@395
   705
      ///\todo It can copy between different types.
alpar@395
   706
      const SymEdgeMap<T>& operator=(const SymEdgeMap<T> &m)
alpar@395
   707
      {
alpar@395
   708
	container = m.container;
alpar@395
   709
	return *this;
alpar@395
   710
      }
alpar@395
   711
      template<typename TT>
alpar@395
   712
      const SymEdgeMap<T>& operator=(const SymEdgeMap<TT> &m)
alpar@395
   713
      {
alpar@395
   714
	copy(m.container.begin(), m.container.end(), container.begin());
alpar@395
   715
	return *this;
alpar@395
   716
      }
alpar@395
   717
      
alpar@395
   718
      void update() {}    //Useless for DynMaps
alpar@395
   719
      void update(T a) {}  //Useless for DynMaps
alpar@395
   720
alpar@395
   721
    };
alpar@395
   722
alpar@395
   723
  };
alpar@395
   724
  
alpar@400
   725
alpar@400
   726
  ///A smart graph class.
alpar@400
   727
alpar@400
   728
  ///This is a simple and fast erasable graph implementation.
alpar@400
   729
  ///
alpar@400
   730
  ///It conforms to the graph interface documented under
alpar@400
   731
  ///the description of \ref GraphSkeleton.
alpar@400
   732
  ///\sa \ref GraphSkeleton.
alpar@400
   733
  class NodeSet {
alpar@400
   734
alpar@400
   735
    //Nodes are double linked.
alpar@400
   736
    //The free nodes are only single linked using the "next" field.
alpar@400
   737
    struct NodeT 
alpar@400
   738
    {
alpar@400
   739
      int first_in,first_out;
alpar@400
   740
      int prev, next;
alpar@400
   741
      //      NodeT() {}
alpar@400
   742
    };
alpar@400
   743
alpar@400
   744
    std::vector<NodeT> nodes;
alpar@400
   745
    //The first node
alpar@400
   746
    int first_node;
alpar@400
   747
    //The first free node
alpar@400
   748
    int first_free_node;
alpar@400
   749
    
alpar@400
   750
  protected:
alpar@400
   751
    
alpar@400
   752
    template <typename Key> class DynMapBase
alpar@400
   753
    {
alpar@400
   754
    protected:
alpar@400
   755
      const NodeSet* G; 
alpar@400
   756
    public:
alpar@400
   757
      virtual void add(const Key k) = NULL;
alpar@400
   758
      virtual void erase(const Key k) = NULL;
alpar@400
   759
      DynMapBase(const NodeSet &_G) : G(&_G) {}
alpar@400
   760
      virtual ~DynMapBase() {}
alpar@400
   761
      friend class NodeSet;
alpar@400
   762
    };
alpar@400
   763
    
alpar@400
   764
  public:
alpar@400
   765
    template <typename T> class EdgeMap;
alpar@400
   766
    template <typename T> class NodeMap;
alpar@400
   767
    
alpar@400
   768
    class Node;
alpar@400
   769
    class Edge;
alpar@400
   770
alpar@400
   771
    //  protected:
alpar@400
   772
    // HELPME:
alpar@400
   773
  protected:
alpar@400
   774
    ///\bug It must be public because of SymEdgeMap.
alpar@400
   775
    ///
alpar@400
   776
    mutable std::vector<DynMapBase<Node> * > dyn_node_maps;
alpar@400
   777
    //mutable std::vector<DynMapBase<Edge> * > dyn_edge_maps;
alpar@400
   778
    
alpar@400
   779
  public:
alpar@400
   780
alpar@400
   781
    class NodeIt;
alpar@400
   782
    class EdgeIt;
alpar@400
   783
    class OutEdgeIt;
alpar@400
   784
    class InEdgeIt;
alpar@400
   785
    
alpar@400
   786
    template <typename T> class NodeMap;
alpar@400
   787
    template <typename T> class EdgeMap;
alpar@400
   788
    
alpar@400
   789
  public:
alpar@400
   790
alpar@400
   791
    NodeSet() : nodes(), first_node(-1),
alpar@400
   792
		  first_free_node(-1) {}
alpar@400
   793
    NodeSet(const NodeSet &_g) : nodes(_g.nodes), first_node(_g.first_node),
alpar@400
   794
				     first_free_node(_g.first_free_node) {}
alpar@400
   795
    
alpar@400
   796
    ~NodeSet()
alpar@400
   797
    {
alpar@400
   798
      for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
alpar@400
   799
	  i!=dyn_node_maps.end(); ++i) (**i).G=NULL;
alpar@400
   800
      //for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin();
alpar@400
   801
      //	  i!=dyn_edge_maps.end(); ++i) (**i).G=NULL;
alpar@400
   802
    }
alpar@400
   803
alpar@400
   804
    int nodeNum() const { return nodes.size(); }  //FIXME: What is this?
alpar@400
   805
    int edgeNum() const { return 0; }  //FIXME: What is this?
alpar@400
   806
alpar@400
   807
    ///\bug This function does something different than
alpar@400
   808
    ///its name would suggests...
alpar@400
   809
    int maxNodeId() const { return nodes.size(); }  //FIXME: What is this?
alpar@400
   810
    ///\bug This function does something different than
alpar@400
   811
    ///its name would suggests...
alpar@400
   812
    int maxEdgeId() const { return 0; }  //FIXME: What is this?
alpar@400
   813
alpar@400
   814
    Node tail(Edge e) const { return INVALID; }
alpar@400
   815
    Node head(Edge e) const { return INVALID; }
alpar@400
   816
alpar@400
   817
    Node aNode(OutEdgeIt e) const { return INVALID; }
alpar@400
   818
    Node aNode(InEdgeIt e) const { return INVALID; }
alpar@400
   819
alpar@400
   820
    Node bNode(OutEdgeIt e) const { return INVALID; }
alpar@400
   821
    Node bNode(InEdgeIt e) const { return INVALID; }
alpar@400
   822
alpar@400
   823
    NodeIt& first(NodeIt& v) const { 
alpar@400
   824
      v=NodeIt(*this); return v; }
alpar@400
   825
    EdgeIt& first(EdgeIt& e) const { 
alpar@400
   826
      e=EdgeIt(*this); return e; }
alpar@400
   827
    OutEdgeIt& first(OutEdgeIt& e, const Node v) const { 
alpar@400
   828
      e=OutEdgeIt(*this,v); return e; }
alpar@400
   829
    InEdgeIt& first(InEdgeIt& e, const Node v) const { 
alpar@400
   830
      e=InEdgeIt(*this,v); return e; }
alpar@400
   831
alpar@400
   832
//     template< typename It >
alpar@400
   833
//     It first() const { It e; first(e); return e; }
alpar@400
   834
alpar@400
   835
//     template< typename It >
alpar@400
   836
//     It first(Node v) const { It e; first(e,v); return e; }
alpar@400
   837
alpar@400
   838
    bool valid(Edge e) const { return false; }
alpar@400
   839
    bool valid(Node n) const { return n.n!=-1; }
alpar@400
   840
    
alpar@400
   841
    void setInvalid(Edge &e) { }
alpar@400
   842
    void setInvalid(Node &n) { n.n=-1; }
alpar@400
   843
    
alpar@400
   844
    template <typename It> It getNext(It it) const
alpar@400
   845
    { It tmp(it); return next(tmp); }
alpar@400
   846
alpar@400
   847
    NodeIt& next(NodeIt& it) const { 
alpar@400
   848
      it.n=nodes[it.n].next; 
alpar@400
   849
      return it; 
alpar@400
   850
    }
alpar@400
   851
    OutEdgeIt& next(OutEdgeIt& it) const { return it; }
alpar@400
   852
    InEdgeIt& next(InEdgeIt& it) const { return it; }
alpar@400
   853
    EdgeIt& next(EdgeIt& it) const { return it; }
alpar@400
   854
alpar@400
   855
    int id(Node v) const { return v.n; }
alpar@400
   856
    int id(Edge e) const { return -1; }
alpar@400
   857
alpar@400
   858
    /// Adds a new node to the graph.
alpar@400
   859
alpar@400
   860
    /// \todo It adds the nodes in a reversed order.
alpar@400
   861
    /// (i.e. the lastly added node becomes the first.)
alpar@400
   862
    Node addNode() {
alpar@400
   863
      int n;
alpar@400
   864
      
alpar@400
   865
      if(first_free_node==-1)
alpar@400
   866
	{
alpar@400
   867
	  n = nodes.size();
alpar@400
   868
	  nodes.push_back(NodeT());
alpar@400
   869
	}
alpar@400
   870
      else {
alpar@400
   871
	n = first_free_node;
alpar@400
   872
	first_free_node = nodes[n].next;
alpar@400
   873
      }
alpar@400
   874
      
alpar@400
   875
      nodes[n].next = first_node;
alpar@400
   876
      if(first_node != -1) nodes[first_node].prev = n;
alpar@400
   877
      first_node = n;
alpar@400
   878
      nodes[n].prev = -1;
alpar@400
   879
      
alpar@400
   880
      nodes[n].first_in = nodes[n].first_out = -1;
alpar@400
   881
      
alpar@400
   882
      Node nn; nn.n=n;
alpar@400
   883
alpar@400
   884
      //Update dynamic maps
alpar@400
   885
      for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
alpar@400
   886
	  i!=dyn_node_maps.end(); ++i) (**i).add(nn);
alpar@400
   887
alpar@400
   888
      return nn;
alpar@400
   889
    }
alpar@400
   890
    
alpar@400
   891
    void erase(Node nn) {
alpar@400
   892
      int n=nn.n;
alpar@400
   893
      
alpar@400
   894
      if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev;
alpar@400
   895
      if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next;
alpar@400
   896
      else first_node = nodes[n].next;
alpar@400
   897
      
alpar@400
   898
      nodes[n].next = first_free_node;
alpar@400
   899
      first_free_node = n;
alpar@400
   900
alpar@400
   901
      //Update dynamic maps
alpar@400
   902
      for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
alpar@400
   903
	  i!=dyn_node_maps.end(); ++i) (**i).erase(nn);
alpar@400
   904
    }
alpar@400
   905
    
alpar@400
   906
    ///\bug Dynamic maps must be updated!
alpar@400
   907
    ///
alpar@400
   908
    void clear() {
alpar@400
   909
      nodes.clear();
alpar@400
   910
      first_node = first_free_node = -1;
alpar@400
   911
    }
alpar@400
   912
alpar@400
   913
    class Node {
alpar@400
   914
      friend class NodeSet;
alpar@400
   915
      template <typename T> friend class NodeMap;
alpar@400
   916
      
alpar@400
   917
      friend class Edge;
alpar@400
   918
      friend class OutEdgeIt;
alpar@400
   919
      friend class InEdgeIt;
alpar@400
   920
alpar@400
   921
    protected:
alpar@400
   922
      int n;
alpar@400
   923
      friend int NodeSet::id(Node v) const; 
alpar@400
   924
      Node(int nn) {n=nn;}
alpar@400
   925
    public:
alpar@400
   926
      Node() {}
alpar@400
   927
      Node (Invalid i) { n=-1; }
alpar@400
   928
      bool operator==(const Node i) const {return n==i.n;}
alpar@400
   929
      bool operator!=(const Node i) const {return n!=i.n;}
alpar@400
   930
      bool operator<(const Node i) const {return n<i.n;}
alpar@400
   931
    };
alpar@400
   932
    
alpar@400
   933
    class NodeIt : public Node {
alpar@400
   934
      friend class NodeSet;
alpar@400
   935
    public:
alpar@400
   936
      NodeIt(const NodeSet& G) : Node(G.first_node) { }
alpar@400
   937
      NodeIt() : Node() { }
alpar@400
   938
    };
alpar@400
   939
alpar@400
   940
    class Edge {
alpar@400
   941
      //friend class NodeSet;
alpar@400
   942
      //template <typename T> friend class EdgeMap;
alpar@400
   943
alpar@400
   944
      //template <typename T> friend class SymNodeSet::SymEdgeMap;      
alpar@400
   945
      //friend Edge SymNodeSet::opposite(Edge) const;
alpar@400
   946
      
alpar@400
   947
      //      friend class Node;
alpar@400
   948
      //      friend class NodeIt;
alpar@400
   949
    protected:
alpar@400
   950
      //friend int NodeSet::id(Edge e) const;
alpar@400
   951
      //      Edge(int nn) {}
alpar@400
   952
    public:
alpar@400
   953
      Edge() { }
alpar@400
   954
      Edge (Invalid) { }
alpar@400
   955
      bool operator==(const Edge i) const {return true;}
alpar@400
   956
      bool operator!=(const Edge i) const {return false;}
alpar@400
   957
      bool operator<(const Edge i) const {return false;}
alpar@400
   958
      ///\bug This is a workaround until somebody tells me how to
alpar@400
   959
      ///make class \c SymNodeSet::SymEdgeMap friend of Edge
alpar@400
   960
      //      int idref() {return -1;}
alpar@400
   961
      //      int idref() const {return -1;}
alpar@400
   962
    };
alpar@400
   963
    
alpar@400
   964
    class EdgeIt : public Edge {
alpar@400
   965
      //friend class NodeSet;
alpar@400
   966
    public:
alpar@400
   967
      EdgeIt(const NodeSet& G) : Edge() { }
alpar@400
   968
      EdgeIt (Invalid i) : Edge(i) { }
alpar@400
   969
      EdgeIt() : Edge() { }
alpar@400
   970
      ///\bug This is a workaround until somebody tells me how to
alpar@400
   971
      ///make class \c SymNodeSet::SymEdgeMap friend of Edge
alpar@400
   972
      //      int idref() {return -1;}
alpar@400
   973
    };
alpar@400
   974
    
alpar@400
   975
    class OutEdgeIt : public Edge {
alpar@400
   976
      friend class NodeSet;
alpar@400
   977
    public: 
alpar@400
   978
      OutEdgeIt() : Edge() { }
alpar@400
   979
      OutEdgeIt (Invalid i) : Edge(i) { }
alpar@400
   980
      OutEdgeIt(const NodeSet& G,const Node v)	: Edge() {}
alpar@400
   981
    };
alpar@400
   982
    
alpar@400
   983
    class InEdgeIt : public Edge {
alpar@400
   984
      friend class NodeSet;
alpar@400
   985
    public: 
alpar@400
   986
      InEdgeIt() : Edge() { }
alpar@400
   987
      InEdgeIt (Invalid i) : Edge(i) { }
alpar@400
   988
      InEdgeIt(const NodeSet& G,Node v) :Edge() {}
alpar@400
   989
    };
alpar@400
   990
alpar@400
   991
    template <typename T> class NodeMap : public DynMapBase<Node>
alpar@400
   992
    {
alpar@400
   993
      std::vector<T> container;
alpar@400
   994
alpar@400
   995
    public:
alpar@400
   996
      typedef T ValueType;
alpar@400
   997
      typedef Node KeyType;
alpar@400
   998
alpar@400
   999
      NodeMap(const NodeSet &_G) :
alpar@400
  1000
	DynMapBase<Node>(_G), container(_G.maxNodeId())
alpar@400
  1001
      {
alpar@400
  1002
	G->dyn_node_maps.push_back(this);
alpar@400
  1003
      }
alpar@400
  1004
      NodeMap(const NodeSet &_G,const T &t) :
alpar@400
  1005
	DynMapBase<Node>(_G), container(_G.maxNodeId(),t)
alpar@400
  1006
      {
alpar@400
  1007
	G->dyn_node_maps.push_back(this);
alpar@400
  1008
      }
alpar@400
  1009
      
alpar@400
  1010
      NodeMap(const NodeMap<T> &m) :
alpar@400
  1011
 	DynMapBase<Node>(*m.G), container(m.container)
alpar@400
  1012
      {
alpar@400
  1013
 	G->dyn_node_maps.push_back(this);
alpar@400
  1014
      }
alpar@400
  1015
alpar@400
  1016
      template<typename TT> friend class NodeMap;
alpar@400
  1017
 
alpar@400
  1018
      ///\todo It can copy between different types.
alpar@400
  1019
      ///
alpar@400
  1020
      template<typename TT> NodeMap(const NodeMap<TT> &m) :
alpar@400
  1021
	DynMapBase<Node>(*m.G)
alpar@400
  1022
      {
alpar@400
  1023
	G->dyn_node_maps.push_back(this);
alpar@400
  1024
	typename std::vector<TT>::const_iterator i;
alpar@400
  1025
	for(typename std::vector<TT>::const_iterator i=m.container.begin();
alpar@400
  1026
	    i!=m.container.end();
alpar@400
  1027
	    i++)
alpar@400
  1028
	  container.push_back(*i);
alpar@400
  1029
      }
alpar@400
  1030
      ~NodeMap()
alpar@400
  1031
      {
alpar@400
  1032
	if(G) {
alpar@400
  1033
	  std::vector<DynMapBase<Node>* >::iterator i;
alpar@400
  1034
	  for(i=G->dyn_node_maps.begin();
alpar@400
  1035
	      i!=G->dyn_node_maps.end() && *i!=this; ++i) ;
alpar@400
  1036
	  //if(*i==this) G->dyn_node_maps.erase(i); //FIXME: Way too slow...
alpar@400
  1037
	  //A better way to do that: (Is this really important?)
alpar@400
  1038
	  if(*i==this) {
alpar@400
  1039
	    *i=G->dyn_node_maps.back();
alpar@400
  1040
	    G->dyn_node_maps.pop_back();
alpar@400
  1041
	  }
alpar@400
  1042
	}
alpar@400
  1043
      }
alpar@400
  1044
alpar@400
  1045
      void add(const Node k) 
alpar@400
  1046
      {
alpar@400
  1047
	if(k.n>=int(container.size())) container.resize(k.n+1);
alpar@400
  1048
      }
alpar@400
  1049
alpar@400
  1050
      void erase(const Node) { }
alpar@400
  1051
      
alpar@400
  1052
      void set(Node n, T a) { container[n.n]=a; }
alpar@400
  1053
      //'T& operator[](Node n)' would be wrong here
alpar@400
  1054
      typename std::vector<T>::reference
alpar@400
  1055
      operator[](Node n) { return container[n.n]; }
alpar@400
  1056
      //'const T& operator[](Node n)' would be wrong here
alpar@400
  1057
      typename std::vector<T>::const_reference 
alpar@400
  1058
      operator[](Node n) const { return container[n.n]; }
alpar@400
  1059
alpar@400
  1060
      ///\warning There is no safety check at all!
alpar@400
  1061
      ///Using operator = between maps attached to different graph may
alpar@400
  1062
      ///cause serious problem.
alpar@400
  1063
      ///\todo Is this really so?
alpar@400
  1064
      ///\todo It can copy between different types.
alpar@400
  1065
      const NodeMap<T>& operator=(const NodeMap<T> &m)
alpar@400
  1066
      {
alpar@400
  1067
	container = m.container;
alpar@400
  1068
	return *this;
alpar@400
  1069
      }
alpar@400
  1070
      template<typename TT>
alpar@400
  1071
      const NodeMap<T>& operator=(const NodeMap<TT> &m)
alpar@400
  1072
      {
alpar@400
  1073
	copy(m.container.begin(), m.container.end(), container.begin());
alpar@400
  1074
	return *this;
alpar@400
  1075
      }
alpar@400
  1076
      
alpar@400
  1077
      void update() {}    //Useless for Dynamic Maps
alpar@400
  1078
      void update(T a) {}  //Useless for Dynamic Maps
alpar@400
  1079
    };
alpar@400
  1080
    
alpar@400
  1081
    template <typename T> class EdgeMap
alpar@400
  1082
    {
alpar@400
  1083
    public:
alpar@400
  1084
      typedef T ValueType;
alpar@400
  1085
      typedef Edge KeyType;
alpar@400
  1086
alpar@400
  1087
      EdgeMap(const NodeSet &) { }
alpar@400
  1088
      EdgeMap(const NodeSet &,const T &) { }
alpar@400
  1089
      EdgeMap(const EdgeMap<T> &) { }
alpar@400
  1090
      //      template<typename TT> friend class EdgeMap;
alpar@400
  1091
alpar@400
  1092
      ///\todo It can copy between different types.
alpar@400
  1093
      ///
alpar@400
  1094
      template<typename TT> EdgeMap(const EdgeMap<TT> &) { }
alpar@400
  1095
      ~EdgeMap() { }
alpar@400
  1096
alpar@400
  1097
      void add(const Edge  ) { }
alpar@400
  1098
      void erase(const Edge) { }
alpar@400
  1099
      
alpar@400
  1100
      void set(Edge, T) { }
alpar@400
  1101
      //T get(Edge n) const { return container[n.n]; }
alpar@400
  1102
      ValueType &operator[](Edge) { return *((T*)(NULL)); }
alpar@400
  1103
      const ValueType &operator[](Edge) const { return *((T*)(NULL)); }
alpar@400
  1104
alpar@400
  1105
      const EdgeMap<T>& operator=(const EdgeMap<T> &) { return *this; }
alpar@400
  1106
    
alpar@400
  1107
      template<typename TT>
alpar@400
  1108
      const EdgeMap<T>& operator=(const EdgeMap<TT> &m) { return *this; }
alpar@400
  1109
      
alpar@400
  1110
      void update() {}
alpar@400
  1111
      void update(T a) {}
alpar@400
  1112
    };
alpar@400
  1113
  };
alpar@400
  1114
alpar@400
  1115
alpar@400
  1116
alpar@400
  1117
  ///This is a simple and fast erasable graph implementation.
alpar@400
  1118
  ///
alpar@400
  1119
  ///It conforms to the graph interface documented under
alpar@400
  1120
  ///the description of \ref GraphSkeleton.
alpar@400
  1121
  ///\sa \ref GraphSkeleton.
alpar@400
  1122
  template<typename GG>
alpar@400
  1123
  class EdgeSet {
alpar@400
  1124
alpar@400
  1125
    typedef GG NodeGraphType;
alpar@400
  1126
alpar@400
  1127
    NodeGraphType &G;
alpar@400
  1128
alpar@400
  1129
    class Node;
alpar@400
  1130
    
alpar@400
  1131
    //Edges are double linked.
alpar@400
  1132
    //The free edges are only single linked using the "next_in" field.
alpar@400
  1133
    struct NodeT 
alpar@400
  1134
    {
alpar@400
  1135
      int first_in,first_out;
alpar@400
  1136
      NodeT() : first_in(-1), first_out(-1) { }
alpar@400
  1137
    };
alpar@400
  1138
alpar@400
  1139
    struct EdgeT 
alpar@400
  1140
    {
alpar@400
  1141
      Node head, tail;
alpar@400
  1142
      int prev_in, prev_out;
alpar@400
  1143
      int next_in, next_out;
alpar@400
  1144
    };
alpar@400
  1145
alpar@400
  1146
    
alpar@400
  1147
    typename NodeGraphType::NodeMap<NodeT> nodes;
alpar@400
  1148
    
alpar@400
  1149
    std::vector<EdgeT> edges;
alpar@400
  1150
    //The first free edge
alpar@400
  1151
    int first_free_edge;
alpar@400
  1152
    
alpar@400
  1153
  protected:
alpar@400
  1154
    
alpar@400
  1155
    template <typename Key> class DynMapBase
alpar@400
  1156
    {
alpar@400
  1157
    protected:
alpar@400
  1158
      const EdgeSet* G; 
alpar@400
  1159
    public:
alpar@400
  1160
      virtual void add(const Key k) = NULL;
alpar@400
  1161
      virtual void erase(const Key k) = NULL;
alpar@400
  1162
      DynMapBase(const EdgeSet &_G) : G(&_G) {}
alpar@400
  1163
      virtual ~DynMapBase() {}
alpar@400
  1164
      friend class EdgeSet;
alpar@400
  1165
    };
alpar@400
  1166
    
alpar@400
  1167
  public:
alpar@400
  1168
    //template <typename T> class NodeMap;
alpar@400
  1169
    template <typename T> class EdgeMap;
alpar@400
  1170
    
alpar@400
  1171
    class Node;
alpar@400
  1172
    class Edge;
alpar@400
  1173
alpar@400
  1174
    //  protected:
alpar@400
  1175
    // HELPME:
alpar@400
  1176
  protected:
alpar@400
  1177
    // mutable std::vector<DynMapBase<Node> * > dyn_node_maps;
alpar@400
  1178
    ///\bug It must be public because of SymEdgeMap.
alpar@400
  1179
    ///
alpar@400
  1180
    mutable std::vector<DynMapBase<Edge> * > dyn_edge_maps;
alpar@400
  1181
    
alpar@400
  1182
  public:
alpar@400
  1183
alpar@400
  1184
    class NodeIt;
alpar@400
  1185
    class EdgeIt;
alpar@400
  1186
    class OutEdgeIt;
alpar@400
  1187
    class InEdgeIt;
alpar@400
  1188
    
alpar@400
  1189
    template <typename T> class NodeMap;
alpar@400
  1190
    template <typename T> class EdgeMap;
alpar@400
  1191
    
alpar@400
  1192
  public:
alpar@400
  1193
alpar@400
  1194
    EdgeSet(const NodeGraphType &_G) : G(_G),
alpar@400
  1195
				       nodes(_G), edges(),
alpar@400
  1196
				       first_free_edge(-1) {}
alpar@400
  1197
    EdgeSet(const EdgeSet &_g) : G(_g.G), nodes(_g.G), edges(_g.edges),
alpar@400
  1198
				 first_free_edge(_g.first_free_edge) {}
alpar@400
  1199
    
alpar@400
  1200
    ~EdgeSet()
alpar@400
  1201
    {
alpar@400
  1202
      // for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
alpar@400
  1203
      //  i!=dyn_node_maps.end(); ++i) (**i).G=NULL;
alpar@400
  1204
      for(typename std::vector<DynMapBase<Edge> * >::iterator
alpar@400
  1205
	    i=dyn_edge_maps.begin();
alpar@400
  1206
	  i!=dyn_edge_maps.end(); ++i) (**i).G=NULL;
alpar@400
  1207
    }
alpar@400
  1208
alpar@400
  1209
    int nodeNum() const { return G.nodeNum(); }  //FIXME: What is this?
alpar@400
  1210
    int edgeNum() const { return edges.size(); }  //FIXME: What is this?
alpar@400
  1211
alpar@400
  1212
    ///\bug This function does something different than
alpar@400
  1213
    ///its name would suggests...
alpar@400
  1214
    int maxNodeId() const { return G.maxNodeId(); }  //FIXME: What is this?
alpar@400
  1215
    ///\bug This function does something different than
alpar@400
  1216
    ///its name would suggests...
alpar@400
  1217
    int maxEdgeId() const { return edges.size(); }  //FIXME: What is this?
alpar@400
  1218
alpar@400
  1219
    Node tail(Edge e) const { return edges[e.n].tail; }
alpar@400
  1220
    Node head(Edge e) const { return edges[e.n].head; }
alpar@400
  1221
alpar@400
  1222
    Node aNode(OutEdgeIt e) const { return edges[e.n].tail; }
alpar@400
  1223
    Node aNode(InEdgeIt e) const { return edges[e.n].head; }
alpar@400
  1224
alpar@400
  1225
    Node bNode(OutEdgeIt e) const { return edges[e.n].head; }
alpar@400
  1226
    Node bNode(InEdgeIt e) const { return edges[e.n].tail; }
alpar@400
  1227
alpar@400
  1228
    NodeIt& first(NodeIt& v) const { 
alpar@400
  1229
      v=NodeIt(*this); return v; }
alpar@400
  1230
    EdgeIt& first(EdgeIt& e) const { 
alpar@400
  1231
      e=EdgeIt(*this); return e; }
alpar@400
  1232
    OutEdgeIt& first(OutEdgeIt& e, const Node v) const { 
alpar@400
  1233
      e=OutEdgeIt(*this,v); return e; }
alpar@400
  1234
    InEdgeIt& first(InEdgeIt& e, const Node v) const { 
alpar@400
  1235
      e=InEdgeIt(*this,v); return e; }
alpar@400
  1236
alpar@400
  1237
//     template< typename It >
alpar@400
  1238
//     It first() const { It e; first(e); return e; }
alpar@400
  1239
alpar@400
  1240
//     template< typename It >
alpar@400
  1241
//     It first(Node v) const { It e; first(e,v); return e; }
alpar@400
  1242
alpar@400
  1243
    bool valid(Edge e) const { return e.n!=-1; }
alpar@400
  1244
    bool valid(Node n) const { return G.valid(n); }
alpar@400
  1245
    
alpar@400
  1246
    void setInvalid(Edge &e) { e.n=-1; }
alpar@400
  1247
    void setInvalid(Node &n) { G.setInvalid(n); }
alpar@400
  1248
    
alpar@400
  1249
    template <typename It> It getNext(It it) const
alpar@400
  1250
    { It tmp(it); return next(tmp); }
alpar@400
  1251
alpar@400
  1252
    NodeIt& next(NodeIt& it) const { G.next(it); return it; }
alpar@400
  1253
    OutEdgeIt& next(OutEdgeIt& it) const
alpar@400
  1254
    { it.n=edges[it.n].next_out; return it; }
alpar@400
  1255
    InEdgeIt& next(InEdgeIt& it) const
alpar@400
  1256
    { it.n=edges[it.n].next_in; return it; }
alpar@400
  1257
    EdgeIt& next(EdgeIt& it) const {
alpar@400
  1258
      if(edges[it.n].next_in!=-1) { 
alpar@400
  1259
	it.n=edges[it.n].next_in;
alpar@400
  1260
      }
alpar@400
  1261
      else {
alpar@400
  1262
	typename NodeGraphType::Node n;
alpar@400
  1263
	for(n=G.next(edges[it.n].head);
alpar@400
  1264
	    G.valid(n) && nodes[n].first_in == -1;
alpar@400
  1265
	    G.next(n)) ;
alpar@400
  1266
	it.n = (G.valid(n))?-1:nodes[n].first_in;
alpar@400
  1267
      }
alpar@400
  1268
      return it;
alpar@400
  1269
    }
alpar@400
  1270
alpar@400
  1271
    int id(Node v) const { return G.id(v); }
alpar@400
  1272
    int id(Edge e) const { return e.n; }
alpar@400
  1273
alpar@400
  1274
    /// Adds a new node to the graph.
alpar@400
  1275
    Node addNode() { return G.AddNode(); }
alpar@400
  1276
    
alpar@400
  1277
    Edge addEdge(Node u, Node v) {
alpar@400
  1278
      int n;
alpar@400
  1279
      
alpar@400
  1280
      if(first_free_edge==-1)
alpar@400
  1281
	{
alpar@400
  1282
	  n = edges.size();
alpar@400
  1283
	  edges.push_back(EdgeT());
alpar@400
  1284
	}
alpar@400
  1285
      else {
alpar@400
  1286
	n = first_free_edge;
alpar@400
  1287
	first_free_edge = edges[n].next_in;
alpar@400
  1288
      }
alpar@400
  1289
      
alpar@400
  1290
      edges[n].tail = u.n; edges[n].head = v.n;
alpar@400
  1291
alpar@400
  1292
      edges[n].next_out = nodes[u.n].first_out;
alpar@400
  1293
      if(nodes[u.n].first_out != -1) edges[nodes[u.n].first_out].prev_out = n;
alpar@400
  1294
      edges[n].next_in = nodes[v.n].first_in;
alpar@400
  1295
      if(nodes[v.n].first_in != -1) edges[nodes[v.n].first_in].prev_in = n;
alpar@400
  1296
      edges[n].prev_in = edges[n].prev_out = -1;
alpar@400
  1297
	
alpar@400
  1298
      nodes[u.n].first_out = nodes[v.n].first_in = n;
alpar@400
  1299
alpar@400
  1300
      Edge e; e.n=n;
alpar@400
  1301
alpar@400
  1302
      //Update dynamic maps
alpar@400
  1303
      for(typename std::vector<DynMapBase<Edge> * >::iterator
alpar@400
  1304
	    i=dyn_edge_maps.begin();
alpar@400
  1305
	  i!=dyn_edge_maps.end(); ++i) (**i).add(e);
alpar@400
  1306
alpar@400
  1307
      return e;
alpar@400
  1308
    }
alpar@400
  1309
alpar@400
  1310
  private:
alpar@400
  1311
    void eraseEdge(int n) {
alpar@400
  1312
      
alpar@400
  1313
      if(edges[n].next_in!=-1)
alpar@400
  1314
	edges[edges[n].next_in].prev_in = edges[n].prev_in;
alpar@400
  1315
      if(edges[n].prev_in!=-1)
alpar@400
  1316
	edges[edges[n].prev_in].next_in = edges[n].next_in;
alpar@400
  1317
      else nodes[edges[n].head].first_in = edges[n].next_in;
alpar@400
  1318
      
alpar@400
  1319
      if(edges[n].next_out!=-1)
alpar@400
  1320
	edges[edges[n].next_out].prev_out = edges[n].prev_out;
alpar@400
  1321
      if(edges[n].prev_out!=-1)
alpar@400
  1322
	edges[edges[n].prev_out].next_out = edges[n].next_out;
alpar@400
  1323
      else nodes[edges[n].tail].first_out = edges[n].next_out;
alpar@400
  1324
      
alpar@400
  1325
      edges[n].next_in = first_free_edge;
alpar@400
  1326
      first_free_edge = -1;      
alpar@400
  1327
alpar@400
  1328
      //Update dynamic maps
alpar@400
  1329
      Edge e; e.n=n;
alpar@400
  1330
      for(typename std::vector<DynMapBase<Edge> * >::iterator
alpar@400
  1331
	    i=dyn_edge_maps.begin();
alpar@400
  1332
	  i!=dyn_edge_maps.end(); ++i) (**i).erase(e);
alpar@400
  1333
    }
alpar@400
  1334
      
alpar@400
  1335
  public:
alpar@400
  1336
alpar@400
  1337
//     void erase(Node nn) {
alpar@400
  1338
//       int n=nn.n;
alpar@400
  1339
//       int m;
alpar@400
  1340
//       while((m=nodes[n].first_in)!=-1) eraseEdge(m);
alpar@400
  1341
//       while((m=nodes[n].first_out)!=-1) eraseEdge(m);
alpar@400
  1342
//     }
alpar@400
  1343
    
alpar@400
  1344
    void erase(Edge e) { eraseEdge(e.n); }
alpar@400
  1345
alpar@400
  1346
//     //\bug Dynamic maps must be updated!
alpar@400
  1347
//     //
alpar@400
  1348
//     void clear() {
alpar@400
  1349
//       nodes.clear();edges.clear();
alpar@400
  1350
//       first_node=first_free_node=first_free_edge=-1;
alpar@400
  1351
//     }
alpar@400
  1352
alpar@400
  1353
    class Node : public NodeGraphType::Node {
alpar@400
  1354
      friend class EdgeSet;
alpar@400
  1355
      //      template <typename T> friend class NodeMap;
alpar@400
  1356
      
alpar@400
  1357
      friend class Edge;
alpar@400
  1358
      friend class OutEdgeIt;
alpar@400
  1359
      friend class InEdgeIt;
alpar@400
  1360
      friend class SymEdge;
alpar@400
  1361
alpar@400
  1362
    protected:
alpar@400
  1363
      friend int EdgeSet::id(Node v) const; 
alpar@400
  1364
      //      Node(int nn) {n=nn;}
alpar@400
  1365
    public:
alpar@400
  1366
      Node() : NodeGraphType::Node() {}
alpar@400
  1367
      Node (Invalid i) : NodeGraphType::Node(i) {}
alpar@400
  1368
      Node(const typename NodeGraphType::Node &n) : NodeGraphType::Node(n) {}
alpar@400
  1369
    };
alpar@400
  1370
    
alpar@400
  1371
    class NodeIt : public NodeGraphType::NodeIt {
alpar@400
  1372
      friend class EdgeSet;
alpar@400
  1373
    public:
alpar@400
  1374
      NodeIt() : NodeGraphType::NodeIt() { }
alpar@400
  1375
      NodeIt (Invalid i) : NodeGraphType::NodeIt(i) {}
alpar@400
  1376
      NodeIt(const EdgeSet& _G) : NodeGraphType::Node(_G.G) { }
alpar@400
  1377
      NodeIt(const typename NodeGraphType::NodeIt &n)
alpar@400
  1378
	: NodeGraphType::NodeIt(n) {}
alpar@400
  1379
      operator Node() { return Node(*this);}
alpar@400
  1380
    };
alpar@400
  1381
alpar@400
  1382
    class Edge {
alpar@400
  1383
      friend class EdgeSet;
alpar@400
  1384
      template <typename T> friend class EdgeMap;
alpar@400
  1385
alpar@400
  1386
      //template <typename T> friend class SymEdgeSet::SymEdgeMap;      
alpar@400
  1387
      //friend Edge SymEdgeSet::opposite(Edge) const;
alpar@400
  1388
      
alpar@400
  1389
      friend class Node;
alpar@400
  1390
      friend class NodeIt;
alpar@400
  1391
    protected:
alpar@400
  1392
      int n;
alpar@400
  1393
      friend int EdgeSet::id(Edge e) const;
alpar@400
  1394
alpar@400
  1395
      Edge(int nn) {n=nn;}
alpar@400
  1396
    public:
alpar@400
  1397
      Edge() { }
alpar@400
  1398
      Edge (Invalid) { n=-1; }
alpar@400
  1399
      bool operator==(const Edge i) const {return n==i.n;}
alpar@400
  1400
      bool operator!=(const Edge i) const {return n!=i.n;}
alpar@400
  1401
      bool operator<(const Edge i) const {return n<i.n;}
alpar@400
  1402
      ///\bug This is a workaround until somebody tells me how to
alpar@400
  1403
      ///make class \c SymEdgeSet::SymEdgeMap friend of Edge
alpar@400
  1404
      int &idref() {return n;}
alpar@400
  1405
      const int &idref() const {return n;}
alpar@400
  1406
    };
alpar@400
  1407
    
alpar@400
  1408
    class EdgeIt : public Edge {
alpar@400
  1409
      friend class EdgeSet;
alpar@400
  1410
    public:
alpar@400
  1411
      EdgeIt(const EdgeSet& G) : Edge() {
alpar@400
  1412
      	typename NodeGraphType::Node m;
alpar@400
  1413
	for(G.first(m);
alpar@400
  1414
	    G.valid(m) && nodes[m].first_in == -1;  G.next[m]);
alpar@400
  1415
	n = G.valid(m)?-1:nodes[m].first_in;
alpar@400
  1416
      }
alpar@400
  1417
      EdgeIt (Invalid i) : Edge(i) { }
alpar@400
  1418
      EdgeIt() : Edge() { }
alpar@400
  1419
      ///\bug This is a workaround until somebody tells me how to
alpar@400
  1420
      ///make class \c SymEdgeSet::SymEdgeMap friend of Edge
alpar@400
  1421
      int &idref() {return n;}
alpar@400
  1422
    };
alpar@400
  1423
    
alpar@400
  1424
    class OutEdgeIt : public Edge {
alpar@400
  1425
      friend class EdgeSet;
alpar@400
  1426
    public: 
alpar@400
  1427
      OutEdgeIt() : Edge() { }
alpar@400
  1428
      OutEdgeIt (Invalid i) : Edge(i) { }
alpar@400
  1429
alpar@400
  1430
      OutEdgeIt(const EdgeSet& G,const Node v) : Edge(nodes[v].first_out) { }
alpar@400
  1431
    };
alpar@400
  1432
    
alpar@400
  1433
    class InEdgeIt : public Edge {
alpar@400
  1434
      friend class EdgeSet;
alpar@400
  1435
    public: 
alpar@400
  1436
      InEdgeIt() : Edge() { }
alpar@400
  1437
      InEdgeIt (Invalid i) : Edge(i) { }
alpar@400
  1438
      InEdgeIt(const EdgeSet& G,Node v) :Edge(nodes[v].first_in) { }
alpar@400
  1439
    };
alpar@400
  1440
alpar@400
  1441
    template <typename T> class NodeMap : public NodeGraphType::NodeMap<T>
alpar@400
  1442
    {
alpar@400
  1443
    public:
alpar@400
  1444
      NodeMap(const EdgeSet &_G) :
alpar@400
  1445
	NodeGraphType::NodeMap<T>(_G.G) { }
alpar@400
  1446
      NodeMap(const EdgeSet &_G,const T &t) :
alpar@400
  1447
	NodeGraphType::NodeMap<T>(_G.G,t) { }
alpar@400
  1448
      //It is unnecessary
alpar@400
  1449
      NodeMap(const typename NodeGraphType::NodeMap<T> &m)
alpar@400
  1450
	: NodeGraphType::NodeMap<T>(m) { }
alpar@400
  1451
alpar@400
  1452
      ///\todo It can copy between different types.
alpar@400
  1453
      ///
alpar@400
  1454
      template<typename TT> friend class NodeMap;
alpar@400
  1455
      NodeMap(const typename NodeGraphType::NodeMap<TT> &m)
alpar@400
  1456
	: NodeGraphType::NodeMap<T>(m) { }
alpar@400
  1457
    };
alpar@400
  1458
    
alpar@400
  1459
    template <typename T> class EdgeMap : public DynMapBase<Edge>
alpar@400
  1460
    {
alpar@400
  1461
      std::vector<T> container;
alpar@400
  1462
alpar@400
  1463
    public:
alpar@400
  1464
      typedef T ValueType;
alpar@400
  1465
      typedef Edge KeyType;
alpar@400
  1466
alpar@400
  1467
      EdgeMap(const EdgeSet &_G) :
alpar@400
  1468
	DynMapBase<Edge>(_G), container(_G.maxEdgeId())
alpar@400
  1469
      {
alpar@400
  1470
	//FIXME: What if there are empty Id's?
alpar@400
  1471
	//FIXME: Can I use 'this' in a constructor?
alpar@400
  1472
	G->dyn_edge_maps.push_back(this);
alpar@400
  1473
      }
alpar@400
  1474
      EdgeMap(const EdgeSet &_G,const T &t) :
alpar@400
  1475
	DynMapBase<Edge>(_G), container(_G.maxEdgeId(),t)
alpar@400
  1476
      {
alpar@400
  1477
	G->dyn_edge_maps.push_back(this);
alpar@400
  1478
      } 
alpar@400
  1479
      EdgeMap(const EdgeMap<T> &m) :
alpar@400
  1480
 	DynMapBase<Edge>(*m.G), container(m.container)
alpar@400
  1481
      {
alpar@400
  1482
 	G->dyn_node_maps.push_back(this);
alpar@400
  1483
      }
alpar@400
  1484
alpar@400
  1485
      template<typename TT> friend class EdgeMap;
alpar@400
  1486
alpar@400
  1487
      ///\todo It can copy between different types.
alpar@400
  1488
      ///
alpar@400
  1489
      template<typename TT> EdgeMap(const EdgeMap<TT> &m) :
alpar@400
  1490
	DynMapBase<Edge>(*m.G)
alpar@400
  1491
      {
alpar@400
  1492
	G->dyn_node_maps.push_back(this);
alpar@400
  1493
	typename std::vector<TT>::const_iterator i;
alpar@400
  1494
	for(typename std::vector<TT>::const_iterator i=m.container.begin();
alpar@400
  1495
	    i!=m.container.end();
alpar@400
  1496
	    i++)
alpar@400
  1497
	  container.push_back(*i);
alpar@400
  1498
      }
alpar@400
  1499
      ~EdgeMap()
alpar@400
  1500
      {
alpar@400
  1501
	if(G) {
alpar@400
  1502
	  typename std::vector<DynMapBase<Edge>* >::iterator i;
alpar@400
  1503
	  for(i=G->dyn_edge_maps.begin();
alpar@400
  1504
	      i!=G->dyn_edge_maps.end() && *i!=this; ++i) ;
alpar@400
  1505
	  //if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow...
alpar@400
  1506
	  //A better way to do that: (Is this really important?)
alpar@400
  1507
	  if(*i==this) {
alpar@400
  1508
	    *i=G->dyn_edge_maps.back();
alpar@400
  1509
	    G->dyn_edge_maps.pop_back();
alpar@400
  1510
	  }
alpar@400
  1511
	}
alpar@400
  1512
      }
alpar@400
  1513
      
alpar@400
  1514
      void add(const Edge k) 
alpar@400
  1515
      {
alpar@400
  1516
	if(k.n>=int(container.size())) container.resize(k.n+1);
alpar@400
  1517
      }
alpar@400
  1518
      void erase(const Edge) { }
alpar@400
  1519
      
alpar@400
  1520
      void set(Edge n, T a) { container[n.n]=a; }
alpar@400
  1521
      //T get(Edge n) const { return container[n.n]; }
alpar@400
  1522
      typename std::vector<T>::reference
alpar@400
  1523
      operator[](Edge n) { return container[n.n]; }
alpar@400
  1524
      typename std::vector<T>::const_reference
alpar@400
  1525
      operator[](Edge n) const { return container[n.n]; }
alpar@400
  1526
alpar@400
  1527
      ///\warning There is no safety check at all!
alpar@400
  1528
      ///Using operator = between maps attached to different graph may
alpar@400
  1529
      ///cause serious problem.
alpar@400
  1530
      ///\todo Is this really so?
alpar@400
  1531
      ///\todo It can copy between different types.
alpar@400
  1532
      const EdgeMap<T>& operator=(const EdgeMap<T> &m)
alpar@400
  1533
      {
alpar@400
  1534
	container = m.container;
alpar@400
  1535
	return *this;
alpar@400
  1536
      }
alpar@400
  1537
      template<typename TT>
alpar@400
  1538
      const EdgeMap<T>& operator=(const EdgeMap<TT> &m)
alpar@400
  1539
      {
alpar@400
  1540
	copy(m.container.begin(), m.container.end(), container.begin());
alpar@400
  1541
	return *this;
alpar@400
  1542
      }
alpar@400
  1543
      
alpar@400
  1544
      void update() {}    //Useless for DynMaps
alpar@400
  1545
      void update(T a) {}  //Useless for DynMaps
alpar@400
  1546
    };
alpar@400
  1547
alpar@400
  1548
  };
alpar@400
  1549
alpar@400
  1550
alpar@400
  1551
alpar@400
  1552
alpar@400
  1553
alpar@400
  1554
alpar@400
  1555
alpar@395
  1556
  
alpar@395
  1557
} //namespace hugo
alpar@395
  1558
alpar@395
  1559
alpar@395
  1560
alpar@395
  1561
alpar@395
  1562
#endif //SMART_GRAPH_H