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