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