src/include/smart_graph.h
author alpar
Sun, 25 Apr 2004 22:28:19 +0000
changeset 402 f90f65ba21d5
parent 398 ecebcedd8960
child 407 e34e1bc610cf
permissions -rw-r--r--
A missing conversion added
alpar@105
     1
// -*- mode:C++ -*-
alpar@105
     2
alpar@185
     3
#ifndef HUGO_SMART_GRAPH_H
alpar@185
     4
#define HUGO_SMART_GRAPH_H
alpar@104
     5
alpar@242
     6
///\file
alpar@242
     7
///\brief SmartGraph and SymSmartGraph classes.
alpar@242
     8
alpar@104
     9
#include <vector>
alpar@129
    10
#include <limits.h>
alpar@104
    11
alpar@253
    12
#include "invalid.h"
alpar@157
    13
alpar@105
    14
namespace hugo {
alpar@104
    15
alpar@185
    16
  class SymSmartGraph;
alpar@185
    17
alpar@186
    18
  ///A smart graph class.
alpar@186
    19
alpar@186
    20
  ///This is a simple and fast graph implementation.
alpar@186
    21
  ///It is also quite memory efficient, but at the price
alpar@186
    22
  ///that <b> it does not support node and edge deletion</b>.
alpar@242
    23
  ///It conforms to the graph interface documented under
alpar@186
    24
  ///the description of \ref GraphSkeleton.
alpar@186
    25
  ///\sa \ref GraphSkeleton.
alpar@402
    26
  ///
alpar@402
    27
  ///\todo Some member functions could be \c static.
alpar@104
    28
  class SmartGraph {
alpar@104
    29
alpar@104
    30
    struct NodeT 
alpar@104
    31
    {
alpar@104
    32
      int first_in,first_out;      
alpar@157
    33
      NodeT() : first_in(-1), first_out(-1) {}
alpar@104
    34
    };
alpar@104
    35
    struct EdgeT 
alpar@104
    36
    {
alpar@104
    37
      int head, tail, next_in, next_out;      
alpar@104
    38
      //FIXME: is this necessary?
alpar@157
    39
      EdgeT() : next_in(-1), next_out(-1) {}  
alpar@104
    40
    };
alpar@104
    41
alpar@104
    42
    std::vector<NodeT> nodes;
alpar@129
    43
alpar@104
    44
    std::vector<EdgeT> edges;
alpar@104
    45
    
alpar@185
    46
    protected:
alpar@185
    47
    
alpar@108
    48
    template <typename Key> class DynMapBase
alpar@108
    49
    {
alpar@108
    50
    protected:
alpar@185
    51
      const SmartGraph* G; 
alpar@108
    52
    public:
alpar@108
    53
      virtual void add(const Key k) = NULL;
alpar@108
    54
      virtual void erase(const Key k) = NULL;
alpar@157
    55
      DynMapBase(const SmartGraph &_G) : G(&_G) {}
alpar@108
    56
      virtual ~DynMapBase() {}
alpar@108
    57
      friend class SmartGraph;
alpar@108
    58
    };
alpar@185
    59
    
alpar@104
    60
  public:
alpar@185
    61
    template <typename T> class EdgeMap;
alpar@185
    62
    template <typename T> class EdgeMap;
alpar@104
    63
alpar@164
    64
    class Node;
alpar@164
    65
    class Edge;
alpar@108
    66
alpar@185
    67
    //  protected:
alpar@185
    68
    // HELPME:
alpar@186
    69
  protected:
alpar@185
    70
    ///\bug It must be public because of SymEdgeMap.
alpar@185
    71
    ///
alpar@164
    72
    mutable std::vector<DynMapBase<Node> * > dyn_node_maps;
alpar@185
    73
    ///\bug It must be public because of SymEdgeMap.
alpar@185
    74
    ///
alpar@164
    75
    mutable std::vector<DynMapBase<Edge> * > dyn_edge_maps;
alpar@108
    76
    
alpar@108
    77
  public:
alpar@108
    78
alpar@164
    79
    class NodeIt;
alpar@164
    80
    class EdgeIt;
alpar@104
    81
    class OutEdgeIt;
alpar@104
    82
    class InEdgeIt;
alpar@104
    83
    
alpar@105
    84
    template <typename T> class NodeMap;
alpar@104
    85
    template <typename T> class EdgeMap;
alpar@104
    86
    
alpar@104
    87
  public:
alpar@104
    88
alpar@104
    89
    SmartGraph() : nodes(), edges() { }
alpar@136
    90
    SmartGraph(const SmartGraph &_g) : nodes(_g.nodes), edges(_g.edges) { }
alpar@104
    91
    
alpar@108
    92
    ~SmartGraph()
alpar@108
    93
    {
alpar@164
    94
      for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
alpar@108
    95
	  i!=dyn_node_maps.end(); ++i) (**i).G=NULL;
alpar@164
    96
      for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin();
alpar@108
    97
	  i!=dyn_edge_maps.end(); ++i) (**i).G=NULL;
alpar@108
    98
    }
