lemon/path.h
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 15 May 2015 10:16:48 +0200
changeset 1145 1de908281369
parent 1092 dceba191c00d
child 1201 1f4f01870c1e
permissions -rw-r--r--
Update Doxyfile.in

- Remove obsolete (as of Doxygen version 1.8.9) config parameters
- Switch SHORT_NAMES off
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 paths
alpar@96
    20
///\file
alpar@96
    21
///\brief Classes for representing paths in digraphs.
alpar@96
    22
///
alpar@96
    23
alpar@96
    24
#ifndef LEMON_PATH_H
alpar@96
    25
#define LEMON_PATH_H
alpar@96
    26
alpar@96
    27
#include <vector>
alpar@96
    28
#include <algorithm>
alpar@96
    29
alpar@96
    30
#include <lemon/error.h>
deba@220
    31
#include <lemon/core.h>
alpar@100
    32
#include <lemon/concepts/path.h>
ggab90@1130
    33
#include <lemon/bits/stl_iterators.h>
alpar@96
    34
alpar@96
    35
namespace lemon {
alpar@96
    36
alpar@96
    37
  /// \addtogroup paths
alpar@96
    38
  /// @{
alpar@96
    39
alpar@96
    40
alpar@96
    41
  /// \brief A structure for representing directed paths in a digraph.
alpar@96
    42
  ///
alpar@96
    43
  /// A structure for representing directed path in a digraph.
kpeter@559
    44
  /// \tparam GR The digraph type in which the path is.
alpar@96
    45
  ///
alpar@96
    46
  /// In a sense, the path can be treated as a list of arcs. The
kpeter@920
    47
  /// LEMON path type stores just this list. As a consequence, it
alpar@97
    48
  /// cannot enumerate the nodes of the path and the source node of
alpar@97
    49
  /// a zero length path is undefined.
alpar@96
    50
  ///
alpar@96
    51
  /// This implementation is a back and front insertable and erasable
alpar@96
    52
  /// path type. It can be indexed in O(1) time. The front and back
alpar@97
    53
  /// insertion and erase is done in O(1) (amortized) time. The
alpar@97
    54
  /// implementation uses two vectors for storing the front and back
alpar@97
    55
  /// insertions.
kpeter@559
    56
  template <typename GR>
alpar@96
    57
  class Path {
alpar@96
    58
  public:
alpar@96
    59
kpeter@559
    60
    typedef GR Digraph;
alpar@96
    61
    typedef typename Digraph::Arc Arc;
alpar@96
    62
alpar@96
    63
    /// \brief Default constructor
alpar@96
    64
    ///
alpar@96
    65
    /// Default constructor
alpar@96
    66
    Path() {}
alpar@96
    67
alpar@990
    68
    /// \brief Copy constructor
alpar@990
    69
    ///
alpar@990
    70
    Path(const Path& cpath) {
alpar@990
    71
      pathCopy(cpath, *this);
alpar@990
    72
    }
alpar@990
    73
alpar@96
    74
    /// \brief Template copy constructor
alpar@96
    75
    ///
alpar@97
    76
    /// This constuctor initializes the path from any other path type.
alpar@97
    77
    /// It simply makes a copy of the given path.
alpar@96
    78
    template <typename CPath>
alpar@96
    79
    Path(const CPath& cpath) {
kpeter@516
    80
      pathCopy(cpath, *this);
alpar@96
    81
    }
alpar@96
    82
alpar@990
    83
    /// \brief Copy assignment
alpar@990
    84
    ///
alpar@990
    85
    Path& operator=(const Path& cpath) {
alpar@990
    86
      pathCopy(cpath, *this);
alpar@990
    87
      return *this;
alpar@990
    88
    }
alpar@990
    89
alpar@96
    90
    /// \brief Template copy assignment
alpar@96
    91
    ///
alpar@97
    92
    /// This operator makes a copy of a path of any other type.
alpar@96
    93
    template <typename CPath>
alpar@96
    94
    Path& operator=(const CPath& cpath) {
kpeter@516
    95
      pathCopy(cpath, *this);
alpar@96
    96
      return *this;
alpar@96
    97
    }
alpar@96
    98
ladanyi@236
    99
    /// \brief LEMON style iterator for path arcs
alpar@96
   100
    ///
alpar@96
   101
    /// This class is used to iterate on the arcs of the paths.
alpar@96
   102
    class ArcIt {
alpar@96
   103
      friend class Path;
alpar@96
   104
    public:
alpar@96
   105
      /// \brief Default constructor
alpar@96
   106
      ArcIt() {}
alpar@96
   107
      /// \brief Invalid constructor
alpar@96
   108
      ArcIt(Invalid) : path(0), idx(-1) {}
alpar@97
   109
      /// \brief Initializate the iterator to the first arc of path
alpar@209
   110
      ArcIt(const Path &_path)
alpar@96
   111
        : path(&_path), idx(_path.empty() ? -1 : 0) {}
alpar@96
   112
alpar@96
   113
    private:
alpar@96
   114
alpar@209
   115
      ArcIt(const Path &_path, int _idx)
alpar@96
   116
        : path(&_path), idx(_idx) {}
alpar@96
   117
alpar@96
   118
    public:
alpar@96
   119
alpar@96
   120
      /// \brief Conversion to Arc
alpar@96
   121
      operator const Arc&() const {
alpar@96
   122
        return path->nth(idx);
alpar@96
   123
      }
alpar@96
   124
alpar@96
   125
      /// \brief Next arc
alpar@209
   126
      ArcIt& operator++() {
alpar@96
   127
        ++idx;
alpar@209
   128
        if (idx >= path->length()) idx = -1;
alpar@209
   129
        return *this;
alpar@96
   130
      }
alpar@96
   131
alpar@96
   132
      /// \brief Comparison operator
alpar@96
   133
      bool operator==(const ArcIt& e) const { return idx==e.idx; }
alpar@96
   134
      /// \brief Comparison operator
alpar@96
   135
      bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
alpar@96
   136
      /// \brief Comparison operator
alpar@96
   137
      bool operator<(const ArcIt& e) const { return idx<e.idx; }
alpar@96
   138
alpar@96
   139
    private:
alpar@96
   140
      const Path *path;
alpar@96
   141
      int idx;
alpar@96
   142
    };
alpar@96
   143
ggab90@1130
   144
    /// \brief Gets the collection of the arcs of the path.
ggab90@1130
   145
    ///
ggab90@1130
   146
    /// This function can be used for iterating on the
ggab90@1130
   147
    /// arcs of the path. It returns a wrapped
ggab90@1130
   148
    /// ArcIt, which looks like an STL container
ggab90@1130
   149
    /// (by having begin() and end()) which you can use in range-based
ggab90@1130
   150
    /// for loops, STL algorithms, etc.
ggab90@1130
   151
    /// For example you can write:
ggab90@1130
   152
    ///\code
ggab90@1130
   153
    /// for(auto a: p.arcs())
ggab90@1130
   154
    ///   doSomething(a);
ggab90@1130
   155
    ///\endcode
ggab90@1130
   156
    LemonRangeWrapper1<ArcIt, Path> arcs() const {
ggab90@1130
   157
      return LemonRangeWrapper1<ArcIt, Path>(*this);
ggab90@1130
   158
    }
ggab90@1130
   159
ggab90@1130
   160
alpar@96
   161
    /// \brief Length of the path.
alpar@96
   162
    int length() const { return head.size() + tail.size(); }
alpar@97
   163
    /// \brief Return whether the path is empty.
alpar@96
   164
    bool empty() const { return head.empty() && tail.empty(); }
alpar@96
   165
alpar@97
   166
    /// \brief Reset the path to an empty one.
alpar@96
   167
    void clear() { head.clear(); tail.clear(); }
alpar@96
   168
kpeter@920
   169
    /// \brief The n-th arc.
alpar@96
   170
    ///
kpeter@559
   171
    /// \pre \c n is in the <tt>[0..length() - 1]</tt> range.
alpar@96
   172
    const Arc& nth(int n) const {
alpar@96
   173
      return n < int(head.size()) ? *(head.rbegin() + n) :
alpar@96
   174
        *(tail.begin() + (n - head.size()));
alpar@96
   175
    }
alpar@96
   176
kpeter@920
   177
    /// \brief Initialize arc iterator to point to the n-th arc
alpar@96
   178
    ///
kpeter@559
   179
    /// \pre \c n is in the <tt>[0..length() - 1]</tt> range.
alpar@96
   180
    ArcIt nthIt(int n) const {
alpar@96
   181
      return ArcIt(*this, n);
alpar@96
   182
    }
alpar@96
   183
alpar@97
   184
    /// \brief The first arc of the path
alpar@96
   185
    const Arc& front() const {
alpar@96
   186
      return head.empty() ? tail.front() : head.back();
alpar@96
   187
    }
alpar@96
   188
alpar@96
   189
    /// \brief Add a new arc before the current path
alpar@96
   190
    void addFront(const Arc& arc) {
alpar@96
   191
      head.push_back(arc);
alpar@96
   192
    }
alpar@96
   193
alpar@96
   194
    /// \brief Erase the first arc of the path
alpar@96
   195
    void eraseFront() {
alpar@96
   196
      if (!head.empty()) {
alpar@96
   197
        head.pop_back();
alpar@96
   198
      } else {
alpar@96
   199
        head.clear();
alpar@96
   200
        int halfsize = tail.size() / 2;
alpar@96
   201
        head.resize(halfsize);
alpar@96
   202
        std::copy(tail.begin() + 1, tail.begin() + halfsize + 1,
alpar@96
   203
                  head.rbegin());
alpar@96
   204
        std::copy(tail.begin() + halfsize + 1, tail.end(), tail.begin());
alpar@96
   205
        tail.resize(tail.size() - halfsize - 1);
alpar@96
   206
      }
alpar@96
   207
    }
alpar@96
   208
alpar@97
   209
    /// \brief The last arc of the path
alpar@96
   210
    const Arc& back() const {
alpar@96
   211
      return tail.empty() ? head.front() : tail.back();
alpar@96
   212
    }
alpar@96
   213
alpar@96
   214
    /// \brief Add a new arc behind the current path
alpar@96
   215
    void addBack(const Arc& arc) {
alpar@96
   216
      tail.push_back(arc);
alpar@96
   217
    }
alpar@96
   218
alpar@96
   219
    /// \brief Erase the last arc of the path
alpar@96
   220
    void eraseBack() {
alpar@96
   221
      if (!tail.empty()) {
alpar@96
   222
        tail.pop_back();
alpar@96
   223
      } else {
alpar@96
   224
        int halfsize = head.size() / 2;
alpar@96
   225
        tail.resize(halfsize);
alpar@96
   226
        std::copy(head.begin() + 1, head.begin() + halfsize + 1,
alpar@96
   227
                  tail.rbegin());
alpar@96
   228
        std::copy(head.begin() + halfsize + 1, head.end(), head.begin());
alpar@96
   229
        head.resize(head.size() - halfsize - 1);
alpar@96
   230
      }
alpar@96
   231
    }
alpar@96
   232
alpar@96
   233
    typedef True BuildTag;
alpar@96
   234
alpar@96
   235
    template <typename CPath>
alpar@96
   236
    void build(const CPath& path) {
alpar@96
   237
      int len = path.length();
alpar@96
   238
      tail.reserve(len);
alpar@96
   239
      for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
alpar@96
   240
        tail.push_back(it);
alpar@96
   241
      }
alpar@96
   242
    }
alpar@96
   243
alpar@96
   244
    template <typename CPath>
alpar@96
   245
    void buildRev(const CPath& path) {
alpar@96
   246
      int len = path.length();
alpar@96
   247
      head.reserve(len);
alpar@96
   248
      for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
alpar@96
   249
        head.push_back(it);
alpar@96
   250
      }
alpar@96
   251
    }
