lemon/smart_graph.h
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 05 May 2008 11:41:30 +0200
changeset 155 5c3604513ed0
parent 139 701c529ba737
child 157 2ccc1afc2c52
permissions -rw-r--r--
Update .hgignore
deba@109
     1
/* -*- C++ -*-
deba@109
     2
 *
deba@109
     3
 * This file is a part of LEMON, a generic C++ optimization library
deba@109
     4
 *
deba@109
     5
 * Copyright (C) 2003-2008
deba@109
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@109
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@109
     8
 *
deba@109
     9
 * Permission to use, modify and distribute this software is granted
deba@109
    10
 * provided that this copyright notice appears in all copies. For
deba@109
    11
 * precise terms see the accompanying LICENSE file.
deba@109
    12
 *
deba@109
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@109
    14
 * express or implied, and with no claim as to its suitability for any
deba@109
    15
 * purpose.
deba@109
    16
 *
deba@109
    17
 */
deba@109
    18
deba@109
    19
#ifndef LEMON_SMART_GRAPH_H
deba@109
    20
#define LEMON_SMART_GRAPH_H
deba@109
    21
deba@109
    22
///\ingroup graphs
deba@109
    23
///\file
deba@109
    24
///\brief SmartDigraph and SmartGraph classes.
deba@109
    25
deba@109
    26
#include <vector>
deba@109
    27
deba@109
    28
#include <lemon/bits/invalid.h>
deba@109
    29
deba@109
    30
#include <lemon/bits/base_extender.h>
deba@109
    31
#include <lemon/bits/graph_extender.h>
deba@109
    32
deba@109
    33
#include <lemon/bits/utility.h>
deba@109
    34
#include <lemon/error.h>
deba@109
    35
deba@109
    36
#include <lemon/bits/graph_extender.h>
deba@109
    37
deba@109
    38
