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