alpar@104
    99
alpar@104
   100
    int nodeNum() const { return nodes.size(); }  //FIXME: What is this?
alpar@104
   101
    int edgeNum() const { return edges.size(); }  //FIXME: What is this?
alpar@104
   102
alpar@186
   103
    ///\bug This function does something different than
alpar@186
   104
    ///its name would suggests...
alpar@108
   105
    int maxNodeId() const { return nodes.size(); }  //FIXME: What is this?
alpar@186
   106
    ///\bug This function does something different than
alpar@186
   107
    ///its name would suggests...
alpar@108
   108
    int maxEdgeId() const { return edges.size(); }  //FIXME: What is this?
alpar@108
   109
alpar@164
   110
    Node tail(Edge e) const { return edges[e.n].tail; }
alpar@164
   111
    Node head(Edge e) const { return edges[e.n].head; }
alpar@104
   112
marci@174
   113
    Node aNode(OutEdgeIt e) const { return edges[e.n].tail; }
marci@174
   114
    Node aNode(InEdgeIt e) const { return edges[e.n].head; }
alpar@104
   115
marci@174
   116
    Node bNode(OutEdgeIt e) const { return edges[e.n].head; }
marci@174
   117
    Node bNode(InEdgeIt e) const { return edges[e.n].tail; }
alpar@104
   118
alpar@164
   119
    NodeIt& first(NodeIt& v) const { 
alpar@164
   120
      v=NodeIt(*this); return v; }
alpar@164
   121
    EdgeIt& first(EdgeIt& e) const { 
alpar@164
   122
      e=EdgeIt(*this); return e; }
alpar@164
   123
    OutEdgeIt& first(OutEdgeIt& e, const Node v) const { 
alpar@104
   124
      e=OutEdgeIt(*this,v); return e; }
alpar@164
   125
    InEdgeIt& first(InEdgeIt& e, const Node v) const { 
alpar@104
   126
      e=InEdgeIt(*this,v); return e; }
alpar@104
   127
marci@353
   128
//     template< typename It >
marci@353
   129
//     It first() const { It e; first(e); return e; }
alpar@104
   130
marci@353
   131
//     template< typename It >
marci@353
   132
//     It first(Node v) const { It e; first(e,v); return e; }
alpar@104
   133
alpar@164
   134
    bool valid(Edge e) const { return e.n!=-1; }
alpar@164
   135
    bool valid(Node n) const { return n.n!=-1; }
alpar@104
   136
    
alpar@164
   137
    void setInvalid(Edge &e) { e.n=-1; }
alpar@164
   138
    void setInvalid(Node &n) { n.n=-1; }
alpar@129
   139
    
alpar@157
   140
    template <typename It> It getNext(It it) const
alpar@157
   141
    { It tmp(it); return next(tmp); }
alpar@104
   142
marci@174
   143
    NodeIt& next(NodeIt& it) const { 
marci@174
   144
      it.n=(it.n+2)%(nodes.size()+1)-1; 
marci@174
   145
      return it; 
marci@174
   146
    }
alpar@157
   147
    OutEdgeIt& next(OutEdgeIt& it) const
