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