src/hugo/smart_graph.h
author klao
Tue, 11 May 2004 22:50:09 +0000
changeset 619 e09818232531
parent 579 859f8c7e2a40
child 706 0fe42b8ec5a6
permissions -rw-r--r--
path improvements
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@164
   139
    bool valid(Edge e) const { return e.n!=-1; }
alpar@164
   140
    bool valid(Node n) const { 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@164
   147
    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@164
   153
    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@164
   168
    int id(Node v) const { return v.n; }
alpar@164
   169
    int id(Edge e) const { 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@164
   240
      Edge(int nn) {n=nn;}
alpar@104
   241
    public:
alpar@164
   242
      Edge() { }
marci@174
   243
      Edge (Invalid) { n=-1; }
alpar@164
   244
      bool operator==(const Edge i) const {return n==i.n;}
alpar@164
   245
      bool operator!=(const Edge i) const {return n!=i.n;}
alpar@164
   246
      bool operator<(const Edge i) const {return n<i.n;}
alpar@185
   247
      ///\bug This is a workaround until somebody tells me how to
alpar@185
   248
      ///make class \c SymSmartGraph::SymEdgeMap friend of Edge
alpar@185
   249
      int &idref() {return n;}
alpar@185
   250
      const int &idref() const {return n;}
alpar@104
   251
    };
alpar@104
   252
    
alpar@164
   253
    class EdgeIt : public Edge {
alpar@104
   254
      friend class SmartGraph;
alpar@104
   255
    public:
alpar@164
   256
      EdgeIt(const SmartGraph& G) : Edge(G.edges.size()-1) { }
alpar@164
   257
      EdgeIt (Invalid i) : Edge(i) { }
alpar@164
   258
      EdgeIt() : Edge() { }
alpar@185
   259
      ///\bug This is a workaround until somebody tells me how to
alpar@185
   260
      ///make class \c SymSmartGraph::SymEdgeMap friend of Edge
alpar@185
   261
      int &idref() {return n;}
alpar@104
   262
    };
alpar@104
   263
    
alpar@164
   264
    class OutEdgeIt : public Edge {
alpar@104
   265
      friend class SmartGraph;
alpar@104
   266
    public: 
alpar@164
   267
      OutEdgeIt() : Edge() { }
alpar@164
   268
      OutEdgeIt (Invalid i) : Edge(i) { }
alpar@157
   269
alpar@164
   270
      OutEdgeIt(const SmartGraph& G,const Node v)
alpar@164
   271
	: Edge(G.nodes[v.n].first_out) {}
alpar@104
   272
    };
alpar@104
   273
    
alpar@164
   274
    class InEdgeIt : public Edge {
alpar@104
   275
      friend class SmartGraph;
alpar@104
   276
    public: 
alpar@164
   277
      InEdgeIt() : Edge() { }
alpar@164
   278
      InEdgeIt (Invalid i) : Edge(i) { }
alpar@164
   279
      InEdgeIt(const SmartGraph& G,Node v) :Edge(G.nodes[v.n].first_in){}
alpar@104
   280
    };
alpar@105
   281
alpar@185
   282
    template <typename T> class NodeMap : public DynMapBase<Node>
alpar@108
   283
    {
alpar@108
   284
      std::vector<T> container;
alpar@105
   285
alpar@108
   286
    public:
alpar@108
   287
      typedef T ValueType;
alpar@164
   288
      typedef Node KeyType;
alpar@105
   289
alpar@185
   290
      NodeMap(const SmartGraph &_G) :
alpar@164
   291
	DynMapBase<Node>(_G), container(_G.maxNodeId())
alpar@108
   292
      {
alpar@108
   293
	G->dyn_node_maps.push_back(this);
alpar@108
   294
      }
alpar@185
   295
      NodeMap(const SmartGraph &_G,const T &t) :
alpar@185
   296
	DynMapBase<Node>(_G), container(_G.maxNodeId(),t)
alpar@185
   297
      {
alpar@185
   298
	G->dyn_node_maps.push_back(this);
alpar@185
   299
      }
alpar@185
   300
      
alpar@185
   301
      NodeMap(const NodeMap<T> &m) :
alpar@185
   302
 	DynMapBase<Node>(*m.G), container(m.container)
alpar@185
   303
      {
alpar@185
   304
 	G->dyn_node_maps.push_back(this);
alpar@185
   305
      }
alpar@185
   306
alpar@185
   307
      template<typename TT> friend class NodeMap;
alpar@185
   308
 
alpar@185
   309
      ///\todo It can copy between different types.
alpar@590
   310
      ///\todo We could use 'copy'
alpar@185
   311
      template<typename TT> NodeMap(const NodeMap<TT> &m) :
alpar@590
   312
	DynMapBase<Node>(*m.G), container(m.container.size())
alpar@185
   313
      {
alpar@185
   314
	G->dyn_node_maps.push_back(this);
alpar@185
   315
	typename std::vector<TT>::const_iterator i;
alpar@185
   316
	for(typename std::vector<TT>::const_iterator i=m.container.begin();
alpar@185
   317
	    i!=m.container.end();
alpar@185
   318
	    i++)
alpar@185
   319
	  container.push_back(*i);
alpar@185
   320
      }
alpar@185
   321
      ~NodeMap()
alpar@108
   322
      {
alpar@108
   323
	if(G) {
alpar@164
   324
	  std::vector<DynMapBase<Node>* >::iterator i;
alpar@108
   325
	  for(i=G->dyn_node_maps.begin();
alpar@108
   326
	      i!=G->dyn_node_maps.end() && *i!=this; ++i) ;
alpar@115
   327
	  //if(*i==this) G->dyn_node_maps.erase(i); //FIXME: Way too slow...
alpar@115
   328
	  //A better way to do that: (Is this really important?)
alpar@115
   329
	  if(*i==this) {
alpar@116
   330
	    *i=G->dyn_node_maps.back();
alpar@115
   331
	    G->dyn_node_maps.pop_back();
alpar@115
   332
	  }
alpar@108
   333
	}
alpar@108
   334
      }
alpar@105
   335
alpar@164
   336
      void add(const Node k) 
alpar@108
   337
      {
alpar@185
   338
	if(k.n>=int(container.size())) container.resize(k.n+1);
alpar@108
   339
      }
alpar@177
   340
alpar@215
   341
      void erase(const Node) { }
alpar@108
   342
      
alpar@164
   343
      void set(Node n, T a) { container[n.n]=a; }
alpar@285
   344
      //'T& operator[](Node n)' would be wrong here
alpar@215
   345
      typename std::vector<T>::reference
alpar@215
   346
      operator[](Node n) { return container[n.n]; }
alpar@285
   347
      //'const T& operator[](Node n)' would be wrong here
alpar@215
   348
      typename std::vector<T>::const_reference 
alpar@215
   349
      operator[](Node n) const { return container[n.n]; }
alpar@108
   350
alpar@185
   351
      ///\warning There is no safety check at all!
alpar@185
   352
      ///Using operator = between maps attached to different graph may
alpar@185
   353
      ///cause serious problem.
alpar@185
   354
      ///\todo Is this really so?
alpar@185
   355
      ///\todo It can copy between different types.
alpar@185
   356
      const NodeMap<T>& operator=(const NodeMap<T> &m)
alpar@185
   357
      {
alpar@185
   358
	container = m.container;
alpar@185
   359
	return *this;
alpar@185
   360
      }
alpar@185
   361
      template<typename TT>
alpar@185
   362
      const NodeMap<T>& operator=(const NodeMap<TT> &m)
alpar@185
   363
      {
alpar@531
   364
	std::copy(m.container.begin(), m.container.end(), container.begin());
alpar@185
   365
	return *this;
alpar@185
   366
      }
alpar@185
   367
      
alpar@285
   368
      void update() {}    //Useless for Dynamic Maps
alpar@285
   369
      void update(T a) {}  //Useless for Dynamic Maps
alpar@108
   370
    };
alpar@108
   371
    
alpar@185
   372
    template <typename T> class EdgeMap : public DynMapBase<Edge>
alpar@108
   373
    {
alpar@108
   374
      std::vector<T> container;
alpar@108
   375
alpar@108
   376
    public:
alpar@108
   377
      typedef T ValueType;
alpar@164
   378
      typedef Edge KeyType;
alpar@108
   379
alpar@185
   380
      EdgeMap(const SmartGraph &_G) :
alpar@164
   381
	DynMapBase<Edge>(_G), container(_G.maxEdgeId())
alpar@108
   382
      {
alpar@108
   383
	//FIXME: What if there are empty Id's?
alpar@115
   384
	//FIXME: Can I use 'this' in a constructor?
alpar@108
   385
	G->dyn_edge_maps.push_back(this);
alpar@108
   386
      }
alpar@185
   387
      EdgeMap(const SmartGraph &_G,const T &t) :
alpar@185
   388
	DynMapBase<Edge>(_G), container(_G.maxEdgeId(),t)
alpar@185
   389
      {
alpar@185
   390
	G->dyn_edge_maps.push_back(this);
alpar@185
   391
      } 
alpar@185
   392
      EdgeMap(const EdgeMap<T> &m) :
alpar@185
   393
 	DynMapBase<Edge>(*m.G), container(m.container)
alpar@185
   394
      {
alpar@503
   395
 	G->dyn_edge_maps.push_back(this);
alpar@185
   396
      }
alpar@185
   397
alpar@185
   398
      template<typename TT> friend class EdgeMap;
alpar@185
   399
alpar@185
   400
      ///\todo It can copy between different types.
alpar@590
   401
      template<typename TT> EdgeMap(const EdgeMap<TT> &m)
alpar@590
   402
	: DynMapBase<Edge>(*m.G), container(m.container.size())
alpar@185
   403
      {
alpar@503
   404
	G->dyn_edge_maps.push_back(this);
alpar@185
   405
	typename std::vector<TT>::const_iterator i;
alpar@185
   406
	for(typename std::vector<TT>::const_iterator i=m.container.begin();
alpar@185
   407
	    i!=m.container.end();
alpar@185
   408
	    i++)
alpar@185
   409
	  container.push_back(*i);
alpar@185
   410
      }
alpar@185
   411
      ~EdgeMap()
alpar@108
   412
      {
alpar@108
   413
	if(G) {
alpar@164
   414
	  std::vector<DynMapBase<Edge>* >::iterator i;
alpar@108
   415
	  for(i=G->dyn_edge_maps.begin();
alpar@108
   416
	      i!=G->dyn_edge_maps.end() && *i!=this; ++i) ;
alpar@115
   417
	  //if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow...
alpar@115
   418
	  //A better way to do that: (Is this really important?)
alpar@115
   419
	  if(*i==this) {
alpar@116
   420
	    *i=G->dyn_edge_maps.back();
alpar@115
   421
	    G->dyn_edge_maps.pop_back();
alpar@115
   422
	  }
alpar@108
   423
	}
alpar@108
   424
      }
alpar@115
   425
      
alpar@164
   426
      void add(const Edge k) 
alpar@108
   427
      {
alpar@108
   428
	if(k.n>=int(container.size())) container.resize(k.n+1);
alpar@108
   429
      }
alpar@215
   430
      void erase(const Edge) { }
alpar@108
   431
      
alpar@164
   432
      void set(Edge n, T a) { container[n.n]=a; }
alpar@209
   433
      //T get(Edge n) const { return container[n.n]; }
alpar@215
   434
      typename std::vector<T>::reference
alpar@215
   435
      operator[](Edge n) { return container[n.n]; }
alpar@215
   436
      typename std::vector<T>::const_reference
alpar@215
   437
      operator[](Edge n) const { return container[n.n]; }
alpar@108
   438
alpar@185
   439
      ///\warning There is no safety check at all!
alpar@185
   440
      ///Using operator = between maps attached to different graph may
alpar@185
   441
      ///cause serious problem.
alpar@185
   442
      ///\todo Is this really so?
alpar@185
   443
      ///\todo It can copy between different types.
alpar@185
   444
      const EdgeMap<T>& operator=(const EdgeMap<T> &m)
alpar@185
   445
      {
alpar@185
   446
	container = m.container;
alpar@185
   447
	return *this;
alpar@185
   448
      }
alpar@185
   449
      template<typename TT>
alpar@185
   450
      const EdgeMap<T>& operator=(const EdgeMap<TT> &m)
alpar@185
   451
      {
alpar@531
   452
	std::copy(m.container.begin(), m.container.end(), container.begin());
alpar@185
   453
	return *this;
alpar@185
   454
      }
alpar@185
   455
      
alpar@108
   456
      void update() {}    //Useless for DynMaps
alpar@108
   457
      void update(T a) {}  //Useless for DynMaps
alpar@108
   458
    };
alpar@185
   459
alpar@104
   460
  };
