src/lemon/smart_graph.h
author klao
Wed, 10 Nov 2004 21:59:59 +0000
changeset 979 b5fb023cdb7b
parent 974 785062a83f8e
child 980 0f1044b7a3af
permissions -rw-r--r--
"make check" pass under icc v8.0

* There are _many_ remarks which are worth examinating! Non-inline (and even
not template) functions in header files for example.
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/smart_graph.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@105
    16
alpar@921
    17
#ifndef LEMON_SMART_GRAPH_H
alpar@921
    18
#define LEMON_SMART_GRAPH_H
alpar@104
    19
klao@491
    20
///\ingroup graphs
alpar@242
    21
///\file
alpar@242
    22
///\brief SmartGraph and SymSmartGraph classes.
alpar@242
    23
alpar@104
    24
#include <vector>
alpar@104
    25
alpar@921
    26
#include <lemon/invalid.h>
alpar@157
    27
klao@946
    28
#include <lemon/clearable_graph_extender.h>
klao@946
    29
#include <lemon/extendable_graph_extender.h>
klao@946
    30
#include <lemon/idmappable_graph_extender.h>
klao@946
    31
#include <lemon/iterable_graph_extender.h>
klao@946
    32
#include <lemon/alteration_observer_registry.h>
klao@946
    33
#include <lemon/default_map.h>
klao@946
    34
klao@977
    35
#include <lemon/utility.h>
deba@782
    36
alpar@921
    37
