lemon/concept/path.h
author deba
Tue, 17 Oct 2006 11:02:05 +0000
changeset 2251 37fa5f83251e
parent 1993 2115143eceea
permissions -rw-r--r--
Documentation for UndirGraphAdaptor
klao@959
     1
/* -*- C++ -*-
klao@959
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
klao@959
     8
 *
klao@959
     9
 * Permission to use, modify and distribute this software is granted
klao@959
    10
 * provided that this copyright notice appears in all copies. For
klao@959
    11
 * precise terms see the accompanying LICENSE file.
klao@959
    12
 *
klao@959
    13
 * This software is provided "AS IS" with no warranty of any kind,
klao@959
    14
 * express or implied, and with no claim as to its suitability for any
klao@959
    15
 * purpose.
klao@959
    16
 *
klao@959
    17
 */
klao@959
    18
klao@959
    19
///\ingroup concept
klao@959
    20
///\file
klao@959
    21
///\brief Classes for representing paths in graphs.
alpar@1151
    22
///
alpar@1151
    23
///\todo Iterators have obsolete style
klao@959
    24
klao@959
    25
#ifndef LEMON_CONCEPT_PATH_H
klao@959
    26
#define LEMON_CONCEPT_PATH_H
klao@959
    27
deba@1993
    28
#include <lemon/bits/invalid.h>
alpar@1643
    29
#include <lemon/concept_check.h>
klao@959
    30
klao@959
    31