alpar@96
   252
alpar@96
   253
  protected:
alpar@96
   254
    typedef std::vector<Arc> Container;
alpar@96
   255
    Container head, tail;
alpar@96
   256
alpar@96
   257
  };
alpar@96
   258
alpar@96
   259
  /// \brief A structure for representing directed paths in a digraph.
alpar@96
   260
  ///
alpar@96
   261
  /// A structure for representing directed path in a digraph.
kpeter@559
   262
  /// \tparam GR The digraph type in which the path is.
alpar@96
   263
  ///
alpar@96
   264
  /// In a sense, the path can be treated as a list of arcs. The
kpeter@920
   265
  /// LEMON path type stores just this list. As a consequence it
alpar@96
   266
  /// cannot enumerate the nodes in the path and the zero length paths
alpar@96
   267
  /// cannot store the source.
alpar@96
   268
  ///
alpar@96
   269
  /// This implementation is a just back insertable and erasable path
alpar@96
   270
  /// type. It can be indexed in O(1) time. The back insertion and
alpar@96
   271
  /// erasure is amortized O(1) time. This implementation is faster
alpar@96
   272
  /// then the \c Path type because it use just one vector for the
alpar@96
   273
  /// arcs.
kpeter@559
   274
  template <typename GR>
alpar@96
   275
  class SimplePath {
alpar@96
   276
  public:
alpar@96
   277
kpeter@559
   278
    typedef GR Digraph;
alpar@96
   279
    typedef typename Digraph::Arc Arc;
alpar@96
   280
alpar@96
   281
    /// \brief Default constructor
alpar@96
   282
    ///
alpar@96
   283
    /// Default constructor
alpar@96
   284
    SimplePath() {}
alpar@96
   285
alpar@990
   286
    /// \brief Copy constructor
alpar@990
   287
    ///
alpar@990
   288
    SimplePath(const SimplePath& cpath) {
alpar@990
   289
      pathCopy(cpath, *this);
alpar@990
   290
    }
alpar@990
   291
alpar@96
   292
    /// \brief Template copy constructor
alpar@96
   293
    ///
alpar@96
   294
    /// This path can be initialized with any other path type. It just
alpar@96
   295
    /// makes a copy of the given path.
alpar@96
   296
    template <typename CPath>
alpar@96
   297
    SimplePath(const CPath& cpath) {
kpeter@516
   298
      pathCopy(cpath, *this);
alpar@96
   299
    }
alpar@96
   300
alpar@990
   301
    /// \brief Copy assignment
alpar@990
   302
    ///
alpar@990
   303
    SimplePath& operator=(const SimplePath& cpath) {
alpar@990
   304
      pathCopy(cpath, *this);
alpar@990
   305
      return *this;
alpar@990
   306
    }
alpar@990
   307
alpar@96
   308
    /// \brief Template copy assignment
alpar@96
   309
    ///
alpar@96
   310
    /// This path can be initialized with any other path type. It just
alpar@96
   311
    /// makes a copy of the given path.
alpar@96
   312
    template <typename CPath>
alpar@96
   313
    SimplePath& operator=(const CPath& cpath) {
kpeter@516
   314
      pathCopy(cpath, *this);
alpar@96
   315
      return *this;
alpar@96
   316
    }
alpar@96
   317
alpar@96
   318
    /// \brief Iterator class to iterate on the arcs of the paths
alpar@96
   319
    ///
alpar@96
   320
    /// This class is used to iterate on the arcs of the paths
alpar@96
   321
    ///
alpar@96
   322
    /// Of course it converts to Digraph::Arc
alpar@96
   323
    class ArcIt {
alpar@96
   324
      friend class SimplePath;
alpar@96
   325
    public:
alpar@96
   326
      /// Default constructor
alpar@96
   327
      ArcIt() {}
alpar@96
   328
      /// Invalid constructor
alpar@96
   329
      ArcIt(Invalid) : path(0), idx(-1) {}
alpar@96
   330
      /// \brief Initializate the constructor to the first arc of path
alpar@209
   331
      ArcIt(const SimplePath &_path)
alpar@96
   332
        : path(&_path), idx(_path.empty() ? -1 : 0) {}
alpar@96
   333
alpar@96
   334
    private:
alpar@96
   335
alpar@96
   336
      /// Constructor with starting point
alpar@209
   337
      ArcIt(const SimplePath &_path, int _idx)
kpeter@1044
   338
        : path(&_path), idx(_idx) {}
alpar@96
   339
alpar@96
   340
    public:
alpar@96
   341
alpar@96
   342
      ///Conversion to Digraph::Arc
alpar@96
   343
      operator const Arc&() const {
alpar@96
   344
        return path->nth(idx);
alpar@96
   345
      }
alpar@96
   346
alpar@96
   347
      /// Next arc
alpar@209
   348
      ArcIt& operator++() {
alpar@96
   349
        ++idx;
alpar@209
   350
        if (idx >= path->length()) idx = -1;
alpar@209
   351
        return *this;
alpar@96
   352
      }
alpar@96
   353
alpar@96
   354
      /// Comparison operator
alpar@96
   355
      bool operator==(const ArcIt& e) const { return idx==e.idx; }
alpar@96
   356
      /// Comparison operator
alpar@96
   357
      bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
alpar@96
   358
      /// Comparison operator
alpar@96
   359
      bool operator<(const ArcIt& e) const { return idx<e.idx; }
alpar@96
   360
alpar@96
   361
    private:
alpar@96
   362
      const SimplePath *path;
alpar@96
   363
      int idx;
alpar@96
   364
    };
alpar@96
   365
ggab90@1130
   366
    /// \brief Gets the collection of the arcs of the path.
ggab90@1130
   367
    ///
ggab90@1130
   368
    /// This function can be used for iterating on the
ggab90@1130
   369
    /// arcs of the path. It returns a wrapped
ggab90@1130
   370
    /// ArcIt, which looks like an STL container
ggab90@1130
   371
    /// (by having begin() and end()) which you can use in range-based
ggab90@1130
   372
    /// for loops, STL algorithms, etc.
ggab90@1130
   373
    /// For example you can write:
ggab90@1130
   374
    ///\code
ggab90@1130
   375
    /// for(auto a: p.arcs())
ggab90@1130
   376
    ///   doSomething(a);
ggab90@1130
   377
    ///\endcode
ggab90@1130
   378
    LemonRangeWrapper1<ArcIt, SimplePath> arcs() const {
ggab90@1130
   379
      return LemonRangeWrapper1<ArcIt, SimplePath>(*this);
ggab90@1130
   380
    }
ggab90@1130
   381
ggab90@1130
   382
alpar@96
   383
    /// \brief Length of the path.
alpar@96
   384
    int length() const { return data.size(); }
alpar@97
   385
    /// \brief Return true if the path is empty.
alpar@96
   386
    bool empty() const { return data.empty(); }
alpar@96
   387
alpar@97
   388
    /// \brief Reset the path to an empty one.
alpar@96
   389
    void clear() { data.clear(); }
alpar@96
   390
kpeter@920
   391
    /// \brief The n-th arc.
alpar@96
   392
    ///
kpeter@559
   393
    /// \pre \c n is in the <tt>[0..length() - 1]</tt> range.
alpar@96
   394
    const Arc& nth(int n) const {
alpar@96
   395
      return data[n];
alpar@96
   396
    }
alpar@96
   397
kpeter@920
   398
    /// \brief  Initializes arc iterator to point to the n-th arc.
alpar@96
   399
    ArcIt nthIt(int n) const {
alpar@96
   400
      return ArcIt(*this, n);
alpar@96
   401
    }
alpar@96
   402
alpar@97
   403
    /// \brief The first arc of the path.
alpar@96
   404
    const Arc& front() const {
alpar@96
   405
      return data.front();
alpar@96
   406
    }
alpar@96
   407
alpar@97
   408
    /// \brief The last arc of the path.
alpar@96
   409
    const Arc& back() const {
alpar@96
   410
      return data.back();
alpar@96
   411
    }
alpar@96
   412
alpar@96
   413
    /// \brief Add a new arc behind the current path.
alpar@96
   414
    void addBack(const Arc& arc) {
alpar@96
   415
      data.push_back(arc);
alpar@96
   416
    }
alpar@96
   417
alpar@96
   418
    /// \brief Erase the last arc of the path
alpar@96
   419
    void eraseBack() {
alpar@96
   420
      data.pop_back();
alpar@96
   421
    }
alpar@96
   422
alpar@96
   423
    typedef True BuildTag;
alpar@96
   424
alpar@96
   425
    template <typename CPath>
alpar@96
   426
    void build(const CPath& path) {
alpar@96
   427
      int len = path.length();
alpar@96
   428
      data.resize(len);
alpar@96
   429
      int index = 0;
alpar@96
   430
      for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
alpar@96
   431
        data[index] = it;;
alpar@96
   432
        ++index;
alpar@96
   433
      }
alpar@96
   434
    }
