src/include/smart_graph.h
author klao
Tue, 27 Apr 2004 13:53:27 +0000
changeset 445 6fe0d7d70674
parent 407 e34e1bc610cf
child 456 02c28d3cf97b
permissions -rw-r--r--
Egy helyes (warning nelkuli) megvalositasa az operator<< -nek az stGraphWrapper
Node es Edge-enek. Csak a konverziok es templates fuggvenyek "alacsony
prioritasa" miatt hasznalhatatlan.

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