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