alpar@104
   148
    { it.n=edges[it.n].next_out; return it; }
alpar@157
   149
    InEdgeIt& next(InEdgeIt& it) const
alpar@104
   150
    { it.n=edges[it.n].next_in; return it; }
alpar@164
   151
    EdgeIt& next(EdgeIt& it) const { --it.n; return it; }
alpar@104
   152
alpar@164
   153
    int id(Node v) const { return v.n; }
alpar@164
   154
    int id(Edge e) const { return e.n; }
alpar@104
   155
alpar@164
   156
    Node addNode() {
alpar@164
   157
      Node n; n.n=nodes.size();
alpar@104
   158
      nodes.push_back(NodeT()); //FIXME: Hmmm...
alpar@108
   159
alpar@164
   160
      for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
alpar@398
   161
	  i!=dyn_node_maps.end(); ++i) (**i).add(n);
alpar@108
   162
alpar@104
   163
      return n;
alpar@104
   164
    }
alpar@108
   165
    
alpar@164
   166
    Edge addEdge(Node u, Node v) {
alpar@164
   167
      Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
alpar@104
   168
      edges[e.n].tail=u.n; edges[e.n].head=v.n;
alpar@104
   169
      edges[e.n].next_out=nodes[u.n].first_out;
alpar@104
   170
      edges[e.n].next_in=nodes[v.n].first_in;
alpar@104
   171
      nodes[u.n].first_out=nodes[v.n].first_in=e.n;
alpar@108
   172
alpar@164
   173
      for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin();
alpar@157
   174
	  i!=dyn_edge_maps.end(); ++i) (**i).add(e);
alpar@108
   175
alpar@104
   176
      return e;
alpar@104
   177
    }
alpar@104
   178
alpar@104
   179
    void clear() {nodes.clear();edges.clear();}
alpar@104
   180
alpar@164
   181
    class Node {
alpar@104
   182
      friend class SmartGraph;
alpar@104
   183
      template <typename T> friend class NodeMap;
alpar@104
   184
      
alpar@164
   185
      friend class Edge;
alpar@104
   186
      friend class OutEdgeIt;
alpar@104
   187
      friend class InEdgeIt;
alpar@164
   188
      friend class SymEdge;
alpar@104
   189
alpar@104
   190
    protected:
alpar@104
   191
      int n;
alpar@164
   192
      friend int SmartGraph::id(Node v) const; 
alpar@164
   193
      Node(int nn) {n=nn;}
alpar@104
   194
    public:
alpar@164
   195
      Node() {}
alpar@164
   196
      Node (Invalid i) { n=-1; }
alpar@164
   197
      bool operator==(const Node i) const {return n==i.n;}
alpar@164
   198
      bool operator!=(const Node i) const {return n!=i.n;}
alpar@164
   199
      bool operator<(const Node i) const {return n<i.n;}
alpar@104
   200
    };
alpar@104
   201
    
alpar@164
   202
    class NodeIt : public Node {
alpar@104
   203
      friend class SmartGraph;
alpar@104
   204
    public:
alpar@402
   205
      NodeIt() : Node() { }
alpar@402
   206
      NodeIt(Invalid i) : Node(i) { }
alpar@164
   207
      NodeIt(const SmartGraph& G) : Node(G.nodes.size()?0:-1) { }
alpar@104
   208
    };
alpar@104
   209
alpar@164
   210
    class Edge {
alpar@104
   211
      friend class SmartGraph;
alpar@104
   212
      template <typename T> friend class EdgeMap;
alpar@185
   213
alpar@185
   214
      //template <typename T> friend class SymSmartGraph::SymEdgeMap;      
alpar@185
   215
      //friend Edge SymSmartGraph::opposite(Edge) const;
alpar@104
   216
      
alpar@164
   217
      friend class Node;
alpar@104
   218
      friend class NodeIt;
alpar@104
   219
    protected:
alpar@104
   220
      int n;
alpar@164
   221
      friend int SmartGraph::id(Edge e) const;
alpar@157
   222
alpar@164
   223
      Edge(int nn) {n=nn;}
alpar@104
   224
    public:
alpar@164
   225
      Edge() { }
marci@174
   226
      Edge (Invalid) { n=-1; }
alpar@164
   227
      bool operator==(const Edge i) const {return n==i.n;}
alpar@164
   228
      bool operator!=(const Edge i) const {return n!=i.n;}
alpar@164
   229
      bool operator<(const Edge i) const {return n<i.n;}
alpar@185
   230
      ///\bug This is a workaround until somebody tells me how to
alpar@185
   231
      ///make class \c SymSmartGraph::SymEdgeMap friend of Edge
alpar@185
   232
      int &idref() {return n;}
alpar@185
   233
      const int &idref() const {return n;}
alpar@104
   234
    };