namespace lemon {
deba@109
    39
deba@109
    40
  class SmartDigraph;
deba@109
    41
  ///Base of SmartDigraph
deba@109
    42
deba@109
    43
  ///Base of SmartDigraph
deba@109
    44
  ///
deba@109
    45
  class SmartDigraphBase {
deba@109
    46
  protected:
deba@109
    47
deba@109
    48
    struct NodeT 
deba@109
    49
    {
deba@109
    50
      int first_in, first_out;      
deba@109
    51
      NodeT() {}
deba@109
    52
    };
deba@109
    53
    struct ArcT 
deba@109
    54
    {
deba@109
    55
      int target, source, next_in, next_out;      
deba@109
    56
      ArcT() {}  
deba@109
    57
    };
deba@109
    58
deba@109
    59
    std::vector<NodeT> nodes;
deba@109
    60
    std::vector<ArcT> arcs;
deba@109
    61
        
deba@109
    62
  public:
deba@109
    63
deba@109
    64
    typedef SmartDigraphBase Graph;
deba@109
    65
deba@109
    66
    class Node;
deba@109
    67
    class Arc;
deba@109
    68
deba@109
    69
  public:
deba@109
    70
deba@109
    71
    SmartDigraphBase() : nodes(), arcs() { }
deba@109
    72
    SmartDigraphBase(const SmartDigraphBase &_g) 
deba@109
    73
      : nodes(_g.nodes), arcs(_g.arcs) { }
deba@109
    74
    
deba@109
    75
    typedef True NodeNumTag;
deba@139
    76
    typedef True EdgeNumTag;
deba@109
    77
deba@109
    78
    int nodeNum() const { return nodes.size(); }
deba@109
    79
    int arcNum() const { return arcs.size(); }
deba@109
    80
deba@109
    81
    int maxNodeId() const { return nodes.size()-1; }
deba@109
    82
    int maxArcId() const { return arcs.size()-1; }
deba@109
    83
deba@109
    84
    Node addNode() {
deba@109
    85
      int n = nodes.size();     
deba@109
    86
      nodes.push_back(NodeT());
deba@109
    87
      nodes[n].first_in = -1;
deba@109
    88
      nodes[n].first_out = -1;
deba@109
    89
      return Node(n);
deba@109
    90
    }
deba@109
    91
    
deba@109
    92
    Arc addArc(Node u, Node v) {
deba@109
    93
      int n = arcs.size(); 
deba@109
    94
      arcs.push_back(ArcT());
deba@109
    95
      arcs[n].source = u._id; 
deba@109
    96
      arcs[n].target = v._id;
deba@109
    97
      arcs[n].next_out = nodes[u._id].first_out;
deba@109
    98
      arcs[n].next_in = nodes[v._id].first_in;
deba@109
    99
      nodes[u._id].first_out = nodes[v._id].first_in = n;
deba@109
   100
deba@109
   101
      return Arc(n);
deba@109
   102
    }
deba@109
   103
deba@109
   104
    void clear() {
deba@109
   105
      arcs.clear();
deba@109
   106
      nodes.clear();
deba@109
   107
    }
deba@109
   108
deba@109
   109
    Node source(Arc a) const { return Node(arcs[a._id].source); }
deba@109
   110
    Node target(Arc a) const { return Node(arcs[a._id].target); }
deba@109
   111
deba@109
   112
    static int id(Node v) { return v._id; }
deba@109
   113
    static int id(Arc a) { return a._id; }
deba@109
   114
deba@109
   115
    static Node nodeFromId(int id) { return Node(id);}
deba@109
   116
    static Arc arcFromId(int id) { return Arc(id);}
deba@109
   117
deba@149
   118
    bool valid(Node n) const { 
deba@149
   119
      return n._id >= 0 && n._id < static_cast<int>(nodes.size()); 
deba@149
   120
    }
deba@149
   121
    bool valid(Arc a) const { 
deba@149
   122
      return a._id >= 0 && a._id < static_cast<int>(arcs.size()); 
deba@149
   123
    }
deba@149
   124
deba@109
   125
    class Node {
deba@109
   126
      friend class SmartDigraphBase;
deba@109
   127
      friend class SmartDigraph;
deba@109
   128
deba@109
   129
    protected:
deba@109
   130
      int _id;
deba@109
   131
      explicit Node(int id) : _id(id) {}
deba@109
   132
    public:
deba@109
   133
      Node() {}
deba@109
   134
      Node (Invalid) : _id(-1) {}
deba@109
   135
      bool operator==(const Node i) const {return _id == i._id;}
deba@109
   136
      bool operator!=(const Node i) const {return _id != i._id;}
deba@109
   137
      bool operator<(const Node i) const {return _id < i._id;}
deba@109
   138
    };
deba@109
   139
    
deba@109
   140
deba@109
   141
    class Arc {
deba@109
   142
      friend class SmartDigraphBase;
deba@109
   143
      friend class SmartDigraph;
deba@109
   144
deba@109
   145
    protected:
deba@109
   146
      int _id;
deba@109
   147
      explicit Arc(int id) : _id(id) {}
deba@109
   148
    public:
deba@109
   149
      Arc() { }
deba@109
   150
      Arc (Invalid) : _id(-1) {}
deba@109
   151
      bool operator==(const Arc i) const {return _id == i._id;}
deba@109
   152
      bool operator!=(const Arc i) const {return _id != i._id;}
deba@109
   153
      bool operator<(const Arc i) const {return _id < i._id;}
deba@109
   154
    };
deba@109
   155
deba@109
   156
    void first(Node& node) const {
deba@109
   157
      node._id = nodes.size() - 1;
deba@109
   158
    }
deba@109
   159
deba@109
   160
    static void next(Node& node) {
deba@109
   161
      --node._id;
deba@109
   162
    }
deba@109
   163
deba@109
   164
    void first(Arc& arc) const {
deba@109
   165
      arc._id = arcs.size() - 1;
deba@109
   166
    }
deba@109
   167
deba@109
   168
    static void next(Arc& arc) {
deba@109
   169
      --arc._id;
deba@109
   170
    }
deba@109
   171
deba@109
   172
    void firstOut(Arc& arc, const Node& node) const {
deba@109
   173
      arc._id = nodes[node._id].first_out;
deba@109
   174
    }
deba@109
   175
deba@109
   176
    void nextOut(Arc& arc) const {
deba@109
   177
      arc._id = arcs[arc._id].next_out;
deba@109
   178
    }
deba@109
   179
deba@109
   180
    void firstIn(Arc& arc, const Node& node) const {
deba@109
   181
      arc._id = nodes[node._id].first_in;
deba@109
   182
    }
deba@109
   183
    
deba@109
   184
    void nextIn(Arc& arc) const {
deba@109
   185
      arc._id = arcs[arc._id].next_in;
deba@109
   186
    }
deba@109
   187
deba@109
   188
  };
deba@109
   189
deba@109
   190
  typedef DigraphExtender<SmartDigraphBase> ExtendedSmartDigraphBase;
deba@109
   191
deba@109
   192
  ///\ingroup graphs
deba@109
   193
  ///
deba@109
   194
  ///\brief A smart directed graph class.
deba@109
   195
  ///
deba@109
   196
  ///This is a simple and fast digraph implementation.
deba@109
   197
  ///It is also quite memory efficient, but at the price
deba@109
   198
  ///that <b> it does support only limited (only stack-like)
deba@109
   199
  ///node and arc deletions</b>.
deba@109
   200
  ///It conforms to the \ref concepts::Digraph "Digraph concept" with
deba@109
   201
  ///an important extra feature that its maps are real \ref
deba@109
   202
  ///concepts::ReferenceMap "reference map"s.
deba@109
   203
  ///
deba@109
   204
  ///\sa concepts::Digraph.
deba@109
   205
  ///
deba@109
   206
  ///\author Alpar Juttner
deba@109
   207
  class SmartDigraph : public ExtendedSmartDigraphBase {
deba@109
   208
  public:
deba@109
   209
deba@109
   210
    typedef ExtendedSmartDigraphBase Parent;
deba@109
   211
deba@109
   212
  private:
deba@109
   213
deba@109
   214
    ///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead.
deba@109
   215
deba@109
   216
    ///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead.
deba@109
   217
    ///
deba@109
   218
    SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {};
deba@109
   219
    ///\brief Assignment of SmartDigraph to another one is \e not allowed.
deba@109
   220
    ///Use DigraphCopy() instead.
deba@109
   221
deba@109
   222
    ///Assignment of SmartDigraph to another one is \e not allowed.
deba@109
   223
    ///Use DigraphCopy() instead.
deba@109
   224
    void operator=(const SmartDigraph &) {}
deba@109
   225
deba@109
   226
  public:
deba@109
   227
    
deba@109
   228
    /// Constructor
deba@109
   229
    
deba@109
   230
    /// Constructor.
deba@109
   231
    ///
deba@109
   232
    SmartDigraph() {};
deba@109
   233
    
deba@109
   234
    ///Add a new node to the digraph.
deba@109
   235
    
deba@109
   236
    /// \return the new node.
deba@109
   237
    ///
deba@109
   238
    Node addNode() { return Parent::addNode(); }
deba@109
   239
    
deba@109
   240
    ///Add a new arc to the digraph.
deba@109
   241
    
deba@109
   242
    ///Add a new arc to the digraph with source node \c s
deba@109
   243
    ///and target node \c t.
deba@109
   244
    ///\return the new arc.
deba@109
   245
    Arc addArc(const Node& s, const Node& t) { 
deba@109
   246
      return Parent::addArc(s, t); 
deba@109
   247
    }
deba@109
   248
deba@109
   249
    /// \brief Using this it is possible to avoid the superfluous memory
deba@109
   250
    /// allocation.
deba@109
   251
deba@109
   252
    /// Using this it is possible to avoid the superfluous memory
deba@109
   253
    /// allocation: if you know that the digraph you want to build will
deba@109
   254
    /// be very large (e.g. it will contain millions of nodes and/or arcs)
deba@109
   255
    /// then it is worth reserving space for this amount before starting
deba@109
   256
    /// to build the digraph.
deba@109
   257
    /// \sa reserveArc
deba@109
   258
    void reserveNode(int n) { nodes.reserve(n); };
deba@109
   259
deba@109
   260
    /// \brief Using this it is possible to avoid the superfluous memory
deba@109
   261
    /// allocation.
deba@109
   262
deba@109
   263
    /// Using this it is possible to avoid the superfluous memory
deba@109
   264
    /// allocation: if you know that the digraph you want to build will
deba@109
   265
    /// be very large (e.g. it will contain millions of nodes and/or arcs)
deba@109
   266
    /// then it is worth reserving space for this amount before starting
deba@109
   267
    /// to build the digraph.
deba@109
   268
    /// \sa reserveNode
deba@109
   269
    void reserveArc(int m) { arcs.reserve(m); };
deba@109
   270
deba@149
   271
    /// \brief Node validity check
deba@149
   272
    ///
deba@149
   273
    /// This function gives back true if the given node is valid,
deba@149
   274
    /// ie. it is a real node of the graph.  
deba@149
   275
    ///
deba@149
   276
    /// \warning A removed node (using Snapshot) could become valid again
deba@149
   277
    /// when new nodes are added to the graph.
deba@149
   278
    bool valid(Node n) const { return Parent::valid(n); }
deba@149
   279
deba@149
   280
    /// \brief Arc validity check
deba@149
   281
    ///
deba@149
   282
    /// This function gives back true if the given arc is valid,
deba@149
   283
    /// ie. it is a real arc of the graph.  
deba@149
   284
    ///
deba@149
   285
    /// \warning A removed arc (using Snapshot) could become valid again
deba@149
   286
    /// when new arcs are added to the graph.
deba@149
   287
    bool valid(Arc a) const { return Parent::valid(a); }
deba@149
   288
deba@109
   289
    ///Clear the digraph.
deba@109
   290
    
deba@109
   291
    ///Erase all the nodes and arcs from the digraph.
deba@109
   292
    ///
deba@109
   293
    void clear() {
deba@109
   294
      Parent::clear();
deba@109
   295
    }
deba@109
   296
deba@109
   297
    ///Split a node.
deba@109
   298
    
deba@109
   299
    ///This function splits a node. First a new node is added to the digraph,
deba@109
   300
    ///then the source of each outgoing arc of \c n is moved to this new node.
deba@109
   301
    ///If \c connect is \c true (this is the default value), then a new arc
deba@109
   302
    ///from \c n to the newly created node is also added.
deba@109
   303
    ///\return The newly created node.
deba@109
   304
    ///
deba@109
   305
    ///\note The <tt>Arc</tt>s
deba@109
   306
    ///referencing a moved arc remain
deba@109
   307
    ///valid. However <tt>InArc</tt>'s and <tt>OutArc</tt>'s
deba@109
   308
    ///may be invalidated.
deba@109
   309
    ///\warning This functionality cannot be used together with the Snapshot
deba@109
   310
    ///feature.
deba@109
   311
    ///\todo It could be implemented in a bit faster way.
deba@109
   312
    Node split(Node n, bool connect = true)
deba@109
   313
    {
deba@109
   314
      Node b = addNode();
deba@109
   315
      nodes[b._id].first_out=nodes[n._id].first_out;
deba@109
   316
      nodes[n._id].first_out=-1;
deba@109
   317
      for(int i=nodes[b._id].first_out;i!=-1;i++) arcs[i].source=b._id;
deba@109
   318
      if(connect) addArc(n,b);
deba@109
   319
      return b;
deba@109
   320
    }
deba@109
   321
deba@109
   322
  public:
deba@109
   323
    
deba@109
   324
    class Snapshot;
deba@109
   325
deba@109
   326
  protected:
deba@109
   327
deba@109
   328
    void restoreSnapshot(const Snapshot &s)
deba@109
   329
    {
deba@109
   330
      while(s.arc_num<arcs.size()) {
deba@109
   331
        Arc arc = arcFromId(arcs.size()-1);
deba@109
   332
	Parent::notifier(Arc()).erase(arc);
deba@109
   333
	nodes[arcs.back().source].first_out=arcs.back().next_out;
deba@109
   334
	nodes[arcs.back().target].first_in=arcs.back().next_in;
deba@109
   335
	arcs.pop_back();
deba@109
   336
      }
deba@109
   337
      while(s.node_num<nodes.size()) {
deba@109
   338
        Node node = nodeFromId(nodes.size()-1);
deba@109
   339
	Parent::notifier(Node()).erase(node);
deba@109
   340
	nodes.pop_back();
deba@109
   341
      }
deba@109
   342
    }    
deba@109
   343
deba@109
   344
  public:
deba@109
   345
deba@109
   346
    ///Class to make a snapshot of the digraph and to restrore to it later.
deba@109
   347
deba@109
   348
    ///Class to make a snapshot of the digraph and to restrore to it later.
deba@109
   349
    ///
deba@109
   350
    ///The newly added nodes and arcs can be removed using the
deba@109
   351
    ///restore() function.
deba@109
   352
    ///\note After you restore a state, you cannot restore
deba@109
   353
    ///a later state, in other word you cannot add again the arcs deleted
deba@109
   354
    ///by restore() using another one Snapshot instance.
deba@109
   355
    ///
deba@109
   356
    ///\warning If you do not use correctly the snapshot that can cause
deba@109
   357
    ///either broken program, invalid state of the digraph, valid but
deba@109
   358
    ///not the restored digraph or no change. Because the runtime performance
deba@109
   359
    ///the validity of the snapshot is not stored.
deba@109
   360
    class Snapshot 
deba@109
   361
    {
deba@109
   362
      SmartDigraph *_graph;
deba@109
   363
    protected:
deba@109
   364
      friend class SmartDigraph;
deba@109
   365
      unsigned int node_num;
deba@109
   366
      unsigned int arc_num;
deba@109
   367
    public:
deba@109
   368
      ///Default constructor.
deba@109
   369
      
deba@109
   370
      ///Default constructor.
deba@109
   371
      ///To actually make a snapshot you must call save().
deba@109
   372
      ///
deba@109
   373
      Snapshot() : _graph(0) {}
deba@109
   374
      ///Constructor that immediately makes a snapshot
deba@109
   375
      
deba@109
   376
      ///This constructor immediately makes a snapshot of the digraph.
deba@109
   377
      ///\param _g The digraph we make a snapshot of.
deba@109
   378
      Snapshot(SmartDigraph &graph) : _graph(&graph) {
deba@109
   379
	node_num=_graph->nodes.size();
deba@109
   380
	arc_num=_graph->arcs.size();
deba@109
   381
      }
deba@109
   382
deba@109
   383
      ///Make a snapshot.
deba@109
   384
deba@109
   385
      ///Make a snapshot of the digraph.
deba@109
   386
      ///
deba@109
   387
      ///This function can be called more than once. In case of a repeated
deba@109
   388
      ///call, the previous snapshot gets lost.
deba@109
   389
      ///\param _g The digraph we make the snapshot of.
deba@109
   390
      void save(SmartDigraph &graph) 
deba@109
   391
      {
deba@109
   392
	_graph=&graph;
deba@109
   393
	node_num=_graph->nodes.size();
deba@109
   394
	arc_num=_graph->arcs.size();
deba@109
   395
      }
deba@109
   396
deba@109
   397
      ///Undo the changes until a snapshot.
deba@109
   398
      
deba@109
   399
      ///Undo the changes until a snapshot created by save().
deba@109
   400
      ///
deba@109
   401
      ///\note After you restored a state, you cannot restore
deba@109
   402
      ///a later state, in other word you cannot add again the arcs deleted
deba@109
   403
      ///by restore().
deba@109
   404
      void restore()
deba@109
   405
      {
deba@109
   406
	_graph->restoreSnapshot(*this);
deba@109
   407
      }
deba@109
   408
    };
deba@109
   409
  };
deba@109
   410
deba@109
   411
deba@109
   412
  class SmartGraphBase {
deba@109
   413
deba@109
   414
  protected:
deba@109
   415
deba@109
   416
    struct NodeT {
deba@109
   417
      int first_out;
deba@109
   418
    };
deba@109
   419
 
deba@109
   420
    struct ArcT {
deba@109
   421
      int target;
deba@109
   422
      int next_out;
deba@109
   423
    };
deba@109
   424
deba@109
   425
    std::vector<NodeT> nodes;
deba@109
   426
    std::vector<ArcT> arcs;
deba@109
   427
deba@109
   428
    int first_free_arc;
deba@109
   429
    
deba@109
   430
  public:
deba@109
   431
    
deba@109
   432
    typedef SmartGraphBase Digraph;
deba@109
   433
deba@109
   434
    class Node;
deba@109
   435
    class Arc;
deba@109
   436
    class Edge;
deba@109
   437
    
deba@109
   438
    class Node {
deba@109
   439
      friend class SmartGraphBase;
deba@109
   440
    protected:
deba@109
   441
deba@109
   442
      int _id;
deba@109
   443
      explicit Node(int id) { _id = id;}
deba@109
   444
deba@109
   445
    public:
deba@109
   446
      Node() {}
deba@109
   447
      Node (Invalid) { _id = -1; }
deba@109
   448
      bool operator==(const Node& node) const {return _id == node._id;}
deba@109
   449
      bool operator!=(const Node& node) const {return _id != node._id;}
deba@109
   450
      bool operator<(const Node& node) const {return _id < node._id;}
deba@109
   451
    };
deba@109
   452
deba@109
   453
    class Edge {
deba@109
   454
      friend class SmartGraphBase;
deba@109
   455
    protected:
deba@109
   456
deba@109
   457
      int _id;
deba@109
   458
      explicit Edge(int id) { _id = id;}
deba@109
   459
deba@109
   460
    public:
deba@109
   461
      Edge() {}
deba@109
   462
      Edge (Invalid) { _id = -1; }
deba@109
   463
      bool operator==(const Edge& arc) const {return _id == arc._id;}
deba@109
   464
      bool operator!=(const Edge& arc) const {return _id != arc._id;}
deba@109
   465
      bool operator<(const Edge& arc) const {return _id < arc._id;}
deba@109
   466
    };
deba@109
   467
deba@109
   468
    class Arc {
deba@109
   469
      friend class SmartGraphBase;
deba@109
   470
    protected:
deba@109
   471
deba@109
   472
      int _id;
deba@109
   473
      explicit Arc(int id) { _id = id;}
deba@109
   474
deba@109
   475
    public:
deba@109
   476
      operator Edge() const { return edgeFromId(_id / 2); }
deba@109
   477
deba@109
   478
      Arc() {}
deba@109
   479
      Arc (Invalid) { _id = -1; }
deba@109
   480
      bool operator==(const Arc& arc) const {return _id == arc._id;}
deba@109
   481
      bool operator!=(const Arc& arc) const {return _id != arc._id;}
deba@109
   482
      bool operator<(const Arc& arc) const {return _id < arc._id;}
deba@109
   483
    };
deba@109
   484
deba@109
   485
deba@109
   486
deba@109
   487
    SmartGraphBase()
deba@109
   488
      : nodes(), arcs() {}
deba@109
   489
deba@109
   490
    
deba@109
   491
    int maxNodeId() const { return nodes.size()-1; } 
deba@109
   492
    int maxEdgeId() const { return arcs.size() / 2 - 1; }
deba@109
   493
    int maxArcId() const { return arcs.size()-1; }
deba@109
   494
deba@109
   495
    Node source(Arc e) const { return Node(arcs[e._id ^ 1].target); }
deba@109
   496
    Node target(Arc e) const { return Node(arcs[e._id].target); }
deba@109
   497
deba@125
   498
    Node u(Edge e) const { return Node(arcs[2 * e._id].target); }
deba@125
   499
    Node v(Edge e) const { return Node(arcs[2 * e._id + 1].target); }
deba@109
   500
deba@109
   501
    static bool direction(Arc e) {
deba@109
   502
      return (e._id & 1) == 1;
deba@109
   503
    }
deba@109
   504
deba@109
   505
    static Arc direct(Edge e, bool d) {
deba@109
   506
      return Arc(e._id * 2 + (d ? 1 : 0));
deba@109
   507
    }
deba@109
   508
deba@109
   509
    void first(Node& node) const { 
deba@109
   510
      node._id = nodes.size() - 1;
deba@109
   511
    }
deba@109
   512
deba@109
   513
    void next(Node& node) const {
deba@109
   514
      --node._id;
deba@109
   515
    }
deba@109
   516
deba@109
   517
    void first(Arc& arc) const { 
deba@109
   518
      arc._id = arcs.size() - 1;
deba@109
   519
    }
deba@109
   520
deba@109
   521
    void next(Arc& arc) const {
deba@109
   522
      --arc._id;
deba@109
   523
    }
deba@109
   524
deba@109
   525
    void first(Edge& arc) const { 
deba@109
   526
      arc._id = arcs.size() / 2 - 1;
deba@109
   527
    }
deba@109
   528
deba@109
   529
    void next(Edge& arc) const {
deba@109
   530
      --arc._id;
deba@109
   531
    }
deba@109
   532
deba@109
   533
    void firstOut(Arc &arc, const Node& v) const {
deba@109
   534
      arc._id = nodes[v._id].first_out;
deba@109
   535
    }
deba@109
   536
    void nextOut(Arc &arc) const {
deba@109
   537
      arc._id = arcs[arc._id].next_out;
deba@109
   538
    }
deba@109
   539
deba@109
   540
    void firstIn(Arc &arc, const Node& v) const {
deba@109
   541
      arc._id = ((nodes[v._id].first_out) ^ 1);
deba@109
   542
      if (arc._id == -2) arc._id = -1;
deba@109
   543
    }
deba@109
   544
    void nextIn(Arc &arc) const {
deba@109
   545
      arc._id = ((arcs[arc._id ^ 1].next_out) ^ 1);
deba@109
   546
      if (arc._id == -2) arc._id = -1;
deba@109
   547
    }
deba@109
   548
deba@109
   549
    void firstInc(Edge &arc, bool& d, const Node& v) const {
deba@109
   550
      int de = nodes[v._id].first_out;
deba@109
   551
      if (de != -1) {
deba@109
   552
        arc._id = de / 2;
deba@109
   553
        d = ((de & 1) == 1);
deba@109
   554
      } else {
deba@109
   555
        arc._id = -1;
deba@109
   556
        d = true;
deba@109
   557
      }
deba@109
   558
    }
deba@109
   559
    void nextInc(Edge &arc, bool& d) const {
deba@109
   560
      int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out);
deba@109
   561
      if (de != -1) {
deba@109
   562
        arc._id = de / 2;
deba@109
   563
        d = ((de & 1) == 1);
deba@109
   564
      } else {
deba@109
   565
        arc._id = -1;
deba@109
   566
        d = true;      
deba@109
   567
      }
deba@109
   568
    }
deba@109
   569
    
deba@109
   570
    static int id(Node v) { return v._id; }
deba@109
   571
    static int id(Arc e) { return e._id; }
deba@109
   572
    static int id(Edge e) { return e._id; }
deba@109
   573
deba@109
   574
    static Node nodeFromId(int id) { return Node(id);}
deba@109
   575
    static Arc arcFromId(int id) { return Arc(id);}
deba@109
   576
    static Edge edgeFromId(int id) { return Edge(id);}
deba@109
   577
deba@149
   578
    bool valid(Node n) const { 
deba@149
   579
      return n._id >= 0 && n._id < static_cast<int>(nodes.size()); 
deba@149
   580
    }
deba@149
   581
    bool valid(Arc a) const { 
deba@149
   582
      return a._id >= 0 && a._id < static_cast<int>(arcs.size());
deba@149
   583
    }
deba@149
   584
    bool valid(Edge e) const { 
deba@149
   585
      return e._id >= 0 && 2 * e._id < static_cast<int>(arcs.size()); 
deba@149
   586
    }
deba@149
   587
deba@109
   588
    Node addNode() {     
deba@109
   589
      int n = nodes.size();
deba@109
   590
      nodes.push_back(NodeT());
deba@109
   591
      nodes[n].first_out = -1;
deba@109
   592
      
deba@109
   593
      return Node(n);
deba@109
   594
    }
deba@109
   595
    
deba@138
   596
    Edge addEdge(Node u, Node v) {
deba@109
   597
      int n = arcs.size();
deba@109
   598
      arcs.push_back(ArcT());
deba@109
   599
      arcs.push_back(ArcT());
deba@109
   600
      
deba@109
   601
      arcs[n].target = u._id;
deba@109
   602
      arcs[n | 1].target = v._id;
deba@109
   603
deba@109
   604
      arcs[n].next_out = nodes[v._id].first_out;
deba@109
   605
      nodes[v._id].first_out = n;
deba@109
   606
deba@109
   607
      arcs[n | 1].next_out = nodes[u._id].first_out;	
deba@109
   608
      nodes[u._id].first_out = (n | 1);
deba@109
   609
deba@109
   610
      return Edge(n / 2);
deba@109
   611
    }
deba@109
   612
    
deba@109
   613
    void clear() {
deba@109
   614
      arcs.clear();
deba@109
   615
      nodes.clear();
deba@109
   616
    }
deba@109
   617
deba@109
   618
  };
