lemon/concepts/path.h
author Alpar Juttner <alpar@cs.elte.hu>
Thu, 14 May 2015 17:13:44 +0200
changeset 1143 f85ee41c84bc
parent 1092 dceba191c00d
child 1198 2236d00ca778
permissions -rw-r--r--
Bugfix in Vf2 - missing initialization (#597)
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@1092
     5
 * Copyright (C) 2003-2013
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
kpeter@785
    21
///\brief The concept of paths
alpar@96
    22
///
alpar@96
    23
deba@529
    24
#ifndef LEMON_CONCEPTS_PATH_H
deba@529
    25
#define LEMON_CONCEPTS_PATH_H
alpar@96
    26
deba@220
    27
#include <lemon/core.h>
alpar@96
    28
#include <lemon/concept_check.h>
ggab90@1130
    29
#include <lemon/bits/stl_iterators.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@785
    42
    /// In a sense, a path can be treated as a list of arcs.
kpeter@785
    43
    /// LEMON path types just store this list. As a consequence, they cannot
kpeter@785
    44
    /// enumerate the nodes on the path directly and a zero length path
kpeter@785
    45
    /// cannot store its source node.
kpeter@785
    46
    ///
kpeter@785
    47
    /// The arcs of a path should be stored in the order of their directions,
kpeter@785
    48
    /// i.e. the target node of each arc should be the same as the source
kpeter@785
    49
    /// node of the next arc. This consistency could be checked using
kpeter@785
    50
    /// \ref checkPath().
kpeter@785
    51
    /// The source and target nodes of a (consistent) path can be obtained
kpeter@785
    52
    /// using \ref pathSource() and \ref pathTarget().
kpeter@785
    53
    ///
kpeter@785
    54
    /// A path can be constructed from another path of any type using the
kpeter@785
    55
    /// copy constructor or the assignment operator.
kpeter@785
    56
    ///
kpeter@559
    57
    /// \tparam GR The digraph type in which the path is.
kpeter@559
    58
    template <typename GR>
alpar@96
    59
    class Path {
alpar@96
    60
    public:
alpar@96
    61
alpar@96
    62
      /// Type of the underlying digraph.
kpeter@559
    63
      typedef GR Digraph;
alpar@96
    64
      /// Arc type of the underlying digraph.
alpar@96
    65
      typedef typename Digraph::Arc Arc;
alpar@96
    66
alpar@96
    67
      class ArcIt;
alpar@96
    68
alpar@96
    69
      /// \brief Default constructor
alpar@96
    70
      Path() {}
alpar@96
    71
kpeter@785
    72
      /// \brief Template copy constructor
alpar@96
    73
      template <typename CPath>
alpar@96
    74
      Path(const CPath& cpath) {}
alpar@96
    75
kpeter@785
    76
      /// \brief Template assigment operator
alpar@96
    77
      template <typename CPath>
kpeter@278
    78
      Path& operator=(const CPath& cpath) {
alpar@1083
    79
        ::lemon::ignore_unused_variable_warning(cpath);
kpeter@278
    80
        return *this;
kpeter@278
    81
      }
alpar@96
    82
kpeter@785
    83
      /// Length of the path, i.e. the number of arcs on the path.
alpar@96
    84
      int length() const { return 0;}
alpar@96
    85
alpar@96
    86
      /// Returns whether the path is empty.
alpar@96
    87
      bool empty() const { return true;}
alpar@96
    88
alpar@96
    89
      /// Resets the path to an empty path.
alpar@96
    90
      void clear() {}
alpar@96
    91
kpeter@785
    92
      /// \brief LEMON style iterator for enumerating the arcs of a path.
alpar@96
    93
      ///
kpeter@785
    94
      /// LEMON style iterator class for enumerating the arcs of a path.
alpar@96
    95
      class ArcIt {
alpar@96
    96
      public:
alpar@209
    97
        /// Default constructor
alpar@209
    98
        ArcIt() {}
alpar@209
    99
        /// Invalid constructor
alpar@209
   100
        ArcIt(Invalid) {}
kpeter@785
   101
        /// Sets the iterator to the first arc of the given path
alpar@209
   102
        ArcIt(const Path &) {}
alpar@96
   103
kpeter@785
   104
        /// Conversion to \c Arc
alpar@209
   105
        operator Arc() const { return INVALID; }
alpar@96
   106
alpar@209
   107
        /// Next arc
alpar@209
   108
        ArcIt& operator++() {return *this;}
alpar@96
   109
alpar@209
   110
        /// Comparison operator
alpar@209
   111
        bool operator==(const ArcIt&) const {return true;}
alpar@209
   112
        /// Comparison operator
alpar@209
   113
        bool operator!=(const ArcIt&) const {return true;}
kpeter@212
   114
        /// Comparison operator
kpeter@212
   115
        bool operator<(const ArcIt&) const {return false;}
alpar@96
   116
alpar@96
   117
      };
alpar@96
   118
ggab90@1130
   119
      /// \brief Gets the collection of the arcs of the path.
ggab90@1130
   120
      ///
ggab90@1130
   121
      /// This function can be used for iterating on the
ggab90@1130
   122
      /// arcs of the path. It returns a wrapped
ggab90@1130
   123
      /// ArcIt, which looks like an STL container
ggab90@1130
   124
      /// (by having begin() and end()) which you can use in range-based
ggab90@1130
   125
      /// for loops, STL algorithms, etc.
ggab90@1130
   126
      /// For example you can write:
ggab90@1130
   127
      ///\code
ggab90@1130
   128
      /// for(auto a: p.arcs())
ggab90@1130
   129
      ///   doSomething(a);
ggab90@1130
   130
      ///\endcode
ggab90@1130
   131
      LemonRangeWrapper1<ArcIt, Path> arcs() const {
ggab90@1130
   132
        return LemonRangeWrapper1<ArcIt, Path>(*this);
ggab90@1130
   133
      }
ggab90@1130
   134
ggab90@1130
   135
alpar@96
   136
      template <typename _Path>
alpar@96
   137
      struct Constraints {
alpar@96
   138
        void constraints() {
alpar@96
   139
          Path<Digraph> pc;
alpar@96
   140
          _Path p, pp(pc);
alpar@96
   141
          int l = p.length();
alpar@96
   142
          int e = p.empty();
alpar@96
   143
          p.clear();
alpar@96
   144
alpar@96
   145
          p = pc;
alpar@96
   146
alpar@96
   147
          typename _Path::ArcIt id, ii(INVALID), i(p);
alpar@96
   148
alpar@96
   149
          ++i;
alpar@96
   150
          typename Digraph::Arc ed = i;
alpar@96
   151
alpar@96
   152
          e = (i == ii);
alpar@96
   153
          e = (i != ii);
alpar@96
   154
          e = (i < ii);
alpar@96
   155
alpar@1083
   156
          ::lemon::ignore_unused_variable_warning(l);
alpar@1083
   157
          ::lemon::ignore_unused_variable_warning(pp);
alpar@1083
   158
          ::lemon::ignore_unused_variable_warning(e);
alpar@1083
   159
          ::lemon::ignore_unused_variable_warning(id);
alpar@1083
   160
          ::lemon::ignore_unused_variable_warning(ii);
alpar@1083
   161
          ::lemon::ignore_unused_variable_warning(ed);
alpar@96
   162
        }
alpar@96
   163
      };
alpar@96
   164
alpar@96
   165
    };
alpar@96
   166
alpar@96
   167
    namespace _path_bits {
alpar@209
   168
alpar@96
   169
      template <typename _Digraph, typename _Path, typename RevPathTag = void>
alpar@96
   170
      struct PathDumperConstraints {
alpar@96
   171
        void constraints() {
alpar@96
   172
          int l = p.length();
alpar@96
   173
          int e = p.empty();
alpar@96
   174
alpar@96
   175
          typename _Path::ArcIt id, i(p);
alpar@96
   176
alpar@96
   177
          ++i;
alpar@96
   178
          typename _Digraph::Arc ed = i;
alpar@96
   179
alpar@96
   180
          e = (i == INVALID);
alpar@96
   181
          e = (i != INVALID);
alpar@96
   182
alpar@1083
   183
          ::lemon::ignore_unused_variable_warning(l);
alpar@1083
   184
          ::lemon::ignore_unused_variable_warning(e);
alpar@1083
   185
          ::lemon::ignore_unused_variable_warning(id);
alpar@1083
   186
          ::lemon::ignore_unused_variable_warning(ed);
alpar@96
   187
        }
alpar@96
   188
        _Path& p;
alpar@975
   189
        PathDumperConstraints() {}
alpar@96
   190
      };
alpar@96
   191
alpar@96
   192
      template <typename _Digraph, typename _Path>
alpar@96
   193
      struct PathDumperConstraints<
alpar@209
   194
        _Digraph, _Path,
alpar@96
   195
        typename enable_if<typename _Path::RevPathTag, void>::type
alpar@96
   196
      > {
alpar@96
   197
        void constraints() {
alpar@96
   198
          int l = p.length();
alpar@96
   199
          int e = p.empty();
alpar@96
   200
alpar@96
   201
          typename _Path::RevArcIt id, i(p);
alpar@96
   202
alpar@96
   203
          ++i;
alpar@96
   204
          typename _Digraph::Arc ed = i;
alpar@96
   205
alpar@96
   206
          e = (i == INVALID);
alpar@96
   207
          e = (i != INVALID);
alpar@96
   208
alpar@1083
   209
          ::lemon::ignore_unused_variable_warning(l);
alpar@1083
   210
          ::lemon::ignore_unused_variable_warning(e);
alpar@1083
   211
          ::lemon::ignore_unused_variable_warning(id);
alpar@1083
   212
          ::lemon::ignore_unused_variable_warning(ed);
alpar@96
   213
        }
alpar@96
   214
        _Path& p;
alpar@975
   215
        PathDumperConstraints() {}
alpar@96
   216
      };
alpar@209
   217
alpar@96
   218
    }
alpar@96
   219
alpar@96
   220
alpar@96
   221
    /// \brief A skeleton structure for path dumpers.
alpar@96
   222
    ///
alpar@96
   223
    /// A skeleton structure for path dumpers. The path dumpers are
kpeter@785
   224
    /// the generalization of the paths, they can enumerate the arcs
kpeter@785
   225
    /// of the path either in forward or in backward order.
kpeter@785
   226
    /// These classes are typically not used directly, they are rather
kpeter@785
   227
    /// used to be assigned to a real path type.
alpar@96
   228
    ///
alpar@96
   229
    /// The main purpose of this concept is that the shortest path
kpeter@785
   230
    /// algorithms can enumerate the arcs easily in reverse order.
kpeter@785
   231
    /// In LEMON, such algorithms give back a (reverse) path dumper that
kpeter@785
   232
    /// can be assigned to a real path. The dumpers can be implemented as
alpar@96
   233
    /// an adaptor class to the predecessor map.
kpeter@559
   234
    ///
kpeter@559
   235
    /// \tparam GR The digraph type in which the path is.
kpeter@559
   236
    template <typename GR>
alpar@96
   237
    class PathDumper {
alpar@96
   238
    public:
alpar@96
   239
alpar@96
   240
      /// Type of the underlying digraph.
kpeter@559
   241
      typedef GR Digraph;
alpar@96
   242
      /// Arc type of the underlying digraph.
alpar@96
   243
      typedef typename Digraph::Arc Arc;
alpar@96
   244
kpeter@785
   245
      /// Length of the path, i.e. the number of arcs on the path.
alpar@96
   246
      int length() const { return 0;}
alpar@96
   247
alpar@96
   248
      /// Returns whether the path is empty.
alpar@96
   249
      bool empty() const { return true;}
alpar@96
   250
alpar@96
   251
      /// \brief Forward or reverse dumping
alpar@96
   252
      ///
kpeter@785
   253
      /// If this tag is defined to be \c True, then reverse dumping
kpeter@785
   254
      /// is provided in the path dumper. In this case, \c RevArcIt
kpeter@785
   255
      /// iterator should be implemented instead of \c ArcIt iterator.
alpar@96
   256
      typedef False RevPathTag;
alpar@96
   257
kpeter@785
   258
      /// \brief LEMON style iterator for enumerating the arcs of a path.
alpar@96
   259
      ///
kpeter@785
   260
      /// LEMON style iterator class for enumerating the arcs of a path.
alpar@96
   261
      class ArcIt {
alpar@96
   262
      public:
alpar@209
   263
        /// Default constructor
alpar@209
   264
        ArcIt() {}
alpar@209
   265
        /// Invalid constructor
alpar@209
   266
        ArcIt(Invalid) {}
kpeter@785
   267
        /// Sets the iterator to the first arc of the given path
alpar@209
   268
        ArcIt(const PathDumper&) {}
alpar@96
   269
kpeter@785
   270
        /// Conversion to \c Arc
alpar@209
   271
        operator Arc() const { return INVALID; }
alpar@96
   272
alpar@209
   273
        /// Next arc
alpar@209
   274
        ArcIt& operator++() {return *this;}
alpar@96
   275
alpar@209
   276
        /// Comparison operator
alpar@209
   277
        bool operator==(const ArcIt&) const {return true;}
alpar@209
   278
        /// Comparison operator
alpar@209
   279
        bool operator!=(const ArcIt&) const {return true;}
kpeter@212
   280
        /// Comparison operator
kpeter@212
   281
        bool operator<(const ArcIt&) const {return false;}
alpar@96
   282
alpar@96
   283
      };
alpar@96
   284
ggab90@1130
   285
      /// \brief Gets the collection of the arcs of the path.
ggab90@1130
   286
      ///
ggab90@1130
   287
      /// This function can be used for iterating on the
ggab90@1130
   288
      /// arcs of the path. It returns a wrapped
ggab90@1130
   289
      /// ArcIt, which looks like an STL container
ggab90@1130
   290
      /// (by having begin() and end()) which you can use in range-based
ggab90@1130
   291
      /// for loops, STL algorithms, etc.
ggab90@1130
   292
      /// For example you can write:
ggab90@1130
   293
      ///\code
ggab90@1130
   294
      /// for(auto a: p.arcs())
ggab90@1130
   295
      ///   doSomething(a);
ggab90@1130
   296
      ///\endcode
ggab90@1130
   297
      LemonRangeWrapper1<ArcIt, PathDumper> arcs() const {
ggab90@1130
   298
        return LemonRangeWrapper1<ArcIt, PathDumper>(*this);
ggab90@1130
   299
      }
ggab90@1130
   300
ggab90@1130
   301
kpeter@785
   302
      /// \brief LEMON style iterator for enumerating the arcs of a path
kpeter@785
   303
      /// in reverse direction.
alpar@96
   304
      ///
kpeter@785
   305
      /// LEMON style iterator class for enumerating the arcs of a path
kpeter@785
   306
      /// in reverse direction.
alpar@96
   307
      class RevArcIt {
alpar@96
   308
      public:
alpar@209
   309
        /// Default constructor
alpar@209
   310
        RevArcIt() {}
alpar@209
   311
        /// Invalid constructor
alpar@209
   312
        RevArcIt(Invalid) {}
kpeter@785
   313
        /// Sets the iterator to the last arc of the given path
alpar@209
   314
        RevArcIt(const PathDumper &) {}
alpar@96
   315
kpeter@785
   316
        /// Conversion to \c Arc
alpar@209
   317
        operator Arc() const { return INVALID; }
alpar@96
   318
alpar@209
   319
        /// Next arc
alpar@209
   320
        RevArcIt& operator++() {return *this;}
alpar@96
   321
alpar@209
   322
        /// Comparison operator
alpar@209
   323
        bool operator==(const RevArcIt&) const {return true;}
alpar@209
   324
        /// Comparison operator
alpar@209
   325
        bool operator!=(const RevArcIt&) const {return true;}
kpeter@212
   326
        /// Comparison operator
kpeter@212
   327
        bool operator<(const RevArcIt&) const {return false;}
alpar@96
   328
alpar@96
   329
      };
alpar@96
   330
ggab90@1130
   331
      /// \brief Gets the collection of the arcs of the path
ggab90@1130
   332
      /// in reverse direction.
ggab90@1130
   333
      ///
ggab90@1130
   334
      /// This function can be used for iterating on the
ggab90@1130
   335
      /// arcs of the path in reverse direction. It returns a wrapped
ggab90@1130
   336
      /// RevArcIt, which looks like an STL container
ggab90@1130
   337
      /// (by having begin() and end()) which you can use in range-based
ggab90@1130
   338
      /// for loops, STL algorithms, etc.
ggab90@1130
   339
      /// For example you can write:
ggab90@1130
   340
      ///\code
ggab90@1130
   341
      /// for(auto a: p.revArcs())
ggab90@1130
   342
      ///   doSomething(a);
ggab90@1130
   343
      ///\endcode
ggab90@1130
   344
      LemonRangeWrapper1<RevArcIt, PathDumper> revArcs() const {
ggab90@1130
   345
        return LemonRangeWrapper1<RevArcIt, PathDumper>(*this);
ggab90@1130
   346
      }
ggab90@1130
   347
ggab90@1130
   348
alpar@96
   349
      template <typename _Path>
alpar@96
   350
      struct Constraints {
alpar@96
   351
        void constraints() {
alpar@96
   352
          function_requires<_path_bits::
alpar@96
   353
            PathDumperConstraints<Digraph, _Path> >();
alpar@96
   354
        }
alpar@96
   355
      };
alpar@96
   356
alpar@96
   357
    };
alpar@96
   358
alpar@96
   359
alpar@96
   360
    ///@}
alpar@96
   361
  }
alpar@96
   362
alpar@96
   363
} // namespace lemon
alpar@96
   364
deba@529
   365
#endif