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