alpar@104
   235
    
alpar@164
   236
    class EdgeIt : public Edge {
alpar@104
   237
      friend class SmartGraph;
alpar@104
   238
    public:
alpar@164
   239
      EdgeIt(const SmartGraph& G) : Edge(G.edges.size()-1) { }
alpar@164
   240
      EdgeIt (Invalid i) : Edge(i) { }
alpar@164
   241
      EdgeIt() : Edge() { }
alpar@185
   242
      ///\bug This is a workaround until somebody tells me how to
alpar@185
   243
      ///make class \c SymSmartGraph::SymEdgeMap friend of Edge
alpar@185
   244
      int &idref() {return n;}
alpar@104
   245
    };
alpar@104
   246
    
alpar@164
   247
    class OutEdgeIt : public Edge {
alpar@104
   248
      friend class SmartGraph;
alpar@104
   249
    public: 
alpar@164
   250
      OutEdgeIt() : Edge() { }
alpar@164
   251
      OutEdgeIt (Invalid i) : Edge(i) { }
alpar@157
   252
alpar@164
   253
      OutEdgeIt(const SmartGraph& G,const Node v)
alpar@164
   254
	: Edge(G.nodes[v.n].first_out) {}
alpar@104
   255
    };
alpar@104
   256
    
alpar@164
   257
    class InEdgeIt : public Edge {
alpar@104
   258
      friend class SmartGraph;
alpar@104
   259
    public: 
alpar@164
   260
      InEdgeIt() : Edge() { }
alpar@164
   261
      InEdgeIt (Invalid i) : Edge(i) { }
alpar@164
   262
      InEdgeIt(const SmartGraph& G,Node v) :Edge(G.nodes[v.n].first_in){}
alpar@104
   263
    };
alpar@105
   264
alpar@185
   265
    template <typename T> class NodeMap : public DynMapBase<Node>
