lemon/path.h
author alpar
Fri, 19 Jan 2007 17:27:22 +0000
changeset 2347 0aaa7ada5395
parent 2335 27aa03cd3121
child 2357 5365600a7a5c
permissions -rw-r--r--
Spellcheck
alpar@906
     1
/* -*- C++ -*-
alpar@906
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     8
 *
alpar@906
     9
 * Permission to use, modify and distribute this software is granted
alpar@906
    10
 * provided that this copyright notice appears in all copies. For
alpar@906
    11
 * precise terms see the accompanying LICENSE file.
alpar@906
    12
 *
alpar@906
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    14
 * express or implied, and with no claim as to its suitability for any
alpar@906
    15
 * purpose.
alpar@906
    16
 *
alpar@906
    17
 */
hegyi@819
    18
hegyi@819
    19
hegyi@819
    20
///\ingroup paths
hegyi@819
    21
///\file
hegyi@819
    22
///\brief Classes for representing paths in graphs.
alpar@1151
    23
///
hegyi@819
    24
alpar@921
    25
#ifndef LEMON_PATH_H
alpar@921
    26
#define LEMON_PATH_H
hegyi@819
    27
hegyi@819
    28
#include <vector>
hegyi@819
    29
#include <algorithm>
hegyi@819
    30
deba@2335
    31
#include <lemon/path_utils.h>
deba@2247
    32
#include <lemon/error.h>
deba@1993
    33
#include <lemon/bits/invalid.h>
hegyi@819
    34
alpar@921
    35