deba@109
   619
deba@109
   620
  typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
deba@109
   621
deba@109
   622
  /// \ingroup graphs
deba@109
   623
  ///
deba@109
   624
  /// \brief A smart undirected graph class.
deba@109
   625
  ///
deba@109
   626
  /// This is a simple and fast graph implementation.
deba@109
   627
  /// It is also quite memory efficient, but at the price
deba@109
   628
  /// that <b> it does support only limited (only stack-like)
deba@109
   629
  /// node and arc deletions</b>.
deba@109
   630
  /// Except from this it conforms to 
deba@109
   631
  /// the \ref concepts::Graph "Graph concept".
deba@109
   632
  ///
deba@109
   633
  /// It also has an
deba@109
   634
  /// important extra feature that
deba@109
   635
  /// its maps are real \ref concepts::ReferenceMap "reference map"s.
deba@109
   636
  ///
deba@109
   637
  /// \sa concepts::Graph.
deba@109
   638
  ///
deba@109
   639
  class SmartGraph : public ExtendedSmartGraphBase {
deba@109
   640
  private:
deba@109
   641
deba@109
   642
    ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
deba@109
   643
deba@109
   644
    ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
deba@109
   645
    ///
deba@109
   646
    SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {};
deba@109
   647
deba@109
   648
    ///\brief Assignment of SmartGraph to another one is \e not allowed.
deba@109
   649
    ///Use GraphCopy() instead.
deba@109
   650
deba@109
   651
    ///Assignment of SmartGraph to another one is \e not allowed.
deba@109
   652
    ///Use GraphCopy() instead.
deba@109
   653
    void operator=(const SmartGraph &) {}
deba@109
   654
deba@109
   655
  public:
deba@109
   656
deba@109
   657
    typedef ExtendedSmartGraphBase Parent;
deba@109
   658
deba@109
   659
    /// Constructor
deba@109
   660
    
deba@109
   661
    /// Constructor.
deba@109
   662
    ///
deba@109
   663
    SmartGraph() {}
deba@109
   664
deba@109
   665
    ///Add a new node to the graph.
deba@109
   666
    
deba@109
   667
    /// \return the new node.
deba@109
   668
    ///
deba@109
   669
    Node addNode() { return Parent::addNode(); }
deba@109
   670
    
deba@109
   671
    ///Add a new edge to the graph.
deba@109
   672
    
deba@109
   673
    ///Add a new edge to the graph with node \c s
deba@109
   674
    ///and \c t.
deba@109
   675
    ///\return the new edge.
deba@109
   676
    Edge addEdge(const Node& s, const Node& t) { 
deba@138
   677
      return Parent::addEdge(s, t); 
deba@109
   678
    }
deba@109
   679
deba@149
   680
    /// \brief Node validity check
deba@149
   681
    ///
deba@149
   682
    /// This function gives back true if the given node is valid,
deba@149
   683
    /// ie. it is a real node of the graph.  
deba@149
   684
    ///
deba@149
   685
    /// \warning A removed node (using Snapshot) could become valid again
deba@149
   686
    /// when new nodes are added to the graph.
deba@149
   687
    bool valid(Node n) const { return Parent::valid(n); }
deba@149
   688
deba@149
   689
    /// \brief Arc validity check
deba@149
   690
    ///
deba@149
   691
    /// This function gives back true if the given arc is valid,
deba@149
   692
    /// ie. it is a real arc of the graph.  
deba@149
   693
    ///
deba@149
   694
    /// \warning A removed arc (using Snapshot) could become valid again
deba@149
   695
    /// when new edges are added to the graph.
deba@149
   696
    bool valid(Arc a) const { return Parent::valid(a); }
deba@149
   697
deba@149
   698
    /// \brief Edge validity check
deba@149
   699
    ///
deba@149
   700
    /// This function gives back true if the given edge is valid,
deba@149
   701
    /// ie. it is a real edge of the graph.  
deba@149
   702
    ///
deba@149
   703
    /// \warning A removed edge (using Snapshot) could become valid again
deba@149
   704
    /// when new edges are added to the graph.
deba@149
   705
    bool valid(Edge e) const { return Parent::valid(e); }
deba@149
   706
deba@109
   707
    ///Clear the graph.
deba@109
   708
    
deba@109
   709
    ///Erase all the nodes and edges from the graph.
deba@109
   710
    ///
deba@109
   711
    void clear() {
deba@109
   712
      Parent::clear();
deba@109
   713
    }
deba@109
   714
deba@109
   715
  public:
deba@109
   716
    
deba@109
   717
    class Snapshot;
deba@109
   718
deba@109
   719
  protected:
deba@109
   720
deba@109
   721
    void saveSnapshot(Snapshot &s)
deba@109
   722
    {
deba@109
   723
      s._graph = this;
deba@109
   724
      s.node_num = nodes.size();
deba@109
   725
      s.arc_num = arcs.size();
deba@109
   726
    }
deba@109
   727
deba@109
   728
    void restoreSnapshot(const Snapshot &s)
deba@109
   729
    {
deba@109
   730
      while(s.arc_num<arcs.size()) {
deba@109
   731
        int n=arcs.size()-1;
deba@109
   732
        Edge arc=edgeFromId(n/2);
deba@109
   733
	Parent::notifier(Edge()).erase(arc);
deba@109
   734
        std::vector<Arc> dir;
deba@109
   735
        dir.push_back(arcFromId(n));
deba@109
   736
        dir.push_back(arcFromId(n-1));
deba@109
   737
	Parent::notifier(Arc()).erase(dir);
deba@109
   738
	nodes[arcs[n].target].first_out=arcs[n].next_out;
deba@109
   739
	nodes[arcs[n-1].target].first_out=arcs[n-1].next_out;
deba@109
   740
	arcs.pop_back();
deba@109
   741
	arcs.pop_back();
deba@109
   742
      }
deba@109
   743
      while(s.node_num<nodes.size()) {
deba@109
   744
        int n=nodes.size()-1;
deba@109
   745
        Node node = nodeFromId(n);
deba@109
   746
	Parent::notifier(Node()).erase(node);
deba@109
   747
	nodes.pop_back();
deba@109
   748
      }
deba@109
   749
    }    
deba@109
   750
deba@109
   751
  public:
deba@109
   752
deba@109
   753
    ///Class to make a snapshot of the digraph and to restrore to it later.
deba@109
   754
deba@109
   755
    ///Class to make a snapshot of the digraph and to restrore to it later.
deba@109
   756
    ///
deba@109
   757
    ///The newly added nodes and arcs can be removed using the
deba@109
   758
    ///restore() function.
deba@109
   759
    ///
deba@109
   760
    ///\note After you restore a state, you cannot restore
deba@109
   761
    ///a later state, in other word you cannot add again the arcs deleted
deba@109
   762
    ///by restore() using another one Snapshot instance.
deba@109
   763
    ///
deba@109
   764
    ///\warning If you do not use correctly the snapshot that can cause
deba@109
   765
    ///either broken program, invalid state of the digraph, valid but
deba@109
   766
    ///not the restored digraph or no change. Because the runtime performance
deba@109
   767
    ///the validity of the snapshot is not stored.
deba@109
   768
    class Snapshot 
deba@109
   769
    {
deba@109
   770
      SmartGraph *_graph;
deba@109
   771
    protected:
deba@109
   772
      friend class SmartGraph;
deba@109
   773
      unsigned int node_num;
deba@109
   774
      unsigned int arc_num;
deba@109
   775
    public:
deba@109
   776
      ///Default constructor.
deba@109
   777
      
deba@109
   778
      ///Default constructor.
deba@109
   779
      ///To actually make a snapshot you must call save().
deba@109
   780
      ///
deba@109
   781
      Snapshot() : _graph(0) {}
deba@109
   782
      ///Constructor that immediately makes a snapshot
deba@109
   783
      
deba@109
   784
      ///This constructor immediately makes a snapshot of the digraph.
deba@109
   785
      ///\param g The digraph we make a snapshot of.
deba@109
   786
      Snapshot(SmartGraph &graph) {
deba@109
   787
        graph.saveSnapshot(*this);
deba@109
   788
      }
deba@109
   789
deba@109
   790
      ///Make a snapshot.
deba@109
   791
deba@109
   792
      ///Make a snapshot of the graph.
deba@109
   793
      ///
deba@109
   794
      ///This function can be called more than once. In case of a repeated
deba@109
   795
      ///call, the previous snapshot gets lost.
deba@109
   796
      ///\param g The digraph we make the snapshot of.
deba@109
   797
      void save(SmartGraph &graph) 
deba@109
   798
      {
deba@109
   799
        graph.saveSnapshot(*this);
deba@109
   800
      }
deba@109
   801
deba@109
   802
      ///Undo the changes until a snapshot.
deba@109
   803
      
deba@109
   804
      ///Undo the changes until a snapshot created by save().
deba@109
   805
      ///
deba@109
   806
      ///\note After you restored a state, you cannot restore
deba@109
   807
      ///a later state, in other word you cannot add again the arcs deleted
deba@109
   808
      ///by restore().
deba@109
   809
      void restore()
deba@109
   810
      {
deba@109
   811
        _graph->restoreSnapshot(*this);
deba@109
   812
      }
deba@109
   813
    };
deba@109
   814
  };
deba@109
   815
  
deba@109
   816
} //namespace lemon
deba@109
   817
deba@109
   818
deba@109
   819
#endif //LEMON_SMART_GRAPH_H