alpar@108
   266
    {
alpar@108
   267
      std::vector<T> container;
alpar@105
   268
alpar@108
   269
    public:
alpar@108
   270
      typedef T ValueType;
alpar@164
   271
      typedef Node KeyType;
alpar@105
   272
alpar@185
   273
      NodeMap(const SmartGraph &_G) :
alpar@164
   274
	DynMapBase<Node>(_G), container(_G.maxNodeId())
alpar@108
   275
      {
alpar@108
   276
	G->dyn_node_maps.push_back(this);
alpar@108
   277
      }
alpar@185
   278
      NodeMap(const SmartGraph &_G,const T &t) :
alpar@185
   279
	DynMapBase<Node>(_G), container(_G.maxNodeId(),t)
alpar@185
   280
      {
alpar@185
   281
	G->dyn_node_maps.push_back(this);
alpar@185
   282
      }
alpar@185
   283
      
alpar@185
   284
      NodeMap(const NodeMap<T> &m) :
alpar@185
   285
 	DynMapBase<Node>(*m.G), container(m.container)
alpar@185
   286
      {
alpar@185
   287
 	G->dyn_node_maps.push_back(this);
alpar@185
   288
      }
alpar@185
   289
alpar@185
   290
      template<typename TT> friend class NodeMap;
alpar@185
   291
 
alpar@185
   292
      ///\todo It can copy between different types.
alpar@185
   293
      ///
alpar@185
   294
      template<typename TT> NodeMap(const NodeMap<TT> &m) :
alpar@185
   295
	DynMapBase<Node>(*m.G)
alpar@185
   296
      {
alpar@185
   297
	G->dyn_node_maps.push_back(this);
alpar@185
   298
	typename std::vector<TT>::const_iterator i;
alpar@185
   299
	for(typename std::vector<TT>::const_iterator i=m.container.begin();
alpar@185
   300
	    i!=m.container.end();
alpar@185
   301
	    i++)
alpar@185
   302
	  container.push_back(*i);
alpar@185
   303
      }
alpar@185
   304
      ~NodeMap()
alpar@108
   305
      {
alpar@108
   306
	if(G) {
alpar@164
   307
	  std::vector<DynMapBase<Node>* >::iterator i;
alpar@108
   308
	  for(i=G->dyn_node_maps.begin();
alpar@108
   309
	      i!=G->dyn_node_maps.end() && *i!=this; ++i) ;
alpar@115
   310
	  //if(*i==this) G->dyn_node_maps.erase(i); //FIXME: Way too slow...
alpar@115
   311
	  //A better way to do that: (Is this really important?)
alpar@115
   312
	  if(*i==this) {
alpar@116
   313
	    *i=G->dyn_node_maps.back();
alpar@115
   314
	    G->dyn_node_maps.pop_back();
alpar@115
   315
	  }
alpar@108
   316
	}
alpar@108
   317
      }
alpar@105
   318
alpar@164
   319
      void add(const Node k) 
alpar@108
   320
      {
alpar@185
   321
	if(k.n>=int(container.size())) container.resize(k.n+1);
alpar@108
   322
      }
alpar@177
   323
alpar@215
   324
      void erase(const Node) { }
alpar@108
   325
      
alpar@164
   326
      void set(Node n, T a) { container[n.n]=a; }
alpar@285
   327
      //'T& operator[](Node n)' would be wrong here
alpar@215
   328
      typename std::vector<T>::reference
alpar@215
   329
      operator[](Node n) { return container[n.n]; }
alpar@285
   330
      //'const T& operator[](Node n)' would be wrong here
alpar@215
   331
      typename std::vector<T>::const_reference 
alpar@215
   332
      operator[](Node n) const { return container[n.n]; }
alpar@108
   333
alpar@185
   334
      ///\warning There is no safety check at all!
alpar@185
   335
      ///Using operator = between maps attached to different graph may
alpar@185
   336
      ///cause serious problem.
alpar@185
   337
      ///\todo Is this really so?
alpar@185
   338
      ///\todo It can copy between different types.
alpar@185
   339
      const NodeMap<T>& operator=(const NodeMap<T> &m)
alpar@185
   340
      {
alpar@185
   341
	container = m.container;
alpar@185
   342
	return *this;
alpar@185
   343
      }
alpar@185
   344
      template<typename TT>
alpar@185
   345
      const NodeMap<T>& operator=(const NodeMap<TT> &m)
alpar@185
   346
      {
alpar@185
   347
	copy(m.container.begin(), m.container.end(), container.begin());
alpar@185
   348
	return *this;
alpar@185
   349
      }
alpar@185
   350
      
alpar@285
   351
      void update() {}    //Useless for Dynamic Maps
alpar@285
   352
      void update(T a) {}  //Useless for Dynamic Maps
alpar@108
   353
    };
alpar@108
   354
    
alpar@185
   355
    template <typename T> class EdgeMap : public DynMapBase<Edge>