alpar@96
   435
alpar@96
   436
    template <typename CPath>
alpar@96
   437
    void buildRev(const CPath& path) {
alpar@96
   438
      int len = path.length();
alpar@96
   439
      data.resize(len);
alpar@96
   440
      int index = len;
alpar@96
   441
      for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
alpar@96
   442
        --index;
alpar@96
   443
        data[index] = it;;
alpar@96
   444
      }
alpar@96
   445
    }
alpar@96
   446
alpar@96
   447
  protected:
alpar@96
   448
    typedef std::vector<Arc> Container;
alpar@96
   449
    Container data;
alpar@96
   450
alpar@96
   451
  };
alpar@96
   452
alpar@96
   453
  /// \brief A structure for representing directed paths in a digraph.
alpar@96
   454
  ///
alpar@96
   455
  /// A structure for representing directed path in a digraph.
kpeter@559
   456
  /// \tparam GR The digraph type in which the path is.
alpar@96
   457
  ///
alpar@96
   458
  /// In a sense, the path can be treated as a list of arcs. The
kpeter@920
   459
  /// LEMON path type stores just this list. As a consequence it
alpar@96
   460
  /// cannot enumerate the nodes in the path and the zero length paths
alpar@96
   461
  /// cannot store the source.
alpar@96
   462
  ///
alpar@96
   463
  /// This implementation is a back and front insertable and erasable
alpar@96
   464
  /// path type. It can be indexed in O(k) time, where k is the rank
alpar@96
   465
  /// of the arc in the path. The length can be computed in O(n)
alpar@96
   466
  /// time. The front and back insertion and erasure is O(1) time
alpar@96
   467
  /// and it can be splited and spliced in O(1) time.
kpeter@559
   468
  template <typename GR>
