lemon/smart_graph.h
author deba
Fri, 15 Jun 2007 14:32:48 +0000
changeset 2456 717a5134ddeb
parent 2391 14a343be7a5a
child 2498 290e43cddc1a
permissions -rw-r--r--
Space reservation for SmartGraph

Doc improvments
alpar@906
     1
/* -*- C++ -*-
alpar@906
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@2391
     5
 * Copyright (C) 2003-2007
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     8
 *
alpar@906
     9
 * Permission to use, modify and distribute this software is granted
alpar@906
    10
 * provided that this copyright notice appears in all copies. For
alpar@906
    11
 * precise terms see the accompanying LICENSE file.
alpar@906
    12
 *
alpar@906
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    14
 * express or implied, and with no claim as to its suitability for any
alpar@906
    15
 * purpose.
alpar@906
    16
 *
alpar@906
    17
 */
alpar@105
    18
alpar@921
    19
#ifndef LEMON_SMART_GRAPH_H
alpar@921
    20
#define LEMON_SMART_GRAPH_H
alpar@104
    21
klao@491
    22
///\ingroup graphs
alpar@242
    23
///\file
deba@2116
    24
///\brief SmartGraph and SmartUGraph classes.
alpar@242
    25
alpar@104
    26
#include <vector>
alpar@104
    27
deba@1993
    28
#include <lemon/bits/invalid.h>
alpar@157
    29
deba@2116
    30
#include <lemon/bits/base_extender.h>
deba@1791
    31
#include <lemon/bits/graph_extender.h>
klao@1034
    32
deba@1993
    33
#include <lemon/bits/utility.h>
deba@2116
    34
#include <lemon/error.h>
deba@782
    35
deba@1979
    36
#include <lemon/bits/graph_extender.h>
deba@1979
    37
alpar@921
    38
