src/work/alpar/list_graph.h
author marci
Fri, 07 May 2004 10:57:31 +0000
changeset 576 d00c33d07114
parent 531 66f1c466889f
permissions -rw-r--r--
(none)
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
    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@503
   319
      Node (Invalid) { 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@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@395
   485
      std::vector<T> container;
alpar@395
   486
alpar@395
   487
    public:
alpar@395
   488
      typedef T ValueType;
alpar@395
   489
      typedef Edge KeyType;
alpar@395
   490
alpar@397
   491
      EdgeMap(const ListGraph &_G) :
alpar@395
   492
	DynMapBase<Edge>(_G), container(_G.maxEdgeId())
alpar@395
   493
      {
alpar@395
   494
	//FIXME: What if there are empty Id's?
alpar@395
   495
	//FIXME: Can I use 'this' in a constructor?
alpar@395
   496
	G->dyn_edge_maps.push_back(this);
alpar@395
   497
      }
alpar@397
   498
      EdgeMap(const ListGraph &_G,const T &t) :
alpar@395
   499
	DynMapBase<Edge>(_G), container(_G.maxEdgeId(),t)
alpar@395
   500
      {
alpar@395
   501
	G->dyn_edge_maps.push_back(this);
alpar@395
   502
      } 
alpar@395
   503
      EdgeMap(const EdgeMap<T> &m) :
alpar@395
   504
 	DynMapBase<Edge>(*m.G), container(m.container)
alpar@395
   505
      {
alpar@503
   506
 	G->dyn_edge_maps.push_back(this);
alpar@395
   507
      }
alpar@395
   508
alpar@395
   509
      template<typename TT> friend class EdgeMap;
alpar@395
   510
alpar@395
   511
      ///\todo It can copy between different types.
alpar@395
   512
      ///
alpar@395
   513
      template<typename TT> EdgeMap(const EdgeMap<TT> &m) :
alpar@395
   514
	DynMapBase<Edge>(*m.G)
alpar@395
   515
      {
alpar@503
   516
	G->dyn_edge_maps.push_back(this);
alpar@395
   517
	typename std::vector<TT>::const_iterator i;
alpar@395
   518
	for(typename std::vector<TT>::const_iterator i=m.container.begin();
alpar@395
   519
	    i!=m.container.end();
alpar@395
   520
	    i++)
alpar@395
   521
	  container.push_back(*i);
alpar@395
   522
      }
alpar@395
   523
      ~EdgeMap()
alpar@395
   524
      {
alpar@395
   525
	if(G) {
alpar@395
   526
	  std::vector<DynMapBase<Edge>* >::iterator i;
alpar@395
   527
	  for(i=G->dyn_edge_maps.begin();
alpar@395
   528
	      i!=G->dyn_edge_maps.end() && *i!=this; ++i) ;
alpar@395
   529
	  //if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow...
alpar@395
   530
	  //A better way to do that: (Is this really important?)
alpar@395
   531
	  if(*i==this) {
alpar@395
   532
	    *i=G->dyn_edge_maps.back();
alpar@395
   533
	    G->dyn_edge_maps.pop_back();
alpar@395
   534
	  }
alpar@395
   535
	}
alpar@395
   536
      }
alpar@395
   537
      
alpar@395
   538
      void add(const Edge k) 
alpar@395
   539
      {
alpar@395
   540
	if(k.n>=int(container.size())) container.resize(k.n+1);
alpar@395
   541
      }
alpar@395
   542
      void erase(const Edge) { }
alpar@395
   543
      
alpar@395
   544
      void set(Edge n, T a) { container[n.n]=a; }
alpar@395
   545
      //T get(Edge n) const { return container[n.n]; }
alpar@395
   546
      typename std::vector<T>::reference
alpar@395
   547
      operator[](Edge n) { return container[n.n]; }
alpar@395
   548
      typename std::vector<T>::const_reference
alpar@395
   549
      operator[](Edge n) const { return container[n.n]; }
alpar@395
   550
alpar@395
   551
      ///\warning There is no safety check at all!
alpar@395
   552
      ///Using operator = between maps attached to different graph may
alpar@395
   553
      ///cause serious problem.
alpar@395
   554
      ///\todo Is this really so?
alpar@395
   555
      ///\todo It can copy between different types.
alpar@395
   556
      const EdgeMap<T>& operator=(const EdgeMap<T> &m)
alpar@395
   557
      {
alpar@395
   558
	container = m.container;
alpar@395
   559
	return *this;
alpar@395
   560
      }
alpar@395
   561
      template<typename TT>
alpar@395
   562
      const EdgeMap<T>& operator=(const EdgeMap<TT> &m)
alpar@395
   563
      {
alpar@531
   564
	std::copy(m.container.begin(), m.container.end(), container.begin());
alpar@395
   565
	return *this;
alpar@395
   566
      }
alpar@395
   567
      
alpar@395
   568
      void update() {}    //Useless for DynMaps
alpar@395
   569
      void update(T a) {}  //Useless for DynMaps
alpar@395
   570
    };
alpar@395
   571
alpar@395
   572
  };
alpar@395
   573
alpar@395
   574
  ///Graph for bidirectional edges.
alpar@395
   575
alpar@395
   576
  ///The purpose of this graph structure is to handle graphs
alpar@395
   577
  ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair
alpar@395
   578
  ///of oppositely directed edges.
alpar@395
   579
  ///There is a new edge map type called
alpar@397
   580
  ///\ref SymListGraph::SymEdgeMap "SymEdgeMap"
alpar@395
   581
  ///that complements this
alpar@395
   582
  ///feature by
alpar@395
   583
  ///storing shared values for the edge pairs. The usual
alpar@395
   584
  ///\ref GraphSkeleton::EdgeMap "EdgeMap"
alpar@395
   585
  ///can be used
alpar@395
   586
  ///as well.
alpar@395
   587
  ///
alpar@395
   588
  ///The oppositely directed edge can also be obtained easily
alpar@395
   589
  ///using \ref opposite.
alpar@397
   590
  ///
alpar@397
   591
  ///Here erase(Edge) deletes a pair of edges.
alpar@397
   592
  ///
alpar@397
   593
  ///\todo this date structure need some reconsiderations. Maybe it
alpar@397
   594
  ///should be implemented independently from ListGraph.
alpar@395
   595
alpar@397
   596
  class SymListGraph : public ListGraph
