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