alpar@108
   356
    {
alpar@108
   357
      std::vector<T> container;
alpar@108
   358
alpar@108
   359
    public:
alpar@108
   360
      typedef T ValueType;
alpar@164
   361
      typedef Edge KeyType;
alpar@108
   362
alpar@185
   363
      EdgeMap(const SmartGraph &_G) :
alpar@164
   364
	DynMapBase<Edge>(_G), container(_G.maxEdgeId())
alpar@108
   365
      {
alpar@108
   366
	//FIXME: What if there are empty Id's?
alpar@115
   367
	//FIXME: Can I use 'this' in a constructor?
alpar@108
   368
	G->dyn_edge_maps.push_back(this);
alpar@108
   369
      }
alpar@185
   370
      EdgeMap(const SmartGraph &_G,const T &t) :
alpar@185
   371
	DynMapBase<Edge>(_G), container(_G.maxEdgeId(),t)
alpar@185
   372
      {
alpar@185
   373
	G->dyn_edge_maps.push_back(this);
alpar@185
   374
      } 
alpar@185
   375
      EdgeMap(const EdgeMap<T> &m) :
alpar@185
   376
 	DynMapBase<Edge>(*m.G), container(m.container)
alpar@185
   377
      {
alpar@185
   378
 	G->dyn_node_maps.push_back(this);
alpar@185
   379
      }
alpar@185
   380
alpar@185
   381
      template<typename TT> friend class EdgeMap;
alpar@185
   382
alpar@185
   383
      ///\todo It can copy between different types.
alpar@185
   384
      ///
alpar@185
   385
      template<typename TT> EdgeMap(const EdgeMap<TT> &m) :
alpar@185
   386
	DynMapBase<Edge>(*m.G)
alpar@185
   387
      {
alpar@185
   388
	G->dyn_node_maps.push_back(this);
alpar@185
   389
	typename std::vector<TT>::const_iterator i;
alpar@185
   390
	for(typename std::vector<TT>::const_iterator i=m.container.begin();
alpar@185
   391
	    i!=m.container.end();
alpar@185
   392
	    i++)
alpar@185
   393
	  container.push_back(*i);
alpar@185
   394
      }
alpar@185
   395
      ~EdgeMap()
alpar@108
   396
      {
alpar@108
   397
	if(G) {
alpar@164
   398
	  std::vector<DynMapBase<Edge>* >::iterator i;
alpar@108
   399
	  for(i=G->dyn_edge_maps.begin();
alpar@108
   400
	      i!=G->dyn_edge_maps.end() && *i!=this; ++i) ;
alpar@115
   401
	  //if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow...
alpar@115
   402
	  //A better way to do that: (Is this really important?)
alpar@115
   403
	  if(*i==this) {
alpar@116
   404
	    *i=G->dyn_edge_maps.back();
alpar@115
   405
	    G->dyn_edge_maps.pop_back();
alpar@115
   406
	  }
alpar@108
   407
	}
alpar@108
   408
      }
alpar@115
   409
      
alpar@164
   410
      void add(const Edge k) 
alpar@108
   411
      {
alpar@108
   412
	if(k.n>=int(container.size())) container.resize(k.n+1);
alpar@108
   413
      }
alpar@215
   414
      void erase(const Edge) { }
alpar@108
   415
      
alpar@164
   416
      void set(Edge n, T a) { container[n.n]=a; }
alpar@209
   417
      //T get(Edge n) const { return container[n.n]; }
alpar@215
   418
      typename std::vector<T>::reference
alpar@215
   419
      operator[](Edge n) { return container[n.n]; }
alpar@215
   420
      typename std::vector<T>::const_reference
alpar@215
   421
      operator[](Edge n) const { return container[n.n]; }
alpar@108
   422
alpar@185
   423
      ///\warning There is no safety check at all!
alpar@185
   424
      ///Using operator = between maps attached to different graph may
alpar@185
   425
      ///cause serious problem.
alpar@185
   426
      ///\todo Is this really so?
alpar@185
   427
      ///\todo It can copy between different types.
alpar@185
   428
      const EdgeMap<T>& operator=(const EdgeMap<T> &m)
alpar@185
   429
      {
alpar@185
   430
	container = m.container;
alpar@185
   431
	return *this;
alpar@185
   432
      }
alpar@185
   433
      template<typename TT>
alpar@185
   434
      const EdgeMap<T>& operator=(const EdgeMap<TT> &m)
alpar@185
   435
      {
alpar@185
   436
	copy(m.container.begin(), m.container.end(), container.begin());
alpar@185
   437
	return *this;
alpar@185
   438
      }
alpar@185
   439
      
alpar@108
   440
      void update() {}    //Useless for DynMaps
alpar@108
   441
      void update(T a) {}  //Useless for DynMaps
alpar@108
   442
    };