alpar@96
   469
  class ListPath {
alpar@96
   470
  public:
alpar@96
   471
kpeter@559
   472
    typedef GR Digraph;
alpar@96
   473
    typedef typename Digraph::Arc Arc;
alpar@96
   474
alpar@96
   475
  protected:
alpar@96
   476
alpar@209
   477
    // the std::list<> is incompatible
alpar@96
   478
    // hard to create invalid iterator
alpar@96
   479
    struct Node {
alpar@96
   480
      Arc arc;
alpar@96
   481
      Node *next, *prev;
alpar@96
   482
    };
alpar@96
   483
alpar@96
   484
    Node *first, *last;
alpar@96
   485
alpar@96
   486
    std::allocator<Node> alloc;
alpar@96
   487
alpar@96
   488
  public:
alpar@209
   489
alpar@96
   490
    /// \brief Default constructor
alpar@96
   491
    ///
alpar@96
   492
    /// Default constructor
alpar@96
   493
    ListPath() : first(0), last(0) {}
alpar@96
   494
alpar@990
   495
    /// \brief Copy constructor
alpar@990
   496
    ///
alpar@990
   497
    ListPath(const ListPath& cpath) : first(0), last(0) {
alpar@990
   498
      pathCopy(cpath, *this);
alpar@990
   499
    }
alpar@990
   500
alpar@96
   501
    /// \brief Template copy constructor
alpar@96
   502
    ///
alpar@96
   503
    /// This path can be initialized with any other path type. It just
alpar@96
   504
    /// makes a copy of the given path.
alpar@96
   505
    template <typename CPath>
alpar@96
   506
    ListPath(const CPath& cpath) : first(0), last(0) {
kpeter@516
   507
      pathCopy(cpath, *this);
alpar@96
   508
    }
alpar@96
   509
alpar@96
   510
    /// \brief Destructor of the path
alpar@96
   511
    ///
alpar@96
   512
    /// Destructor of the path
alpar@96
   513
    ~ListPath() {
alpar@96
   514
      clear();
alpar@96
   515
    }
alpar@96
   516
alpar@990
   517
    /// \brief Copy assignment
alpar@990
   518
    ///
alpar@990
   519
    ListPath& operator=(const ListPath& cpath) {
alpar@990
   520
      pathCopy(cpath, *this);
alpar@990
   521
      return *this;
alpar@990
   522
    }
alpar@990
   523
alpar@96
   524
    /// \brief Template copy assignment
alpar@96
   525
    ///
alpar@96
   526
    /// This path can be initialized with any other path type. It just
alpar@96
   527
    /// makes a copy of the given path.
alpar@96
   528
    template <typename CPath>
alpar@96
   529
    ListPath& operator=(const CPath& cpath) {
kpeter@516
   530
      pathCopy(cpath, *this);
alpar@96
   531
      return *this;
alpar@96
   532
    }
alpar@96
   533
alpar@96
   534
    /// \brief Iterator class to iterate on the arcs of the paths
alpar@96
   535
    ///
alpar@96
   536
    /// This class is used to iterate on the arcs of the paths
alpar@96
   537
    ///
alpar@96
   538
    /// Of course it converts to Digraph::Arc
alpar@96
   539
    class ArcIt {
alpar@96
   540
      friend class ListPath;
alpar@96
   541
    public:
alpar@96
   542
      /// Default constructor
alpar@96
   543
      ArcIt() {}
alpar@96
   544
      /// Invalid constructor
alpar@96
   545
      ArcIt(Invalid) : path(0), node(0) {}
alpar@96
   546
      /// \brief Initializate the constructor to the first arc of path
alpar@209
   547
      ArcIt(const ListPath &_path)
alpar@96
   548
        : path(&_path), node(_path.first) {}
alpar@96
   549
alpar@96
   550
    protected:
alpar@96
   551
alpar@209
   552
      ArcIt(const ListPath &_path, Node *_node)
alpar@96
   553
        : path(&_path), node(_node) {}
alpar@96
   554
alpar@96
   555
alpar@96
   556
    public:
alpar@96
   557
alpar@96
   558
      ///Conversion to Digraph::Arc
alpar@96
   559
      operator const Arc&() const {
alpar@96
   560
        return node->arc;
alpar@96
   561
      }
alpar@96
   562
alpar@96
   563
      /// Next arc
alpar@209
   564
      ArcIt& operator++() {
alpar@96
   565
        node = node->next;
alpar@209
   566
        return *this;
alpar@96
   567
      }
alpar@96
   568
alpar@96
   569
      /// Comparison operator
alpar@96
   570
      bool operator==(const ArcIt& e) const { return node==e.node; }
alpar@96
   571
      /// Comparison operator
alpar@96
   572
      bool operator!=(const ArcIt& e) const { return node!=e.node; }
alpar@96
   573
      /// Comparison operator
alpar@96
   574
      bool operator<(const ArcIt& e) const { return node<e.node; }
alpar@96
   575
alpar@96
   576
    private:
alpar@96
   577
      const ListPath *path;
alpar@96
   578
      Node *node;
alpar@96
   579
    };
alpar@96
   580
ggab90@1130
   581
    /// \brief Gets the collection of the arcs of the path.
ggab90@1130
   582
    ///
ggab90@1130
   583
    /// This function can be used for iterating on the
ggab90@1130
   584
    /// arcs of the path. It returns a wrapped
ggab90@1130
   585
    /// ArcIt, which looks like an STL container
ggab90@1130
   586
    /// (by having begin() and end()) which you can use in range-based
ggab90@1130
   587
    /// for loops, STL algorithms, etc.
ggab90@1130
   588
    /// For example you can write:
ggab90@1130
   589
    ///\code
ggab90@1130
   590
    /// for(auto a: p.arcs())
ggab90@1130
   591
    ///   doSomething(a);
ggab90@1130
   592
    ///\endcode
ggab90@1130
   593
    LemonRangeWrapper1<ArcIt, ListPath> arcs() const {
ggab90@1130
   594
      return LemonRangeWrapper1<ArcIt, ListPath>(*this);
ggab90@1130
   595
    }
ggab90@1130
   596
ggab90@1130
   597
kpeter@920
   598
    /// \brief The n-th arc.
alpar@96
   599
    ///
kpeter@920
   600
    /// This function looks for the n-th arc in O(n) time.
kpeter@559
   601
    /// \pre \c n is in the <tt>[0..length() - 1]</tt> range.
alpar@96
   602
    const Arc& nth(int n) const {
alpar@96
   603
      Node *node = first;
alpar@96
   604
      for (int i = 0; i < n; ++i) {
alpar@96
   605
        node = node->next;
alpar@96
   606
      }
alpar@96
   607
      return node->arc;
alpar@96
   608
    }
alpar@96
   609
kpeter@920
   610
    /// \brief Initializes arc iterator to point to the n-th arc.
alpar@96
   611
    ArcIt nthIt(int n) const {
alpar@96
   612
      Node *node = first;
alpar@96
   613
      for (int i = 0; i < n; ++i) {
alpar@96
   614
        node = node->next;
alpar@96
   615
      }
alpar@96
   616
      return ArcIt(*this, node);
alpar@96
   617
    }
alpar@96
   618
alpar@96
   619
    /// \brief Length of the path.
alpar@96
   620
    int length() const {
alpar@96
   621
      int len = 0;
alpar@96
   622
      Node *node = first;
alpar@96
   623
      while (node != 0) {
alpar@96
   624
        node = node->next;
alpar@96
   625
        ++len;
alpar@96
   626
      }
alpar@96
   627
      return len;
alpar@96
   628
    }
alpar@96
   629
alpar@97
   630
    /// \brief Return true if the path is empty.
alpar@96
   631
    bool empty() const { return first == 0; }
alpar@96
   632
alpar@97
   633
    /// \brief Reset the path to an empty one.
alpar@96
   634
    void clear() {
alpar@96
   635
      while (first != 0) {
alpar@96
   636
        last = first->next;
alpar@96
   637
        alloc.destroy(first);
alpar@96
   638
        alloc.deallocate(first, 1);
alpar@96
   639
        first = last;
alpar@96
   640
      }
alpar@96
   641
    }
alpar@96
   642
alpar@97
   643
    /// \brief The first arc of the path
alpar@96
   644
    const Arc& front() const {
alpar@96
   645
      return first->arc;
alpar@96
   646
    }
alpar@96
   647
alpar@96
   648
    /// \brief Add a new arc before the current path
alpar@96
   649
    void addFront(const Arc& arc) {
alpar@96
   650
      Node *node = alloc.allocate(1);
alpar@96
   651
      alloc.construct(node, Node());
alpar@96
   652
      node->prev = 0;
alpar@96
   653
      node->next = first;
alpar@96
   654
      node->arc = arc;
alpar@96
   655
      if (first) {
alpar@96
   656
        first->prev = node;
alpar@96
   657
        first = node;
alpar@96
   658
      } else {
alpar@96
   659
        first = last = node;
alpar@96
   660
      }
alpar@96
   661
    }
alpar@96
   662
alpar@96
   663
    /// \brief Erase the first arc of the path
alpar@96
   664
    void eraseFront() {
alpar@96
   665
      Node *node = first;
alpar@96
   666
      first = first->next;
alpar@96
   667
      if (first) {
alpar@96
   668
        first->prev = 0;
alpar@96
   669
      } else {
alpar@96
   670
        last = 0;
alpar@96
   671
      }
alpar@96
   672
      alloc.destroy(node);
alpar@96
   673
      alloc.deallocate(node, 1);
alpar@96
   674
    }
alpar@96
   675
alpar@97
   676
    /// \brief The last arc of the path.
alpar@96
   677
    const Arc& back() const {
alpar@96
   678
      return last->arc;
alpar@96
   679
    }
alpar@96
   680
alpar@96
   681
    /// \brief Add a new arc behind the current path.
alpar@96
   682
    void addBack(const Arc& arc) {
alpar@96
   683
      Node *node = alloc.allocate(1);
alpar@96
   684
      alloc.construct(node, Node());
alpar@96
   685
      node->next = 0;
alpar@96
   686
      node->prev = last;
alpar@96
   687
      node->arc = arc;
alpar@96
   688
      if (last) {
alpar@96
   689
        last->next = node;
alpar@96
   690
        last = node;
alpar@96
   691
      } else {
alpar@96
   692
        last = first = node;
alpar@96
   693
      }
alpar@96
   694
    }
alpar@96
   695
alpar@96
   696
    /// \brief Erase the last arc of the path
alpar@96
   697
    void eraseBack() {
alpar@96
   698
      Node *node = last;
alpar@96
   699
      last = last->prev;
alpar@96
   700
      if (last) {
alpar@96
   701
        last->next = 0;
alpar@96
   702
      } else {
alpar@96
   703
        first = 0;
alpar@96
   704
      }
alpar@96
   705
      alloc.destroy(node);
alpar@96
   706
      alloc.deallocate(node, 1);
alpar@96
   707
    }
alpar@96
   708
alpar@97
   709
    /// \brief Splice a path to the back of the current path.
alpar@96
   710
    ///
alpar@97
   711
    /// It splices \c tpath to the back of the current path and \c
alpar@96
   712
    /// tpath becomes empty. The time complexity of this function is
alpar@96
   713
    /// O(1).
alpar@96
   714
    void spliceBack(ListPath& tpath) {
alpar@96
   715
      if (first) {
alpar@96
   716
        if (tpath.first) {
alpar@96
   717
          last->next = tpath.first;
alpar@96
   718
          tpath.first->prev = last;
alpar@96
   719
          last = tpath.last;
alpar@96
   720
        }
alpar@96
   721
      } else {
alpar@96
   722
        first = tpath.first;
alpar@96
   723
        last = tpath.last;
alpar@96
   724
      }
alpar@96
   725
      tpath.first = tpath.last = 0;
alpar@96
   726
    }
alpar@96
   727
alpar@97
   728
    /// \brief Splice a path to the front of the current path.
alpar@96
   729
    ///
alpar@97
   730
    /// It splices \c tpath before the current path and \c tpath
alpar@96
   731
    /// becomes empty. The time complexity of this function
alpar@96
   732
    /// is O(1).
alpar@96
   733
    void spliceFront(ListPath& tpath) {
alpar@96
   734
      if (first) {
alpar@96
   735
        if (tpath.first) {
alpar@96
   736
          first->prev = tpath.last;
alpar@96
   737
          tpath.last->next = first;
alpar@96
   738
          first = tpath.first;
alpar@96
   739
        }
alpar@96
   740
      } else {
alpar@96
   741
        first = tpath.first;
alpar@96
   742
        last = tpath.last;
alpar@96
   743
      }
alpar@96
   744
      tpath.first = tpath.last = 0;
alpar@96
   745
    }
alpar@96
   746
alpar@97
   747
    /// \brief Splice a path into the current path.
alpar@96
   748
    ///
alpar@96
   749
    /// It splices the \c tpath into the current path before the
alpar@96
   750
    /// position of \c it iterator and \c tpath becomes empty. The
alpar@97
   751
    /// time complexity of this function is O(1). If the \c it is
alpar@97
   752
    /// \c INVALID then it will splice behind the current path.
alpar@96
   753
    void splice(ArcIt it, ListPath& tpath) {
alpar@96
   754
      if (it.node) {
alpar@96
   755
        if (tpath.first) {
alpar@96
   756
          tpath.first->prev = it.node->prev;
alpar@96
   757
          if (it.node->prev) {
alpar@96
   758
            it.node->prev->next = tpath.first;
alpar@96
   759
          } else {
alpar@96
   760
            first = tpath.first;
alpar@96
   761
          }
alpar@96
   762
          it.node->prev = tpath.last;
alpar@96
   763
          tpath.last->next = it.node;
alpar@96
   764
        }
alpar@96
   765
      } else {
alpar@96
   766
        if (first) {
alpar@96
   767
          if (tpath.first) {
alpar@96
   768
            last->next = tpath.first;
alpar@96
   769
            tpath.first->prev = last;
alpar@96
   770
            last = tpath.last;
alpar@96
   771
          }
alpar@96
   772
        } else {
alpar@96
   773
          first = tpath.first;
alpar@96
   774
          last = tpath.last;
alpar@96
   775
        }
alpar@96
   776
      }
alpar@96
   777
      tpath.first = tpath.last = 0;
alpar@96
   778
    }
alpar@96
   779
alpar@97
   780
    /// \brief Split the current path.
alpar@96
   781
    ///
alpar@97
   782
    /// It splits the current path into two parts. The part before
alpar@97
   783
    /// the iterator \c it will remain in the current path and the part
alpar@97
   784
    /// starting with
alpar@97
   785
    /// \c it will put into \c tpath. If \c tpath have arcs
alpar@97
   786
    /// before the operation they are removed first.  The time
alpar@97
   787
    /// complexity of this function is O(1) plus the the time of emtying
alpar@97
   788
    /// \c tpath. If \c it is \c INVALID then it just clears \c tpath
alpar@96
   789
    void split(ArcIt it, ListPath& tpath) {
alpar@96
   790
      tpath.clear();
alpar@96
   791
      if (it.node) {
alpar@96
   792
        tpath.first = it.node;
alpar@96
   793
        tpath.last = last;
alpar@96
   794
        if (it.node->prev) {
alpar@96
   795
          last = it.node->prev;
alpar@96
   796
          last->next = 0;
alpar@96
   797
        } else {
alpar@96
   798
          first = last = 0;
alpar@96
   799
        }
alpar@96
   800
        it.node->prev = 0;
alpar@96
   801
      }
alpar@96
   802
    }
alpar@96
   803
alpar@96
   804
alpar@96
   805
    typedef True BuildTag;
alpar@96
   806
alpar@96
   807
    template <typename CPath>
alpar@96
   808
    void build(const CPath& path) {
alpar@96
   809
      for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
alpar@96
   810
        addBack(it);
alpar@96
   811
      }
alpar@96
   812
    }
