lemon/concepts/path.h
author deba
Sun, 25 May 2008 16:35:05 +0000
changeset 2611 b526b87d8c2f
parent 2468 16615642ac7b
permissions -rw-r--r--
Fixing bug in named parameters (Back port bug fix of ticket #95)
alpar@2260
     1
/* -*- C++ -*-
alpar@2260
     2
 *
alpar@2260
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@2260
     4
 *
alpar@2553
     5
 * Copyright (C) 2003-2008
alpar@2260
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@2260
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@2260
     8
 *
alpar@2260
     9
 * Permission to use, modify and distribute this software is granted
alpar@2260
    10
 * provided that this copyright notice appears in all copies. For
alpar@2260
    11
 * precise terms see the accompanying LICENSE file.
alpar@2260
    12
 *
alpar@2260
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@2260
    14
 * express or implied, and with no claim as to its suitability for any
alpar@2260
    15
 * purpose.
alpar@2260
    16
 *
alpar@2260
    17
 */
alpar@2260
    18
alpar@2260
    19
///\ingroup concept
alpar@2260
    20
///\file
alpar@2260
    21
///\brief Classes for representing paths in graphs.
alpar@2260
    22
///
alpar@2260
    23
///\todo Iterators have obsolete style
alpar@2260
    24
alpar@2260
    25
#ifndef LEMON_CONCEPT_PATH_H
alpar@2260
    26
#define LEMON_CONCEPT_PATH_H
alpar@2260
    27
alpar@2260
    28
#include <lemon/bits/invalid.h>
deba@2335
    29
#include <lemon/bits/utility.h>
alpar@2260
    30
#include <lemon/concept_check.h>
alpar@2260
    31
alpar@2260
    32
namespace lemon {
alpar@2260
    33
  namespace concepts {
deba@2335
    34
alpar@2260
    35
    /// \addtogroup concept
alpar@2260
    36
    /// @{
alpar@2260
    37
deba@2335
    38
    /// \brief A skeleton structure for representing directed paths in
deba@2335
    39
    /// a graph.
deba@2335
    40
    ///
deba@2335
    41
    /// A skeleton structure for representing directed paths in a
deba@2335
    42
    /// graph.  
deba@2335
    43
    /// \param _Graph The graph type in which the path is.
deba@2335
    44
    ///
deba@2335
    45
    /// In a sense, the path can be treated as a list of edges. The
deba@2335
    46
    /// lemon path type stores just this list. As a consequence it
deba@2335
    47
    /// cannot enumerate the nodes in the path and the zero length
deba@2335
    48
    /// paths cannot store the source.
deba@2335
    49
    ///
deba@2335
    50
    template <typename _Graph>
alpar@2260
    51
    class Path {
alpar@2260
    52
    public:
alpar@2260
    53
alpar@2260
    54
      /// Type of the underlying graph.
alpar@2260
    55
      typedef _Graph Graph;
alpar@2260
    56
      /// Edge type of the underlying graph.
alpar@2260
    57
      typedef typename Graph::Edge Edge;
alpar@2260
    58
alpar@2260
    59
      class EdgeIt;
alpar@2260
    60
deba@2335
    61
      /// \brief Default constructor
deba@2335
    62
      Path() {}
deba@2335
    63
deba@2335
    64
      /// \brief Template constructor
deba@2335
    65
      template <typename CPath>
deba@2335
    66
      Path(const CPath& cpath) {}
deba@2335
    67
deba@2335
    68
      /// \brief Template assigment
deba@2335
    69
      template <typename CPath>
deba@2335
    70
      Path& operator=(const CPath& cpath) {}
alpar@2260
    71
alpar@2260
    72
      /// Length of the path ie. the number of edges in the path.
deba@2335
    73
      int length() const { return 0;}
alpar@2260
    74
alpar@2260
    75
      /// Returns whether the path is empty.
alpar@2260
    76
      bool empty() const { return true;}
alpar@2260
    77
alpar@2260
    78
      /// Resets the path to an empty path.
alpar@2260
    79
      void clear() {}
alpar@2260
    80
deba@2335
    81
      /// \brief Lemon style iterator for path edges
alpar@2260
    82
      ///
deba@2335
    83
      /// This class is used to iterate on the edges of the paths.
alpar@2260
    84
      class EdgeIt {
alpar@2260
    85
      public:
alpar@2260
    86
	/// Default constructor
alpar@2260
    87
	EdgeIt() {}
alpar@2260
    88
	/// Invalid constructor
alpar@2260
    89
	EdgeIt(Invalid) {}
deba@2335
    90
	/// Constructor for first edge
alpar@2260
    91
	EdgeIt(const Path &) {}
alpar@2260
    92
deba@2335
    93
        /// Conversion to Edge
alpar@2260
    94
	operator Edge() const { return INVALID; }
alpar@2260
    95
alpar@2260
    96
	/// Next edge
alpar@2260
    97
	EdgeIt& operator++() {return *this;}
alpar@2260
    98
alpar@2260
    99
	/// Comparison operator
alpar@2260
   100
	bool operator==(const EdgeIt&) const {return true;}
alpar@2260
   101
	/// Comparison operator
alpar@2260
   102
	bool operator!=(const EdgeIt&) const {return true;}
alpar@2260
   103
 	/// Comparison operator
alpar@2260
   104
 	bool operator<(const EdgeIt&) const {return false;}
alpar@2260
   105
alpar@2260
   106
      };
alpar@2260
   107
deba@2335
   108
      template <typename _Path>
deba@2335
   109
      struct Constraints {
deba@2335
   110
        void constraints() {
deba@2335
   111
          Path<Graph> pc;
deba@2335
   112
          _Path p, pp(pc);
deba@2335
   113
          int l = p.length();
deba@2335
   114
          int e = p.empty();
deba@2335
   115
          p.clear();
alpar@2260
   116
deba@2335
   117
          p = pc;
alpar@2260
   118
deba@2335
   119
          typename _Path::EdgeIt id, ii(INVALID), i(p);
deba@2335
   120
deba@2335
   121
          ++i;
deba@2335
   122
          typename Graph::Edge ed = i;
deba@2335
   123
deba@2335
   124
          e = (i == ii);
deba@2335
   125
          e = (i != ii);
deba@2335
   126
          e = (i < ii);
deba@2335
   127
deba@2335
   128
          ignore_unused_variable_warning(l);
deba@2335
   129
          ignore_unused_variable_warning(pp);
deba@2335
   130
          ignore_unused_variable_warning(e);
deba@2335
   131
          ignore_unused_variable_warning(id);
deba@2335
   132
          ignore_unused_variable_warning(ii);
deba@2335
   133
          ignore_unused_variable_warning(ed);
deba@2335
   134
        }
deba@2335
   135
      };
deba@2335
   136
deba@2335
   137
    };
deba@2335
   138
deba@2335
   139
    namespace _path_bits {
deba@2335
   140
      
deba@2335
   141
      template <typename _Graph, typename _Path, typename RevPathTag = void>
deba@2335
   142
      struct PathDumperConstraints {
deba@2335
   143
        void constraints() {
deba@2335
   144
          int l = p.length();
deba@2335
   145
          int e = p.empty();
deba@2335
   146
deba@2468
   147
          typename _Path::EdgeIt id, i(p);
deba@2335
   148
deba@2335
   149
          ++i;
deba@2335
   150
          typename _Graph::Edge ed = i;
deba@2335
   151
deba@2468
   152
          e = (i == INVALID);
deba@2468
   153
          e = (i != INVALID);
deba@2335
   154
deba@2335
   155
          ignore_unused_variable_warning(l);
deba@2335
   156
          ignore_unused_variable_warning(e);
deba@2335
   157
          ignore_unused_variable_warning(id);
deba@2335
   158
          ignore_unused_variable_warning(ed);
deba@2335
   159
        }
deba@2335
   160
        _Path& p;
deba@2335
   161
      };
deba@2335
   162
deba@2335
   163
      template <typename _Graph, typename _Path>
deba@2335
   164
      struct PathDumperConstraints<
deba@2335
   165
        _Graph, _Path, 
deba@2335
   166
        typename enable_if<typename _Path::RevPathTag, void>::type
deba@2335
   167
      > {
deba@2335
   168
        void constraints() {
deba@2335
   169
          int l = p.length();
deba@2335
   170
          int e = p.empty();
deba@2335
   171
deba@2468
   172
          typename _Path::RevEdgeIt id, i(p);
deba@2335
   173
deba@2335
   174
          ++i;
deba@2335
   175
          typename _Graph::Edge ed = i;
deba@2335
   176
deba@2468
   177
          e = (i == INVALID);
deba@2468
   178
          e = (i != INVALID);
deba@2335
   179
deba@2335
   180
          ignore_unused_variable_warning(l);
deba@2335
   181
          ignore_unused_variable_warning(e);
deba@2335
   182
          ignore_unused_variable_warning(id);
deba@2335
   183
          ignore_unused_variable_warning(ed);
deba@2335
   184
        }
deba@2335
   185
        _Path& p;
deba@2335
   186
      };
deba@2335
   187
    
deba@2335
   188
    }
deba@2335
   189
deba@2335
   190
deba@2335
   191
    /// \brief A skeleton structure for path dumpers.
deba@2335
   192
    ///
deba@2335
   193
    /// A skeleton structure for path dumpers. The path dumpers are
deba@2335
   194
    /// the generalization of the paths. The path dumpers can
deba@2335
   195
    /// enumerate the edges of the path wheter in forward or in
deba@2335
   196
    /// backward order.  In most time these classes are not used
deba@2335
   197
    /// directly rather it used to assign a dumped class to a real
deba@2335
   198
    /// path type.
deba@2335
   199
    ///
deba@2335
   200
    /// The main purpose of this concept is that the shortest path
deba@2335
   201
    /// algorithms can enumerate easily the edges in reverse order.
deba@2335
   202
    /// If we would like to give back a real path from these
deba@2335
   203
    /// algorithms then we should create a temporarly path object. In
deba@2335
   204
    /// Lemon such algorithms gives back a path dumper what can
deba@2335
   205
    /// assigned to a real path and the dumpers can be implemented as
deba@2335
   206
    /// an adaptor class to the predecessor map.
deba@2335
   207
deba@2335
   208
    /// \param _Graph  The graph type in which the path is.
deba@2335
   209
    ///
deba@2335
   210
    /// The paths can be constructed from any path type by a
deba@2335
   211
    /// template constructor or a template assignment operator.
deba@2335
   212
    /// 
deba@2335
   213
    template <typename _Graph>
deba@2335
   214
    class PathDumper {
deba@2335
   215
    public:
deba@2335
   216
deba@2335
   217
      /// Type of the underlying graph.
deba@2335
   218
      typedef _Graph Graph;
deba@2335
   219
      /// Edge type of the underlying graph.
deba@2335
   220
      typedef typename Graph::Edge Edge;
deba@2335
   221
deba@2335
   222
      /// Length of the path ie. the number of edges in the path.
deba@2335
   223
      int length() const { return 0;}
deba@2335
   224
deba@2335
   225
      /// Returns whether the path is empty.
deba@2335
   226
      bool empty() const { return true;}
deba@2335
   227
deba@2335
   228
      /// \brief Forward or reverse dumping
alpar@2260
   229
      ///
deba@2335
   230
      /// If the RevPathTag is defined and true then reverse dumping
deba@2357
   231
      /// is provided in the path dumper. In this case instead of the
deba@2357
   232
      /// EdgeIt the RevEdgeIt iterator should be implemented in the
deba@2357
   233
      /// dumper.
deba@2335
   234
      typedef False RevPathTag;
deba@2335
   235
deba@2335
   236
      /// \brief Lemon style iterator for path edges
alpar@2260
   237
      ///
deba@2335
   238
      /// This class is used to iterate on the edges of the paths.
deba@2335
   239
      class EdgeIt {
deba@2335
   240
      public:
deba@2335
   241
	/// Default constructor
deba@2335
   242
	EdgeIt() {}
deba@2335
   243
	/// Invalid constructor
deba@2335
   244
	EdgeIt(Invalid) {}
deba@2335
   245
	/// Constructor for first edge
deba@2335
   246
	EdgeIt(const PathDumper&) {}
deba@2335
   247
deba@2335
   248
        /// Conversion to Edge
deba@2335
   249
	operator Edge() const { return INVALID; }
deba@2335
   250
deba@2335
   251
	/// Next edge
deba@2335
   252
	EdgeIt& operator++() {return *this;}
deba@2335
   253
deba@2335
   254
	/// Comparison operator
deba@2335
   255
	bool operator==(const EdgeIt&) const {return true;}
deba@2335
   256
	/// Comparison operator
deba@2335
   257
	bool operator!=(const EdgeIt&) const {return true;}
deba@2335
   258
 	/// Comparison operator
deba@2335
   259
 	bool operator<(const EdgeIt&) const {return false;}
deba@2335
   260
deba@2335
   261
      };
deba@2335
   262
deba@2335
   263
      /// \brief Lemon style iterator for path edges
alpar@2260
   264
      ///
deba@2335
   265
      /// This class is used to iterate on the edges of the paths in
deba@2335
   266
      /// reverse direction.
deba@2357
   267
      class RevEdgeIt {
alpar@2260
   268
      public:
deba@2335
   269
	/// Default constructor
deba@2357
   270
	RevEdgeIt() {}
deba@2335
   271
	/// Invalid constructor
deba@2357
   272
	RevEdgeIt(Invalid) {}
deba@2335
   273
	/// Constructor for first edge
deba@2357
   274
	RevEdgeIt(const PathDumper &) {}
alpar@2260
   275
deba@2335
   276
        /// Conversion to Edge
deba@2335
   277
	operator Edge() const { return INVALID; }
alpar@2260
   278
deba@2335
   279
	/// Next edge
deba@2357
   280
	RevEdgeIt& operator++() {return *this;}
alpar@2260
   281
deba@2335
   282
	/// Comparison operator
deba@2357
   283
	bool operator==(const RevEdgeIt&) const {return true;}
deba@2335
   284
	/// Comparison operator
deba@2357
   285
	bool operator!=(const RevEdgeIt&) const {return true;}
deba@2335
   286
 	/// Comparison operator
deba@2357
   287
 	bool operator<(const RevEdgeIt&) const {return false;}
alpar@2260
   288
alpar@2260
   289
      };
alpar@2260
   290
alpar@2260
   291
      template <typename _Path>
alpar@2260
   292
      struct Constraints {
deba@2335
   293
        void constraints() {
deba@2335
   294
          function_requires<_path_bits::
deba@2335
   295
            PathDumperConstraints<Graph, _Path> >();
deba@2335
   296
        }
alpar@2260
   297
      };
alpar@2260
   298
alpar@2260
   299
    };
alpar@2260
   300
deba@2335
   301
deba@2335
   302
    ///@}
alpar@2260
   303
  }
alpar@2260
   304
alpar@2260
   305
} // namespace lemon
alpar@2260
   306
alpar@2260
   307
#endif // LEMON_CONCEPT_PATH_H