alpar@395
   597
  {
alpar@395
   598
  public:
alpar@395
   599
    template<typename T> class SymEdgeMap;
alpar@395
   600
    template<typename T> friend class SymEdgeMap;
alpar@395
   601
alpar@397
   602
    SymListGraph() : ListGraph() { }
alpar@397
   603
    SymListGraph(const ListGraph &_g) : ListGraph(_g) { }
alpar@397
   604
    ///Adds a pair of oppositely directed edges to the graph.
alpar@395
   605
    Edge addEdge(Node u, Node v)
alpar@395
   606
    {
alpar@397
   607
      Edge e = ListGraph::addEdge(u,v);
alpar@397
   608
      ListGraph::addEdge(v,u);
alpar@395
   609
      return e;
alpar@395
   610
    }
alpar@395
   611
alpar@397
   612
    void erase(Node n) { ListGraph::erase(n); }
alpar@395
   613
    ///The oppositely directed edge.
alpar@395
   614
alpar@395
   615
    ///Returns the oppositely directed
alpar@395
   616
    ///pair of the edge \c e.
alpar@395
   617
    Edge opposite(Edge e) const
alpar@395
   618
    {
alpar@395
   619
      Edge f;
alpar@395
   620
      f.idref() = e.idref() - 2*(e.idref()%2) + 1;
alpar@395
   621
      return f;
alpar@395
   622
    }
alpar@395
   623
    
alpar@397
   624
    ///Removes a pair of oppositely directed edges to the graph.
alpar@397
   625
    void erase(Edge e) {
alpar@397
   626
      ListGraph::erase(opposite(e));
alpar@397
   627
      ListGraph::erase(e);
alpar@397
   628
    }
alpar@397
   629
    
alpar@395
   630
    ///Common data storage for the edge pairs.
alpar@395
   631
alpar@395
   632
    ///This map makes it possible to store data shared by the oppositely
alpar@395
   633
    ///directed pairs of edges.
alpar@395
   634
    template <typename T> class SymEdgeMap : public DynMapBase<Edge>
alpar@395
   635
    {
alpar@395
   636
      std::vector<T> container;
alpar@395
   637
      
alpar@395
   638
    public:
alpar@395
   639
      typedef T ValueType;
alpar@395
   640
      typedef Edge KeyType;
alpar@395
   641
alpar@397
   642
      SymEdgeMap(const SymListGraph &_G) :
alpar@395
   643
	DynMapBase<Edge>(_G), container(_G.maxEdgeId()/2)
alpar@395
   644
      {
alpar@397
   645
	static_cast<const SymListGraph*>(G)->dyn_edge_maps.push_back(this);
alpar@395
   646
      }
alpar@397
   647
      SymEdgeMap(const SymListGraph &_G,const T &t) :
alpar@395
   648
	DynMapBase<Edge>(_G), container(_G.maxEdgeId()/2,t)
alpar@395
   649
      {
alpar@395
   650
	G->dyn_edge_maps.push_back(this);
alpar@395
   651
      }
alpar@395
   652
alpar@395
   653
      SymEdgeMap(const SymEdgeMap<T> &m) :
alpar@395
   654
 	DynMapBase<SymEdge>(*m.G), container(m.container)
alpar@395
   655
      {
alpar@395
   656
 	G->dyn_node_maps.push_back(this);
alpar@395
   657
      }
alpar@395
   658
alpar@395
   659
      //      template<typename TT> friend class SymEdgeMap;
alpar@395
   660
alpar@395
   661
      ///\todo It can copy between different types.
alpar@395
   662
      ///
alpar@395
   663
alpar@395
   664
      template<typename TT> SymEdgeMap(const SymEdgeMap<TT> &m) :
alpar@395
   665
	DynMapBase<SymEdge>(*m.G)
alpar@395
   666
      {
alpar@395
   667
	G->dyn_node_maps.push_back(this);
alpar@395
   668
	typename std::vector<TT>::const_iterator i;
alpar@395
   669
	for(typename std::vector<TT>::const_iterator i=m.container.begin();
alpar@395
   670
	    i!=m.container.end();
alpar@395
   671
	    i++)
alpar@395
   672
	  container.push_back(*i);
alpar@395
   673
      }
alpar@395
   674
 
alpar@395
   675
      ~SymEdgeMap()
alpar@395
   676
      {
alpar@395
   677
	if(G) {
alpar@395
   678
	  std::vector<DynMapBase<Edge>* >::iterator i;
alpar@397
   679
	  for(i=static_cast<const SymListGraph*>(G)->dyn_edge_maps.begin();
alpar@397
   680
	      i!=static_cast<const SymListGraph*>(G)->dyn_edge_maps.end()
alpar@395
   681
		&& *i!=this; ++i) ;
alpar@395
   682
	  //if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow...
alpar@395
   683
	  //A better way to do that: (Is this really important?)
alpar@395
   684
	  if(*i==this) {
alpar@397
   685
	    *i=static_cast<const SymListGraph*>(G)->dyn_edge_maps.back();
alpar@397
   686
	    static_cast<const SymListGraph*>(G)->dyn_edge_maps.pop_back();
alpar@395
   687
	  }
alpar@395
   688
	}
alpar@395
   689
      }
alpar@395
   690
      
alpar@395
   691
      void add(const Edge k) 
alpar@395
   692
      {
alpar@395
   693
	if(!k.idref()%2&&k.idref()/2>=int(container.size()))
alpar@395
   694
	  container.resize(k.idref()/2+1);
alpar@395
   695
      }
alpar@395
   696
      void erase(const Edge k) { }
alpar@395
   697
      
alpar@395
   698
      void set(Edge n, T a) { container[n.idref()/2]=a; }
alpar@395
   699
      //T get(Edge n) const { return container[n.idref()/2]; }
alpar@395
   700
      typename std::vector<T>::reference
alpar@395
   701
      operator[](Edge n) { return container[n.idref()/2]; }
alpar@395
   702
      typename std::vector<T>::const_reference
alpar@395
   703
      operator[](Edge n) const { return container[n.idref()/2]; }
alpar@395
   704
alpar@395
   705
      ///\warning There is no safety check at all!
alpar@395
   706
      ///Using operator = between maps attached to different graph may
alpar@395
   707
      ///cause serious problem.
alpar@395
   708
      ///\todo Is this really so?
alpar@395
   709
      ///\todo It can copy between different types.
alpar@395
   710
      const SymEdgeMap<T>& operator=(const SymEdgeMap<T> &m)
alpar@395
   711
      {
alpar@395
   712
	container = m.container;
alpar@395
   713
	return *this;
alpar@395
   714
      }
alpar@395
   715
      template<typename TT>
alpar@395
   716
      const SymEdgeMap<T>& operator=(const SymEdgeMap<TT> &m)
alpar@395
   717
      {
alpar@531
   718
	std::copy(m.container.begin(), m.container.end(), container.begin());
alpar@395
   719
	return *this;
alpar@395
   720
      }
alpar@395
   721
      
alpar@395
   722
      void update() {}    //Useless for DynMaps
alpar@395
   723
      void update(T a) {}  //Useless for DynMaps
alpar@395
   724
alpar@395
   725
    };
alpar@395
   726
alpar@395
   727
  };