alpar@185
   443
alpar@104
   444
  };
alpar@185
   445
alpar@185
   446
  ///Graph for bidirectional edges.
alpar@185
   447
alpar@185
   448
  ///The purpose of this graph structure is to handle graphs
alpar@185
   449
  ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair
alpar@186
   450
  ///of oppositely directed edges.
alpar@186
   451
  ///There is a new edge map type called
alpar@186
   452
  ///\ref SymSmartGraph::SymEdgeMap "SymEdgeMap"
alpar@186
   453
  ///that complements this
alpar@186
   454
  ///feature by
alpar@186
   455
  ///storing shared values for the edge pairs. The usual
alpar@186
   456
  ///\ref GraphSkeleton::EdgeMap "EdgeMap"
alpar@186
   457
  ///can be used
alpar@185
   458
  ///as well.
alpar@185
   459
  ///
alpar@186
   460
  ///The oppositely directed edge can also be obtained easily
alpar@186
   461
  ///using \ref opposite.
alpar@186
   462
  ///\warning It shares the similarity with \ref SmartGraph that
alpar@186
   463
  ///it is not possible to delete edges or nodes from the graph.
alpar@186
   464
  //\sa \ref SmartGraph.
alpar@185
   465
alpar@185
   466
  class SymSmartGraph : public SmartGraph
