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