namespace lemon {
alpar@104
    38
klao@946
    39
  /// \addtogroup graphs
klao@946
    40
  /// @{
alpar@185
    41
alpar@973
    42
  class SmartGraph;
alpar@969
    43
  ///Base of SmartGraph
alpar@969
    44
alpar@969
    45
  ///Base of SmartGraph
alpar@969
    46
  ///
klao@946
    47
  class SmartGraphBase {
alpar@104
    48
alpar@973
    49
    friend class SmatGraph;
alpar@973
    50
alpar@973
    51
  protected:
alpar@104
    52
    struct NodeT 
alpar@104
    53
    {
alpar@104
    54
      int first_in,first_out;      
alpar@157
    55
      NodeT() : first_in(-1), first_out(-1) {}
alpar@104
    56
    };
alpar@104
    57
    struct EdgeT 
alpar@104
    58
    {
alpar@104
    59
      int head, tail, next_in, next_out;      
alpar@104
    60
      //FIXME: is this necessary?
alpar@157
    61
      EdgeT() : next_in(-1), next_out(-1) {}  
alpar@104
    62
    };
alpar@104
    63
alpar@104
    64
    std::vector<NodeT> nodes;
alpar@129
    65
alpar@104
    66
    std::vector<EdgeT> edges;
alpar@104
    67
    
alpar@185
    68
    
alpar@104
    69
  public:
deba@782
    70
klao@946
    71
    typedef SmartGraphBase Graph;
alpar@104
    72
alpar@164
    73
    class Node;
alpar@164
    74
    class Edge;
alpar@108
    75
alpar@104
    76
    
alpar@104
    77
  public:
alpar@104
    78
klao@946
    79
    SmartGraphBase() : nodes(), edges() { }
klao@946
    80
    SmartGraphBase(const SmartGraphBase &_g) : nodes(_g.nodes), edges(_g.edges) { }
alpar@104
    81
    
klao@977
    82
    typedef True NodeNumTag;
klao@977
    83
    typedef True EdgeNumTag;
klao@977
    84
alpar@813
    85
    ///Number of nodes.
alpar@813
    86
    int nodeNum() const { return nodes.size(); }
alpar@813
    87
    ///Number of edges.
alpar@813
    88
    int edgeNum() const { return edges.size(); }
alpar@104
    89
alpar@813
    90
    /// Maximum node ID.
alpar@813
    91
    
alpar@813
    92
    /// Maximum node ID.
alpar@813
    93
    ///\sa id(Node)
alpar@813
    94
    int maxNodeId() const { return nodes.size()-1; }
alpar@813
    95
    /// Maximum edge ID.
alpar@813
    96
    
alpar@813
    97
    /// Maximum edge ID.
alpar@813
    98
    ///\sa id(Edge)
alpar@813
    99
    int maxEdgeId() const { return edges.size()-1; }
alpar@108
   100
alpar@164
   101
    Node tail(Edge e) const { return edges[e.n].tail; }
alpar@164
   102
    Node head(Edge e) const { return edges[e.n].head; }
alpar@104
   103
alpar@813
   104
    /// Node ID.
alpar@813
   105
    
alpar@813
   106
    /// The ID of a valid Node is a nonnegative integer not greater than
alpar@813
   107
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
alpar@813
   108
    /// and the greatest node ID can be actually less then \ref maxNodeId().
alpar@813
   109
    ///
alpar@813
   110
    /// The ID of the \ref INVALID node is -1.
alpar@813
   111
    ///\return The ID of the node \c v. 
alpar@713
   112
    static int id(Node v) { return v.n; }
alpar@813
   113
    /// Edge ID.
alpar@813
   114
    
alpar@813
   115
    /// The ID of a valid Edge is a nonnegative integer not greater than
alpar@813
   116
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
alpar@813
   117
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
alpar@813
   118
    ///
alpar@813
   119
    /// The ID of the \ref INVALID edge is -1.
alpar@813
   120
    ///\return The ID of the edge \c e. 
alpar@713
   121
    static int id(Edge e) { return e.n; }
alpar@104
   122
alpar@164
   123
    Node addNode() {
alpar@164
   124
      Node n; n.n=nodes.size();
alpar@104
   125
      nodes.push_back(NodeT()); //FIXME: Hmmm...
alpar@104
   126
      return n;
alpar@104
   127
    }
alpar@108
   128
    
alpar@164
   129
    Edge addEdge(Node u, Node v) {
alpar@164
   130
      Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
alpar@104
   131
      edges[e.n].tail=u.n; edges[e.n].head=v.n;
alpar@104
   132
      edges[e.n].next_out=nodes[u.n].first_out;
alpar@104
   133
      edges[e.n].next_in=nodes[v.n].first_in;
alpar@104
   134
      nodes[u.n].first_out=nodes[v.n].first_in=e.n;
alpar@108
   135
alpar@104
   136
      return e;
alpar@104
   137
    }
alpar@104
   138
deba@782
   139
    void clear() {
deba@782
   140
      edges.clear();
deba@782
   141
      nodes.clear();
deba@782
   142
    }
alpar@104
   143
klao@946
   144
alpar@164
   145
    class Node {
klao@946
   146
      friend class SmartGraphBase;
alpar@973
   147
      friend class SmartGraph;
alpar@104
   148
alpar@104
   149
    protected:
alpar@104
   150
      int n;
alpar@973
   151
      ///\todo It should be removed (or at least define a setToId() instead).
alpar@973
   152
      ///
alpar@164
   153
      Node(int nn) {n=nn;}
alpar@104
   154
    public:
alpar@164
   155
      Node() {}
alpar@503
   156
      Node (Invalid) { n=-1; }
alpar@164
   157
      bool operator==(const Node i) const {return n==i.n;}
alpar@164
   158
      bool operator!=(const Node i) const {return n!=i.n;}
alpar@164
   159
      bool operator<(const Node i) const {return n<i.n;}
alpar@104
   160
    };
alpar@104
   161
    
alpar@104
   162
alpar@164
   163
    class Edge {
klao@946
   164
      friend class SmartGraphBase;
alpar@973
   165
      friend class SmartGraph;
alpar@185
   166
alpar@104
   167
    protected:
alpar@104
   168
      int n;
alpar@973
   169
      ///\todo It should be removed (or at least define a setToId() instead).
alpar@973
   170
      ///
alpar@905
   171
      Edge(int nn) {n=nn;}
alpar@706
   172
    public:
alpar@164
   173
      Edge() { }
marci@174
   174
      Edge (Invalid) { n=-1; }
alpar@164
   175
      bool operator==(const Edge i) const {return n==i.n;}
alpar@164
   176
      bool operator!=(const Edge i) const {return n!=i.n;}
alpar@164
   177
      bool operator<(const Edge i) const {return n<i.n;}
klao@946
   178
    };
alpar@905
   179
klao@946
   180
    void first(Node& node) const {
klao@946
   181
      node.n = nodes.size() - 1;
klao@946
   182
    }
klao@946
   183
klao@946
   184
    static void next(Node& node) {
klao@946
   185
      --node.n;
klao@946
   186
    }
klao@946
   187
klao@946
   188
    void first(Edge& edge) const {
klao@946
   189
      edge.n = edges.size() - 1;
klao@946
   190
    }
klao@946
   191
klao@946
   192
    static void next(Edge& edge) {
klao@946
   193
      --edge.n;
klao@946
   194
    }
klao@946
   195
klao@946
   196
    void firstOut(Edge& edge, const Node& node) const {
klao@946
   197
      edge.n = nodes[node.n].first_out;
klao@946
   198
    }
klao@946
   199
klao@946
   200
    void nextOut(Edge& edge) const {
klao@946
   201
      edge.n = edges[edge.n].next_out;
klao@946
   202
    }
klao@946
   203
klao@946
   204
    void firstIn(Edge& edge, const Node& node) const {
klao@946
   205
      edge.n = nodes[node.n].first_in;
klao@946
   206
    }
alpar@104
   207
    
klao@946
   208
    void nextIn(Edge& edge) const {
klao@946
   209
      edge.n = edges[edge.n].next_in;
klao@946
   210
    }
alpar@105
   211
alpar@969
   212
    Edge _findEdge(Node u,Node v, Edge prev = INVALID) 
alpar@969
   213
    {
alpar@969
   214
      int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
alpar@969
   215
      while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out;
alpar@969
   216
      prev.n=e;
alpar@969
   217
      return prev;
alpar@969
   218
    }
alpar@973
   219
alpar@104
   220
  };
alpar@185
   221
klao@946
   222
  typedef AlterableGraphExtender<SmartGraphBase> AlterableSmartGraphBase;
klao@946
   223
  typedef IterableGraphExtender<AlterableSmartGraphBase> IterableSmartGraphBase;
klao@946
   224
  typedef IdMappableGraphExtender<IterableSmartGraphBase> IdMappableSmartGraphBase;
klao@946
   225
  typedef DefaultMappableGraphExtender<IdMappableSmartGraphBase> MappableSmartGraphBase;
klao@946
   226
  typedef ExtendableGraphExtender<MappableSmartGraphBase> ExtendableSmartGraphBase;
klao@946
   227
  typedef ClearableGraphExtender<ExtendableSmartGraphBase> ClearableSmartGraphBase;
deba@937
   228
alpar@950
   229
  ///A smart graph class.
deba@937
   230
alpar@950
   231
  ///This is a simple and fast graph implementation.
alpar@950
   232
  ///It is also quite memory efficient, but at the price
alpar@974
   233
  ///that <b> it does support only limited (only stack-like)
alpar@974
   234
  ///node and edge deletions</b>.
alpar@950
   235
  ///It conforms to 
klao@959
   236
  ///the \ref concept::ExtendableGraph "ExtendableGraph" concept.
klao@959
   237
  ///\sa concept::ExtendableGraph.
alpar@950
   238
  ///
alpar@950
   239
  ///\todo Some member functions could be \c static.
alpar@950
   240
  ///
alpar@950
   241
  ///\author Alpar Juttner
alpar@969
   242
  class SmartGraph :public ClearableSmartGraphBase {
alpar@969
   243
  public:
alpar@969
   244
    /// Finds an edge between two nodes.
alpar@973
   245
    
alpar@969
   246
    /// Finds an edge from node \c u to node \c v.
alpar@969
   247
    ///
alpar@969
   248
    /// If \c prev is \ref INVALID (this is the default value), then
alpar@969
   249
    /// it finds the first edge from \c u to \c v. Otherwise it looks for
alpar@969
   250
    /// the next edge from \c u to \c v after \c prev.
alpar@969
   251
    /// \return The found edge or \ref INVALID if there is no such an edge.
alpar@969
   252
    ///
alpar@969
   253
    /// Thus you can iterate through each edge from \c u to \c v as it follows.
alpar@969
   254
    /// \code
alpar@969
   255
    /// for(Edge e=G.findEdge(u,v);e!=INVALID;e=G.findEdge(u,v,e)) {
alpar@969
   256
    ///   ...
alpar@969
   257
    /// }
alpar@969
   258
    /// \endcode
alpar@969
   259
    /// \todo Possibly it should be a global function.
alpar@969
   260
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
alpar@969
   261
    {
alpar@969
   262
      return _findEdge(u,v,prev);
alpar@969
   263
    }
alpar@973
   264
    
alpar@973
   265
    ///Internal data structure to store snapshots
alpar@973
   266
    
alpar@973
   267
    ///\ingroup graphs
alpar@973
   268
    ///\sa makeSnapShot()
alpar@973
   269
    ///\sa rollBack()
alpar@973
   270
    struct SnapShot 
alpar@973
   271
    {
alpar@973
   272
      unsigned int node_num;
alpar@973
   273
      unsigned int edge_num;
alpar@973
   274
    };
alpar@973
   275
    
alpar@973
   276
    ///Make a snapshot of the graph.
alpar@973
   277
alpar@973
   278
    ///Make a snapshot of the graph.
alpar@973
   279
    ///
alpar@973
   280
    ///The newly added nodes and edges can be removed using the
alpar@973
   281
    ///rollBack() function.
alpar@973
   282
    ///
alpar@973
   283
    ///\return An stucture SnapShot describing the pesent state of the
alpar@973
   284
    ///graph.
alpar@973
   285
    ///\note After you rolled back to a state, you cannot roll "back" to
alpar@973
   286
    ///a later state, in other word you cannot add again the edges deleted
alpar@973
   287
    ///by rollBack().
alpar@974
   288
    ///\todo This function might be called saveState() or getState().
alpar@973
   289
    SnapShot makeSnapShot() 
alpar@973
   290
    {
alpar@973
   291
      SnapShot s;
alpar@973
   292
      s.node_num=nodes.size();
alpar@973
   293
      s.edge_num=edges.size();
alpar@973
   294
      return s;
alpar@973
   295
    }
alpar@973
   296
    
alpar@973
   297
    ///Undo the changes until a snapshot.
alpar@973
   298
alpar@973
   299
    ///Undo the changes until a snapshot created by makeSnapShot().
alpar@973
   300
    ///
alpar@973
   301
    ///\param s an internal stucture given back by makeSnapShot()
alpar@973
   302
    ///\note After you rolled back to a state, you cannot "roll forward" to
alpar@973
   303
    ///a later state, in other word you cannot add again the edges deleted
alpar@973
   304
    ///by rollBack().
alpar@973
   305
    ///
alpar@973
   306
    ///\todo This function might be called undo().
alpar@973
   307
    
alpar@973
   308
    void rollBack(const SnapShot &s)
alpar@973
   309
    {
alpar@973
   310
      while(s.edge_num>edges.size()) {
alpar@973
   311
	edge_observers.erase(Edge(edges.size()-1));
alpar@973
   312
	nodes[edges.back().head].first_in=edges.back().next_in;
alpar@973
   313
	nodes[edges.back().tail].first_out=edges.back().next_out;
alpar@973
   314
	edges.pop_back();
alpar@973
   315
      }
alpar@973
   316
      //nodes.resize(s.nodes_num);
alpar@973
   317
      while(s.node_num>nodes.size()) {
alpar@973
   318
	node_observers.erase(Node(nodes.size()-1));
alpar@973
   319
	nodes.pop_back();
alpar@973
   320
      }
alpar@973
   321
    }
alpar@973
   322
  };
alpar@950
   323
  
alpar@407
   324
  /// @}  
alpar@921
   325
} //namespace lemon
alpar@104
   326
alpar@157
   327
alpar@921
   328
#endif //LEMON_SMART_GRAPH_H