alpar@395
   728
  
alpar@400
   729
alpar@401
   730
  ///A graph class containing only nodes.
alpar@400
   731
alpar@401
   732
  ///This class implements a graph structure without edges.
alpar@401
   733
  ///The most useful application of this class is to be the node set of an
alpar@401
   734
  ///\ref EdgeSet class.
alpar@400
   735
  ///
alpar@400
   736
  ///It conforms to the graph interface documented under
alpar@401
   737
  ///the description of \ref GraphSkeleton with the exception that you cannot
alpar@401
   738
  ///add (or delete) edges. The usual edge iterators are exists, but they are
alpar@401
   739
  ///always \ref INVALID.
alpar@401
   740
  ///\sa \ref GraphSkeleton
alpar@508
   741
  ///\sa \ref EdgeSet
alpar@400
   742
  class NodeSet {
alpar@400
   743
alpar@400
   744
    //Nodes are double linked.
alpar@400
   745
    //The free nodes are only single linked using the "next" field.
alpar@400
   746
    struct NodeT 
alpar@400
   747
    {
alpar@400
   748
      int first_in,first_out;
alpar@400
   749
      int prev, next;
alpar@400
   750
      //      NodeT() {}
alpar@400
   751
    };
alpar@400
   752
alpar@400
   753
    std::vector<NodeT> nodes;
alpar@400
   754
    //The first node
alpar@400
   755
    int first_node;
alpar@400
   756
    //The first free node
alpar@400
   757
    int first_free_node;
alpar@400
   758
    
alpar@400
   759
  protected:
alpar@400
   760
    
alpar@400
   761
    template <typename Key> class DynMapBase
alpar@400
   762
    {
alpar@400
   763
    protected:
alpar@400
   764
      const NodeSet* G; 
alpar@400
   765
    public:
alpar@515
   766
      virtual void add(const Key k) = 0;
alpar@515
   767
      virtual void erase(const Key k) = 0;
alpar@400
   768
      DynMapBase(const NodeSet &_G) : G(&_G) {}
alpar@400
   769
      virtual ~DynMapBase() {}
alpar@400
   770
      friend class NodeSet;
alpar@400
   771
    };
alpar@400
   772
    
alpar@400
   773
  public:
alpar@400
   774
    template <typename T> class EdgeMap;
alpar@400
   775
    template <typename T> class NodeMap;
alpar@400
   776
    
alpar@400
   777
    class Node;
alpar@400
   778
    class Edge;
alpar@400
   779
alpar@400
   780
    //  protected:
alpar@400
   781
    // HELPME:
alpar@400
   782
  protected:
alpar@400
   783
    ///\bug It must be public because of SymEdgeMap.
alpar@400
   784
    ///
alpar@400
   785
    mutable std::vector<DynMapBase<Node> * > dyn_node_maps;
alpar@400
   786
    //mutable std::vector<DynMapBase<Edge> * > dyn_edge_maps;
alpar@400
   787
    
alpar@400
   788
  public:
alpar@400
   789
alpar@400
   790
    class NodeIt;
alpar@400
   791
    class EdgeIt;
alpar@400
   792
    class OutEdgeIt;
alpar@400
   793
    class InEdgeIt;
alpar@400
   794
    
alpar@400
   795
    template <typename T> class NodeMap;
alpar@400
   796
    template <typename T> class EdgeMap;
alpar@400
   797
    
alpar@400
   798
  public:
alpar@400
   799
alpar@408
   800
    ///Default constructor
alpar@400
   801
    NodeSet() : nodes(), first_node(-1),
alpar@400
   802
		  first_free_node(-1) {}
alpar@408
   803
    ///Copy constructor
alpar@400
   804
    NodeSet(const NodeSet &_g) : nodes(_g.nodes), first_node(_g.first_node),
alpar@400
   805
				     first_free_node(_g.first_free_node) {}
alpar@400
   806
    
alpar@400
   807
    ~NodeSet()
alpar@400
   808
    {
alpar@400
   809
      for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
alpar@400
   810
	  i!=dyn_node_maps.end(); ++i) (**i).G=NULL;
alpar@400
   811
      //for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin();
alpar@400
   812
      //	  i!=dyn_edge_maps.end(); ++i) (**i).G=NULL;
alpar@400
   813
    }
alpar@400
   814
alpar@400
   815
    int nodeNum() const { return nodes.size(); }  //FIXME: What is this?
alpar@400
   816
    int edgeNum() const { return 0; }  //FIXME: What is this?
alpar@400
   817
alpar@400
   818
    ///\bug This function does something different than
alpar@400
   819
    ///its name would suggests...
alpar@400
   820
    int maxNodeId() const { return nodes.size(); }  //FIXME: What is this?
alpar@400
   821
    ///\bug This function does something different than
alpar@400
   822
    ///its name would suggests...
alpar@400
   823
    int maxEdgeId() const { return 0; }  //FIXME: What is this?
alpar@400
   824
alpar@400
   825
    Node tail(Edge e) const { return INVALID; }
alpar@400
   826
    Node head(Edge e) const { return INVALID; }
alpar@400
   827
alpar@400
   828
    Node aNode(OutEdgeIt e) const { return INVALID; }
alpar@400
   829
    Node aNode(InEdgeIt e) const { return INVALID; }
alpar@400
   830
alpar@400
   831
    Node bNode(OutEdgeIt e) const { return INVALID; }
alpar@400
   832
    Node bNode(InEdgeIt e) const { return INVALID; }
