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