alpar@96
   813
alpar@96
   814
    template <typename CPath>
alpar@96
   815
    void buildRev(const CPath& path) {
alpar@96
   816
      for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
alpar@96
   817
        addFront(it);
alpar@96
   818
      }
alpar@96
   819
    }
alpar@96
   820
alpar@96
   821
  };
alpar@96
   822
alpar@96
   823
  /// \brief A structure for representing directed paths in a digraph.
alpar@96
   824
  ///
alpar@96
   825
  /// A structure for representing directed path in a digraph.
kpeter@559
   826
  /// \tparam GR The digraph type in which the path is.
alpar@96
   827
  ///
alpar@96
   828
  /// In a sense, the path can be treated as a list of arcs. The
kpeter@920
   829
  /// LEMON path type stores just this list. As a consequence it
alpar@97
   830
  /// cannot enumerate the nodes in the path and the source node of
alpar@97
   831
  /// a zero length path is undefined.
alpar@96
   832
  ///
alpar@97
   833
  /// This implementation is completly static, i.e. it can be copy constucted
alpar@97
   834
  /// or copy assigned from another path, but otherwise it cannot be
alpar@97
   835
  /// modified.
alpar@97
   836
  ///
alpar@97
   837
  /// Being the the most memory efficient path type in LEMON,