alpar@400
   833
alpar@400
   834
    NodeIt& first(NodeIt& v) const { 
alpar@400
   835
      v=NodeIt(*this); return v; }
alpar@400
   836
    EdgeIt& first(EdgeIt& e) const { 
alpar@400
   837
      e=EdgeIt(*this); return e; }
alpar@400
   838
    OutEdgeIt& first(OutEdgeIt& e, const Node v) const { 
alpar@400
   839
      e=OutEdgeIt(*this,v); return e; }
alpar@400
   840
    InEdgeIt& first(InEdgeIt& e, const Node v) const { 
alpar@400
   841
      e=InEdgeIt(*this,v); return e; }
alpar@400
   842
alpar@400
   843
//     template< typename It >
alpar@400
   844
//     It first() const { It e; first(e); return e; }
alpar@400
   845
alpar@400
   846
//     template< typename It >
alpar@400
   847
//     It first(Node v) const { It e; first(e,v); return e; }
alpar@400
   848
alpar@400
   849
    bool valid(Edge e) const { return false; }
alpar@400
   850
    bool valid(Node n) const { return n.n!=-1; }
alpar@400
   851
    
alpar@400
   852
    void setInvalid(Edge &e) { }
alpar@400
   853
    void setInvalid(Node &n) { n.n=-1; }
alpar@400
   854
    
alpar@400
   855
    template <typename It> It getNext(It it) const
alpar@400
   856
    { It tmp(it); return next(tmp); }
alpar@400
   857
alpar@400
   858
    NodeIt& next(NodeIt& it) const { 
alpar@400
   859
      it.n=nodes[it.n].next; 
alpar@400
   860
      return it; 
alpar@400
   861
    }
alpar@400
   862
    OutEdgeIt& next(OutEdgeIt& it) const { return it; }
alpar@400
   863
    InEdgeIt& next(InEdgeIt& it) const { return it; }
alpar@400
   864
    EdgeIt& next(EdgeIt& it) const { return it; }
alpar@400
   865
alpar@400
   866
    int id(Node v) const { return v.n; }
alpar@400
   867
    int id(Edge e) const { return -1; }
alpar@400
   868
alpar@400
   869
    /// Adds a new node to the graph.
alpar@400
   870
alpar@400
   871
    /// \todo It adds the nodes in a reversed order.
alpar@400
   872
    /// (i.e. the lastly added node becomes the first.)
alpar@400
   873
    Node addNode() {
alpar@400
   874
      int n;
alpar@400
   875
      
alpar@400
   876
      if(first_free_node==-1)
alpar@400
   877
	{
alpar@400
   878
	  n = nodes.size();
alpar@400
   879
	  nodes.push_back(NodeT());
alpar@400
   880
	}
alpar@400
   881
      else {
alpar@400
   882
	n = first_free_node;
alpar@400
   883
	first_free_node = nodes[n].next;
alpar@400
   884
      }
alpar@400
   885
      
alpar@400
   886
      nodes[n].next = first_node;
alpar@400
   887
      if(first_node != -1) nodes[first_node].prev = n;
alpar@400
   888
      first_node = n;
alpar@400
   889
      nodes[n].prev = -1;
alpar@400
   890
      
alpar@400
   891
      nodes[n].first_in = nodes[n].first_out = -1;
alpar@400
   892
      
alpar@400
   893
      Node nn; nn.n=n;
alpar@400
   894
alpar@400
   895
      //Update dynamic maps
alpar@400
   896
      for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
alpar@400
   897
	  i!=dyn_node_maps.end(); ++i) (**i).add(nn);
alpar@400
   898
alpar@400
   899
      return nn;
alpar@400
   900
    }
alpar@400
   901
    
alpar@400
   902
    void erase(Node nn) {
alpar@400
   903
      int n=nn.n;
alpar@400
   904
      
alpar@400
   905
      if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev;
alpar@400
   906
      if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next;
alpar@400
   907
      else first_node = nodes[n].next;
alpar@400
   908
      
alpar@400
   909
      nodes[n].next = first_free_node;
alpar@400
   910
      first_free_node = n;
alpar@400
   911
alpar@400
   912
      //Update dynamic maps
alpar@400
   913
      for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
alpar@400
   914
	  i!=dyn_node_maps.end(); ++i) (**i).erase(nn);
alpar@400
   915
    }
alpar@400
   916
    
alpar@400
   917
    ///\bug Dynamic maps must be updated!
alpar@400
   918
    ///
alpar@400
   919
    void clear() {
alpar@400
   920
      nodes.clear();
alpar@400
   921
      first_node = first_free_node = -1;
alpar@400
   922
    }
alpar@400
   923
alpar@400
   924
    class Node {
alpar@400
   925
      friend class NodeSet;
alpar@400
   926
      template <typename T> friend class NodeMap;
alpar@400
   927
      
alpar@400
   928
      friend class Edge;
alpar@400
   929
      friend class OutEdgeIt;
alpar@400
   930
      friend class InEdgeIt;
alpar@400
   931
alpar@400
   932
    protected:
alpar@400
   933
      int n;
alpar@400
   934
      friend int NodeSet::id(Node v) const; 
alpar@400
   935
      Node(int nn) {n=nn;}
alpar@400
   936
    public:
alpar@400
   937
      Node() {}
alpar@400
   938
      Node (Invalid i) { n=-1; }
alpar@400
   939
      bool operator==(const Node i) const {return n==i.n;}
alpar@400
   940
      bool operator!=(const Node i) const {return n!=i.n;}
alpar@400
   941
      bool operator<(const Node i) const {return n<i.n;}
alpar@400
   942
    };
alpar@400
   943
    