alpar@185
   467
  {
alpar@185
   468
  public:
alpar@186
   469
    template<typename T> class SymEdgeMap;
alpar@186
   470
    template<typename T> friend class SymEdgeMap;
alpar@186
   471
alpar@185
   472
    SymSmartGraph() : SmartGraph() { }
alpar@185
   473
    SymSmartGraph(const SmartGraph &_g) : SmartGraph(_g) { }
alpar@398
   474
    ///Adds a pair of oppositely directed edges to the graph.
alpar@185
   475
    Edge addEdge(Node u, Node v)
alpar@185
   476
    {
alpar@185
   477
      Edge e = SmartGraph::addEdge(u,v);
alpar@185
   478
      SmartGraph::addEdge(v,u);
alpar@185
   479
      return e;
alpar@185
   480
    }
alpar@185
   481
alpar@186
   482
    ///The oppositely directed edge.
alpar@186
   483
alpar@186
   484
    ///Returns the oppositely directed
alpar@186
   485
    ///pair of the edge \c e.
alpar@185
   486
    Edge opposite(Edge e) const
alpar@185
   487
    {
alpar@185
   488
      Edge f;
alpar@185
   489
      f.idref() = e.idref() - 2*(e.idref()%2) + 1;
alpar@185
   490
      return f;
alpar@185
   491
    }
alpar@185
   492
    
alpar@186
   493
    ///Common data storage for the edge pairs.
alpar@186
   494
alpar@186
   495
    ///This map makes it possible to store data shared by the oppositely
alpar@186
   496
    ///directed pairs of edges.
alpar@185
   497
    template <typename T> class SymEdgeMap : public DynMapBase<Edge>
alpar@185
   498
    {
alpar@185
   499
      std::vector<T> container;
alpar@185
   500
      
alpar@185
   501
    public:
alpar@185
   502
      typedef T ValueType;
alpar@185
   503
      typedef Edge KeyType;
alpar@185
   504
alpar@186
   505
      SymEdgeMap(const SymSmartGraph &_G) :
alpar@185
   506
	DynMapBase<Edge>(_G), container(_G.maxEdgeId()/2)
alpar@185
   507
      {
alpar@186
   508
	static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.push_back(this);
alpar@185
   509
      }
alpar@186
   510
      SymEdgeMap(const SymSmartGraph &_G,const T &t) :
alpar@185
   511
	DynMapBase<Edge>(_G), container(_G.maxEdgeId()/2,t)
alpar@185
   512
      {
alpar@185
   513
	G->dyn_edge_maps.push_back(this);
alpar@185
   514
      }
alpar@185
   515
alpar@185
   516
      SymEdgeMap(const SymEdgeMap<T> &m) :
alpar@185
   517
 	DynMapBase<SymEdge>(*m.G), container(m.container)
alpar@185
   518
      {
alpar@185
   519
 	G->dyn_node_maps.push_back(this);
alpar@185
   520
      }
alpar@185
   521
alpar@185
   522
      //      template<typename TT> friend class SymEdgeMap;
alpar@185
   523
alpar@185
   524
      ///\todo It can copy between different types.
alpar@185
   525
      ///
alpar@185
   526
alpar@185
   527
      template<typename TT> SymEdgeMap(const SymEdgeMap<TT> &m) :
alpar@185
   528
	DynMapBase<SymEdge>(*m.G)
alpar@185
   529
      {
alpar@185
   530
	G->dyn_node_maps.push_back(this);
alpar@185
   531
	typename std::vector<TT>::const_iterator i;
alpar@185
   532
	for(typename std::vector<TT>::const_iterator i=m.container.begin();
alpar@185
   533
	    i!=m.container.end();
alpar@185
   534
	    i++)
alpar@185
   535
	  container.push_back(*i);
alpar@185
   536
      }
alpar@185
   537
 
alpar@185
   538
      ~SymEdgeMap()
alpar@185
   539
      {
alpar@185
   540
	if(G) {
alpar@185
   541
	  std::vector<DynMapBase<Edge>* >::iterator i;
alpar@186
   542
	  for(i=static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.begin();
alpar@186
   543
	      i!=static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.end()
alpar@186
   544
		&& *i!=this; ++i) ;
alpar@185
   545
	  //if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow...
alpar@185
   546
	  //A better way to do that: (Is this really important?)
alpar@185
   547
	  if(*i==this) {
alpar@186
   548
	    *i=static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.back();
alpar@186
   549
	    static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.pop_back();
alpar@185
   550
	  }
alpar@185
   551
	}
alpar@185
   552
      }
alpar@185
   553
      
alpar@185
   554
      void add(const Edge k) 
alpar@185
   555
      {
alpar@185
   556
	if(!k.idref()%2&&k.idref()/2>=int(container.size()))
alpar@185
   557
	  container.resize(k.idref()/2+1);
alpar@185
   558
      }
alpar@185
   559
      void erase(const Edge k) { }
alpar@185
   560
      
alpar@185
   561
      void set(Edge n, T a) { container[n.idref()/2]=a; }
alpar@209
   562
      //T get(Edge n) const { return container[n.idref()/2]; }
alpar@215
   563
      typename std::vector<T>::reference
alpar@215
   564
      operator[](Edge n) { return container[n.idref()/2]; }
alpar@215
   565
      typename std::vector<T>::const_reference
alpar@215
   566
      operator[](Edge n) const { return container[n.idref()/2]; }
alpar@185
   567
alpar@185
   568
      ///\warning There is no safety check at all!
alpar@185
   569
      ///Using operator = between maps attached to different graph may
alpar@185
   570
      ///cause serious problem.
alpar@185
   571
      ///\todo Is this really so?
alpar@185
   572
      ///\todo It can copy between different types.
alpar@185
   573
      const SymEdgeMap<T>& operator=(const SymEdgeMap<T> &m)
alpar@185
   574
      {
alpar@185
   575
	container = m.container;
alpar@185
   576
	return *this;
alpar@185
   577
      }
alpar@185
   578
      template<typename TT>
alpar@185
   579
      const SymEdgeMap<T>& operator=(const SymEdgeMap<TT> &m)
alpar@185
   580
      {
alpar@185
   581
	copy(m.container.begin(), m.container.end(), container.begin());
alpar@185
   582
	return *this;
alpar@185
   583
      }
alpar@185
   584
      
alpar@185
   585
      void update() {}    //Useless for DynMaps
alpar@185
   586
      void update(T a) {}  //Useless for DynMaps
alpar@185
   587
alpar@185
   588
    };
alpar@185
   589
alpar@185
   590
  };
alpar@185
   591
  
alpar@185
   592
  
alpar@105
   593
} //namespace hugo
alpar@104
   594
alpar@157
   595
alpar@157
   596
alpar@157
   597
alpar@104
   598
#endif //SMART_GRAPH_H