alpar@97
   838
  /// it is intented to be
alpar@97
   839
  /// used when you want to store a large number of paths.
kpeter@559
   840
  template <typename GR>
alpar@96
   841
  class StaticPath {
alpar@96
   842
  public:
alpar@96
   843
kpeter@559
   844
    typedef GR Digraph;
alpar@96
   845
    typedef typename Digraph::Arc Arc;
alpar@96
   846
alpar@96
   847
    /// \brief Default constructor
alpar@96
   848
    ///
alpar@96
   849
    /// Default constructor
ggab90@1130
   850
    StaticPath() : len(0), _arcs(0) {}
alpar@209
   851
alpar@990
   852
    /// \brief Copy constructor
alpar@990
   853
    ///
ggab90@1130
   854
    StaticPath(const StaticPath& cpath) : _arcs(0) {
alpar@990
   855
      pathCopy(cpath, *this);
alpar@990
   856
    }
alpar@990
   857
alpar@96
   858
    /// \brief Template copy constructor
alpar@96
   859
    ///
alpar@97
   860
    /// This path can be initialized from any other path type.
alpar@96
   861
    template <typename CPath>
ggab90@1130
   862
    StaticPath(const CPath& cpath) : _arcs(0) {
kpeter@516
   863
      pathCopy(cpath, *this);
alpar@96
   864
    }
alpar@96
   865
alpar@96
   866
    /// \brief Destructor of the path
alpar@96
   867
    ///
alpar@96
   868
    /// Destructor of the path
alpar@96
   869
    ~StaticPath() {
ggab90@1130
   870
      if (_arcs) delete[] _arcs;
alpar@96
   871
    }
alpar@96
   872
alpar@990
   873
    /// \brief Copy assignment
alpar@990
   874
    ///
alpar@990
   875
    StaticPath& operator=(const StaticPath& cpath) {
alpar@990
   876
      pathCopy(cpath, *this);
alpar@990
   877
      return *this;
alpar@990
   878
    }
alpar@990
   879
alpar@96
   880
    /// \brief Template copy assignment
alpar@96
   881
    ///
alpar@97
   882
    /// This path can be made equal to any other path type. It simply
alpar@96
   883
    /// makes a copy of the given path.
alpar@96
   884
    template <typename CPath>
alpar@96
   885
    StaticPath& operator=(const CPath& cpath) {
kpeter@516
   886
      pathCopy(cpath, *this);
alpar@96
   887
      return *this;
alpar@96
   888
    }
alpar@96
   889
alpar@96
   890
    /// \brief Iterator class to iterate on the arcs of the paths
alpar@96
   891
    ///
alpar@96
   892
    /// This class is used to iterate on the arcs of the paths
alpar@96
   893
    ///
alpar@96
   894
    /// Of course it converts to Digraph::Arc
alpar@96
   895
    class ArcIt {
alpar@96
   896
      friend class StaticPath;
alpar@96
   897
    public:
alpar@96
   898
      /// Default constructor
alpar@96
   899
      ArcIt() {}
alpar@96
   900
      /// Invalid constructor
alpar@96
   901
      ArcIt(Invalid) : path(0), idx(-1) {}
alpar@96
   902
      /// Initializate the constructor to the first arc of path
alpar@209
   903
      ArcIt(const StaticPath &_path)
alpar@96
   904
        : path(&_path), idx(_path.empty() ? -1 : 0) {}
alpar@96
   905
alpar@96
   906
    private:
alpar@96
   907
alpar@96
   908
      /// Constructor with starting point
alpar@209
   909
      ArcIt(const StaticPath &_path, int _idx)
alpar@96
   910
        : idx(_idx), path(&_path) {}
alpar@96
   911
alpar@96
   912
    public:
alpar@96
   913
alpar@96
   914
      ///Conversion to Digraph::Arc
alpar@96
   915
      operator const Arc&() const {
alpar@96
   916
        return path->nth(idx);
alpar@96
   917
      }
alpar@96
   918
alpar@96
   919
      /// Next arc
alpar@209
   920
      ArcIt& operator++() {
alpar@96
   921
        ++idx;
alpar@209
   922
        if (idx >= path->length()) idx = -1;
alpar@209
   923
        return *this;
alpar@96
   924
      }
alpar@96
   925
alpar@96
   926
      /// Comparison operator
alpar@96
   927
      bool operator==(const ArcIt& e) const { return idx==e.idx; }
alpar@96
   928
      /// Comparison operator
alpar@96
   929
      bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
alpar@96
   930
      /// Comparison operator
alpar@96
   931
      bool operator<(const ArcIt& e) const { return idx<e.idx; }
alpar@96
   932
alpar@96
   933
    private:
alpar@96
   934
      const StaticPath *path;
alpar@96
   935
      int idx;
alpar@96
   936
    };
ggab90@1130
   937
    
ggab90@1130
   938
    /// \brief Gets the collection of the arcs of the path.
ggab90@1130
   939
    ///
ggab90@1130
   940
    /// This function can be used for iterating on the
ggab90@1130
   941
    /// arcs of the path. It returns a wrapped
ggab90@1130
   942
    /// ArcIt, which looks like an STL container
ggab90@1130
   943
    /// (by having begin() and end()) which you can use in range-based
ggab90@1130
   944
    /// for loops, STL algorithms, etc.
ggab90@1130
   945
    /// For example you can write:
ggab90@1130
   946
    ///\code
ggab90@1130
   947
    /// for(auto a: p.arcs())
ggab90@1130
   948
    ///   doSomething(a);
ggab90@1130
   949
    ///\endcode
ggab90@1130
   950
    LemonRangeWrapper1<ArcIt, StaticPath> arcs() const {
ggab90@1130
   951
      return LemonRangeWrapper1<ArcIt, StaticPath>(*this);
ggab90@1130
   952
    }
ggab90@1130
   953
    
alpar@96
   954
kpeter@920
   955
    /// \brief The n-th arc.
alpar@96
   956
    ///
kpeter@559
   957
    /// \pre \c n is in the <tt>[0..length() - 1]</tt> range.
alpar@96
   958
    const Arc& nth(int n) const {
ggab90@1130
   959
      return _arcs[n];
alpar@96
   960
    }
alpar@96
   961
kpeter@920
   962
    /// \brief The arc iterator pointing to the n-th arc.
alpar@96
   963
    ArcIt nthIt(int n) const {
alpar@96
   964
      return ArcIt(*this, n);
alpar@96
   965
    }
alpar@96
   966
alpar@97
   967
    /// \brief The length of the path.
alpar@96
   968
    int length() const { return len; }
alpar@96
   969
alpar@97
   970
    /// \brief Return true when the path is empty.
alpar@96
   971
    int empty() const { return len == 0; }
alpar@96
   972
kpeter@313
   973
    /// \brief Erase all arcs in the digraph.
alpar@96
   974
    void clear() {
alpar@96
   975
      len = 0;
ggab90@1130
   976
      if (_arcs) delete[] _arcs;
ggab90@1130
   977
      _arcs = 0;
alpar@96
   978
    }
alpar@96
   979
alpar@97
   980
    /// \brief The first arc of the path.
alpar@96
   981
    const Arc& front() const {
ggab90@1130
   982
      return _arcs[0];
alpar@96
   983
    }
alpar@96
   984
alpar@97
   985
    /// \brief The last arc of the path.
alpar@96
   986
    const Arc& back() const {
ggab90@1130
   987
      return _arcs[len - 1];
alpar@96
   988
    }
alpar@96
   989
alpar@96
   990
alpar@96
   991
    typedef True BuildTag;
alpar@96
   992
alpar@96
   993
    template <typename CPath>
alpar@96
   994
    void build(const CPath& path) {
alpar@96
   995
      len = path.length();
ggab90@1130
   996
      _arcs = new Arc[len];
alpar@96
   997
      int index = 0;
alpar@96
   998
      for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
ggab90@1130
   999
        _arcs[index] = it;
alpar@96
  1000
        ++index;
alpar@96
  1001
      }
alpar@96
  1002
    }