alpar@400
   944
    class NodeIt : public Node {
alpar@400
   945
      friend class NodeSet;
alpar@400
   946
    public:
alpar@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@531
  1084
	std::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@515
  1155
  public:
alpar@400
  1156
    class Node;
alpar@531
  1157
    int id(Node v) const; 
alpar@531
  1158
alpar@531
  1159
    class Node : public NodeGraphType::Node {
alpar@531
  1160
      friend class EdgeSet;
alpar@531
  1161
      //      template <typename T> friend class NodeMap;
alpar@531
  1162
      
alpar@531
  1163
      friend class Edge;
alpar@531
  1164
      friend class OutEdgeIt;
alpar@531
  1165
      friend class InEdgeIt;
alpar@531
  1166
      friend class SymEdge;
alpar@531
  1167
alpar@531
  1168
    public:
alpar@531
  1169
      friend int EdgeSet::id(Node v) const; 
alpar@531
  1170
      //      Node(int nn) {n=nn;}
alpar@531
  1171
    public:
alpar@531
  1172
      Node() : NodeGraphType::Node() {}
alpar@531
  1173
      Node (Invalid i) : NodeGraphType::Node(i) {}
alpar@531
  1174
      Node(const typename NodeGraphType::Node &n) : NodeGraphType::Node(n) {}
alpar@531
  1175
    };
alpar@531
  1176
    
alpar@531
  1177
    class NodeIt : public NodeGraphType::NodeIt {
alpar@531
  1178
      friend class EdgeSet;
alpar@531
  1179
    public:
alpar@531
  1180
      NodeIt() : NodeGraphType::NodeIt() { }
alpar@531
  1181
      NodeIt (Invalid i) : NodeGraphType::NodeIt(i) {}
alpar@531
  1182
      NodeIt(const EdgeSet& _G) : NodeGraphType::NodeIt(_G.G) { }
alpar@531
  1183
      NodeIt(const typename NodeGraphType::NodeIt &n)
alpar@531
  1184
	: NodeGraphType::NodeIt(n) {}
alpar@531
  1185
      operator Node() { return Node(*this);}
alpar@531
  1186
    };
alpar@515
  1187
alpar@515
  1188
  private:
alpar@400
  1189
    //Edges are double linked.
alpar@400
  1190
    //The free edges are only single linked using the "next_in" field.
alpar@400
  1191
    struct NodeT 
alpar@400
  1192
    {
alpar@400
  1193
      int first_in,first_out;
alpar@400
  1194
      NodeT() : first_in(-1), first_out(-1) { }
alpar@400
  1195
    };
alpar@400
  1196
alpar@400
  1197
    struct EdgeT 
alpar@400
  1198
    {
alpar@400
  1199
      Node head, tail;
alpar@400
  1200
      int prev_in, prev_out;
alpar@400
  1201
      int next_in, next_out;
alpar@400
  1202
    };
alpar@400
  1203
alpar@400
  1204
    
alpar@515
  1205
    typename NodeGraphType::template NodeMap<NodeT> nodes;
alpar@400
  1206
    
alpar@400
  1207
    std::vector<EdgeT> edges;
alpar@400
  1208
    //The first free edge
alpar@400
  1209
    int first_free_edge;
alpar@400
  1210
    
alpar@400
  1211
  protected:
alpar@400
  1212
    
alpar@400
  1213
    template <typename Key> class DynMapBase
alpar@400
  1214
    {
alpar@400
  1215
    protected:
alpar@400
  1216
      const EdgeSet* G; 
alpar@400
  1217
    public:
alpar@515
  1218
      virtual void add(const Key k) = 0;
alpar@515
  1219
      virtual void erase(const Key k) = 0;
alpar@400
  1220
      DynMapBase(const EdgeSet &_G) : G(&_G) {}
alpar@400
  1221
      virtual ~DynMapBase() {}
alpar@400
  1222
      friend class EdgeSet;
alpar@400
  1223
    };
alpar@400
  1224
    
alpar@400
  1225
  public:
alpar@400
  1226
    //template <typename T> class NodeMap;
alpar@400
  1227
    template <typename T> class EdgeMap;
alpar@400
  1228
    
alpar@400
  1229
    class Node;
alpar@400
  1230
    class Edge;
alpar@400
  1231
alpar@400
  1232
    //  protected:
alpar@400
  1233
    // HELPME:
alpar@400
  1234
  protected:
alpar@400
  1235
    // mutable std::vector<DynMapBase<Node> * > dyn_node_maps;
alpar@400
  1236
    ///\bug It must be public because of SymEdgeMap.
alpar@400
  1237
    ///
alpar@400
  1238
    mutable std::vector<DynMapBase<Edge> * > dyn_edge_maps;
alpar@400
  1239
    
alpar@400
  1240
  public:
alpar@400
  1241
alpar@400
  1242
    class NodeIt;
alpar@400
  1243
    class EdgeIt;
alpar@400
  1244
    class OutEdgeIt;
alpar@400
  1245
    class InEdgeIt;
alpar@400
  1246
    
alpar@400
  1247
    template <typename T> class NodeMap;
alpar@400
  1248
    template <typename T> class EdgeMap;
alpar@400
  1249
    
alpar@400
  1250
  public:
alpar@400
  1251
alpar@408
  1252
    ///Constructor
alpar@408
  1253
    
alpar@408
  1254
    ///Construates a new graph based on the nodeset of an existing one.
alpar@408
  1255
    ///\param _G the base graph.
alpar@408
  1256
    ///\todo It looks like a copy constructor, but it isn't.
alpar@401
  1257
    EdgeSet(NodeGraphType &_G) : G(_G),
alpar@401
  1258
				 nodes(_G), edges(),
alpar@401
  1259
				 first_free_edge(-1) { }
alpar@408
  1260
    ///Copy constructor
alpar@408
  1261
alpar@408
  1262
    ///Makes a copy of an EdgeSet.
alpar@408
  1263
    ///It will be based on the same graph.
alpar@400
  1264
    EdgeSet(const EdgeSet &_g) : G(_g.G), nodes(_g.G), edges(_g.edges),
alpar@401
  1265
				 first_free_edge(_g.first_free_edge) { }
alpar@400
  1266
    
alpar@400
  1267
    ~EdgeSet()
alpar@400
  1268
    {
alpar@400
  1269
      // for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
alpar@400
  1270
      //  i!=dyn_node_maps.end(); ++i) (**i).G=NULL;
alpar@400
  1271
      for(typename std::vector<DynMapBase<Edge> * >::iterator
alpar@400
  1272
	    i=dyn_edge_maps.begin();
alpar@400
  1273
	  i!=dyn_edge_maps.end(); ++i) (**i).G=NULL;
alpar@400
  1274
    }
alpar@400
  1275
alpar@400
  1276
    int nodeNum() const { return G.nodeNum(); }  //FIXME: What is this?
alpar@400
  1277
    int edgeNum() const { return edges.size(); }  //FIXME: What is this?
alpar@400
  1278
alpar@400
  1279
    ///\bug This function does something different than
alpar@400
  1280
    ///its name would suggests...
alpar@400
  1281
    int maxNodeId() const { return G.maxNodeId(); }  //FIXME: What is this?
alpar@400
  1282
    ///\bug This function does something different than
alpar@400
  1283
    ///its name would suggests...
alpar@400
  1284
    int maxEdgeId() const { return edges.size(); }  //FIXME: What is this?
alpar@400
  1285
alpar@400
  1286
    Node tail(Edge e) const { return edges[e.n].tail; }
alpar@400
  1287
    Node head(Edge e) const { return edges[e.n].head; }
alpar@400
  1288
alpar@400
  1289
    Node aNode(OutEdgeIt e) const { return edges[e.n].tail; }
alpar@400
  1290
    Node aNode(InEdgeIt e) const { return edges[e.n].head; }
alpar@400
  1291
alpar@400
  1292
    Node bNode(OutEdgeIt e) const { return edges[e.n].head; }
alpar@400
  1293
    Node bNode(InEdgeIt e) const { return edges[e.n].tail; }
alpar@400
  1294
alpar@400
  1295
    NodeIt& first(NodeIt& v) const { 
alpar@400
  1296
      v=NodeIt(*this); return v; }
alpar@400
  1297
    EdgeIt& first(EdgeIt& e) const { 
alpar@400
  1298
      e=EdgeIt(*this); return e; }
alpar@400
  1299
    OutEdgeIt& first(OutEdgeIt& e, const Node v) const { 
alpar@400
  1300
      e=OutEdgeIt(*this,v); return e; }
alpar@400
  1301
    InEdgeIt& first(InEdgeIt& e, const Node v) const { 
alpar@400
  1302
      e=InEdgeIt(*this,v); return e; }
alpar@400
  1303
alpar@400
  1304
//     template< typename It >
alpar@400
  1305
//     It first() const { It e; first(e); return e; }
alpar@400
  1306
alpar@400
  1307
//     template< typename It >
alpar@400
  1308
//     It first(Node v) const { It e; first(e,v); return e; }
alpar@400
  1309
alpar@400
  1310
    bool valid(Edge e) const { return e.n!=-1; }
alpar@400
  1311
    bool valid(Node n) const { return G.valid(n); }
alpar@400
  1312
    
alpar@400
  1313
    void setInvalid(Edge &e) { e.n=-1; }
alpar@400
  1314
    void setInvalid(Node &n) { G.setInvalid(n); }
alpar@400
  1315
    
alpar@400
  1316
    template <typename It> It getNext(It it) const
alpar@400
  1317
    { It tmp(it); return next(tmp); }
alpar@400
  1318
alpar@400
  1319
    NodeIt& next(NodeIt& it) const { G.next(it); return it; }
alpar@400
  1320
    OutEdgeIt& next(OutEdgeIt& it) const
alpar@400
  1321
    { it.n=edges[it.n].next_out; return it; }
alpar@400
  1322
    InEdgeIt& next(InEdgeIt& it) const
alpar@400
  1323
    { it.n=edges[it.n].next_in; return it; }
alpar@400
  1324
    EdgeIt& next(EdgeIt& it) const {
alpar@400
  1325
      if(edges[it.n].next_in!=-1) { 
alpar@400
  1326
	it.n=edges[it.n].next_in;
alpar@400
  1327
      }
alpar@400
  1328
      else {
alpar@503
  1329
	NodeIt n;
alpar@503
  1330
	for(n=next(edges[it.n].head);
alpar@503
  1331
	    valid(n) && nodes[n].first_in == -1;
alpar@503
  1332
	    next(n)) ;
alpar@503
  1333
	it.n = (valid(n))?-1:nodes[n].first_in;
alpar@400
  1334
      }
alpar@400
  1335
      return it;
alpar@400
  1336
    }
alpar@400
  1337
alpar@400
  1338
    int id(Edge e) const { return e.n; }
alpar@400
  1339
alpar@400
  1340
    /// Adds a new node to the graph.
alpar@400
  1341
    Node addNode() { return G.AddNode(); }
alpar@400
  1342
    
alpar@400
  1343
    Edge addEdge(Node u, Node v) {
alpar@400
  1344
      int n;
alpar@400
  1345
      
alpar@400
  1346
      if(first_free_edge==-1)
alpar@400
  1347
	{
alpar@400
  1348
	  n = edges.size();
alpar@400
  1349
	  edges.push_back(EdgeT());
alpar@400
  1350
	}
alpar@400
  1351
      else {
alpar@400
  1352
	n = first_free_edge;
alpar@400
  1353
	first_free_edge = edges[n].next_in;
alpar@400
  1354
      }
alpar@400
  1355
      
alpar@401
  1356
      edges[n].tail = u; edges[n].head = v;
alpar@400
  1357
alpar@401
  1358
      edges[n].next_out = nodes[u].first_out;
alpar@401
  1359
      if(nodes[u].first_out != -1) edges[nodes[u].first_out].prev_out = n;
alpar@401
  1360
      edges[n].next_in = nodes[v].first_in;
alpar@401
  1361
      if(nodes[v].first_in != -1) edges[nodes[v].first_in].prev_in = n;
alpar@400
  1362
      edges[n].prev_in = edges[n].prev_out = -1;
alpar@400
  1363
	
alpar@401
  1364
      nodes[u].first_out = nodes[v].first_in = n;
alpar@400
  1365
alpar@400
  1366
      Edge e; e.n=n;
alpar@400
  1367
alpar@400
  1368
      //Update dynamic maps
alpar@400
  1369
      for(typename std::vector<DynMapBase<Edge> * >::iterator
alpar@400
  1370
	    i=dyn_edge_maps.begin();
alpar@400
  1371
	  i!=dyn_edge_maps.end(); ++i) (**i).add(e);
alpar@400
  1372
alpar@400
  1373
      return e;
alpar@400
  1374
    }
alpar@400
  1375
alpar@400
  1376
  private:
alpar@400
  1377
    void eraseEdge(int n) {
alpar@400
  1378
      
alpar@400
  1379
      if(edges[n].next_in!=-1)
alpar@400
  1380
	edges[edges[n].next_in].prev_in = edges[n].prev_in;
alpar@400
  1381
      if(edges[n].prev_in!=-1)
alpar@400
  1382
	edges[edges[n].prev_in].next_in = edges[n].next_in;
alpar@400
  1383
      else nodes[edges[n].head].first_in = edges[n].next_in;
alpar@400
  1384
      
alpar@400
  1385
      if(edges[n].next_out!=-1)
alpar@400
  1386
	edges[edges[n].next_out].prev_out = edges[n].prev_out;
alpar@400
  1387
      if(edges[n].prev_out!=-1)
alpar@400
  1388
	edges[edges[n].prev_out].next_out = edges[n].next_out;
alpar@400
  1389
      else nodes[edges[n].tail].first_out = edges[n].next_out;
alpar@400
  1390
      
alpar@400
  1391
      edges[n].next_in = first_free_edge;
alpar@400
  1392
      first_free_edge = -1;      
alpar@400
  1393
alpar@400
  1394
      //Update dynamic maps
alpar@400
  1395
      Edge e; e.n=n;
alpar@400
  1396
      for(typename std::vector<DynMapBase<Edge> * >::iterator
alpar@400
  1397
	    i=dyn_edge_maps.begin();
alpar@400
  1398
	  i!=dyn_edge_maps.end(); ++i) (**i).erase(e);
alpar@400
  1399
    }
alpar@400
  1400
      
alpar@400
  1401
  public:
alpar@400
  1402
alpar@400
  1403
//     void erase(Node nn) {
alpar@400
  1404
//       int n=nn.n;
alpar@400
  1405
//       int m;
alpar@400
  1406
//       while((m=nodes[n].first_in)!=-1) eraseEdge(m);
alpar@400
  1407
//       while((m=nodes[n].first_out)!=-1) eraseEdge(m);
alpar@400
  1408
//     }
alpar@400
  1409
    
alpar@400
  1410
    void erase(Edge e) { eraseEdge(e.n); }
alpar@400
  1411
alpar@400
  1412
//     //\bug Dynamic maps must be updated!
alpar@400
  1413
//     //
alpar@400
  1414
//     void clear() {
alpar@400
  1415
//       nodes.clear();edges.clear();
alpar@400
  1416
//       first_node=first_free_node=first_free_edge=-1;
alpar@400
  1417
//     }
alpar@400
  1418
alpar@400
  1419
    class Edge {
alpar@400
  1420
      friend class EdgeSet;
alpar@400
  1421
      template <typename T> friend class EdgeMap;
alpar@400
  1422
alpar@400
  1423
      //template <typename T> friend class SymEdgeSet::SymEdgeMap;      
alpar@400
  1424
      //friend Edge SymEdgeSet::opposite(Edge) const;
alpar@400
  1425
      
alpar@400
  1426
      friend class Node;
alpar@400
  1427
      friend class NodeIt;
alpar@400
  1428
    protected:
alpar@400
  1429
      int n;
alpar@400
  1430
      friend int EdgeSet::id(Edge e) const;
alpar@400
  1431
alpar@400
  1432
      Edge(int nn) {n=nn;}
alpar@400
  1433
    public:
alpar@400
  1434
      Edge() { }
alpar@400
  1435
      Edge (Invalid) { n=-1; }
alpar@400
  1436
      bool operator==(const Edge i) const {return n==i.n;}
alpar@400
  1437
      bool operator!=(const Edge i) const {return n!=i.n;}
alpar@400
  1438
      bool operator<(const Edge i) const {return n<i.n;}
alpar@400
  1439
      ///\bug This is a workaround until somebody tells me how to
alpar@400
  1440
      ///make class \c SymEdgeSet::SymEdgeMap friend of Edge
alpar@400
  1441
      int &idref() {return n;}
alpar@400
  1442
      const int &idref() const {return n;}
alpar@400
  1443
    };
alpar@400
  1444
    
alpar@400
  1445
    class EdgeIt : public Edge {
alpar@400
  1446
      friend class EdgeSet;
alpar@400
  1447
    public:
alpar@400
  1448
      EdgeIt(const EdgeSet& G) : Edge() {
alpar@503
  1449
	//      	typename NodeGraphType::Node m;
alpar@503
  1450
        NodeIt m;
alpar@400
  1451
	for(G.first(m);
alpar@503
  1452
	    G.valid(m) && G.nodes[m].first_in == -1;  G.next(m));
alpar@515
  1453
	//AJJAJ! This is a non sense!!!!!!!
alpar@515
  1454
	this->n = G.valid(m)?-1:G.nodes[m].first_in;
alpar@400
  1455
      }
alpar@400
  1456
      EdgeIt (Invalid i) : Edge(i) { }
alpar@400
  1457
      EdgeIt() : Edge() { }
alpar@400
  1458
      ///\bug This is a workaround until somebody tells me how to
alpar@400
  1459
      ///make class \c SymEdgeSet::SymEdgeMap friend of Edge
alpar@515
  1460
      int &idref() {return this->n;}
alpar@400
  1461
    };
alpar@400
  1462
    
alpar@400
  1463
    class OutEdgeIt : public Edge {
alpar@400
  1464
      friend class EdgeSet;
alpar@400
  1465
    public: 
alpar@400
  1466
      OutEdgeIt() : Edge() { }
alpar@400
  1467
      OutEdgeIt (Invalid i) : Edge(i) { }
alpar@400
  1468
alpar@400
  1469
      OutEdgeIt(const EdgeSet& G,const Node v) : Edge(nodes[v].first_out) { }
alpar@400
  1470
    };
alpar@400
  1471
    
alpar@400
  1472
    class InEdgeIt : public Edge {
alpar@400
  1473
      friend class EdgeSet;
alpar@400
  1474
    public: 
alpar@400
  1475
      InEdgeIt() : Edge() { }
alpar@400
  1476
      InEdgeIt (Invalid i) : Edge(i) { }
alpar@400
  1477
      InEdgeIt(const EdgeSet& G,Node v) :Edge(nodes[v].first_in) { }
alpar@400
  1478
    };
alpar@400
  1479
alpar@515
  1480
    template <typename T> class NodeMap : 
alpar@515
  1481
      public NodeGraphType::template NodeMap<T>
alpar@400
  1482
    {
alpar@400
  1483
    public:
alpar@400
  1484
      NodeMap(const EdgeSet &_G) :
alpar@515
  1485
        NodeGraphType::NodeMap(_G.G) { } //AJAJJ <T> would be wrong!!!
alpar@400
  1486
      NodeMap(const EdgeSet &_G,const T &t) :
alpar@515
  1487
	NodeGraphType::NodeMap(_G.G,t) { }
alpar@400
  1488
      //It is unnecessary
alpar@515
  1489
      NodeMap(const typename NodeGraphType::template NodeMap<T> &m) :
alpar@515
  1490
	NodeGraphType::NodeMap(m) { }
alpar@400
  1491
alpar@400
  1492
      ///\todo It can copy between different types.
alpar@400
  1493
      ///
alpar@401
  1494
      template<typename TT>
alpar@515
  1495
      NodeMap(const typename NodeGraphType::template NodeMap<TT> &m)
alpar@515
  1496
	: NodeGraphType::NodeMap(m) { }
alpar@400
  1497
    };
alpar@400
  1498
    
alpar@400
  1499
    template <typename T> class EdgeMap : public DynMapBase<Edge>
alpar@400
  1500
    {
alpar@400
  1501
      std::vector<T> container;
alpar@400
  1502
alpar@400
  1503
    public:
alpar@400
  1504
      typedef T ValueType;
alpar@400
  1505
      typedef Edge KeyType;
alpar@400
  1506
alpar@400
  1507
      EdgeMap(const EdgeSet &_G) :
alpar@400
  1508
	DynMapBase<Edge>(_G), container(_G.maxEdgeId())
alpar@400
  1509
      {
alpar@400
  1510
	//FIXME: What if there are empty Id's?
alpar@400
  1511
	//FIXME: Can I use 'this' in a constructor?
alpar@400
  1512
	G->dyn_edge_maps.push_back(this);
alpar@400
  1513
      }
alpar@400
  1514
      EdgeMap(const EdgeSet &_G,const T &t) :
alpar@400
  1515
	DynMapBase<Edge>(_G), container(_G.maxEdgeId(),t)
alpar@400
  1516
      {
alpar@400
  1517
	G->dyn_edge_maps.push_back(this);
alpar@400
  1518
      } 
alpar@400
  1519
      EdgeMap(const EdgeMap<T> &m) :
alpar@400
  1520
 	DynMapBase<Edge>(*m.G), container(m.container)
alpar@400
  1521
      {
alpar@503
  1522
 	G->dyn_edge_maps.push_back(this);
alpar@400
  1523
      }
alpar@400
  1524
alpar@400
  1525
      template<typename TT> friend class EdgeMap;
alpar@400
  1526
alpar@400
  1527
      ///\todo It can copy between different types.
alpar@400
  1528
      ///
alpar@400
  1529
      template<typename TT> EdgeMap(const EdgeMap<TT> &m) :
alpar@400
  1530
	DynMapBase<Edge>(*m.G)
alpar@400
  1531
      {
alpar@503
  1532
	G->dyn_edge_maps.push_back(this);
alpar@400
  1533
	typename std::vector<TT>::const_iterator i;
alpar@400
  1534
	for(typename std::vector<TT>::const_iterator i=m.container.begin();
alpar@400
  1535
	    i!=m.container.end();
alpar@400
  1536
	    i++)
alpar@400
  1537
	  container.push_back(*i);
alpar@400
  1538
      }
alpar@400
  1539
      ~EdgeMap()
alpar@400
  1540
      {
alpar@400
  1541
	if(G) {
alpar@400
  1542
	  typename std::vector<DynMapBase<Edge>* >::iterator i;
alpar@400
  1543
	  for(i=G->dyn_edge_maps.begin();
alpar@400
  1544
	      i!=G->dyn_edge_maps.end() && *i!=this; ++i) ;
alpar@400
  1545
	  //if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow...
alpar@400
  1546
	  //A better way to do that: (Is this really important?)
alpar@400
  1547
	  if(*i==this) {
alpar@400
  1548
	    *i=G->dyn_edge_maps.back();
alpar@400
  1549
	    G->dyn_edge_maps.pop_back();
alpar@400
  1550
	  }
alpar@400
  1551
	}
alpar@400
  1552
      }
alpar@400
  1553
      
alpar@400
  1554
      void add(const Edge k) 
alpar@400
  1555
      {
alpar@400
  1556
	if(k.n>=int(container.size())) container.resize(k.n+1);
alpar@400
  1557
      }
alpar@400
  1558
      void erase(const Edge) { }
alpar@400
  1559
      
alpar@400
  1560
      void set(Edge n, T a) { container[n.n]=a; }
alpar@400
  1561
      //T get(Edge n) const { return container[n.n]; }
alpar@400
  1562
      typename std::vector<T>::reference
alpar@400
  1563
      operator[](Edge n) { return container[n.n]; }
alpar@400
  1564
      typename std::vector<T>::const_reference
alpar@400
  1565
      operator[](Edge n) const { return container[n.n]; }
alpar@400
  1566
alpar@400
  1567
      ///\warning There is no safety check at all!
alpar@400
  1568
      ///Using operator = between maps attached to different graph may
alpar@400
  1569
      ///cause serious problem.
alpar@400
  1570
      ///\todo Is this really so?
alpar@400
  1571
      ///\todo It can copy between different types.
alpar@400
  1572
      const EdgeMap<T>& operator=(const EdgeMap<T> &m)
alpar@400
  1573
      {
alpar@400
  1574
	container = m.container;
alpar@400
  1575
	return *this;
alpar@400
  1576
      }
alpar@400
  1577
      template<typename TT>
alpar@400
  1578
      const EdgeMap<T>& operator=(const EdgeMap<TT> &m)
alpar@400
  1579
      {
alpar@531
  1580
	std::copy(m.container.begin(), m.container.end(), container.begin());
alpar@400
  1581
	return *this;
alpar@400
  1582
      }
alpar@400
  1583
      
alpar@400
  1584
      void update() {}    //Useless for DynMaps
alpar@400
  1585
      void update(T a) {}  //Useless for DynMaps
alpar@400
  1586
    };
alpar@400
  1587
alpar@400
  1588
  };
alpar@406
  1589
alpar@531
  1590
  template< typename GG>
alpar@531
  1591
  int EdgeSet<GG>::id(Node v) const { return G.id(v); }
alpar@531
  1592
alpar@406
  1593
/// @}  
alpar@406
  1594
alpar@395
  1595
} //namespace hugo
alpar@395
  1596
alpar@405
  1597
#endif //HUGO_LIST_GRAPH_H