namespace lemon {
klao@959
    32
  namespace concept {
klao@959
    33
    /// \addtogroup concept
klao@959
    34
    /// @{
klao@959
    35
klao@959
    36
alpar@967
    37
    //! \brief A skeleton structure for representing directed paths in a graph.
klao@959
    38
    //!
klao@959
    39
    //! A skeleton structure for representing directed paths in a graph.
deba@2247
    40
    //! \param _Graph The graph type in which the path is.
klao@959
    41
    //!
jacint@1270
    42
    //! In a sense, the path can be treated as a graph, for it has \c NodeIt
klao@959
    43
    //! and \c EdgeIt with the same usage. These types converts to the \c Node
klao@959
    44
    //! and \c Edge of the original graph.
deba@2247
    45
    template<typename _Graph>
klao@959
    46
    class Path {
klao@959
    47
    public:
klao@959
    48
klao@959
    49
      /// Type of the underlying graph.
deba@2247
    50
      typedef _Graph Graph;
klao@959
    51
      /// Edge type of the underlying graph.
deba@2247
    52
      typedef typename Graph::Edge Edge;
klao@959
    53
      /// Node type of the underlying graph.
deba@2247
    54
      typedef typename Graph::Node Node;
deba@2247
    55
klao@959
    56
      class NodeIt;
klao@959
    57
      class EdgeIt;
klao@959
    58
alpar@1624
    59
      /// \param _g The graph in which the path is.
klao@959
    60
      ///
alpar@1643
    61
      Path(const Graph &_g) {
alpar@1643
    62
	ignore_unused_variable_warning(_g);
alpar@1643
    63
      }
klao@959
    64
deba@2247
    65
      /// Length of the path ie. the number of edges in the path.
alpar@1282
    66
      int length() const {return 0;}
deba@2247
    67
klao@959
    68
      /// Returns whether the path is empty.
klao@959
    69
      bool empty() const { return true;}
klao@959
    70
klao@959
    71
      /// Resets the path to an empty path.
klao@959
    72
      void clear() {}
klao@959
    73
klao@959
    74
      /// \brief Starting point of the path.
klao@959
    75
      ///
klao@959
    76
      /// Starting point of the path.
klao@959
    77
      /// Returns INVALID if the path is empty.
deba@2247
    78
      Node target() const {return INVALID;}
klao@959
    79
      /// \brief End point of the path.
klao@959
    80
      ///
klao@959
    81
      /// End point of the path.
klao@959
    82
      /// Returns INVALID if the path is empty.
deba@2247
    83
      Node source() const {return INVALID;}
klao@959
    84
alpar@986
    85
      /// \brief The target of an edge.
klao@959
    86
      ///
alpar@986
    87
      /// Returns node iterator pointing to the target node of the
klao@959
    88
      /// given edge iterator.
alpar@1367
    89
      NodeIt target(const EdgeIt&) const {return INVALID;}
klao@959
    90
alpar@986
    91
      /// \brief The source of an edge.
klao@959
    92
      ///
alpar@986
    93
      /// Returns node iterator pointing to the source node of the
klao@959
    94
      /// given edge iterator.
alpar@1367
    95
      NodeIt source(const EdgeIt&) const {return INVALID;}
klao@959
    96
deba@2247
    97
      /// \brief Iterator class to iterate on the nodes of the paths
deba@2247
    98
      ///
deba@2247
    99
      /// This class is used to iterate on the nodes of the paths
deba@2247
   100
      ///
deba@2247
   101
      /// Of course it converts to Graph::Node.
klao@959
   102
      class NodeIt {
klao@959
   103
      public:
klao@959
   104
	/// Default constructor
klao@959
   105
	NodeIt() {}
klao@959
   106
	/// Invalid constructor
klao@959
   107
	NodeIt(Invalid) {}
klao@959
   108
	/// Constructor with starting point
alpar@1367
   109
	NodeIt(const Path &) {}
klao@959
   110
klao@959
   111
	///Conversion to Graph::Node
deba@2247
   112
	operator Node() const { return INVALID; }
klao@959
   113
	/// Next node
klao@959
   114
	NodeIt& operator++() {return *this;}
klao@959
   115
klao@959
   116
	/// Comparison operator
alpar@1367
   117
	bool operator==(const NodeIt&) const {return true;}
klao@959
   118
	/// Comparison operator
alpar@1367
   119
	bool operator!=(const NodeIt&) const {return true;}
deba@2247
   120
 	/// Comparison operator
deba@2247
   121
 	bool operator<(const NodeIt&) const {return false;}
klao@959
   122
klao@959
   123
      };
klao@959
   124
deba@2247
   125
      /// \brief Iterator class to iterate on the edges of the paths
deba@2247
   126
      ///
deba@2247
   127
      /// This class is used to iterate on the edges of the paths
deba@2247
   128
      ///
deba@2247
   129
      /// Of course it converts to Graph::Edge
deba@2247
   130
      class EdgeIt {
deba@2247
   131
      public:
deba@2247
   132
	/// Default constructor
deba@2247
   133
	EdgeIt() {}
deba@2247
   134
	/// Invalid constructor
deba@2247
   135
	EdgeIt(Invalid) {}
deba@2247
   136
	/// Constructor with starting point
deba@2247
   137
	EdgeIt(const Path &) {}
deba@2247
   138
deba@2247
   139
	operator Edge() const { return INVALID; }
deba@2247
   140
deba@2247
   141
	/// Next edge
deba@2247
   142
	EdgeIt& operator++() {return *this;}
deba@2247
   143
deba@2247
   144
	/// Comparison operator
deba@2247
   145
	bool operator==(const EdgeIt&) const {return true;}
deba@2247
   146
	/// Comparison operator
deba@2247
   147
	bool operator!=(const EdgeIt&) const {return true;}
deba@2247
   148
 	/// Comparison operator
deba@2247
   149
 	bool operator<(const EdgeIt&) const {return false;}
deba@2247
   150
deba@2247
   151
      };
deba@2247
   152
deba@2247
   153
klao@959
   154
      friend class Builder;
klao@959
   155
deba@2247
   156
      /// \brief Class to build paths
deba@2247
   157
      ///
deba@2247
   158
      /// This class is used to fill a path with edges.
deba@2247
   159
      ///
deba@2247
   160
      /// You can push new edges to the front and to the back of the path in
deba@2247
   161
      /// arbitrary order then you should commit these changes to the graph.
deba@2247
   162
      ///
deba@2247
   163
      /// While the builder is active (after the first modifying
deba@2247
   164
      /// operation and until the call of \ref commit()) the
deba@2247
   165
      /// underlining Path is in a "transitional" state (operations on
deba@2247
   166
      /// it have undefined result).
klao@959
   167
      class Builder {
klao@959
   168
      public:
klao@959
   169
deba@2247
   170
        /// Constructor
klao@959
   171
deba@2247
   172
        /// Constructor
deba@2247
   173
	/// \param _path the path you want to fill in.
klao@959
   174
	///
alpar@1228
   175
deba@2247
   176
	Builder(Path &_path) { ignore_unused_variable_warning(_path); }
klao@959
   177
klao@959
   178
	/// Sets the starting node of the path.
klao@959
   179
klao@959
   180
	/// Sets the starting node of the path. Edge added to the path
klao@959
   181
	/// afterwards have to be incident to this node.
jacint@1270
   182
	/// You \em must start building an empty path with these functions.
klao@959
   183
	/// (And you \em must \em not use it later).
klao@959
   184
	/// \sa pushFront()
klao@959
   185
	/// \sa pushBack()
deba@2247
   186
	void setStartNode(const Node &) {}
klao@959
   187
klao@959
   188
	///Push a new edge to the front of the path
klao@959
   189
klao@959
   190
	///Push a new edge to the front of the path.
klao@959
   191
	///If the path is empty, you \em must call \ref setStartNode() before
klao@959
   192
	///the first use of \ref pushFront().
deba@2247
   193
	void pushFront(const Edge&) {}
klao@959
   194
klao@959
   195
	///Push a new edge to the back of the path
klao@959
   196
klao@959
   197
	///Push a new edge to the back of the path.
klao@959
   198
	///If the path is empty, you \em must call \ref setStartNode() before
klao@959
   199
	///the first use of \ref pushBack().
deba@2247
   200
	void pushBack(const Edge&) {}
klao@959
   201
klao@959
   202
	///Commit the changes to the path.
deba@2247
   203
deba@2247
   204
	///Commit the changes to the path.
deba@2247
   205
        ///
klao@959
   206
	void commit() {}
klao@959
   207
klao@959
   208
	///Reserve (front) storage for the builder in advance.
klao@959
   209
jacint@1270
   210
	///If you know a reasonable upper bound on the number of the edges
klao@959
   211
	///to add to the front of the path,
klao@959
   212
	///using this function you may speed up the building.
alpar@1367
   213
	void reserveFront(size_t) {}
klao@959
   214
	///Reserve (back) storage for the builder in advance.
klao@959
   215
jacint@1270
   216
	///If you know a reasonable upper bound on the number of the edges
klao@959
   217
	///to add to the back of the path,
klao@959
   218
	///using this function you may speed up the building.
alpar@1367
   219
	void reserveBack(size_t) {}
klao@959
   220
      };
deba@2247
   221
deba@2247
   222
      template <typename _Path>
deba@2247
   223
      struct Constraints {
deba@2247
   224
	void constraints() {
deba@2247
   225
          typedef typename _Path::Node Node;
deba@2247
   226
          typedef typename _Path::NodeIt NodeIt;
deba@2247
   227
          typedef typename Graph::Node GraphNode;
deba@2247
   228
deba@2247
   229
          typedef typename _Path::Edge Edge;
deba@2247
   230
          typedef typename _Path::EdgeIt EdgeIt;
deba@2247
   231
          typedef typename Graph::Edge GraphEdge;
deba@2247
   232
deba@2247
   233
          typedef typename _Path::Builder Builder;
deba@2247
   234
deba@2247
   235
          path = _Path(graph);
deba@2247
   236
deba@2247
   237
          bool b = cpath.empty();
deba@2247
   238
          int l = cpath.length();
deba@2247
   239
deba@2247
   240
          Node gn;
deba@2247
   241
          Edge ge;
deba@2247
   242
          gn = cpath.source();
deba@2247
   243
          gn = cpath.target();
deba@2247
   244
deba@2247
   245
          NodeIt nit;
deba@2247
   246
          EdgeIt eit(INVALID);
deba@2247
   247
          nit = path.source(eit);
deba@2247
   248
          nit = path.target(eit);
deba@2247
   249
          
deba@2247
   250
          nit = NodeIt();
deba@2247
   251
          nit = NodeIt(cpath);
deba@2247
   252
          nit = INVALID;
deba@2247
   253
          gn = nit;
deba@2247
   254
          ++nit;
deba@2247
   255
          b = nit == nit;
deba@2247
   256
          b = nit != nit;
deba@2247
   257
          b = nit < nit;
deba@2247
   258
deba@2247
   259
          eit = EdgeIt();
deba@2247
   260
          eit = EdgeIt(cpath);
deba@2247
   261
          eit = INVALID;
deba@2247
   262
          ge = eit;
deba@2247
   263
          ++eit;
deba@2247
   264
          b = eit == eit;
deba@2247
   265
          b = eit != eit;
deba@2247
   266
          b = eit < eit;
deba@2247
   267
deba@2247
   268
          size_t st = 0;
deba@2247
   269
deba@2247
   270
          Builder builder(path); 
deba@2247
   271
          builder.setStartNode(gn);
deba@2247
   272
          builder.pushFront(ge);
deba@2247
   273
          builder.pushBack(ge);
deba@2247
   274
          builder.commit();
deba@2247
   275
          builder.reserveFront(st);
deba@2247
   276
          builder.reserveBack(st);
deba@2247
   277
          
deba@2247
   278
          ignore_unused_variable_warning(l);
deba@2247
   279
          ignore_unused_variable_warning(b);
deba@2247
   280
	}
deba@2247
   281
deba@2247
   282
        const Graph& graph;
deba@2247
   283
        const _Path& cpath;
deba@2247
   284
        _Path& path;
deba@2247
   285
      };
deba@2247
   286
klao@959
   287
    };
klao@959
   288
klao@959
   289
  ///@}
klao@959
   290
  }
klao@959
   291
klao@959
   292
} // namespace lemon
klao@959
   293
klao@959
   294
#endif // LEMON_CONCEPT_PATH_H