alpar@96
  1003
alpar@96
  1004
    template <typename CPath>
alpar@96
  1005
    void buildRev(const CPath& path) {
alpar@96
  1006
      len = path.length();
ggab90@1130
  1007
      _arcs = new Arc[len];
alpar@96
  1008
      int index = len;
alpar@96
  1009
      for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
alpar@96
  1010
        --index;
ggab90@1130
  1011
        _arcs[index] = it;
alpar@96
  1012
      }
alpar@96
  1013
    }
alpar@96
  1014
alpar@96
  1015
  private:
alpar@96
  1016
    int len;
ggab90@1130
  1017
    Arc* _arcs;
alpar@96
  1018
  };
alpar@96
  1019
alpar@98
  1020
  ///////////////////////////////////////////////////////////////////////
alpar@98
  1021
  // Additional utilities
alpar@98
  1022
  ///////////////////////////////////////////////////////////////////////
alpar@98
  1023
alpar@98
  1024
  namespace _path_bits {
alpar@98
  1025
alpar@98
  1026
    template <typename Path, typename Enable = void>
deba@144
  1027
    struct RevPathTagIndicator {
alpar@98
  1028
      static const bool value = false;
alpar@98
  1029
    };
alpar@98
  1030
deba@144
  1031
    template <typename Path>
deba@144
  1032
    struct RevPathTagIndicator<
alpar@209
  1033
      Path,
deba@144
  1034
      typename enable_if<typename Path::RevPathTag, void>::type
deba@144
  1035
      > {
deba@144
  1036
      static const bool value = true;
deba@144
  1037
    };
deba@144
  1038
deba@144
  1039
    template <typename Path, typename Enable = void>
deba@144
  1040
    struct BuildTagIndicator {
deba@144
  1041
      static const bool value = false;
deba@144
  1042
    };
deba@144
  1043
deba@144
  1044
    template <typename Path>
deba@144
  1045
    struct BuildTagIndicator<
alpar@209
  1046
      Path,
deba@144
  1047
      typename enable_if<typename Path::BuildTag, void>::type
alpar@98
  1048
    > {
alpar@98
  1049
      static const bool value = true;
alpar@98
  1050
    };
alpar@98
  1051
kpeter@516
  1052
    template <typename From, typename To,
kpeter@516
  1053
              bool buildEnable = BuildTagIndicator<To>::value>
kpeter@498
  1054
    struct PathCopySelectorForward {
kpeter@516
  1055
      static void copy(const From& from, To& to) {
kpeter@516
  1056
        to.clear();
kpeter@516
  1057
        for (typename From::ArcIt it(from); it != INVALID; ++it) {
kpeter@516
  1058
          to.addBack(it);
alpar@98
  1059
        }
alpar@98
  1060
      }
alpar@98
  1061
    };
alpar@98
  1062
kpeter@516
  1063
    template <typename From, typename To>
kpeter@516
  1064
    struct PathCopySelectorForward<From, To, true> {
kpeter@516
  1065
      static void copy(const From& from, To& to) {
kpeter@516
  1066
        to.clear();
kpeter@516
  1067
        to.build(from);
kpeter@498
  1068
      }
kpeter@498
  1069
    };
kpeter@498
  1070
kpeter@516
  1071
    template <typename From, typename To,
kpeter@516
  1072
              bool buildEnable = BuildTagIndicator<To>::value>
kpeter@498
  1073
    struct PathCopySelectorBackward {
kpeter@516
  1074
      static void copy(const From& from, To& to) {
kpeter@516
  1075
        to.clear();
kpeter@516
  1076
        for (typename From::RevArcIt it(from); it != INVALID; ++it) {
kpeter@516
  1077
          to.addFront(it);
alpar@98
  1078
        }
alpar@98
  1079
      }
alpar@98
  1080
    };
alpar@98
  1081
kpeter@516
  1082
    template <typename From, typename To>
kpeter@516
  1083
    struct PathCopySelectorBackward<From, To, true> {
kpeter@516
  1084
      static void copy(const From& from, To& to) {
kpeter@516
  1085
        to.clear();
kpeter@516
  1086
        to.buildRev(from);
alpar@98
  1087
      }
alpar@98
  1088
    };
alpar@98
  1089
alpar@877
  1090
kpeter@516
  1091
    template <typename From, typename To,
kpeter@516
  1092
              bool revEnable = RevPathTagIndicator<From>::value>
kpeter@498
  1093
    struct PathCopySelector {
kpeter@516
  1094
      static void copy(const From& from, To& to) {
kpeter@516
  1095
        PathCopySelectorForward<From, To>::copy(from, to);
alpar@877
  1096
      }
kpeter@498
  1097
    };
kpeter@498
  1098
kpeter@516
  1099
    template <typename From, typename To>
kpeter@516
  1100
    struct PathCopySelector<From, To, true> {
kpeter@516
  1101
      static void copy(const From& from, To& to) {
kpeter@516
  1102
        PathCopySelectorBackward<From, To>::copy(from, to);
alpar@877
  1103
      }
kpeter@498
  1104
    };
kpeter@498
  1105
alpar@98
  1106
  }
alpar@98
  1107
alpar@98
  1108
alpar@98
  1109
  /// \brief Make a copy of a path.
alpar@98
  1110
  ///
kpeter@516
  1111
  /// This function makes a copy of a path.
kpeter@516
  1112
  template <typename From, typename To>
kpeter@516
  1113
  void pathCopy(const From& from, To& to) {
kpeter@516
  1114
    checkConcept<concepts::PathDumper<typename From::Digraph>, From>();
kpeter@516
  1115
    _path_bits::PathCopySelector<From, To>::copy(from, to);
kpeter@516
  1116
  }
kpeter@516
  1117
kpeter@516
  1118
  /// \brief Deprecated version of \ref pathCopy().
