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