lemon/concepts/path.h
author Alpar Juttner <alpar@cs.elte.hu>
Thu, 08 Nov 2012 09:07:41 +0100
changeset 1006 764826c6e2b4
parent 785 9ae88e7c04a7
parent 975 b873350e6258
child 1084 8b2d4e5d96e4
permissions -rw-r--r--
Merge bugfix #440
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@440
     5
 * Copyright (C) 2003-2009
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>
alpar@96
    29
alpar@96
    30
namespace lemon {
alpar@96
    31
  namespace concepts {
alpar@96
    32
alpar@96
    33
    /// \addtogroup concept
alpar@96
    34
    /// @{
alpar@96
    35
alpar@96
    36
    /// \brief A skeleton structure for representing directed paths in
alpar@96
    37
    /// a digraph.
alpar@96
    38
    ///
alpar@96
    39
    /// A skeleton structure for representing directed paths in a
alpar@209
    40
    /// digraph.
kpeter@785
    41
    /// In a sense, a path can be treated as a list of arcs.
kpeter@785
    42
    /// LEMON path types just store this list. As a consequence, they cannot
kpeter@785
    43
    /// enumerate the nodes on the path directly and a zero length path
kpeter@785
    44
    /// cannot store its source node.
kpeter@785
    45
    ///
kpeter@785
    46
    /// The arcs of a path should be stored in the order of their directions,
kpeter@785
    47
    /// i.e. the target node of each arc should be the same as the source
kpeter@785
    48
    /// node of the next arc. This consistency could be checked using
kpeter@785
    49
    /// \ref checkPath().
kpeter@785
    50
    /// The source and target nodes of a (consistent) path can be obtained
kpeter@785
    51
    /// using \ref pathSource() and \ref pathTarget().
kpeter@785
    52
    ///
kpeter@785
    53
    /// A path can be constructed from another path of any type using the
kpeter@785
    54
    /// copy constructor or the assignment operator.
kpeter@785
    55
    ///
kpeter@559
    56
    /// \tparam GR The digraph type in which the path is.
kpeter@559
    57
    template <typename GR>
alpar@96
    58
    class Path {
alpar@96
    59
    public:
alpar@96
    60
alpar@96
    61
      /// Type of the underlying digraph.
kpeter@559
    62
      typedef GR Digraph;
alpar@96
    63
      /// Arc type of the underlying digraph.
alpar@96
    64
      typedef typename Digraph::Arc Arc;
alpar@96
    65
alpar@96
    66
      class ArcIt;
alpar@96
    67
alpar@96
    68
      /// \brief Default constructor
alpar@96
    69
      Path() {}
alpar@96
    70
kpeter@785
    71
      /// \brief Template copy constructor
alpar@96
    72
      template <typename CPath>
alpar@96
    73
      Path(const CPath& cpath) {}
alpar@96
    74
kpeter@785
    75
      /// \brief Template assigment operator
alpar@96
    76
      template <typename CPath>
kpeter@278
    77
      Path& operator=(const CPath& cpath) {
kpeter@278
    78
        ignore_unused_variable_warning(cpath);
kpeter@278
    79
        return *this;
kpeter@278
    80
      }
alpar@96
    81
kpeter@785
    82
      /// Length of the path, i.e. the number of arcs on the path.
alpar@96
    83
      int length() const { return 0;}
alpar@96
    84
alpar@96
    85
      /// Returns whether the path is empty.
alpar@96
    86
      bool empty() const { return true;}
alpar@96
    87
alpar@96
    88
      /// Resets the path to an empty path.
alpar@96
    89
      void clear() {}
alpar@96
    90
kpeter@785
    91
      /// \brief LEMON style iterator for enumerating the arcs of a path.
alpar@96
    92
      ///
kpeter@785
    93
      /// LEMON style iterator class for enumerating the arcs of a path.
alpar@96
    94
      class ArcIt {
alpar@96
    95
      public:
alpar@209
    96
        /// Default constructor
alpar@209
    97
        ArcIt() {}
alpar@209
    98
        /// Invalid constructor
alpar@209
    99
        ArcIt(Invalid) {}
kpeter@785
   100
        /// Sets the iterator to the first arc of the given path
alpar@209
   101
        ArcIt(const Path &) {}
alpar@96
   102
kpeter@785
   103
        /// Conversion to \c Arc
alpar@209
   104
        operator Arc() const { return INVALID; }
alpar@96
   105
alpar@209
   106
        /// Next arc
alpar@209
   107
        ArcIt& operator++() {return *this;}
alpar@96
   108
alpar@209
   109
        /// Comparison operator
alpar@209
   110
        bool operator==(const ArcIt&) const {return true;}
alpar@209
   111
        /// Comparison operator
alpar@209
   112
        bool operator!=(const ArcIt&) const {return true;}
kpeter@212
   113
        /// Comparison operator
kpeter@212
   114
        bool operator<(const ArcIt&) const {return false;}
alpar@96
   115
alpar@96
   116
      };
alpar@96
   117
alpar@96
   118
      template <typename _Path>
alpar@96
   119
      struct Constraints {
alpar@96
   120
        void constraints() {
alpar@96
   121
          Path<Digraph> pc;
alpar@96
   122
          _Path p, pp(pc);
alpar@96
   123
          int l = p.length();
alpar@96
   124
          int e = p.empty();
alpar@96
   125
          p.clear();
alpar@96
   126
alpar@96
   127
          p = pc;
alpar@96
   128
alpar@96
   129
          typename _Path::ArcIt id, ii(INVALID), i(p);
alpar@96
   130
alpar@96
   131
          ++i;
alpar@96
   132
          typename Digraph::Arc ed = i;
alpar@96
   133
alpar@96
   134
          e = (i == ii);
alpar@96
   135
          e = (i != ii);
alpar@96
   136
          e = (i < ii);
alpar@96
   137
alpar@96
   138
          ignore_unused_variable_warning(l);
alpar@96
   139
          ignore_unused_variable_warning(pp);
alpar@96
   140
          ignore_unused_variable_warning(e);
alpar@96
   141
          ignore_unused_variable_warning(id);
alpar@96
   142
          ignore_unused_variable_warning(ii);
alpar@96
   143
          ignore_unused_variable_warning(ed);
alpar@96
   144
        }
alpar@96
   145
      };
alpar@96
   146
alpar@96
   147
    };
alpar@96
   148
alpar@96
   149
    namespace _path_bits {
alpar@209
   150
alpar@96
   151
      template <typename _Digraph, typename _Path, typename RevPathTag = void>
alpar@96
   152
      struct PathDumperConstraints {
alpar@96
   153
        void constraints() {
alpar@96
   154
          int l = p.length();
alpar@96
   155
          int e = p.empty();
alpar@96
   156
alpar@96
   157
          typename _Path::ArcIt id, i(p);
alpar@96
   158
alpar@96
   159
          ++i;
alpar@96
   160
          typename _Digraph::Arc ed = i;
alpar@96
   161
alpar@96
   162
          e = (i == INVALID);
alpar@96
   163
          e = (i != INVALID);
alpar@96
   164
alpar@96
   165
          ignore_unused_variable_warning(l);
alpar@96
   166
          ignore_unused_variable_warning(e);
alpar@96
   167
          ignore_unused_variable_warning(id);
alpar@96
   168
          ignore_unused_variable_warning(ed);
alpar@96
   169
        }
alpar@96
   170
        _Path& p;
alpar@975
   171
        PathDumperConstraints() {}
alpar@96
   172
      };
alpar@96
   173
alpar@96
   174
      template <typename _Digraph, typename _Path>
alpar@96
   175
      struct PathDumperConstraints<
alpar@209
   176
        _Digraph, _Path,
alpar@96
   177
        typename enable_if<typename _Path::RevPathTag, void>::type
alpar@96
   178
      > {
alpar@96
   179
        void constraints() {
alpar@96
   180
          int l = p.length();
alpar@96
   181
          int e = p.empty();
alpar@96
   182
alpar@96
   183
          typename _Path::RevArcIt id, i(p);
alpar@96
   184
alpar@96
   185
          ++i;
alpar@96
   186
          typename _Digraph::Arc ed = i;
alpar@96
   187
alpar@96
   188
          e = (i == INVALID);
alpar@96
   189
          e = (i != INVALID);
alpar@96
   190
alpar@96
   191
          ignore_unused_variable_warning(l);
alpar@96
   192
          ignore_unused_variable_warning(e);
alpar@96
   193
          ignore_unused_variable_warning(id);
alpar@96
   194
          ignore_unused_variable_warning(ed);
alpar@96
   195
        }
alpar@96
   196
        _Path& p;
alpar@975
   197
        PathDumperConstraints() {}
alpar@96
   198
      };
alpar@209
   199
alpar@96
   200
    }
alpar@96
   201
alpar@96
   202
alpar@96
   203
    /// \brief A skeleton structure for path dumpers.
alpar@96
   204
    ///
alpar@96
   205
    /// A skeleton structure for path dumpers. The path dumpers are
kpeter@785
   206
    /// the generalization of the paths, they can enumerate the arcs
kpeter@785
   207
    /// of the path either in forward or in backward order.
kpeter@785
   208
    /// These classes are typically not used directly, they are rather
kpeter@785
   209
    /// used to be assigned to a real path type.
alpar@96
   210
    ///
alpar@96
   211
    /// The main purpose of this concept is that the shortest path
kpeter@785
   212
    /// algorithms can enumerate the arcs easily in reverse order.
kpeter@785
   213
    /// In LEMON, such algorithms give back a (reverse) path dumper that
kpeter@785
   214
    /// can be assigned to a real path. The dumpers can be implemented as
alpar@96
   215
    /// an adaptor class to the predecessor map.
kpeter@559
   216
    ///
kpeter@559
   217
    /// \tparam GR The digraph type in which the path is.
kpeter@559
   218
    template <typename GR>
alpar@96
   219
    class PathDumper {
alpar@96
   220
    public:
alpar@96
   221
alpar@96
   222
      /// Type of the underlying digraph.
kpeter@559
   223
      typedef GR Digraph;
alpar@96
   224
      /// Arc type of the underlying digraph.
alpar@96
   225
      typedef typename Digraph::Arc Arc;
alpar@96
   226
kpeter@785
   227
      /// Length of the path, i.e. the number of arcs on the path.
alpar@96
   228
      int length() const { return 0;}
alpar@96
   229
alpar@96
   230
      /// Returns whether the path is empty.
alpar@96
   231
      bool empty() const { return true;}
alpar@96
   232
alpar@96
   233
      /// \brief Forward or reverse dumping
alpar@96
   234
      ///
kpeter@785
   235
      /// If this tag is defined to be \c True, then reverse dumping
kpeter@785
   236
      /// is provided in the path dumper. In this case, \c RevArcIt
kpeter@785
   237
      /// iterator should be implemented instead of \c ArcIt iterator.
alpar@96
   238
      typedef False RevPathTag;
alpar@96
   239
kpeter@785
   240
      /// \brief LEMON style iterator for enumerating the arcs of a path.
alpar@96
   241
      ///
kpeter@785
   242
      /// LEMON style iterator class for enumerating the arcs of a path.
alpar@96
   243
      class ArcIt {
alpar@96
   244
      public:
alpar@209
   245
        /// Default constructor
alpar@209
   246
        ArcIt() {}
alpar@209
   247
        /// Invalid constructor
alpar@209
   248
        ArcIt(Invalid) {}
kpeter@785
   249
        /// Sets the iterator to the first arc of the given path
alpar@209
   250
        ArcIt(const PathDumper&) {}
alpar@96
   251
kpeter@785
   252
        /// Conversion to \c Arc
alpar@209
   253
        operator Arc() const { return INVALID; }
alpar@96
   254
alpar@209
   255
        /// Next arc
alpar@209
   256
        ArcIt& operator++() {return *this;}
alpar@96
   257
alpar@209
   258
        /// Comparison operator
alpar@209
   259
        bool operator==(const ArcIt&) const {return true;}
alpar@209
   260
        /// Comparison operator
alpar@209
   261
        bool operator!=(const ArcIt&) const {return true;}
kpeter@212
   262
        /// Comparison operator
kpeter@212
   263
        bool operator<(const ArcIt&) const {return false;}
alpar@96
   264
alpar@96
   265
      };
alpar@96
   266
kpeter@785
   267
      /// \brief LEMON style iterator for enumerating the arcs of a path
kpeter@785
   268
      /// in reverse direction.
alpar@96
   269
      ///
kpeter@785
   270
      /// LEMON style iterator class for enumerating the arcs of a path
kpeter@785
   271
      /// in reverse direction.
alpar@96
   272
      class RevArcIt {
alpar@96
   273
      public:
alpar@209
   274
        /// Default constructor
alpar@209
   275
        RevArcIt() {}
alpar@209
   276
        /// Invalid constructor
alpar@209
   277
        RevArcIt(Invalid) {}
kpeter@785
   278
        /// Sets the iterator to the last arc of the given path
alpar@209
   279
        RevArcIt(const PathDumper &) {}
alpar@96
   280
kpeter@785
   281
        /// Conversion to \c Arc
alpar@209
   282
        operator Arc() const { return INVALID; }
alpar@96
   283
alpar@209
   284
        /// Next arc
alpar@209
   285
        RevArcIt& operator++() {return *this;}
alpar@96
   286
alpar@209
   287
        /// Comparison operator
alpar@209
   288
        bool operator==(const RevArcIt&) const {return true;}
alpar@209
   289
        /// Comparison operator
alpar@209
   290
        bool operator!=(const RevArcIt&) const {return true;}
kpeter@212
   291
        /// Comparison operator
kpeter@212
   292
        bool operator<(const RevArcIt&) const {return false;}
alpar@96
   293
alpar@96
   294
      };
alpar@96
   295
alpar@96
   296
      template <typename _Path>
alpar@96
   297
      struct Constraints {
alpar@96
   298
        void constraints() {
alpar@96
   299
          function_requires<_path_bits::
alpar@96
   300
            PathDumperConstraints<Digraph, _Path> >();
alpar@96
   301
        }
alpar@96
   302
      };
alpar@96
   303
alpar@96
   304
    };
alpar@96
   305
alpar@96
   306
alpar@96
   307
    ///@}
alpar@96
   308
  }
alpar@96
   309
alpar@96
   310
} // namespace lemon
alpar@96
   311
deba@529
   312
#endif