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