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