alpar@185
   461
alpar@185
   462
  ///Graph for bidirectional edges.
alpar@185
   463
alpar@185
   464
  ///The purpose of this graph structure is to handle graphs
alpar@185
   465
  ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair
alpar@186
   466
  ///of oppositely directed edges.
alpar@186
   467
  ///There is a new edge map type called
alpar@186
   468
  ///\ref SymSmartGraph::SymEdgeMap "SymEdgeMap"
alpar@186
   469
  ///that complements this
alpar@186
   470
  ///feature by
alpar@186
   471
  ///storing shared values for the edge pairs. The usual
alpar@186
   472
  ///\ref GraphSkeleton::EdgeMap "EdgeMap"
alpar@186
   473
  ///can be used
alpar@185
   474
  ///as well.
alpar@185
   475
  ///
alpar@186
   476
  ///The oppositely directed edge can also be obtained easily
alpar@186
   477
  ///using \ref opposite.
alpar@186
   478
  ///\warning It shares the similarity with \ref SmartGraph that
alpar@186
   479
  ///it is not possible to delete edges or nodes from the graph.
alpar@186
   480
  //\sa \ref SmartGraph.
alpar@185
   481
alpar@185
   482
  class SymSmartGraph : public SmartGraph
alpar@185
   483
  {
alpar@185
   484
  public:
alpar@186
   485
    template<typename T> class SymEdgeMap;
alpar@186
   486
    template<typename T> friend class SymEdgeMap;
alpar@186
   487
alpar@185
   488
    SymSmartGraph() : SmartGraph() { }
alpar@185
   489
    SymSmartGraph(const SmartGraph &_g) : SmartGraph(_g) { }
alpar@398
   490
    ///Adds a pair of oppositely directed edges to the graph.
alpar@185
   491
    Edge addEdge(Node u, Node v)
alpar@185
   492
    {
alpar@185
   493
      Edge e = SmartGraph::addEdge(u,v);
alpar@185
   494
      SmartGraph::addEdge(v,u);
alpar@185
   495
      return e;
alpar@185
   496
    }
alpar@185
   497
alpar@186
   498
    ///The oppositely directed edge.
alpar@186
   499
alpar@186
   500
    ///Returns the oppositely directed
alpar@186
   501
    ///pair of the edge \c e.
alpar@185
   502
    Edge opposite(Edge e) const
alpar@185
   503
    {
alpar@185
   504
      Edge f;
alpar@185
   505
      f.idref() = e.idref() - 2*(e.idref()%2) + 1;
alpar@185
   506
      return f;
alpar@185
   507
    }
alpar@185
   508
    
alpar@186
   509
    ///Common data storage for the edge pairs.
alpar@186
   510
alpar@186
   511
    ///This map makes it possible to store data shared by the oppositely
alpar@186
   512
    ///directed pairs of edges.
alpar@185
   513
    template <typename T> class SymEdgeMap : public DynMapBase<Edge>
alpar@185
   514
    {
alpar@185
   515
      std::vector<T> container;
alpar@185
   516
      
alpar@185
   517
    public:
alpar@185
   518
      typedef T ValueType;
alpar@185
   519
      typedef Edge KeyType;
alpar@185
   520
alpar@186
   521
      SymEdgeMap(const SymSmartGraph &_G) :
alpar@185
   522
	DynMapBase<Edge>(_G), container(_G.maxEdgeId()/2)
alpar@185
   523
      {
alpar@186
   524
	static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.push_back(this);
alpar@185
   525
      }
alpar@186
   526
      SymEdgeMap(const SymSmartGraph &_G,const T &t) :
alpar@185
   527
	DynMapBase<Edge>(_G), container(_G.maxEdgeId()/2,t)
alpar@185
   528
      {
alpar@185
   529
	G->dyn_edge_maps.push_back(this);
alpar@185
   530
      }
alpar@185
   531
alpar@185
   532
      SymEdgeMap(const SymEdgeMap<T> &m) :
alpar@185
   533
 	DynMapBase<SymEdge>(*m.G), container(m.container)
alpar@185
   534
      {
alpar@185
   535
 	G->dyn_node_maps.push_back(this);
alpar@185
   536
      }
alpar@185
   537
alpar@185
   538
      //      template<typename TT> friend class SymEdgeMap;
alpar@185
   539
alpar@185
   540
      ///\todo It can copy between different types.
alpar@185
   541
      ///
alpar@185
   542
alpar@590
   543
      template<typename TT> SymEdgeMap(const SymEdgeMap<TT> &m)
alpar@590
   544
	: DynMapBase<SymEdge>(*m.G), container(m.container.size())
alpar@185
   545
      {
alpar@185
   546
	G->dyn_node_maps.push_back(this);
alpar@185
   547
	typename std::vector<TT>::const_iterator i;
alpar@185
   548
	for(typename std::vector<TT>::const_iterator i=m.container.begin();
alpar@185
   549
	    i!=m.container.end();
alpar@185
   550
	    i++)
alpar@185
   551
	  container.push_back(*i);
alpar@185
   552
      }
alpar@185
   553
 
alpar@185
   554
      ~SymEdgeMap()
alpar@185
   555
      {
alpar@185
   556
	if(G) {
alpar@185
   557
	  std::vector<DynMapBase<Edge>* >::iterator i;
alpar@186
   558
	  for(i=static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.begin();
alpar@186
   559
	      i!=static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.end()
alpar@186
   560
		&& *i!=this; ++i) ;
alpar@185
   561
	  //if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow...
alpar@185
   562
	  //A better way to do that: (Is this really important?)
alpar@185
   563
	  if(*i==this) {
alpar@186
   564
	    *i=static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.back();
alpar@186
   565
	    static_cast<const SymSmartGraph*>(G)->dyn_edge_maps.pop_back();
alpar@185
   566
	  }
alpar@185
   567
	}
alpar@185
   568
      }
alpar@185
   569
      
alpar@185
   570
      void add(const Edge k) 
alpar@185
   571
      {
alpar@185
   572
	if(!k.idref()%2&&k.idref()/2>=int(container.size()))
alpar@185
   573
	  container.resize(k.idref()/2+1);
alpar@185
   574
      }
alpar@185
   575
      void erase(const Edge k) { }
alpar@185
   576
      
alpar@185
   577
      void set(Edge n, T a) { container[n.idref()/2]=a; }
alpar@209
   578
      //T get(Edge n) const { return container[n.idref()/2]; }
alpar@215
   579
      typename std::vector<T>::reference
alpar@215
   580
      operator[](Edge n) { return container[n.idref()/2]; }
alpar@215
   581
      typename std::vector<T>::const_reference
alpar@215
   582
      operator[](Edge n) const { return container[n.idref()/2]; }
alpar@185
   583
alpar@185
   584
      ///\warning There is no safety check at all!
alpar@185
   585
      ///Using operator = between maps attached to different graph may
alpar@185
   586
      ///cause serious problem.
alpar@185
   587
      ///\todo Is this really so?
alpar@185
   588
      ///\todo It can copy between different types.
alpar@185
   589
      const SymEdgeMap<T>& operator=(const SymEdgeMap<T> &m)
alpar@185
   590
      {
alpar@185
   591
	container = m.container;
alpar@185
   592
	return *this;
alpar@185
   593
      }
alpar@185
   594
      template<typename TT>
alpar@185
   595
      const SymEdgeMap<T>& operator=(const SymEdgeMap<TT> &m)
alpar@185
   596
      {
alpar@531
   597
	std::copy(m.container.begin(), m.container.end(), container.begin());
alpar@185
   598
	return *this;
alpar@185
   599
      }
alpar@185
   600
      
alpar@185
   601
      void update() {}    //Useless for DynMaps
alpar@185
   602
      void update(T a) {}  //Useless for DynMaps
alpar@185
   603
alpar@185
   604
    };
alpar@185
   605
alpar@185
   606
  };
alpar@185
   607
  
alpar@407
   608
  /// @}  
alpar@407
   609
alpar@105
   610
} //namespace hugo
alpar@104
   611
alpar@157
   612
alpar@157
   613
alpar@157
   614
alpar@590
   615
#endif //HUGO_SMART_GRAPH_H