namespace lemon {
alpar@104
    39
alpar@973
    40
  class SmartGraph;
alpar@969
    41
  ///Base of SmartGraph
alpar@969
    42
alpar@969
    43
  ///Base of SmartGraph
alpar@969
    44
  ///
klao@946
    45
  class SmartGraphBase {
deba@2190
    46
  protected:
alpar@104
    47
alpar@104
    48
    struct NodeT 
alpar@104
    49
    {
deba@2190
    50
      int first_in, first_out;      
deba@2190
    51
      NodeT() {}
alpar@104
    52
    };
alpar@104
    53
    struct EdgeT 
alpar@104
    54
    {
alpar@986
    55
      int target, source, next_in, next_out;      
deba@2190
    56
      EdgeT() {}  
alpar@104
    57
    };
alpar@104
    58
alpar@104
    59
    std::vector<NodeT> nodes;
alpar@129
    60
alpar@104
    61
    std::vector<EdgeT> edges;
alpar@104
    62
    
alpar@185
    63
    
alpar@104
    64
  public:
deba@782
    65
klao@946
    66
    typedef SmartGraphBase Graph;
alpar@104
    67
alpar@164
    68
    class Node;
alpar@164
    69
    class Edge;
alpar@108
    70
alpar@104
    71
    
alpar@104
    72
  public:
alpar@104
    73
klao@946
    74
    SmartGraphBase() : nodes(), edges() { }
deba@1718
    75
    SmartGraphBase(const SmartGraphBase &_g) 
deba@1718
    76
      : nodes(_g.nodes), edges(_g.edges) { }
alpar@104
    77
    
klao@977
    78
    typedef True NodeNumTag;
klao@977
    79
    typedef True EdgeNumTag;
klao@977
    80
alpar@813
    81
    int nodeNum() const { return nodes.size(); }
alpar@813
    82
    int edgeNum() const { return edges.size(); }
alpar@104
    83
deba@1791
    84
    int maxNodeId() const { return nodes.size()-1; }
deba@1791
    85
    int maxEdgeId() const { return edges.size()-1; }
alpar@108
    86
alpar@2128
    87
    Node addNode() {
deba@2190
    88
      int n = nodes.size();     
deba@2190
    89
      nodes.push_back(NodeT());
deba@2190
    90
      nodes[n].first_in = -1;
deba@2190
    91
      nodes[n].first_out = -1;
deba@2190
    92
      return Node(n);
alpar@2128
    93
    }
alpar@2128
    94
    
alpar@2128
    95
    Edge addEdge(Node u, Node v) {
deba@2190
    96
      int n = edges.size(); 
deba@2190
    97
      edges.push_back(EdgeT());
deba@2190
    98
      edges[n].source = u.id; 
deba@2190
    99
      edges[n].target = v.id;
deba@2190
   100
      edges[n].next_out = nodes[u.id].first_out;
deba@2190
   101
      edges[n].next_in = nodes[v.id].first_in;
deba@2190
   102
      nodes[u.id].first_out = nodes[v.id].first_in = n;
alpar@2128
   103
deba@2190
   104
      return Edge(n);
alpar@2128
   105
    }
alpar@2128
   106
deba@2190
   107
    void clear() {
deba@2190
   108
      edges.clear();
deba@2190
   109
      nodes.clear();
deba@2190
   110
    }
alpar@2128
   111
deba@2190
   112
    Node source(Edge e) const { return Node(edges[e.id].source); }
deba@2190
   113
    Node target(Edge e) const { return Node(edges[e.id].target); }
alpar@104
   114
deba@2190
   115
    static int id(Node v) { return v.id; }
deba@2190
   116
    static int id(Edge e) { return e.id; }
alpar@104
   117
deba@1791
   118
    static Node nodeFromId(int id) { return Node(id);}
deba@1791
   119
    static Edge edgeFromId(int id) { return Edge(id);}
deba@1106
   120
alpar@164
   121
    class Node {
klao@946
   122
      friend class SmartGraphBase;
alpar@973
   123
      friend class SmartGraph;
alpar@104
   124
alpar@104
   125
    protected:
deba@2190
   126
      int id;
deba@2190
   127
      explicit Node(int _id) : id(_id) {}
alpar@104
   128
    public:
alpar@164
   129
      Node() {}
deba@2190
   130
      Node (Invalid) : id(-1) {}
deba@2190
   131
      bool operator==(const Node i) const {return id == i.id;}
deba@2190
   132
      bool operator!=(const Node i) const {return id != i.id;}
deba@2190
   133
      bool operator<(const Node i) const {return id < i.id;}
alpar@104
   134
    };
alpar@104
   135
    
alpar@104
   136
alpar@164
   137
    class Edge {
klao@946
   138
      friend class SmartGraphBase;
alpar@973
   139
      friend class SmartGraph;
alpar@185
   140
alpar@104
   141
    protected:
deba@2190
   142
      int id;
deba@2190
   143
      explicit Edge(int _id) : id(_id) {}
alpar@706
   144
    public:
alpar@164
   145
      Edge() { }
deba@2190
   146
      Edge (Invalid) : id(-1) {}
deba@2190
   147
      bool operator==(const Edge i) const {return id == i.id;}
deba@2190
   148
      bool operator!=(const Edge i) const {return id != i.id;}
deba@2190
   149
      bool operator<(const Edge i) const {return id < i.id;}
klao@946
   150
    };
alpar@905
   151
klao@946
   152
    void first(Node& node) const {
deba@2190
   153
      node.id = nodes.size() - 1;
klao@946
   154
    }
klao@946
   155
klao@946
   156
    static void next(Node& node) {
deba@2190
   157
      --node.id;
klao@946
   158
    }
klao@946
   159
klao@946
   160
    void first(Edge& edge) const {
deba@2190
   161
      edge.id = edges.size() - 1;
klao@946
   162
    }
klao@946
   163
klao@946
   164
    static void next(Edge& edge) {
deba@2190
   165
      --edge.id;
klao@946
   166
    }
klao@946
   167
klao@946
   168
    void firstOut(Edge& edge, const Node& node) const {
deba@2190
   169
      edge.id = nodes[node.id].first_out;
klao@946
   170
    }
klao@946
   171
klao@946
   172
    void nextOut(Edge& edge) const {
deba@2190
   173
      edge.id = edges[edge.id].next_out;
klao@946
   174
    }
klao@946
   175
klao@946
   176
    void firstIn(Edge& edge, const Node& node) const {
deba@2190
   177
      edge.id = nodes[node.id].first_in;
klao@946
   178
    }
alpar@104
   179
    
klao@946
   180
    void nextIn(Edge& edge) const {
deba@2190
   181
      edge.id = edges[edge.id].next_in;
klao@946
   182
    }
alpar@105
   183
alpar@104
   184
  };
alpar@185
   185
deba@1979
   186
  typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
deba@937
   187
deba@1791
   188
  /// \ingroup graphs
alpar@1161
   189
alpar@950
   190
  ///A smart graph class.
deba@937
   191
alpar@950
   192
  ///This is a simple and fast graph implementation.
alpar@950
   193
  ///It is also quite memory efficient, but at the price
alpar@974
   194
  ///that <b> it does support only limited (only stack-like)
alpar@974
   195
  ///node and edge deletions</b>.
alpar@950
   196
  ///It conforms to 
alpar@2260
   197
  ///the \ref concepts::Graph "Graph concept" with an
alpar@2256
   198
  ///important extra feature that
alpar@2260
   199
  ///its maps are real \ref concepts::ReferenceMap "reference map"s.
alpar@2256
   200
  ///
alpar@2260
   201
  ///\sa concepts::Graph.
alpar@950
   202
  ///
alpar@950
   203
  ///\author Alpar Juttner
deba@1669
   204
  class SmartGraph : public ExtendedSmartGraphBase {
alpar@969
   205
  public:
deba@1979
   206
deba@1979
   207
    typedef ExtendedSmartGraphBase Parent;
deba@1979
   208
deba@2190
   209
  private:
alpar@973
   210
alpar@2128
   211
    ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
alpar@2128
   212
alpar@2128
   213
    ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
alpar@2128
   214
    ///
deba@2190
   215
    SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {};
alpar@2132
   216
    ///\brief Assignment of SmartGraph to another one is \e not allowed.
alpar@2128
   217
    ///Use GraphCopy() instead.
alpar@2128
   218
alpar@2132
   219
    ///Assignment of SmartGraph to another one is \e not allowed.
alpar@2128
   220
    ///Use GraphCopy() instead.
alpar@2128
   221
    void operator=(const SmartGraph &) {}
alpar@1011
   222
alpar@1011
   223
  public:
alpar@2128
   224
    
alpar@2128
   225
    /// Constructor
alpar@2128
   226
    
alpar@2128
   227
    /// Constructor.
alpar@2128
   228
    ///
alpar@2128
   229
    SmartGraph() {};
alpar@2128
   230
    
alpar@2128
   231
    ///Add a new node to the graph.
alpar@2128
   232
    
alpar@2128
   233
    /// \return the new node.
alpar@2128
   234
    ///
alpar@2128
   235
    Node addNode() { return Parent::addNode(); }
alpar@2128
   236
    
alpar@2128
   237
    ///Add a new edge to the graph.
alpar@2128
   238
    
alpar@2128
   239
    ///Add a new edge to the graph with source node \c s
alpar@2128
   240
    ///and target node \c t.
alpar@2128
   241
    ///\return the new edge.
alpar@2128
   242
    Edge addEdge(const Node& s, const Node& t) { 
alpar@2128
   243
      return Parent::addEdge(s, t); 
alpar@2128
   244
    }
alpar@2128
   245
deba@2456
   246
    /// \brief Using this it is possible to avoid the superfluous memory
deba@2456
   247
    /// allocation.
deba@2456
   248
deba@2456
   249
    /// Using this it is possible to avoid the superfluous memory
deba@2456
   250
    /// allocation: if you know that the graph you want to build will
deba@2456
   251
    /// be very large (e.g. it will contain millions of nodes and/or edges)
deba@2456
   252
    /// then it is worth reserving space for this amount before starting
deba@2456
   253
    /// to build the graph.
deba@2456
   254
    /// \sa reserveEdge
deba@2456
   255
    void reserveNode(int n) { nodes.reserve(n); };
deba@2456
   256
deba@2456
   257
    /// \brief Using this it is possible to avoid the superfluous memory
deba@2456
   258
    /// allocation.
deba@2456
   259
deba@2456
   260
    /// Using this it is possible to avoid the superfluous memory
deba@2456
   261
    /// allocation: if you know that the graph you want to build will
deba@2456
   262
    /// be very large (e.g. it will contain millions of nodes and/or edges)
deba@2456
   263
    /// then it is worth reserving space for this amount before starting
deba@2456
   264
    /// to build the graph.
deba@2456
   265
    /// \sa reserveNode
deba@2456
   266
    void reserveEdge(int m) { edges.reserve(m); };
deba@2456
   267
deba@2190
   268
    ///Clear the graph.
alpar@2128
   269
    
deba@2190
   270
    ///Erase all the nodes and edges from the graph.
deba@2190
   271
    ///
alpar@2128
   272
    void clear() {
deba@2190
   273
      Parent::clear();
alpar@2128
   274
    }
alpar@1284
   275
alpar@1284
   276
    ///Split a node.
alpar@1284
   277
    
alpar@1284
   278
    ///This function splits a node. First a new node is added to the graph,
alpar@1284
   279
    ///then the source of each outgoing edge of \c n is moved to this new node.
alpar@1284
   280
    ///If \c connect is \c true (this is the default value), then a new edge
alpar@1284
   281
    ///from \c n to the newly created node is also added.
alpar@1284
   282
    ///\return The newly created node.
alpar@1284
   283
    ///
alpar@1284
   284
    ///\note The <tt>Edge</tt>s
alpar@1284
   285
    ///referencing a moved edge remain
alpar@1284
   286
    ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
alpar@1284
   287
    ///may be invalidated.
alpar@1770
   288
    ///\warning This functionality cannot be used together with the Snapshot
alpar@1284
   289
    ///feature.
alpar@1284
   290
    ///\todo It could be implemented in a bit faster way.
alpar@2128
   291
    Node split(Node n, bool connect = true)
alpar@1284
   292
    {
alpar@2128
   293
      Node b = addNode();
deba@2190
   294
      nodes[b.id].first_out=nodes[n.id].first_out;
deba@2190
   295
      nodes[n.id].first_out=-1;
deba@2190
   296
      for(int i=nodes[b.id].first_out;i!=-1;i++) edges[i].source=b.id;
alpar@2128
   297
      if(connect) addEdge(n,b);
deba@1718
   298
      return b;
alpar@1284
   299
    }
alpar@1284
   300
deba@2190
   301
  public:
deba@2190
   302
    
deba@2190
   303
    class Snapshot;
deba@2190
   304
deba@2190
   305
  protected:
deba@2190
   306
deba@2190
   307
    void restoreSnapshot(const Snapshot &s)
deba@2190
   308
    {
deba@2190
   309
      while(s.edge_num<edges.size()) {
deba@2190
   310
        Edge edge = edgeFromId(edges.size()-1);
deba@2381
   311
	Parent::notifier(Edge()).erase(edge);
deba@2190
   312
	nodes[edges.back().source].first_out=edges.back().next_out;
deba@2190
   313
	nodes[edges.back().target].first_in=edges.back().next_in;
deba@2190
   314
	edges.pop_back();
deba@2190
   315
      }
deba@2190
   316
      while(s.node_num<nodes.size()) {
deba@2190
   317
        Node node = nodeFromId(nodes.size()-1);
deba@2381
   318
	Parent::notifier(Node()).erase(node);
deba@2190
   319
	nodes.pop_back();
deba@2190
   320
      }
deba@2190
   321
    }    
deba@2190
   322
deba@2190
   323
  public:
deba@2190
   324
alpar@1011
   325
    ///Class to make a snapshot of the graph and to restrore to it later.
alpar@1011
   326
alpar@1011
   327
    ///Class to make a snapshot of the graph and to restrore to it later.
alpar@1011
   328
    ///
alpar@1011
   329
    ///The newly added nodes and edges can be removed using the
alpar@1011
   330
    ///restore() function.
alpar@1011
   331
    ///\note After you restore a state, you cannot restore
alpar@1011
   332
    ///a later state, in other word you cannot add again the edges deleted
alpar@2132
   333
    ///by restore() using another one Snapshot instance.
alpar@1011
   334
    ///
deba@2190
   335
    ///\warning If you do not use correctly the snapshot that can cause
deba@2190
   336
    ///either broken program, invalid state of the graph, valid but
deba@2190
   337
    ///not the restored graph or no change. Because the runtime performance
deba@2190
   338
    ///the validity of the snapshot is not stored.
alpar@1770
   339
    class Snapshot 
alpar@1011
   340
    {
alpar@1011
   341
      SmartGraph *g;
alpar@1011
   342
    protected:
alpar@1011
   343
      friend class SmartGraph;
alpar@1011
   344
      unsigned int node_num;
alpar@1011
   345
      unsigned int edge_num;
alpar@1011
   346
    public:
zsuzska@1274
   347
      ///Default constructor.
alpar@1011
   348
      
zsuzska@1274
   349
      ///Default constructor.
alpar@1011
   350
      ///To actually make a snapshot you must call save().
alpar@1011
   351
      ///
alpar@1770
   352
      Snapshot() : g(0) {}
alpar@1011
   353
      ///Constructor that immediately makes a snapshot
alpar@1011
   354
      
alpar@1011
   355
      ///This constructor immediately makes a snapshot of the graph.
alpar@1011
   356
      ///\param _g The graph we make a snapshot of.
alpar@1770
   357
      Snapshot(SmartGraph &_g) :g(&_g) {
alpar@1011
   358
	node_num=g->nodes.size();
alpar@1011
   359
	edge_num=g->edges.size();
alpar@1011
   360
      }
alpar@1011
   361
alpar@1011
   362
      ///Make a snapshot.
alpar@1011
   363
alpar@1011
   364
      ///Make a snapshot of the graph.
alpar@1011
   365
      ///
alpar@1011
   366
      ///This function can be called more than once. In case of a repeated
alpar@1011
   367
      ///call, the previous snapshot gets lost.
alpar@1011
   368
      ///\param _g The graph we make the snapshot of.
alpar@1011
   369
      void save(SmartGraph &_g) 
alpar@1011
   370
      {
alpar@1011
   371
	g=&_g;
alpar@1011
   372
	node_num=g->nodes.size();
alpar@1011
   373
	edge_num=g->edges.size();
alpar@1011
   374
      }
alpar@1011
   375
alpar@1011
   376
      ///Undo the changes until a snapshot.
alpar@1011
   377
      
alpar@1011
   378
      ///Undo the changes until a snapshot created by save().
alpar@1011
   379
      ///
alpar@1011
   380
      ///\note After you restored a state, you cannot restore
alpar@1011
   381
      ///a later state, in other word you cannot add again the edges deleted
alpar@1011
   382
      ///by restore().
alpar@1011
   383
      void restore()
alpar@1011
   384
      {
alpar@1770
   385
	g->restoreSnapshot(*this);
alpar@1011
   386
      }
alpar@1011
   387
    };
alpar@973
   388
  };
klao@1034
   389
klao@1034
   390
deba@2338
   391
  class SmartUGraphBase {
deba@2116
   392
deba@2338
   393
  protected:
deba@2338
   394
deba@2338
   395
    struct NodeT {
deba@2338
   396
      int first_out;
deba@2338
   397
    };
deba@2338
   398
 
deba@2338
   399
    struct EdgeT {
deba@2338
   400
      int target;
deba@2338
   401
      int next_out;
deba@2338
   402
    };
deba@2338
   403
deba@2338
   404
    std::vector<NodeT> nodes;
deba@2338
   405
    std::vector<EdgeT> edges;
deba@2338
   406
deba@2338
   407
    int first_free_edge;
deba@2338
   408
    
deba@2338
   409
  public:
deba@2338
   410
    
deba@2338
   411
    typedef SmartUGraphBase Graph;
deba@2342
   412
deba@2342
   413
    class Node;
deba@2342
   414
    class Edge;
deba@2342
   415
    class UEdge;
deba@2338
   416
    
deba@2338
   417
    class Node {
deba@2338
   418
      friend class SmartUGraphBase;
deba@2338
   419
    protected:
deba@2338
   420
deba@2338
   421
      int id;
deba@2338
   422
      explicit Node(int pid) { id = pid;}
deba@2338
   423
deba@2338
   424
    public:
deba@2338
   425
      Node() {}
deba@2338
   426
      Node (Invalid) { id = -1; }
deba@2338
   427
      bool operator==(const Node& node) const {return id == node.id;}
deba@2338
   428
      bool operator!=(const Node& node) const {return id != node.id;}
deba@2338
   429
      bool operator<(const Node& node) const {return id < node.id;}
deba@2338
   430
    };
deba@2338
   431
deba@2338
   432
    class UEdge {
deba@2338
   433
      friend class SmartUGraphBase;
deba@2338
   434
    protected:
deba@2338
   435
deba@2338
   436
      int id;
deba@2338
   437
      explicit UEdge(int pid) { id = pid;}
deba@2338
   438
deba@2338
   439
    public:
deba@2338
   440
      UEdge() {}
deba@2338
   441
      UEdge (Invalid) { id = -1; }
deba@2338
   442
      bool operator==(const UEdge& edge) const {return id == edge.id;}
deba@2338
   443
      bool operator!=(const UEdge& edge) const {return id != edge.id;}
deba@2338
   444
      bool operator<(const UEdge& edge) const {return id < edge.id;}
deba@2338
   445
    };
deba@2338
   446
deba@2338
   447
    class Edge {
deba@2338
   448
      friend class SmartUGraphBase;
deba@2338
   449
    protected:
deba@2338
   450
deba@2338
   451
      int id;
deba@2338
   452
      explicit Edge(int pid) { id = pid;}
deba@2338
   453
deba@2338
   454
    public:
deba@2343
   455
      operator UEdge() const { return uEdgeFromId(id / 2); }
deba@2338
   456
deba@2338
   457
      Edge() {}
deba@2338
   458
      Edge (Invalid) { id = -1; }
deba@2338
   459
      bool operator==(const Edge& edge) const {return id == edge.id;}
deba@2338
   460
      bool operator!=(const Edge& edge) const {return id != edge.id;}
deba@2338
   461
      bool operator<(const Edge& edge) const {return id < edge.id;}
deba@2338
   462
    };
deba@2338
   463
deba@2338
   464
deba@2338
   465
deba@2338
   466
    SmartUGraphBase()
deba@2338
   467
      : nodes(), edges() {}
deba@2338
   468
deba@2338
   469
    
deba@2338
   470
    int maxNodeId() const { return nodes.size()-1; } 
deba@2338
   471
    int maxUEdgeId() const { return edges.size() / 2 - 1; }
deba@2338
   472
    int maxEdgeId() const { return edges.size()-1; }
deba@2338
   473
deba@2338
   474
    Node source(Edge e) const { return Node(edges[e.id ^ 1].target); }
deba@2338
   475
    Node target(Edge e) const { return Node(edges[e.id].target); }
deba@2338
   476
deba@2338
   477
    Node source(UEdge e) const { return Node(edges[2 * e.id].target); }
deba@2338
   478
    Node target(UEdge e) const { return Node(edges[2 * e.id + 1].target); }
deba@2338
   479
deba@2338
   480
    static bool direction(Edge e) {
deba@2338
   481
      return (e.id & 1) == 1;
deba@2338
   482
    }
deba@2338
   483
deba@2338
   484
    static Edge direct(UEdge e, bool d) {
deba@2338
   485
      return Edge(e.id * 2 + (d ? 1 : 0));
deba@2338
   486
    }
deba@2338
   487
deba@2338
   488
    void first(Node& node) const { 
deba@2338
   489
      node.id = nodes.size() - 1;
deba@2338
   490
    }
deba@2338
   491
deba@2338
   492
    void next(Node& node) const {
deba@2338
   493
      --node.id;
deba@2338
   494
    }
deba@2338
   495
deba@2338
   496
    void first(Edge& edge) const { 
deba@2338
   497
      edge.id = edges.size() - 1;
deba@2338
   498
    }
deba@2338
   499
deba@2338
   500
    void next(Edge& edge) const {
deba@2338
   501
      --edge.id;
deba@2338
   502
    }
deba@2338
   503
deba@2338
   504
    void first(UEdge& edge) const { 
deba@2338
   505
      edge.id = edges.size() / 2 - 1;
deba@2338
   506
    }
deba@2338
   507
deba@2338
   508
    void next(UEdge& edge) const {
deba@2338
   509
      --edge.id;
deba@2338
   510
    }
deba@2338
   511
deba@2338
   512
    void firstOut(Edge &edge, const Node& v) const {
deba@2338
   513
      edge.id = nodes[v.id].first_out;
deba@2338
   514
    }
deba@2338
   515
    void nextOut(Edge &edge) const {
deba@2338
   516
      edge.id = edges[edge.id].next_out;
deba@2338
   517
    }
deba@2338
   518
deba@2338
   519
    void firstIn(Edge &edge, const Node& v) const {
deba@2338
   520
      edge.id = ((nodes[v.id].first_out) ^ 1);
deba@2339
   521
      if (edge.id == -2) edge.id = -1;
deba@2338
   522
    }
deba@2338
   523
    void nextIn(Edge &edge) const {
deba@2338
   524
      edge.id = ((edges[edge.id ^ 1].next_out) ^ 1);
deba@2339
   525
      if (edge.id == -2) edge.id = -1;
deba@2338
   526
    }
deba@2338
   527
deba@2338
   528
    void firstInc(UEdge &edge, bool& d, const Node& v) const {
deba@2338
   529
      int de = nodes[v.id].first_out;
deba@2381
   530
      if (de != -1) {
deba@2381
   531
        edge.id = de / 2;
deba@2381
   532
        d = ((de & 1) == 1);
deba@2381
   533
      } else {
deba@2381
   534
        edge.id = -1;
deba@2381
   535
        d = true;
deba@2381
   536
      }
deba@2338
   537
    }
deba@2338
   538
    void nextInc(UEdge &edge, bool& d) const {
deba@2338
   539
      int de = (edges[(edge.id * 2) | (d ? 1 : 0)].next_out);
deba@2381
   540
      if (de != -1) {
deba@2381
   541
        edge.id = de / 2;
deba@2381
   542
        d = ((de & 1) == 1);
deba@2381
   543
      } else {
deba@2381
   544
        edge.id = -1;
deba@2381
   545
        d = true;      
deba@2381
   546
      }
deba@2338
   547
    }
deba@2338
   548
    
deba@2338
   549
    static int id(Node v) { return v.id; }
deba@2338
   550
    static int id(Edge e) { return e.id; }
deba@2338
   551
    static int id(UEdge e) { return e.id; }
deba@2338
   552
deba@2338
   553
    static Node nodeFromId(int id) { return Node(id);}
deba@2338
   554
    static Edge edgeFromId(int id) { return Edge(id);}
deba@2338
   555
    static UEdge uEdgeFromId(int id) { return UEdge(id);}
deba@2338
   556
deba@2338
   557
    Node addNode() {     
deba@2338
   558
      int n = nodes.size();
deba@2338
   559
      nodes.push_back(NodeT());
deba@2338
   560
      nodes[n].first_out = -1;
deba@2338
   561
      
deba@2338
   562
      return Node(n);
deba@2338
   563
    }
deba@2338
   564
    
deba@2338
   565
    UEdge addEdge(Node u, Node v) {
deba@2338
   566
      int n = edges.size();
deba@2338
   567
      edges.push_back(EdgeT());
deba@2338
   568
      edges.push_back(EdgeT());
deba@2338
   569
      
deba@2338
   570
      edges[n].target = u.id;
deba@2338
   571
      edges[n | 1].target = v.id;
deba@2338
   572
deba@2338
   573
      edges[n].next_out = nodes[v.id].first_out;
deba@2338
   574
      edges[n | 1].next_out = nodes[u.id].first_out;
deba@2338
   575
	
deba@2338
   576
      nodes[v.id].first_out = n;
deba@2338
   577
      nodes[u.id].first_out = (n | 1);
deba@2338
   578
deba@2338
   579
      return UEdge(n / 2);
deba@2338
   580
    }
deba@2338
   581
    
deba@2338
   582
    void clear() {
deba@2338
   583
      edges.clear();
deba@2338
   584
      nodes.clear();
deba@2338
   585
    }
deba@2338
   586
deba@2338
   587
  };
deba@2338
   588
deba@2338
   589
  typedef UGraphExtender<SmartUGraphBase> ExtendedSmartUGraphBase;
deba@2116
   590
deba@2116
   591
  /// \ingroup graphs
deba@2116
   592
  ///
deba@2116
   593
  /// \brief A smart undirected graph class.
deba@2116
   594
  ///
deba@2116
   595
  /// This is a simple and fast undirected graph implementation.
deba@2116
   596
  /// It is also quite memory efficient, but at the price
deba@2116
   597
  /// that <b> it does support only limited (only stack-like)
deba@2116
   598
  /// node and edge deletions</b>.
deba@2116
   599
  /// Except from this it conforms to 
alpar@2260
   600
  /// the \ref concepts::UGraph "UGraph concept".
alpar@2256
   601
  ///
alpar@2256
   602
  ///It also has an
alpar@2256
   603
  ///important extra feature that
alpar@2260
   604
  ///its maps are real \ref concepts::ReferenceMap "reference map"s.
alpar@2256
   605
  ///
alpar@2260
   606
  /// \sa concepts::UGraph.
deba@2116
   607
  ///
deba@2116
   608
  class SmartUGraph : public ExtendedSmartUGraphBase {
alpar@2128
   609
  private:
deba@2190
   610
alpar@2128
   611
    ///SmartUGraph is \e not copy constructible. Use UGraphCopy() instead.
alpar@2128
   612
alpar@2128
   613
    ///SmartUGraph is \e not copy constructible. Use UGraphCopy() instead.
alpar@2128
   614
    ///
alpar@2128
   615
    SmartUGraph(const SmartUGraph &) : ExtendedSmartUGraphBase() {};
deba@2190
   616
alpar@2132
   617
    ///\brief Assignment of SmartUGraph to another one is \e not allowed.
alpar@2128
   618
    ///Use UGraphCopy() instead.
alpar@2128
   619
alpar@2132
   620
    ///Assignment of SmartUGraph to another one is \e not allowed.
alpar@2128
   621
    ///Use UGraphCopy() instead.
alpar@2128
   622
    void operator=(const SmartUGraph &) {}
deba@2190
   623
alpar@2128
   624
  public:
deba@2190
   625
deba@2190
   626
    typedef ExtendedSmartUGraphBase Parent;
deba@2338
   627
    typedef Parent::OutEdgeIt IncEdgeIt;
deba@2190
   628
alpar@2128
   629
    /// Constructor
alpar@2128
   630
    
alpar@2128
   631
    /// Constructor.
alpar@2128
   632
    ///
alpar@2128
   633
    SmartUGraph() {}
deba@2190
   634
deba@2190
   635
    ///Add a new node to the graph.
deba@2190
   636
    
deba@2190
   637
    /// \return the new node.
deba@2190
   638
    ///
deba@2190
   639
    Node addNode() { return Parent::addNode(); }
deba@2190
   640
    
deba@2190
   641
    ///Add a new undirected edge to the graph.
deba@2190
   642
    
deba@2190
   643
    ///Add a new undirected edge to the graph with node \c s
deba@2190
   644
    ///and \c t.
deba@2190
   645
    ///\return the new undirected edge.
deba@2190
   646
    UEdge addEdge(const Node& s, const Node& t) { 
deba@2190
   647
      return Parent::addEdge(s, t); 
deba@2190
   648
    }
deba@2190
   649
deba@2190
   650
    ///Clear the graph.
deba@2190
   651
    
deba@2190
   652
    ///Erase all the nodes and edges from the graph.
deba@2190
   653
    ///
deba@2190
   654
    void clear() {
deba@2190
   655
      Parent::clear();
deba@2190
   656
    }
deba@2190
   657
deba@2190
   658
  public:
deba@2190
   659
    
deba@2190
   660
    class Snapshot;
deba@2190
   661
deba@2190
   662
  protected:
deba@2190
   663
deba@2338
   664
    void saveSnapshot(Snapshot &s)
deba@2338
   665
    {
deba@2386
   666
      s.graph = this;
deba@2338
   667
      s.node_num = nodes.size();
deba@2338
   668
      s.edge_num = edges.size();
deba@2338
   669
    }
deba@2190
   670
deba@2190
   671
    void restoreSnapshot(const Snapshot &s)
deba@2190
   672
    {
deba@2190
   673
      while(s.edge_num<edges.size()) {
deba@2338
   674
        int n=edges.size()-1;
deba@2338
   675
        UEdge edge=uEdgeFromId(n/2);
deba@2381
   676
	Parent::notifier(UEdge()).erase(edge);
deba@2190
   677
        std::vector<Edge> dir;
deba@2338
   678
        dir.push_back(edgeFromId(n));
deba@2338
   679
        dir.push_back(edgeFromId(n-1));
deba@2381
   680
	Parent::notifier(Edge()).erase(dir);
deba@2338
   681
	nodes[edges[n].target].first_out=edges[n].next_out;
deba@2338
   682
	nodes[edges[n-1].target].first_out=edges[n-1].next_out;
deba@2338
   683
	edges.pop_back();
deba@2190
   684
	edges.pop_back();
deba@2190
   685
      }
deba@2190
   686
      while(s.node_num<nodes.size()) {
deba@2338
   687
        int n=nodes.size()-1;
deba@2338
   688
        Node node = nodeFromId(n);
deba@2381
   689
	Parent::notifier(Node()).erase(node);
deba@2190
   690
	nodes.pop_back();
deba@2190
   691
      }
deba@2190
   692
    }    
deba@2190
   693
deba@2190
   694
  public:
deba@2190
   695
deba@2190
   696
    ///Class to make a snapshot of the graph and to restrore to it later.
deba@2190
   697
deba@2190
   698
    ///Class to make a snapshot of the graph and to restrore to it later.
deba@2190
   699
    ///
deba@2190
   700
    ///The newly added nodes and edges can be removed using the
deba@2190
   701
    ///restore() function.
deba@2190
   702
    ///
deba@2190
   703
    ///\note After you restore a state, you cannot restore
deba@2190
   704
    ///a later state, in other word you cannot add again the edges deleted
deba@2190
   705
    ///by restore() using another one Snapshot instance.
deba@2190
   706
    ///
deba@2190
   707
    ///\warning If you do not use correctly the snapshot that can cause
deba@2190
   708
    ///either broken program, invalid state of the graph, valid but
deba@2190
   709
    ///not the restored graph or no change. Because the runtime performance
deba@2190
   710
    ///the validity of the snapshot is not stored.
deba@2190
   711
    class Snapshot 
deba@2190
   712
    {
deba@2386
   713
      SmartUGraph *graph;
deba@2190
   714
    protected:
deba@2190
   715
      friend class SmartUGraph;
deba@2190
   716
      unsigned int node_num;
deba@2190
   717
      unsigned int edge_num;
deba@2190
   718
    public:
deba@2190
   719
      ///Default constructor.
deba@2190
   720
      
deba@2190
   721
      ///Default constructor.
deba@2190
   722
      ///To actually make a snapshot you must call save().
deba@2190
   723
      ///
deba@2386
   724
      Snapshot() : graph(0) {}
deba@2190
   725
      ///Constructor that immediately makes a snapshot
deba@2190
   726
      
deba@2190
   727
      ///This constructor immediately makes a snapshot of the graph.
alpar@2350
   728
      ///\param g The graph we make a snapshot of.
deba@2338
   729
      Snapshot(SmartUGraph &g) {
deba@2338
   730
        g.saveSnapshot(*this);
deba@2190
   731
      }
deba@2190
   732
deba@2190
   733
      ///Make a snapshot.
deba@2190
   734
deba@2190
   735
      ///Make a snapshot of the graph.
deba@2190
   736
      ///
deba@2190
   737
      ///This function can be called more than once. In case of a repeated
deba@2190
   738
      ///call, the previous snapshot gets lost.
alpar@2350
   739
      ///\param g The graph we make the snapshot of.
deba@2338
   740
      void save(SmartUGraph &g) 
deba@2190
   741
      {
deba@2338
   742
        g.saveSnapshot(*this);
deba@2190
   743
      }
deba@2190
   744
deba@2190
   745
      ///Undo the changes until a snapshot.
deba@2190
   746
      
deba@2190
   747
      ///Undo the changes until a snapshot created by save().
deba@2190
   748
      ///
deba@2190
   749
      ///\note After you restored a state, you cannot restore
deba@2190
   750
      ///a later state, in other word you cannot add again the edges deleted
deba@2190
   751
      ///by restore().
deba@2190
   752
      void restore()
deba@2190
   753
      {
deba@2386
   754
        graph->restoreSnapshot(*this);
deba@2190
   755
      }
deba@2190
   756
    };
deba@2116
   757
  };
deba@2116
   758
deba@2116
   759
deba@2116
   760
  class SmartBpUGraphBase {
deba@2116
   761
  public:
deba@2116
   762
deba@2116
   763
    class NodeSetError : public LogicError {
deba@2162
   764
    public:
alpar@2151
   765
      virtual const char* what() const throw() { 
deba@2116
   766
	return "lemon::SmartBpUGraph::NodeSetError";
deba@2116
   767
      }
deba@2116
   768
    };
deba@2116
   769
deba@2116
   770
  protected:
deba@2116
   771
deba@2116
   772
    struct NodeT {
deba@2116
   773
      int first;
deba@2116
   774
      NodeT() {}
deba@2116
   775
      NodeT(int _first) : first(_first) {}
deba@2116
   776
    };
deba@2116
   777
deba@2116
   778
    struct UEdgeT {
deba@2116
   779
      int aNode, next_out;
deba@2116
   780
      int bNode, next_in;
deba@2116
   781
    };
deba@2116
   782
deba@2116
   783
    std::vector<NodeT> aNodes;
deba@2116
   784
    std::vector<NodeT> bNodes;
deba@2116
   785
deba@2116
   786
    std::vector<UEdgeT> edges;
deba@2116
   787
deba@2116
   788
  public:
deba@2116
   789
  
deba@2116
   790
    class Node {
deba@2116
   791
      friend class SmartBpUGraphBase;
deba@2116
   792
    protected:
deba@2116
   793
      int id;
deba@2116
   794
deba@2190
   795
      explicit Node(int _id) : id(_id) {}
deba@2116
   796
    public:
deba@2116
   797
      Node() {}
deba@2190
   798
      Node(Invalid) : id(-1) {}
deba@2116
   799
      bool operator==(const Node i) const {return id==i.id;}
deba@2116
   800
      bool operator!=(const Node i) const {return id!=i.id;}
deba@2116
   801
      bool operator<(const Node i) const {return id<i.id;}
deba@2116
   802
    };
deba@2116
   803
deba@2116
   804
    class UEdge {
deba@2116
   805
      friend class SmartBpUGraphBase;
deba@2116
   806
    protected:
deba@2116
   807
      int id;
deba@2116
   808
deba@2190
   809
      UEdge(int _id) : id(_id) {}
deba@2116
   810
    public:
deba@2116
   811
      UEdge() {}
deba@2190
   812
      UEdge(Invalid) : id(-1) {}
deba@2116
   813
      bool operator==(const UEdge i) const {return id==i.id;}
deba@2116
   814
      bool operator!=(const UEdge i) const {return id!=i.id;}
deba@2116
   815
      bool operator<(const UEdge i) const {return id<i.id;}
deba@2116
   816
    };
deba@2116
   817
deba@2116
   818
    void firstANode(Node& node) const {
deba@2116
   819
      node.id = 2 * aNodes.size() - 2;
deba@2116
   820
      if (node.id < 0) node.id = -1; 
deba@2116
   821
    }
deba@2116
   822
    void nextANode(Node& node) const {
deba@2116
   823
      node.id -= 2;
deba@2116
   824
      if (node.id < 0) node.id = -1; 
deba@2116
   825
    }
deba@2116
   826
deba@2116
   827
    void firstBNode(Node& node) const {
deba@2116
   828
      node.id = 2 * bNodes.size() - 1;
deba@2116
   829
    }
deba@2116
   830
    void nextBNode(Node& node) const {
deba@2116
   831
      node.id -= 2;
deba@2116
   832
    }
deba@2116
   833
deba@2116
   834
    void first(Node& node) const {
deba@2116
   835
      if (aNodes.size() > 0) {
deba@2116
   836
	node.id = 2 * aNodes.size() - 2;
deba@2116
   837
      } else {
deba@2116
   838
	node.id = 2 * bNodes.size() - 1;
deba@2116
   839
      }
deba@2116
   840
    }
deba@2116
   841
    void next(Node& node) const {
deba@2116
   842
      node.id -= 2;
deba@2116
   843
      if (node.id == -2) {
deba@2116
   844
	node.id = 2 * bNodes.size() - 1;
deba@2116
   845
      }
deba@2116
   846
    }
deba@2116
   847
  
deba@2116
   848
    void first(UEdge& edge) const {
deba@2116
   849
      edge.id = edges.size() - 1;
deba@2116
   850
    }
deba@2116
   851
    void next(UEdge& edge) const {
deba@2116
   852
      --edge.id;
deba@2116
   853
    }
deba@2116
   854
deba@2116
   855
    void firstFromANode(UEdge& edge, const Node& node) const {
deba@2116
   856
      LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
deba@2116
   857
      edge.id = aNodes[node.id >> 1].first;
deba@2116
   858
    }
deba@2116
   859
    void nextFromANode(UEdge& edge) const {
deba@2116
   860
      edge.id = edges[edge.id].next_out;
deba@2116
   861
    }
deba@2116
   862
deba@2116
   863
    void firstFromBNode(UEdge& edge, const Node& node) const {
deba@2116
   864
      LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
deba@2116
   865
      edge.id = bNodes[node.id >> 1].first;
deba@2116
   866
    }
deba@2116
   867
    void nextFromBNode(UEdge& edge) const {
deba@2116
   868
      edge.id = edges[edge.id].next_in;
deba@2116
   869
    }
deba@2116
   870
deba@2116
   871
    static int id(const Node& node) {
deba@2116
   872
      return node.id;
deba@2116
   873
    }
deba@2116
   874
    static Node nodeFromId(int id) {
deba@2116
   875
      return Node(id);
deba@2116
   876
    }
deba@2116
   877
    int maxNodeId() const {
deba@2116
   878
      return aNodes.size() > bNodes.size() ?
deba@2116
   879
	aNodes.size() * 2 - 2 : bNodes.size() * 2 - 1;
deba@2116
   880
    }
deba@2116
   881
  
deba@2116
   882
    static int id(const UEdge& edge) {
deba@2116
   883
      return edge.id;
deba@2116
   884
    }
deba@2116
   885
    static UEdge uEdgeFromId(int id) {
deba@2116
   886
      return UEdge(id);
deba@2116
   887
    }
deba@2116
   888
    int maxUEdgeId() const {
deba@2116
   889
      return edges.size();
deba@2116
   890
    }
deba@2116
   891
  
deba@2116
   892
    static int aNodeId(const Node& node) {
deba@2116
   893
      return node.id >> 1;
deba@2116
   894
    }
deba@2231
   895
    static Node nodeFromANodeId(int id) {
deba@2116
   896
      return Node(id << 1);
deba@2116
   897
    }
deba@2116
   898
    int maxANodeId() const {
deba@2116
   899
      return aNodes.size();
deba@2116
   900
    }
deba@2116
   901
deba@2116
   902
    static int bNodeId(const Node& node) {
deba@2116
   903
      return node.id >> 1;
deba@2116
   904
    }
deba@2231
   905
    static Node nodeFromBNodeId(int id) {
deba@2116
   906
      return Node((id << 1) + 1);
deba@2116
   907
    }
deba@2116
   908
    int maxBNodeId() const {
deba@2116
   909
      return bNodes.size();
deba@2116
   910
    }
deba@2116
   911
deba@2116
   912
    Node aNode(const UEdge& edge) const {
deba@2116
   913
      return Node(edges[edge.id].aNode);
deba@2116
   914
    }
deba@2116
   915
    Node bNode(const UEdge& edge) const {
deba@2116
   916
      return Node(edges[edge.id].bNode);
deba@2116
   917
    }
deba@2116
   918
deba@2116
   919
    static bool aNode(const Node& node) {
deba@2116
   920
      return (node.id & 1) == 0;
deba@2116
   921
    }
deba@2116
   922
deba@2116
   923
    static bool bNode(const Node& node) {
deba@2116
   924
      return (node.id & 1) == 1;
deba@2116
   925
    }
deba@2116
   926
deba@2116
   927
    Node addANode() {
deba@2116
   928
      NodeT nodeT;
deba@2116
   929
      nodeT.first = -1;
deba@2116
   930
      aNodes.push_back(nodeT);
deba@2116
   931
      return Node(aNodes.size() * 2 - 2);
deba@2116
   932
    }
deba@2116
   933
deba@2116
   934
    Node addBNode() {
deba@2116
   935
      NodeT nodeT;
deba@2116
   936
      nodeT.first = -1;
deba@2116
   937
      bNodes.push_back(nodeT);
deba@2116
   938
      return Node(bNodes.size() * 2 - 1);
deba@2116
   939
    }
deba@2116
   940
deba@2116
   941
    UEdge addEdge(const Node& source, const Node& target) {
deba@2116
   942
      LEMON_ASSERT(((source.id ^ target.id) & 1) == 1, NodeSetError());
deba@2116
   943
      UEdgeT edgeT;
deba@2116
   944
      if ((source.id & 1) == 0) {
deba@2116
   945
	edgeT.aNode = source.id;
deba@2116
   946
	edgeT.bNode = target.id;
deba@2116
   947
      } else {
deba@2116
   948
	edgeT.aNode = target.id;
deba@2116
   949
	edgeT.bNode = source.id;
deba@2116
   950
      }
deba@2116
   951
      edgeT.next_out = aNodes[edgeT.aNode >> 1].first;
deba@2116
   952
      aNodes[edgeT.aNode >> 1].first = edges.size();
deba@2116
   953
      edgeT.next_in = bNodes[edgeT.bNode >> 1].first;
deba@2116
   954
      bNodes[edgeT.bNode >> 1].first = edges.size();
deba@2116
   955
      edges.push_back(edgeT);
deba@2116
   956
      return UEdge(edges.size() - 1);
deba@2116
   957
    }
deba@2116
   958
deba@2116
   959
    void clear() {
deba@2116
   960
      aNodes.clear();
deba@2116
   961
      bNodes.clear();
deba@2116
   962
      edges.clear();
deba@2116
   963
    }
deba@2116
   964
deba@2116
   965
    typedef True NodeNumTag;
deba@2116
   966
    int nodeNum() const { return aNodes.size() + bNodes.size(); }
deba@2116
   967
    int aNodeNum() const { return aNodes.size(); }
deba@2116
   968
    int bNodeNum() const { return bNodes.size(); }
deba@2116
   969
deba@2116
   970
    typedef True EdgeNumTag;
deba@2116
   971
    int uEdgeNum() const { return edges.size(); }
deba@2116
   972
deba@2116
   973
  };
deba@2116
   974
deba@2116
   975
deba@2231
   976
  typedef BpUGraphExtender<BidirBpUGraphExtender<SmartBpUGraphBase> >
deba@2231
   977
  ExtendedSmartBpUGraphBase;
deba@2116
   978
deba@2116
   979
  /// \ingroup graphs
deba@2116
   980
  ///
deba@2116
   981
  /// \brief A smart bipartite undirected graph class.
deba@2116
   982
  ///
deba@2116
   983
  /// This is a simple and fast bipartite undirected graph implementation.
deba@2116
   984
  /// It is also quite memory efficient, but at the price
deba@2116
   985
  /// that <b> it does not support node and edge deletions</b>.
deba@2116
   986
  /// Except from this it conforms to 
alpar@2260
   987
  /// the \ref concepts::BpUGraph "BpUGraph concept".
alpar@2256
   988
  ///
alpar@2256
   989
  ///It also has an
alpar@2256
   990
  ///important extra feature that
alpar@2260
   991
  ///its maps are real \ref concepts::ReferenceMap "reference map"s.
alpar@2256
   992
  ///
alpar@2260
   993
  /// \sa concepts::BpUGraph.
deba@2116
   994
  ///
deba@2190
   995
  class SmartBpUGraph : public ExtendedSmartBpUGraphBase {
deba@2190
   996
  private:
deba@2190
   997
deba@2190
   998
    /// \brief SmartBpUGraph is \e not copy constructible.
deba@2190
   999
    ///
deba@2190
  1000
    ///SmartBpUGraph is \e not copy constructible.
deba@2190
  1001
    SmartBpUGraph(const SmartBpUGraph &) : ExtendedSmartBpUGraphBase() {};
deba@2190
  1002
deba@2190
  1003
    /// \brief Assignment of SmartBpUGraph to another one is \e not
deba@2190
  1004
    /// allowed.
deba@2190
  1005
    ///
deba@2190
  1006
    /// Assignment of SmartBpUGraph to another one is \e not allowed.
deba@2190
  1007
    void operator=(const SmartBpUGraph &) {}
deba@2190
  1008
deba@2190
  1009
  public:
deba@2190
  1010
deba@2190
  1011
    typedef ExtendedSmartBpUGraphBase Parent;
deba@2190
  1012
deba@2190
  1013
    ///Constructor
deba@2190
  1014
    
deba@2190
  1015
    ///Constructor.
deba@2190
  1016
    ///
deba@2190
  1017
    SmartBpUGraph() : ExtendedSmartBpUGraphBase() {}
deba@2190
  1018
deba@2190
  1019
    ///Add a new ANode to the graph.
deba@2190
  1020
    
deba@2190
  1021
    /// \return the new node.
deba@2190
  1022
    ///
deba@2190
  1023
    Node addANode() { return Parent::addANode(); }
deba@2190
  1024
deba@2190
  1025
    ///Add a new BNode to the graph.
deba@2190
  1026
    
deba@2190
  1027
    /// \return the new node.
deba@2190
  1028
    ///
deba@2190
  1029
    Node addBNode() { return Parent::addBNode(); }
deba@2190
  1030
    
deba@2190
  1031
    ///Add a new undirected edge to the graph.
deba@2190
  1032
    
deba@2190
  1033
    ///Add a new undirected edge to the graph with node \c s
deba@2190
  1034
    ///and \c t.
deba@2190
  1035
    ///\return the new undirected edge.
deba@2190
  1036
    UEdge addEdge(const Node& s, const Node& t) { 
deba@2190
  1037
      return Parent::addEdge(s, t); 
deba@2190
  1038
    }
deba@2190
  1039
deba@2190
  1040
    ///Clear the graph.
deba@2190
  1041
    
deba@2190
  1042
    ///Erase all the nodes and edges from the graph.
deba@2190
  1043
    ///
deba@2190
  1044
    void clear() {
deba@2190
  1045
      Parent::clear();
deba@2190
  1046
    }
deba@2190
  1047
    
deba@2190
  1048
  public:
deba@2190
  1049
deba@2190
  1050
    class Snapshot;
deba@2190
  1051
deba@2190
  1052
  protected:
deba@2190
  1053
    
deba@2190
  1054
    void restoreSnapshot(const Snapshot &s)
deba@2190
  1055
    {
deba@2190
  1056
      while(s.edge_num<edges.size()) {
deba@2190
  1057
        UEdge edge = uEdgeFromId(edges.size()-1);
deba@2381
  1058
	Parent::notifier(UEdge()).erase(edge);
deba@2190
  1059
        std::vector<Edge> dir;
deba@2190
  1060
        dir.push_back(Parent::direct(edge, true));
deba@2190
  1061
        dir.push_back(Parent::direct(edge, false));
deba@2381
  1062
	Parent::notifier(Edge()).erase(dir);
deba@2190
  1063
	aNodes[edges.back().aNode >> 1].first=edges.back().next_out;
deba@2190
  1064
	bNodes[edges.back().bNode >> 1].first=edges.back().next_in;
deba@2190
  1065
	edges.pop_back();
deba@2190
  1066
      }
deba@2190
  1067
      while(s.anode_num<aNodes.size()) {
deba@2231
  1068
        Node node = nodeFromANodeId(aNodes.size() - 1);
deba@2381
  1069
	Parent::notifier(ANode()).erase(node);
deba@2381
  1070
	Parent::notifier(Node()).erase(node);
deba@2190
  1071
	aNodes.pop_back();
deba@2190
  1072
      }
deba@2190
  1073
      while(s.bnode_num<bNodes.size()) {
deba@2231
  1074
        Node node = nodeFromBNodeId(bNodes.size() - 1);
deba@2381
  1075
	Parent::notifier(BNode()).erase(node);
deba@2381
  1076
	Parent::notifier(Node()).erase(node);
deba@2190
  1077
	bNodes.pop_back();
deba@2190
  1078
      }
deba@2190
  1079
    }    
deba@2190
  1080
deba@2190
  1081
  public:
deba@2190
  1082
deba@2190
  1083
    ///Class to make a snapshot of the graph and to restrore to it later.
deba@2190
  1084
deba@2190
  1085
    ///Class to make a snapshot of the graph and to restrore to it later.
deba@2190
  1086
    ///
deba@2190
  1087
    ///The newly added nodes and edges can be removed using the
deba@2190
  1088
    ///restore() function.
deba@2190
  1089
    ///
deba@2190
  1090
    ///\note After you restore a state, you cannot restore
deba@2190
  1091
    ///a later state, in other word you cannot add again the edges deleted
deba@2190
  1092
    ///by restore() using another one Snapshot instance.
deba@2190
  1093
    ///
deba@2190
  1094
    ///\warning If you do not use correctly the snapshot that can cause
deba@2190
  1095
    ///either broken program, invalid state of the graph, valid but
deba@2190
  1096
    ///not the restored graph or no change. Because the runtime performance
deba@2190
  1097
    ///the validity of the snapshot is not stored.
deba@2190
  1098
    class Snapshot 
deba@2190
  1099
    {
deba@2190
  1100
      SmartBpUGraph *g;
deba@2190
  1101
    protected:
deba@2190
  1102
      friend class SmartBpUGraph;
deba@2190
  1103
      unsigned int anode_num;
deba@2190
  1104
      unsigned int bnode_num;
deba@2190
  1105
      unsigned int edge_num;
deba@2190
  1106
    public:
deba@2190
  1107
      ///Default constructor.
deba@2190
  1108
      
deba@2190
  1109
      ///Default constructor.
deba@2190
  1110
      ///To actually make a snapshot you must call save().
deba@2190
  1111
      ///
deba@2190
  1112
      Snapshot() : g(0) {}
deba@2190
  1113
deba@2190
  1114
      ///Constructor that immediately makes a snapshot
deba@2190
  1115
      
deba@2190
  1116
      ///This constructor immediately makes a snapshot of the graph.
deba@2190
  1117
      ///\param _g The graph we make a snapshot of.
deba@2190
  1118
      Snapshot(SmartBpUGraph &_g) : g(&_g) {
deba@2190
  1119
	anode_num=g->aNodes.size();
deba@2190
  1120
	bnode_num=g->bNodes.size();
deba@2190
  1121
	edge_num=g->edges.size();
deba@2190
  1122
      }
deba@2190
  1123
deba@2190
  1124
      ///Make a snapshot.
deba@2190
  1125
deba@2190
  1126
      ///Make a snapshot of the graph.
deba@2190
  1127
      ///
deba@2190
  1128
      ///This function can be called more than once. In case of a repeated
deba@2190
  1129
      ///call, the previous snapshot gets lost.
deba@2190
  1130
      ///\param _g The graph we make the snapshot of.
deba@2190
  1131
      void save(SmartBpUGraph &_g) 
deba@2190
  1132
      {
deba@2190
  1133
	g=&_g;
deba@2190
  1134
	anode_num=g->aNodes.size();
deba@2190
  1135
	bnode_num=g->bNodes.size();
deba@2190
  1136
	edge_num=g->edges.size();
deba@2190
  1137
      }
deba@2190
  1138
deba@2190
  1139
      ///Undo the changes until a snapshot.
deba@2190
  1140
      
deba@2190
  1141
      ///Undo the changes until a snapshot created by save().
deba@2190
  1142
      ///
deba@2190
  1143
      ///\note After you restored a state, you cannot restore
deba@2190
  1144
      ///a later state, in other word you cannot add again the edges deleted
deba@2190
  1145
      ///by restore().
deba@2190
  1146
      void restore()
deba@2190
  1147
      {
deba@2190
  1148
	g->restoreSnapshot(*this);
deba@2190
  1149
      }
deba@2190
  1150
    };
deba@2190
  1151
  };
deba@2116
  1152
deba@2116
  1153
  
deba@2116
  1154
  /// @}  
alpar@921
  1155
} //namespace lemon
alpar@104
  1156
alpar@157
  1157
alpar@921
  1158
#endif //LEMON_SMART_GRAPH_H