kpeter@516
  1119
  ///
kpeter@516
  1120
  /// Deprecated version of \ref pathCopy() (only for reverse compatibility).
kpeter@516
  1121
  template <typename To, typename From>
kpeter@516
  1122
  void copyPath(To& to, const From& from) {
kpeter@516
  1123
    pathCopy(from, to);
alpar@98
  1124
  }
alpar@98
  1125
alpar@98
  1126
  /// \brief Check the consistency of a path.
alpar@98
  1127
  ///
alpar@98
  1128
  /// This function checks that the target of each arc is the same
alpar@209
  1129
  /// as the source of the next one.
alpar@209
  1130
  ///
alpar@98
  1131
  template <typename Digraph, typename Path>
alpar@98
  1132
  bool checkPath(const Digraph& digraph, const Path& path) {
alpar@98
  1133
    typename Path::ArcIt it(path);
alpar@98
  1134
    if (it == INVALID) return true;
alpar@98
  1135
    typename Digraph::Node node = digraph.target(it);
alpar@98
  1136
    ++it;
alpar@98
  1137
    while (it != INVALID) {
alpar@98
  1138
      if (digraph.source(it) != node) return false;
alpar@98
  1139
      node = digraph.target(it);
alpar@98
  1140
      ++it;
alpar@98
  1141
    }
alpar@98
  1142
    return true;
alpar@98
  1143
  }
alpar@98
  1144
alpar@98
  1145
  /// \brief The source of a path
alpar@98
  1146
  ///
kpeter@514
  1147
  /// This function returns the source node of the given path.
kpeter@514
  1148
  /// If the path is empty, then it returns \c INVALID.
alpar@98
  1149
  template <typename Digraph, typename Path>
alpar@98
  1150
  typename Digraph::Node pathSource(const Digraph& digraph, const Path& path) {
kpeter@514
  1151
    return path.empty() ? INVALID : digraph.source(path.front());
alpar@98
  1152
  }
alpar@98
  1153
alpar@98
  1154
  /// \brief The target of a path
alpar@98
  1155
  ///
kpeter@514
  1156
  /// This function returns the target node of the given path.
kpeter@514
  1157
  /// If the path is empty, then it returns \c INVALID.
alpar@98
  1158
  template <typename Digraph, typename Path>
alpar@98
  1159
  typename Digraph::Node pathTarget(const Digraph& digraph, const Path& path) {
kpeter@514
  1160
    return path.empty() ? INVALID : digraph.target(path.back());
alpar@98
  1161
  }
alpar@98
  1162
alpar@98
  1163
  /// \brief Class which helps to iterate through the nodes of a path
alpar@98
  1164
  ///
alpar@98
  1165
  /// In a sense, the path can be treated as a list of arcs. The
kpeter@920
  1166
  /// LEMON path type stores only this list. As a consequence, it
alpar@98
  1167
  /// cannot enumerate the nodes in the path and the zero length paths
alpar@98
  1168
  /// cannot have a source node.
alpar@98
  1169
  ///
alpar@98
  1170
  /// This class implements the node iterator of a path structure. To
alpar@98
  1171
  /// provide this feature, the underlying digraph should be passed to
alpar@98
  1172
  /// the constructor of the iterator.
alpar@98
  1173
  template <typename Path>
alpar@98
  1174
  class PathNodeIt {
alpar@98
  1175
  private:
alpar@98
  1176
    const typename Path::Digraph *_digraph;
alpar@98
  1177
    typename Path::ArcIt _it;
alpar@98
  1178
    typename Path::Digraph::Node _nd;
alpar@98
  1179
alpar@98
  1180
  public:
alpar@98
  1181
alpar@98
  1182
    typedef typename Path::Digraph Digraph;
alpar@98
  1183
    typedef typename Digraph::Node Node;
alpar@209
  1184
alpar@98
  1185
    /// Default constructor
alpar@98
  1186
    PathNodeIt() {}
alpar@98
  1187
    /// Invalid constructor
alpar@209
  1188
    PathNodeIt(Invalid)
alpar@98
  1189
      : _digraph(0), _it(INVALID), _nd(INVALID) {}
alpar@98
  1190
    /// Constructor
alpar@209
  1191
    PathNodeIt(const Digraph& digraph, const Path& path)
alpar@98
  1192
      : _digraph(&digraph), _it(path) {
alpar@98
  1193
      _nd = (_it != INVALID ? _digraph->source(_it) : INVALID);
alpar@98
  1194
    }
alpar@98
  1195
    /// Constructor
alpar@209
  1196
    PathNodeIt(const Digraph& digraph, const Path& path, const Node& src)
alpar@98
  1197
      : _digraph(&digraph), _it(path), _nd(src) {}
alpar@98
  1198
alpar@98
  1199
    ///Conversion to Digraph::Node
alpar@98
  1200
    operator Node() const {
alpar@98
  1201
      return _nd;
alpar@98
  1202
    }
alpar@98
  1203
alpar@98
  1204
    /// Next node
alpar@98
  1205
    PathNodeIt& operator++() {
alpar@98
  1206
      if (_it == INVALID) _nd = INVALID;
alpar@98
  1207
      else {
alpar@209
  1208
        _nd = _digraph->target(_it);
alpar@209
  1209
        ++_it;
alpar@98
  1210
      }
alpar@98
  1211
      return *this;
alpar@98
  1212
    }
alpar@98
  1213
alpar@98
  1214
    /// Comparison operator
alpar@209
  1215
    bool operator==(const PathNodeIt& n) const {
alpar@209
  1216
      return _it == n._it && _nd == n._nd;
alpar@98
  1217
    }
alpar@98
  1218
    /// Comparison operator
alpar@209
  1219
    bool operator!=(const PathNodeIt& n) const {
alpar@209
  1220
      return _it != n._it || _nd != n._nd;
alpar@98
  1221
    }
alpar@98
  1222
    /// Comparison operator
alpar@209
  1223
    bool operator<(const PathNodeIt& n) const {
alpar@98
  1224
      return (_it < n._it && _nd != INVALID);
alpar@98
  1225
    }
alpar@209
  1226
alpar@98
  1227
  };
alpar@209
  1228
ggab90@1130
  1229
  /// \brief Gets the collection of the nodes of the path.
ggab90@1130
  1230
  ///
ggab90@1130
  1231
  /// This function can be used for iterating on the
ggab90@1130
  1232
  /// nodes of the path. It returns a wrapped
ggab90@1130
  1233
  /// PathNodeIt, which looks like an STL container
ggab90@1130
  1234
  /// (by having begin() and end()) which you can use in range-based
ggab90@1130
  1235
  /// for loops, STL algorithms, etc.
ggab90@1130
  1236
  /// For example you can write:
ggab90@1130
  1237
  ///\code
ggab90@1130
  1238
  /// for(auto u: pathNodes(g,p))
ggab90@1130
  1239
  ///   doSomething(u);
ggab90@1130
  1240
  ///\endcode
ggab90@1130
  1241
  template<typename Path>
ggab90@1130
  1242
  LemonRangeWrapper2<PathNodeIt<Path>, typename Path::Digraph, Path>
ggab90@1130
  1243
      pathNodes(const typename Path::Digraph &g, const Path &p) {
ggab90@1130
  1244
    return
ggab90@1130
  1245
        LemonRangeWrapper2<PathNodeIt<Path>, typename Path::Digraph, Path>(g,p);
ggab90@1130
  1246
  }
ggab90@1130
  1247
alpar@96
  1248
  ///@}
alpar@96
  1249
alpar@96
  1250
} // namespace lemon
alpar@96
  1251
alpar@96
  1252
#endif // LEMON_PATH_H