namespace lemon {
hegyi@819
    36
hegyi@819
    37
  /// \addtogroup paths
hegyi@819
    38
  /// @{
hegyi@819
    39
hegyi@819
    40
deba@2335
    41
  /// \brief A structure for representing directed paths in a graph.
deba@2335
    42
  ///
deba@2335
    43
  /// A structure for representing directed path in a graph.
deba@2335
    44
  /// \param Graph The graph type in which the path is.
deba@2335
    45
  ///
deba@2335
    46
  /// In a sense, the path can be treated as a list of edges. The
deba@2335
    47
  /// lemon path type stores just this list. As a consequence it
deba@2335
    48
  /// cannot enumerate the nodes in the path and the zero length paths
deba@2335
    49
  /// cannot store the source.
deba@2335
    50
  ///
deba@2335
    51
  /// This implementation is a back and front insertable and erasable
deba@2335
    52
  /// path type. It can be indexed in O(1) time. The front and back
deba@2335
    53
  /// insertion and erasure is amortized O(1) time. The
deba@2335
    54
  /// impelementation is based on two opposite organized vectors.
deba@2335
    55
  template <typename _Graph>
deba@2247
    56
  class Path {
hegyi@819
    57
  public:
deba@2335
    58
deba@2335
    59
    typedef _Graph Graph;
deba@2247
    60
    typedef typename Graph::Edge Edge;
deba@2247
    61
deba@2335
    62
    /// \brief Default constructor
deba@2335
    63
    ///
deba@2335
    64
    /// Default constructor
deba@2335
    65
    Path() {}
hegyi@819
    66
deba@2335
    67
    /// \brief Template copy constructor
deba@2247
    68
    ///
deba@2335
    69
    /// This path can be initialized with any other path type. It just
deba@2335
    70
    /// makes a copy of the given path.
deba@2335
    71
    template <typename CPath>
deba@2335
    72
    Path(const CPath& cpath) {
deba@2335
    73
      copyPath(*this, cpath);
hegyi@819
    74
    }
hegyi@819
    75
deba@2335
    76
    /// \brief Template copy assignment
hegyi@819
    77
    ///
deba@2335
    78
    /// This path can be initialized with any other path type. It just
deba@2335
    79
    /// makes a copy of the given path.
deba@2335
    80
    template <typename CPath>
deba@2335
    81
    Path& operator=(const CPath& cpath) {
deba@2335
    82
      copyPath(*this, cpath);
deba@2335
    83
      return *this;
hegyi@819
    84
    }
hegyi@819
    85
deba@2335
    86
    /// \brief Lemon style iterator for path edges
deba@2247
    87
    ///
deba@2335
    88
    /// This class is used to iterate on the edges of the paths.
deba@2335
    89
    class EdgeIt {
deba@2247
    90
      friend class Path;
deba@2247
    91
    public:
deba@2335
    92
      /// \brief Default constructor
deba@2335
    93
      EdgeIt() {}
deba@2335
    94
      /// \brief Invalid constructor
deba@2335
    95
      EdgeIt(Invalid) : path(0), idx(-1) {}
deba@2335
    96
      /// \brief Initializate the constructor to the first edge of path
deba@2335
    97
      EdgeIt(const Path &_path) 
deba@2335
    98
        : path(&_path), idx(_path.empty() ? -1 : 0) {}
hegyi@819
    99
deba@2335
   100
    private:
hegyi@819
   101
deba@2335
   102
      EdgeIt(const Path &_path, int _idx) 
deba@2335
   103
        : idx(_idx), path(&_path) {}
deba@2247
   104
deba@2335
   105
    public:
deba@2335
   106
deba@2335
   107
      /// \brief Conversion to Edge
deba@2335
   108
      operator const Edge&() const {
deba@2335
   109
        return path->nth(idx);
deba@2247
   110
      }
deba@2247
   111
deba@2335
   112
      /// \brief Next edge
deba@2335
   113
      EdgeIt& operator++() { 
deba@2335
   114
        ++idx;
deba@2335
   115
        if (idx >= path->length()) idx = -1; 
deba@2247
   116
        return *this; 
deba@2247
   117
      }
deba@2247
   118
deba@2247
   119
      /// \brief Comparison operator
deba@2335
   120
      bool operator==(const EdgeIt& e) const { return idx==e.idx; }
deba@2247
   121
      /// \brief Comparison operator
deba@2335
   122
      bool operator!=(const EdgeIt& e) const { return idx!=e.idx; }
deba@2247
   123
      /// \brief Comparison operator
deba@2335
   124
      bool operator<(const EdgeIt& e) const { return idx<e.idx; }
deba@2247
   125
deba@2247
   126
    private:
deba@2247
   127
      const Path *path;
deba@2335
   128
      int idx;
deba@2247
   129
    };
deba@2247
   130
deba@2335
   131
    /// \brief Length of the path.
deba@2335
   132
    int length() const { return head.size() + tail.size(); }
deba@2335
   133
    /// \brief Returns whether the path is empty.
deba@2335
   134
    bool empty() const { return head.empty() && tail.empty(); }
deba@2335
   135
deba@2335
   136
    /// \brief Resets the path to an empty path.
deba@2335
   137
    void clear() { head.clear(); tail.clear(); }
deba@2335
   138
deba@2335
   139
    /// \brief Gives back the nth edge.
deba@2335
   140
    ///
deba@2335
   141
    /// \pre n is in the [0..length() - 1] range
deba@2335
   142
    const Edge& nth(int n) const {
deba@2335
   143
      return n < (int)head.size() ? *(head.rbegin() + n) :
deba@2335
   144
        *(tail.begin() + (n - head.size()));
deba@2335
   145
    }
deba@2335
   146
deba@2335
   147
    /// \brief Initializes edge iterator to point to the nth edge
deba@2335
   148
    ///
deba@2335
   149
    /// \pre n is in the [0..length() - 1] range
deba@2335
   150
    EdgeIt nthIt(int n) const {
deba@2335
   151
      return EdgeIt(*this, n);
deba@2335
   152
    }
deba@2335
   153
deba@2335
   154
    /// \brief Gives back the first edge of the path
deba@2335
   155
    const Edge& front() const {
deba@2335
   156
      return head.empty() ? tail.front() : head.back();
deba@2335
   157
    }
deba@2335
   158
deba@2335
   159
    /// \brief Add a new edge before the current path
deba@2335
   160
    void addFront(const Edge& edge) {
deba@2335
   161
      head.push_back(edge);
deba@2335
   162
    }
deba@2335
   163
deba@2335
   164
    /// \brief Erase the first edge of the path
deba@2335
   165
    void eraseFront() {
deba@2335
   166
      if (!head.empty()) {
deba@2335
   167
        head.pop_back();
deba@2335
   168
      } else {
deba@2335
   169
        head.clear();
deba@2335
   170
        int halfsize = tail.size() / 2;
deba@2335
   171
        head.resize(halfsize);
deba@2335
   172
        std::copy(tail.begin() + 1, tail.begin() + halfsize + 1,
deba@2335
   173
                  head.rbegin());
deba@2335
   174
        std::copy(tail.begin() + halfsize + 1, tail.end(), tail.begin());
deba@2335
   175
        tail.resize(tail.size() - halfsize - 1);
deba@2335
   176
      }
deba@2335
   177
    }
deba@2335
   178
deba@2335
   179
    /// \brief Gives back the last edge of the path
deba@2335
   180
    const Edge& back() const {
deba@2335
   181
      return tail.empty() ? head.front() : tail.back();
deba@2335
   182
    }
deba@2335
   183
deba@2335
   184
    /// \brief Add a new edge behind the current path
deba@2335
   185
    void addBack(const Edge& edge) {
deba@2335
   186
      tail.push_back(edge);
deba@2335
   187
    }
deba@2335
   188
deba@2335
   189
    /// \brief Erase the last edge of the path
deba@2335
   190
    void eraseBack() {
deba@2335
   191
      if (!tail.empty()) {
deba@2335
   192
        tail.pop_back();
deba@2335
   193
      } else {
deba@2335
   194
        int halfsize = head.size() / 2;
deba@2335
   195
        tail.resize(halfsize);
deba@2335
   196
        std::copy(head.begin() + 1, head.begin() + halfsize + 1,
deba@2335
   197
                  tail.rbegin());
deba@2335
   198
        std::copy(head.begin() + halfsize + 1, head.end(), head.begin());
deba@2335
   199
        head.resize(head.size() - halfsize - 1);
deba@2335
   200
      }
deba@2335
   201
    }
deba@2335
   202
deba@2335
   203
deba@2335
   204
deba@2335
   205
    typedef True BuildTag;
deba@2335
   206
deba@2335
   207
    template <typename CPath>
deba@2335
   208
    void build(const CPath& path) {
deba@2335
   209
      int len = path.length();
deba@2335
   210
      tail.reserve(len);
deba@2335
   211
      for (typename CPath::EdgeIt it(path); it != INVALID; ++it) {
deba@2335
   212
        tail.push_back(it);
deba@2335
   213
      }
deba@2335
   214
    }
deba@2335
   215
deba@2335
   216
    template <typename CPath>
deba@2335
   217
    void buildRev(const CPath& path) {
deba@2335
   218
      int len = path.length();
deba@2335
   219
      head.reserve(len);
deba@2335
   220
      for (typename CPath::RevIt it(path); it != INVALID; ++it) {
deba@2335
   221
        head.push_back(it);
deba@2335
   222
      }
deba@2335
   223
    }
deba@2335
   224
deba@2335
   225
  protected:
deba@2335
   226
    typedef std::vector<Edge> Container;
deba@2335
   227
    Container head, tail;
deba@2335
   228
deba@2335
   229
  };
deba@2335
   230
deba@2335
   231
  /// \brief A structure for representing directed paths in a graph.
deba@2335
   232
  ///
deba@2335
   233
  /// A structure for representing directed path in a graph.
deba@2335
   234
  /// \param Graph The graph type in which the path is.
deba@2335
   235
  ///
deba@2335
   236
  /// In a sense, the path can be treated as a list of edges. The
deba@2335
   237
  /// lemon path type stores just this list. As a consequence it
deba@2335
   238
  /// cannot enumerate the nodes in the path and the zero length paths
deba@2335
   239
  /// cannot store the source.
deba@2335
   240
  ///
deba@2335
   241
  /// This implementation is a just back insertable and erasable path
deba@2335
   242
  /// type. It can be indexed in O(1) time. The back insertion and
deba@2335
   243
  /// erasure is amortized O(1) time. This implementation is faster
deba@2335
   244
  /// then the \c Path type because it use just one vector for the
deba@2335
   245
  /// edges.
deba@2335
   246
  template <typename _Graph>
deba@2335
   247
  class SimplePath {
deba@2335
   248
  public:
deba@2335
   249
deba@2335
   250
    typedef _Graph Graph;
deba@2335
   251
    typedef typename Graph::Edge Edge;
deba@2335
   252
deba@2335
   253
    /// \brief Default constructor
deba@2335
   254
    ///
deba@2335
   255
    /// Default constructor
deba@2335
   256
    SimplePath() {}
deba@2335
   257
deba@2335
   258
    /// \brief Template copy constructor
deba@2335
   259
    ///
deba@2335
   260
    /// This path can be initialized with any other path type. It just
deba@2335
   261
    /// makes a copy of the given path.
deba@2335
   262
    template <typename CPath>
deba@2335
   263
    SimplePath(const CPath& cpath) {
deba@2335
   264
      copyPath(*this, cpath);
deba@2335
   265
    }
deba@2335
   266
deba@2335
   267
    /// \brief Template copy assignment
deba@2335
   268
    ///
deba@2335
   269
    /// This path can be initialized with any other path type. It just
deba@2335
   270
    /// makes a copy of the given path.
deba@2335
   271
    template <typename CPath>
deba@2335
   272
    SimplePath& operator=(const CPath& cpath) {
deba@2335
   273
      copyPath(*this, cpath);
deba@2335
   274
      return *this;
deba@2335
   275
    }
deba@2335
   276
deba@2247
   277
    /// \brief Iterator class to iterate on the edges of the paths
deba@2247
   278
    ///
deba@2247
   279
    /// This class is used to iterate on the edges of the paths
deba@2335
   280
    ///
deba@2247
   281
    /// Of course it converts to Graph::Edge
hegyi@819
   282
    class EdgeIt {
deba@2335
   283
      friend class SimplePath;
deba@2335
   284
    public:
deba@2335
   285
      /// Default constructor
deba@2335
   286
      EdgeIt() {}
deba@2335
   287
      /// Invalid constructor
deba@2335
   288
      EdgeIt(Invalid) : path(0), idx(-1) {}
deba@2335
   289
      /// \brief Initializate the constructor to the first edge of path
deba@2335
   290
      EdgeIt(const SimplePath &_path) 
deba@2335
   291
        : path(&_path), idx(_path.empty() ? -1 : 0) {}
deba@2335
   292
deba@2335
   293
    private:
deba@2335
   294
deba@2335
   295
      /// Constructor with starting point
deba@2335
   296
      EdgeIt(const SimplePath &_path, int _idx) 
deba@2335
   297
        : idx(_idx), path(&_path) {}
deba@2335
   298
deba@2247
   299
    public:
hegyi@819
   300
deba@2335
   301
      ///Conversion to Graph::Edge
deba@2335
   302
      operator const Edge&() const {
deba@2335
   303
        return path->nth(idx);
hegyi@819
   304
      }
hegyi@819
   305
deba@2335
   306
      /// Next edge
deba@2247
   307
      EdgeIt& operator++() { 
deba@2335
   308
        ++idx;
deba@2335
   309
        if (idx >= path->length()) idx = -1; 
deba@2247
   310
        return *this; 
deba@2247
   311
      }
deba@2247
   312
hegyi@819
   313
      /// Comparison operator
deba@2335
   314
      bool operator==(const EdgeIt& e) const { return idx==e.idx; }
deba@2335
   315
      /// Comparison operator
deba@2335
   316
      bool operator!=(const EdgeIt& e) const { return idx!=e.idx; }
deba@2335
   317
      /// Comparison operator
deba@2335
   318
      bool operator<(const EdgeIt& e) const { return idx<e.idx; }
deba@2247
   319
deba@2335
   320
    private:
deba@2335
   321
      const SimplePath *path;
deba@2335
   322
      int idx;
deba@2335
   323
    };
deba@2335
   324
deba@2335
   325
    /// \brief Length of the path.
deba@2335
   326
    int length() const { return data.size(); }
deba@2335
   327
    /// \brief Returns whether the path is empty.
deba@2335
   328
    bool empty() const { return data.empty(); }
deba@2335
   329
deba@2335
   330
    /// \brief Resets the path to an empty path.
deba@2335
   331
    void clear() { data.clear(); }
deba@2335
   332
deba@2335
   333
    /// \brief Gives back the nth edge.
deba@2335
   334
    ///
deba@2335
   335
    /// \pre n is in the [0..length() - 1] range
deba@2335
   336
    const Edge& nth(int n) const {
deba@2335
   337
      return data[n];
deba@2335
   338
    }
deba@2335
   339
deba@2335
   340
    /// \brief  Initializes edge iterator to point to the nth edge.
deba@2335
   341
    EdgeIt nthIt(int n) const {
deba@2335
   342
      return EdgeIt(*this, n);
deba@2335
   343
    }
deba@2335
   344
deba@2335
   345
    /// \brief Gives back the last edge of the path.
deba@2335
   346
    const Edge& back() const {
deba@2335
   347
      return data.back();
deba@2335
   348
    }
deba@2335
   349
deba@2335
   350
    /// \brief Add a new edge behind the current path.
deba@2335
   351
    void addBack(const Edge& edge) {
deba@2335
   352
      data.push_back(edge);
deba@2335
   353
    }
deba@2335
   354
deba@2335
   355
    /// \brief Erase the last edge of the path
deba@2335
   356
    void eraseBack() {
deba@2335
   357
      data.pop_back();
deba@2335
   358
    }
deba@2335
   359
deba@2335
   360
    typedef True BuildTag;
deba@2335
   361
deba@2335
   362
    template <typename CPath>
deba@2335
   363
    void build(const CPath& path) {
deba@2335
   364
      int len = path.length();
deba@2335
   365
      data.resize(len);
deba@2335
   366
      int index = 0;
deba@2335
   367
      for (typename CPath::EdgeIt it(path); it != INVALID; ++it) {
deba@2335
   368
        data[index] = it;;
deba@2335
   369
        ++index;
deba@2335
   370
      }
deba@2335
   371
    }
deba@2335
   372
deba@2335
   373
    template <typename CPath>
deba@2335
   374
    void buildRev(const CPath& path) {
deba@2335
   375
      int len = path.length();
deba@2335
   376
      data.resize(len);
deba@2335
   377
      int index = len;
deba@2335
   378
      for (typename CPath::RevIt it(path); it != INVALID; ++it) {
deba@2335
   379
        --index;
deba@2335
   380
        data[index] = it;;
deba@2335
   381
      }
deba@2335
   382
    }
deba@2335
   383
deba@2335
   384
  protected:
deba@2335
   385
    typedef std::vector<Edge> Container;
deba@2335
   386
    Container data;
deba@2335
   387
deba@2335
   388
  };
deba@2335
   389
deba@2335
   390
  /// \brief A structure for representing directed paths in a graph.
deba@2335
   391
  ///
deba@2335
   392
  /// A structure for representing directed path in a graph.
deba@2335
   393
  /// \param Graph The graph type in which the path is.
deba@2335
   394
  ///
deba@2335
   395
  /// In a sense, the path can be treated as a list of edges. The
deba@2335
   396
  /// lemon path type stores just this list. As a consequence it
deba@2335
   397
  /// cannot enumerate the nodes in the path and the zero length paths
deba@2335
   398
  /// cannot store the source.
deba@2335
   399
  ///
deba@2335
   400
  /// This implementation is a back and front insertable and erasable
deba@2335
   401
  /// path type. It can be indexed in O(k) time, where k is the rank
deba@2335
   402
  /// of the edge in the path. The length can be computed in O(n)
deba@2335
   403
  /// time. The front and back insertion and erasure is O(1) time
deba@2335
   404
  /// and it can be splited and spliced in O(1) time.
deba@2335
   405
  template <typename _Graph>
deba@2335
   406
  class ListPath {
deba@2335
   407
  public:
deba@2335
   408
deba@2335
   409
    typedef _Graph Graph;
deba@2335
   410
    typedef typename Graph::Edge Edge;
deba@2335
   411
deba@2335
   412
  protected:
deba@2335
   413
deba@2335
   414
    // the std::list<> is incompatible 
deba@2335
   415
    // hard to create invalid iterator
deba@2335
   416
    struct Node {
deba@2335
   417
      Edge edge;
deba@2335
   418
      Node *next, *prev;
deba@2335
   419
    };
deba@2335
   420
deba@2335
   421
    Node *first, *last;
deba@2335
   422
deba@2335
   423
    std::allocator<Node> alloc;
deba@2335
   424
deba@2335
   425
  public:
deba@2335
   426
 
deba@2335
   427
    /// \brief Default constructor
deba@2335
   428
    ///
deba@2335
   429
    /// Default constructor
deba@2335
   430
    ListPath() : first(0), last(0) {}
deba@2335
   431
deba@2335
   432
    /// \brief Template copy constructor
deba@2335
   433
    ///
deba@2335
   434
    /// This path can be initialized with any other path type. It just
deba@2335
   435
    /// makes a copy of the given path.
deba@2335
   436
    template <typename CPath>
deba@2335
   437
    ListPath(const CPath& cpath) : first(0), last(0) {
deba@2335
   438
      copyPath(*this, cpath);
deba@2335
   439
    }
deba@2335
   440
deba@2335
   441
    /// \brief Destructor of the path
deba@2335
   442
    ///
deba@2335
   443
    /// Destructor of the path
deba@2335
   444
    ~ListPath() {
deba@2335
   445
      clear();
deba@2335
   446
    }
deba@2335
   447
deba@2335
   448
    /// \brief Template copy assignment
deba@2335
   449
    ///
deba@2335
   450
    /// This path can be initialized with any other path type. It just
deba@2335
   451
    /// makes a copy of the given path.
deba@2335
   452
    template <typename CPath>
deba@2335
   453
    ListPath& operator=(const CPath& cpath) {
deba@2335
   454
      copyPath(*this, cpath);
deba@2335
   455
      return *this;
deba@2335
   456
    }
deba@2335
   457
deba@2335
   458
    /// \brief Iterator class to iterate on the edges of the paths
deba@2335
   459
    ///
deba@2335
   460
    /// This class is used to iterate on the edges of the paths
deba@2335
   461
    ///
deba@2335
   462
    /// Of course it converts to Graph::Edge
deba@2335
   463
    class EdgeIt {
deba@2335
   464
      friend class ListPath;
deba@2335
   465
    public:
deba@2335
   466
      /// Default constructor
deba@2335
   467
      EdgeIt() {}
deba@2335
   468
      /// Invalid constructor
deba@2335
   469
      EdgeIt(Invalid) : path(0), node(0) {}
deba@2335
   470
      /// \brief Initializate the constructor to the first edge of path
deba@2335
   471
      EdgeIt(const ListPath &_path) 
deba@2335
   472
        : path(&_path), node(_path.first) {}
deba@2335
   473
deba@2335
   474
    protected:
deba@2335
   475
deba@2335
   476
      EdgeIt(const ListPath &_path, Node *_node) 
deba@2335
   477
        : path(&_path), node(_node) {}
deba@2335
   478
deba@2335
   479
deba@2335
   480
    public:
deba@2335
   481
deba@2335
   482
      ///Conversion to Graph::Edge
deba@2335
   483
      operator const Edge&() const {
deba@2335
   484
        return node->edge;
deba@2335
   485
      }
deba@2335
   486
deba@2335
   487
      /// Next edge
deba@2335
   488
      EdgeIt& operator++() { 
deba@2335
   489
        node = node->next;
deba@2335
   490
        return *this; 
deba@2335
   491
      }
deba@2335
   492
hegyi@819
   493
      /// Comparison operator
deba@2335
   494
      bool operator==(const EdgeIt& e) const { return node==e.node; }
deba@2335
   495
      /// Comparison operator
deba@2335
   496
      bool operator!=(const EdgeIt& e) const { return node!=e.node; }
deba@2335
   497
      /// Comparison operator
deba@2335
   498
      bool operator<(const EdgeIt& e) const { return node<e.node; }
deba@2247
   499
deba@2335
   500
    private:
deba@2335
   501
      const ListPath *path;
deba@2335
   502
      Node *node;
deba@2335
   503
    };
deba@2335
   504
deba@2335
   505
    /// \brief Gives back the nth edge.
deba@2335
   506
    ///
deba@2335
   507
    /// Gives back the nth edge in O(n) time.
deba@2335
   508
    /// \pre n is in the [0..length() - 1] range
deba@2335
   509
    const Edge& nth(int n) const {
deba@2335
   510
      Node *node = first;
deba@2335
   511
      for (int i = 0; i < n; ++i) {
deba@2335
   512
        node = node->next;
deba@2335
   513
      }
deba@2335
   514
      return node->edge;
deba@2335
   515
    }
deba@2335
   516
deba@2335
   517
    /// \brief Initializes edge iterator to point to the nth edge.
deba@2335
   518
    EdgeIt nthIt(int n) const {
deba@2335
   519
      Node *node = first;
deba@2335
   520
      for (int i = 0; i < n; ++i) {
deba@2335
   521
        node = node->next;
deba@2335
   522
      }
deba@2335
   523
      return EdgeIt(*this, node);
deba@2335
   524
    }
deba@2335
   525
deba@2335
   526
    /// \brief Length of the path.
deba@2335
   527
    int length() const {
deba@2335
   528
      int len = 0;
deba@2335
   529
      Node *node = first;
deba@2335
   530
      while (node != 0) {
deba@2335
   531
        node = node->next;
deba@2335
   532
        ++len;
deba@2335
   533
      }
deba@2335
   534
      return len;
deba@2335
   535
    }
deba@2335
   536
deba@2335
   537
    /// \brief Returns whether the path is empty.
deba@2335
   538
    bool empty() const { return first == 0; }
deba@2335
   539
deba@2335
   540
    /// \brief Resets the path to an empty path.
deba@2335
   541
    void clear() {
deba@2335
   542
      while (first != 0) {
deba@2335
   543
        last = first->next;
deba@2335
   544
        alloc.destroy(first);
deba@2335
   545
        alloc.deallocate(first, 1);
deba@2335
   546
        first = last;
deba@2335
   547
      }
deba@2335
   548
    }
deba@2335
   549
deba@2335
   550
    /// \brief Gives back the first edge of the path
deba@2335
   551
    const Edge& front() const {
deba@2335
   552
      return first->edge;
deba@2335
   553
    }
deba@2335
   554
deba@2335
   555
    /// \brief Add a new edge before the current path
deba@2335
   556
    void addFront(const Edge& edge) {
deba@2335
   557
      Node *node = alloc.allocate(1);
deba@2335
   558
      alloc.construct(node, Node());
deba@2335
   559
      node->prev = 0;
deba@2335
   560
      node->next = first;
deba@2335
   561
      node->edge = edge;
deba@2335
   562
      if (first) {
deba@2335
   563
        first->prev = node;
deba@2335
   564
        first = node;
deba@2335
   565
      } else {
deba@2335
   566
        first = last = node;
deba@2335
   567
      }
deba@2335
   568
    }
deba@2335
   569
deba@2335
   570
    /// \brief Erase the first edge of the path
deba@2335
   571
    void eraseFront() {
deba@2335
   572
      Node *node = first;
deba@2335
   573
      first = first->next;
deba@2335
   574
      if (first) {
deba@2335
   575
        first->prev = 0;
deba@2335
   576
      } else {
deba@2335
   577
        last = 0;
deba@2335
   578
      }
deba@2335
   579
      alloc.destroy(node);
deba@2335
   580
      alloc.deallocate(node, 1);
deba@2335
   581
    }
deba@2335
   582
deba@2335
   583
    /// \brief Gives back the last edge of the path.
deba@2335
   584
    const Edge& back() const {
deba@2335
   585
      return last->edge;
deba@2335
   586
    }
deba@2335
   587
deba@2335
   588
    /// \brief Add a new edge behind the current path.
deba@2335
   589
    void addBack(const Edge& edge) {
deba@2335
   590
      Node *node = alloc.allocate(1);
deba@2335
   591
      alloc.construct(node, Node());
deba@2335
   592
      node->next = 0;
deba@2335
   593
      node->prev = last;
deba@2335
   594
      node->edge = edge;
deba@2335
   595
      if (last) {
deba@2335
   596
        last->next = node;
deba@2335
   597
        last = node;
deba@2335
   598
      } else {
deba@2335
   599
        last = first = node;
deba@2335
   600
      }
deba@2335
   601
    }
deba@2335
   602
deba@2335
   603
    /// \brief Erase the last edge of the path
deba@2335
   604
    void eraseBack() {
deba@2335
   605
      Node *node = last;
deba@2335
   606
      last = last->prev;
deba@2335
   607
      if (last) {
deba@2335
   608
        last->next = 0;
deba@2335
   609
      } else {
deba@2335
   610
        first = 0;
deba@2335
   611
      }
deba@2335
   612
      alloc.destroy(node);
deba@2335
   613
      alloc.deallocate(node, 1);
deba@2335
   614
    }
deba@2335
   615
deba@2335
   616
    /// \brief Splicing the given path to the current path.
deba@2335
   617
    ///
deba@2335
   618
    /// It splices the \c tpath to the back of the current path and \c
deba@2335
   619
    /// tpath becomes empty. The time complexity of this function is
deba@2335
   620
    /// O(1).
deba@2335
   621
    void spliceBack(ListPath& tpath) {
deba@2335
   622
      if (first) {
deba@2335
   623
        if (tpath.first) {
deba@2335
   624
          last->next = tpath.first;
deba@2335
   625
          tpath.first->prev = last;
deba@2335
   626
          last = tpath.last;
deba@2335
   627
        }
deba@2335
   628
      } else {
deba@2335
   629
        first = tpath.first;
deba@2335
   630
        last = tpath.last;
deba@2335
   631
      }
deba@2335
   632
      tpath.first = tpath.last = 0;
deba@2335
   633
    }
deba@2335
   634
deba@2335
   635
    /// \brief Splicing the given path to the current path.
deba@2335
   636
    ///
deba@2335
   637
    /// It splices the \c tpath before the current path and \c tpath
deba@2335
   638
    /// becomes empty. The time complexity of this function
deba@2335
   639
    /// is O(1).
deba@2335
   640
    void spliceFront(ListPath& tpath) {
deba@2335
   641
      if (first) {
deba@2335
   642
        if (tpath.first) {
deba@2335
   643
          first->prev = tpath.last;
deba@2335
   644
          tpath.last->next = first;
deba@2335
   645
          first = tpath.first;
deba@2335
   646
        }
deba@2335
   647
      } else {
deba@2335
   648
        first = tpath.first;
deba@2335
   649
        last = tpath.last;
deba@2335
   650
      }
deba@2335
   651
      tpath.first = tpath.last = 0;
deba@2335
   652
    }
deba@2335
   653
deba@2335
   654
    /// \brief Splicing the given path into the current path.
deba@2335
   655
    ///
deba@2335
   656
    /// It splices the \c tpath into the current path before the
deba@2335
   657
    /// position of \c it iterator and \c tpath becomes empty. The
deba@2335
   658
    /// time complexity of this function is O(1). If the \c it is \c
deba@2335
   659
    /// INVALID then it will splice behind the current path.
deba@2335
   660
    void splice(EdgeIt it, ListPath& tpath) {
deba@2335
   661
      if (it.node) {
deba@2335
   662
        if (tpath.first) {
deba@2335
   663
          tpath.first->prev = it.node->prev;
deba@2335
   664
          if (it.node->prev) {
deba@2335
   665
            it.node->prev->next = tpath.first;
deba@2335
   666
          } else {
deba@2335
   667
            first = tpath.first;
deba@2335
   668
          }
deba@2335
   669
          it.node->prev = tpath.last;
deba@2335
   670
          tpath.last->next = it.node;
deba@2335
   671
        }
deba@2335
   672
      } else {
deba@2335
   673
        if (first) {
deba@2335
   674
          if (tpath.first) {
deba@2335
   675
            last->next = tpath.first;
deba@2335
   676
            tpath.first->prev = last;
deba@2335
   677
            last = tpath.last;
deba@2335
   678
          }
deba@2335
   679
        } else {
deba@2335
   680
          first = tpath.first;
deba@2335
   681
          last = tpath.last;
deba@2335
   682
        }
deba@2335
   683
      }
deba@2335
   684
      tpath.first = tpath.last = 0;
deba@2335
   685
    }
deba@2335
   686
deba@2335
   687
    /// \brief Spliting the current path.
deba@2335
   688
    ///
deba@2335
   689
    /// It splits the current path into two parts. The part before \c
deba@2335
   690
    /// it iterator will remain in the current path and the part from
deba@2335
   691
    /// the it will put into the \c tpath. If the \c tpath had edges
deba@2335
   692
    /// before the operation they will be removed first.  The time
deba@2335
   693
    /// complexity of this function is O(1) plus the clearing of \c
deba@2335
   694
    /// tpath. If the \c it is \c INVALID then it just clears \c
deba@2335
   695
    /// tpath.
deba@2335
   696
    void split(EdgeIt it, ListPath& tpath) {
deba@2335
   697
      tpath.clear();
deba@2335
   698
      if (it.node) {
deba@2335
   699
        tpath.first = it.node;
deba@2335
   700
        tpath.last = last;
deba@2335
   701
        if (it.node->prev) {
deba@2335
   702
          last = it.node->prev;
deba@2335
   703
          last->next = 0;
deba@2335
   704
        } else {
deba@2335
   705
          first = last = 0;
deba@2335
   706
        }
deba@2335
   707
        it.node->prev = 0;
deba@2335
   708
      }
deba@2335
   709
    }
deba@2335
   710
deba@2335
   711
deba@2335
   712
    typedef True BuildTag;
deba@2335
   713
deba@2335
   714
    template <typename CPath>
deba@2335
   715
    void build(const CPath& path) {
deba@2335
   716
      for (typename CPath::EdgeIt it(path); it != INVALID; ++it) {
deba@2335
   717
        addBack(it);
deba@2335
   718
      }
deba@2335
   719
    }
deba@2335
   720
deba@2335
   721
    template <typename CPath>
deba@2335
   722
    void buildRev(const CPath& path) {
deba@2335
   723
      for (typename CPath::RevIt it(path); it != INVALID; ++it) {
deba@2335
   724
        addFront(it);
deba@2335
   725
      }
deba@2335
   726
    }
deba@2335
   727
deba@2335
   728
  };
deba@2335
   729
deba@2335
   730
  /// \brief A structure for representing directed paths in a graph.
deba@2335
   731
  ///
deba@2335
   732
  /// A structure for representing directed path in a graph.
deba@2335
   733
  /// \param Graph The graph type in which the path is.
deba@2335
   734
  ///
deba@2335
   735
  /// In a sense, the path can be treated as a list of edges. The
deba@2335
   736
  /// lemon path type stores just this list. As a consequence it
deba@2335
   737
  /// cannot enumerate the nodes in the path and the zero length paths
deba@2335
   738
  /// cannot store the source.
deba@2335
   739
  ///
deba@2335
   740
  /// This implementation is completly static, so it cannot be
deba@2335
   741
  /// modified exclude the assign an other path. It is intented to be
athos@2336
   742
  /// used when you want to store a large number of paths because it is
deba@2335
   743
  /// the most memory efficient path type in the lemon.
deba@2335
   744
  template <typename _Graph>
deba@2335
   745
  class StaticPath {
deba@2335
   746
  public:
deba@2335
   747
deba@2335
   748
    typedef _Graph Graph;
deba@2335
   749
    typedef typename Graph::Edge Edge;
deba@2335
   750
deba@2335
   751
    /// \brief Default constructor
deba@2335
   752
    ///
deba@2335
   753
    /// Default constructor
deba@2335
   754
    StaticPath() : len(0), edges(0) {}
deba@2335
   755
    
deba@2335
   756
    /// \brief Template copy constructor
deba@2335
   757
    ///
deba@2335
   758
    /// This path can be initialized with any other path type. It just
deba@2335
   759
    /// makes a copy of the given path.
deba@2335
   760
    template <typename CPath>
deba@2335
   761
    StaticPath(const CPath& cpath) : edges(0) {
deba@2335
   762
      copyPath(*this, cpath);
deba@2335
   763
    }
deba@2335
   764
deba@2335
   765
    /// \brief Destructor of the path
deba@2335
   766
    ///
deba@2335
   767
    /// Destructor of the path
deba@2335
   768
    ~StaticPath() {
deba@2335
   769
      if (edges) delete[] edges;
deba@2335
   770
    }
deba@2335
   771
deba@2335
   772
    /// \brief Template copy assignment
deba@2335
   773
    ///
deba@2335
   774
    /// This path can be initialized with any other path type. It just
deba@2335
   775
    /// makes a copy of the given path.
deba@2335
   776
    template <typename CPath>
deba@2335
   777
    StaticPath& operator=(const CPath& cpath) {
deba@2335
   778
      copyPath(*this, cpath);
deba@2335
   779
      return *this;
deba@2335
   780
    }
deba@2335
   781
deba@2335
   782
    /// \brief Iterator class to iterate on the edges of the paths
deba@2335
   783
    ///
deba@2335
   784
    /// This class is used to iterate on the edges of the paths
deba@2335
   785
    ///
deba@2335
   786
    /// Of course it converts to Graph::Edge
deba@2335
   787
    class EdgeIt {
deba@2335
   788
      friend class StaticPath;
deba@2335
   789
    public:
deba@2335
   790
      /// Default constructor
deba@2335
   791
      EdgeIt() {}
deba@2335
   792
      /// Invalid constructor
deba@2335
   793
      EdgeIt(Invalid) : path(0), idx(-1) {}
deba@2335
   794
      /// Initializate the constructor to the first edge of path
deba@2335
   795
      EdgeIt(const StaticPath &_path) 
deba@2335
   796
        : path(&_path), idx(_path.empty() ? -1 : 0) {}
hegyi@819
   797
hegyi@819
   798
    private:
deba@2247
   799
deba@2335
   800
      /// Constructor with starting point
deba@2335
   801
      EdgeIt(const StaticPath &_path, int _idx) 
deba@2335
   802
        : idx(_idx), path(&_path) {}
deba@2335
   803
deba@2335
   804
    public:
deba@2335
   805
deba@2335
   806
      ///Conversion to Graph::Edge
deba@2335
   807
      operator const Edge&() const {
deba@2335
   808
        return path->nth(idx);
deba@2335
   809
      }
deba@2335
   810
deba@2335
   811
      /// Next edge
deba@2335
   812
      EdgeIt& operator++() { 
deba@2335
   813
        ++idx;
deba@2335
   814
        if (idx >= path->length()) idx = -1; 
deba@2335
   815
        return *this; 
deba@2335
   816
      }
deba@2335
   817
deba@2335
   818
      /// Comparison operator
deba@2335
   819
      bool operator==(const EdgeIt& e) const { return idx==e.idx; }
deba@2335
   820
      /// Comparison operator
deba@2335
   821
      bool operator!=(const EdgeIt& e) const { return idx!=e.idx; }
deba@2335
   822
      /// Comparison operator
deba@2335
   823
      bool operator<(const EdgeIt& e) const { return idx<e.idx; }
deba@2335
   824
deba@2335
   825
    private:
deba@2335
   826
      const StaticPath *path;
deba@2335
   827
      int idx;
hegyi@819
   828
    };
hegyi@819
   829
deba@2335
   830
    /// \brief Gives back the nth edge.
deba@2335
   831
    ///
deba@2335
   832
    /// \pre n is in the [0..length() - 1] range
deba@2335
   833
    const Edge& nth(int n) const {
deba@2335
   834
      return edges[n];
deba@2335
   835
    }
hegyi@819
   836
deba@2335
   837
    /// \brief Initializes edge iterator to point to the nth edge.
deba@2335
   838
    EdgeIt nthIt(int n) const {
deba@2335
   839
      return EdgeIt(*this, n);
deba@2335
   840
    }
hegyi@819
   841
deba@2335
   842
    /// \brief Gives back the length of the path.
deba@2335
   843
    int length() const { return len; }
hegyi@819
   844
deba@2335
   845
    /// \brief Returns true when the path is empty.
deba@2335
   846
    int empty() const { return len == 0; }
hegyi@819
   847
deba@2335
   848
    /// \break Erase all edge in the graph.
deba@2335
   849
    void clear() {
deba@2335
   850
      len = 0;
deba@2335
   851
      if (edges) delete[] edges;
deba@2335
   852
      edges = 0;
deba@2335
   853
    }
hegyi@819
   854
deba@2335
   855
    /// \brief Gives back the first edge of the path.
deba@2335
   856
    const Edge& front() const {
deba@2335
   857
      return edges[0];
deba@2335
   858
    }
hegyi@819
   859
deba@2335
   860
    /// \brief Gives back the last edge of the path.
deba@2335
   861
    const Edge& back() const {
deba@2335
   862
      return edges[len - 1];
deba@2335
   863
    }
deba@2335
   864
deba@2335
   865
deba@2335
   866
    typedef True BuildTag;
deba@2335
   867
deba@2335
   868
    template <typename CPath>
deba@2335
   869
    void build(const CPath& path) {
deba@2335
   870
      len = path.length();
deba@2335
   871
      edges = new Edge[len];
deba@2335
   872
      int index = 0;
deba@2335
   873
      for (typename CPath::EdgeIt it(path); it != INVALID; ++it) {
deba@2335
   874
        edges[index] = it;
deba@2335
   875
        ++index;
deba@2247
   876
      }
deba@2335
   877
    }
hegyi@819
   878
deba@2335
   879
    template <typename CPath>
deba@2335
   880
    void buildRev(const CPath& path) {
deba@2335
   881
      len = path.length();
deba@2335
   882
      edges = new Edge[len];
deba@2335
   883
      int index = len;
deba@2335
   884
      for (typename CPath::RevIt it(path); it != INVALID; ++it) {
deba@2335
   885
        --index;
deba@2335
   886
        edges[index] = it;
deba@2247
   887
      }
deba@2335
   888
    }
hegyi@837
   889
deba@2335
   890
  private:
deba@2335
   891
    int len;
deba@2335
   892
    Edge* edges;
hegyi@819
   893
  };
hegyi@819
   894
hegyi@819
   895
  ///@}
hegyi@819
   896
alpar@921
   897
} // namespace lemon
hegyi@819
   898
alpar@921
   899
#endif // LEMON_PATH_H