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