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