lemon/path.h
author alpar
Tue, 21 Feb 2006 12:37:00 +0000
changeset 1977 8ef02f0c4245
parent 1909 2d806130e700
child 1993 2115143eceea
permissions -rw-r--r--
RefPtr: a reference counted pointer class
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
@defgroup paths Path Structures
hegyi@819
    21
@ingroup datas
alpar@921
    22
\brief Path structures implemented in LEMON.
hegyi@819
    23
alpar@921
    24
LEMON provides flexible data structures
hegyi@819
    25
to work with paths.
hegyi@819
    26
hegyi@819
    27
All of them have the same interface, especially they can be built or extended
hegyi@819
    28
using a standard Builder subclass. This make is easy to have e.g. the Dijkstra
hegyi@819
    29
algorithm to store its result in any kind of path structure.
hegyi@819
    30
klao@959
    31
\sa lemon::concept::Path
hegyi@819
    32
hegyi@819
    33
*/
hegyi@819
    34
hegyi@819
    35
///\ingroup paths
hegyi@819
    36
///\file
hegyi@819
    37
///\brief Classes for representing paths in graphs.
alpar@1151
    38
///
alpar@1151
    39
///\todo Iterators have obsolete style
hegyi@819
    40
alpar@921
    41
#ifndef LEMON_PATH_H
alpar@921
    42
#define LEMON_PATH_H
hegyi@819
    43
hegyi@819
    44
#include <deque>
hegyi@819
    45
#include <vector>
hegyi@819
    46
#include <algorithm>
hegyi@819
    47
alpar@921
    48
#include <lemon/invalid.h>
hegyi@819
    49
alpar@921
    50
namespace lemon {
hegyi@819
    51
hegyi@819
    52
  /// \addtogroup paths
hegyi@819
    53
  /// @{
hegyi@819
    54
hegyi@819
    55
hegyi@819
    56
  //! \brief A structure for representing directed paths in a graph.
hegyi@819
    57
  //!
hegyi@819
    58
  //! A structure for representing directed path in a graph.
hegyi@819
    59
  //! \param Graph The graph type in which the path is.
hegyi@819
    60
  //! \param DM DebugMode, defaults to DefaultDebugMode.
hegyi@837
    61
  //!
hegyi@819
    62
  //! In a sense, the path can be treated as a graph, for is has \c NodeIt
hegyi@819
    63
  //! and \c EdgeIt with the same usage. These types converts to the \c Node
hegyi@819
    64
  //! and \c Edge of the original graph.
hegyi@819
    65
  //!
hegyi@819
    66
  //! \todo Thoroughfully check all the range and consistency tests.
hegyi@831
    67
  template<typename Graph>
hegyi@819
    68
  class DirPath {
hegyi@819
    69
  public:
hegyi@819
    70
    /// Edge type of the underlying graph.
hegyi@837
    71
    typedef typename Graph::Edge GraphEdge;
hegyi@819
    72
    /// Node type of the underlying graph.
hegyi@819
    73
    typedef typename Graph::Node GraphNode;
hegyi@819
    74
    class NodeIt;
hegyi@819
    75
    class EdgeIt;
hegyi@819
    76
hegyi@819
    77
  protected:
hegyi@819
    78
    const Graph *gr;
hegyi@819
    79
    typedef std::vector<GraphEdge> Container;
hegyi@819
    80
    Container edges;
hegyi@819
    81
hegyi@819
    82
  public:
hegyi@819
    83
hegyi@819
    84
    /// \param _G The graph in which the path is.
hegyi@819
    85
    ///
hegyi@819
    86
    DirPath(const Graph &_G) : gr(&_G) {}
hegyi@819
    87
hegyi@819
    88
    /// \brief Subpath constructor.
hegyi@819
    89
    ///
hegyi@819
    90
    /// Subpath defined by two nodes.
hegyi@819
    91
    /// \warning It is an error if the two edges are not in order!
hegyi@819
    92
    DirPath(const DirPath &P, const NodeIt &a, const NodeIt &b) {
hegyi@819
    93
      gr = P.gr;
hegyi@819
    94
      edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
hegyi@819
    95
    }
hegyi@819
    96
hegyi@819
    97
    /// \brief Subpath constructor.
hegyi@819
    98
    ///
hegyi@819
    99
    /// Subpath defined by two edges. Contains edges in [a,b)
hegyi@819
   100
    /// \warning It is an error if the two edges are not in order!
hegyi@819
   101
    DirPath(const DirPath &P, const EdgeIt &a, const EdgeIt &b) {
hegyi@819
   102
      gr = P.gr;
hegyi@819
   103
      edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
hegyi@819
   104
    }
hegyi@819
   105
hegyi@819
   106
    /// Length of the path.
alpar@1282
   107
    int length() const { return edges.size(); }
hegyi@819
   108
    /// Returns whether the path is empty.
hegyi@819
   109
    bool empty() const { return edges.empty(); }
hegyi@819
   110
hegyi@819
   111
    /// Resets the path to an empty path.
hegyi@819
   112
    void clear() { edges.clear(); }
hegyi@819
   113
hegyi@819
   114
    /// \brief Starting point of the path.
hegyi@819
   115
    ///
hegyi@819
   116
    /// Starting point of the path.
hegyi@819
   117
    /// Returns INVALID if the path is empty.
alpar@986
   118
    GraphNode source() const {
alpar@986
   119
      return empty() ? INVALID : gr->source(edges[0]);
hegyi@819
   120
    }
hegyi@819
   121
    /// \brief End point of the path.
hegyi@819
   122
    ///
hegyi@819
   123
    /// End point of the path.
hegyi@819
   124
    /// Returns INVALID if the path is empty.
alpar@986
   125
    GraphNode target() const {
alpar@986
   126
      return empty() ? INVALID : gr->target(edges[length()-1]);
hegyi@819
   127
    }
hegyi@819
   128
hegyi@819
   129
    /// \brief Initializes node or edge iterator to point to the first
hegyi@819
   130
    /// node or edge.
hegyi@819
   131
    ///
hegyi@819
   132
    /// \sa nth
hegyi@819
   133
    template<typename It>
hegyi@819
   134
    It& first(It &i) const { return i=It(*this); }
hegyi@819
   135
hegyi@819
   136
    /// \brief Initializes node iterator to point to the node of a given index.
hegyi@819
   137
    NodeIt& nth(NodeIt &i, int n) const {
hegyi@819
   138
      return i=NodeIt(*this, n);
hegyi@819
   139
    }
hegyi@819
   140
hegyi@819
   141
    /// \brief Initializes edge iterator to point to the edge of a given index.
hegyi@819
   142
    EdgeIt& nth(EdgeIt &i, int n) const {
hegyi@819
   143
      return i=EdgeIt(*this, n);
hegyi@819
   144
    }
hegyi@819
   145
alpar@986
   146
    /// \brief Returns node iterator pointing to the target node of the
hegyi@819
   147
    /// given edge iterator.
alpar@986
   148
    NodeIt target(const EdgeIt& e) const {
hegyi@819
   149
      return NodeIt(*this, e.idx+1);
hegyi@819
   150
    }
hegyi@819
   151
alpar@986
   152
    /// \brief Returns node iterator pointing to the source node of the
hegyi@819
   153
    /// given edge iterator.
alpar@986
   154
    NodeIt source(const EdgeIt& e) const {
hegyi@819
   155
      return NodeIt(*this, e.idx);
hegyi@819
   156
    }
hegyi@819
   157
hegyi@819
   158
hegyi@819
   159
    /* Iterator classes */
hegyi@819
   160
hegyi@819
   161
    /**
hegyi@819
   162
     * \brief Iterator class to iterate on the edges of the paths
hegyi@837
   163
     *
hegyi@819
   164
     * This class is used to iterate on the edges of the paths
hegyi@819
   165
     *
hegyi@819
   166
     * Of course it converts to Graph::Edge
hegyi@837
   167
     *
hegyi@819
   168
     */
hegyi@819
   169
    class EdgeIt {
hegyi@819
   170
      friend class DirPath;
hegyi@819
   171
hegyi@819
   172
      int idx;
hegyi@819
   173
      const DirPath *p;
hegyi@819
   174
    public:
hegyi@819
   175
      /// Default constructor
hegyi@819
   176
      EdgeIt() {}
hegyi@819
   177
      /// Invalid constructor
hegyi@819
   178
      EdgeIt(Invalid) : idx(-1), p(0) {}
hegyi@819
   179
      /// Constructor with starting point
hegyi@819
   180
      EdgeIt(const DirPath &_p, int _idx = 0) :
hegyi@819
   181
	idx(_idx), p(&_p) { validate(); }
hegyi@819
   182
hegyi@819
   183
      ///Validity check
hegyi@819
   184
      bool valid() const { return idx!=-1; }
hegyi@819
   185
hegyi@819
   186
      ///Conversion to Graph::Edge
hegyi@819
   187
      operator GraphEdge () const {
hegyi@819
   188
	return valid() ? p->edges[idx] : INVALID;
hegyi@819
   189
      }
hegyi@819
   190
hegyi@819
   191
      /// Next edge
hegyi@819
   192
      EdgeIt& operator++() { ++idx; validate(); return *this; }
hegyi@819
   193
hegyi@819
   194
      /// Comparison operator
hegyi@819
   195
      bool operator==(const EdgeIt& e) const { return idx==e.idx; }
hegyi@819
   196
      /// Comparison operator
hegyi@819
   197
      bool operator!=(const EdgeIt& e) const { return idx!=e.idx; }
hegyi@819
   198
      /// Comparison operator
hegyi@819
   199
      bool operator<(const EdgeIt& e) const { return idx<e.idx; }
hegyi@819
   200
hegyi@819
   201
    private:
alpar@1282
   202
      void validate() { if(idx >= p->length() ) idx=-1; }
hegyi@819
   203
    };
hegyi@819
   204
hegyi@819
   205
    /**
hegyi@819
   206
     * \brief Iterator class to iterate on the nodes of the paths
hegyi@837
   207
     *
hegyi@819
   208
     * This class is used to iterate on the nodes of the paths
hegyi@819
   209
     *
hegyi@819
   210
     * Of course it converts to Graph::Node
hegyi@837
   211
     *
hegyi@819
   212
     */
hegyi@819
   213
    class NodeIt {
hegyi@819
   214
      friend class DirPath;
hegyi@819
   215
hegyi@819
   216
      int idx;
hegyi@819
   217
      const DirPath *p;
hegyi@819
   218
    public:
hegyi@819
   219
      /// Default constructor
hegyi@819
   220
      NodeIt() {}
hegyi@819
   221
      /// Invalid constructor
hegyi@819
   222
      NodeIt(Invalid) : idx(-1), p(0) {}
hegyi@819
   223
      /// Constructor with starting point
hegyi@819
   224
      NodeIt(const DirPath &_p, int _idx = 0) :
hegyi@819
   225
	idx(_idx), p(&_p) { validate(); }
hegyi@819
   226
hegyi@819
   227
      ///Validity check
hegyi@819
   228
      bool valid() const { return idx!=-1; }
hegyi@819
   229
hegyi@819
   230
      ///Conversion to Graph::Node
hegyi@819
   231
      operator const GraphNode& () const {
hegyi@819
   232
	if(idx >= p->length())
alpar@986
   233
	  return p->target();
hegyi@819
   234
	else if(idx >= 0)
alpar@986
   235
	  return p->gr->source(p->edges[idx]);
hegyi@819
   236
	else
hegyi@819
   237
	  return INVALID;
hegyi@819
   238
      }
hegyi@819
   239
      /// Next node
hegyi@819
   240
      NodeIt& operator++() { ++idx; validate(); return *this; }
hegyi@819
   241
hegyi@819
   242
      /// Comparison operator
hegyi@819
   243
      bool operator==(const NodeIt& e) const { return idx==e.idx; }
hegyi@819
   244
      /// Comparison operator
hegyi@819
   245
      bool operator!=(const NodeIt& e) const { return idx!=e.idx; }
hegyi@819
   246
      /// Comparison operator
hegyi@819
   247
      bool operator<(const NodeIt& e) const { return idx<e.idx; }
hegyi@819
   248
hegyi@819
   249
    private:
alpar@1282
   250
      void validate() { if(idx > p->length() ) idx=-1; }
hegyi@819
   251
    };
hegyi@819
   252
hegyi@837
   253
    friend class Builder;
hegyi@819
   254
hegyi@819
   255
    /**
hegyi@819
   256
     * \brief Class to build paths
hegyi@837
   257
     *
hegyi@819
   258
     * This class is used to fill a path with edges.
hegyi@819
   259
     *
hegyi@819
   260
     * You can push new edges to the front and to the back of the path in
hegyi@819
   261
     * arbitrary order then you should commit these changes to the graph.
hegyi@819
   262
     *
hegyi@819
   263
     * Fundamentally, for most "Paths" (classes fulfilling the
hegyi@819
   264
     * PathConcept) while the builder is active (after the first modifying
hegyi@819
   265
     * operation and until the commit()) the original Path is in a
hegyi@819
   266
     * "transitional" state (operations on it have undefined result). But
hegyi@819
   267
     * in the case of DirPath the original path remains unchanged until the
hegyi@819
   268
     * commit. However we don't recomend that you use this feature.
hegyi@819
   269
     */
hegyi@819
   270
    class Builder {
hegyi@819
   271
      DirPath &P;
hegyi@819
   272
      Container front, back;
hegyi@819
   273
hegyi@819
   274
    public:
alpar@1228
   275
      ///\param _p the path you want to fill in.
hegyi@819
   276
      ///
alpar@1228
   277
      Builder(DirPath &_p) : P(_p) {}
hegyi@819
   278
hegyi@819
   279
      /// Sets the starting node of the path.
hegyi@837
   280
hegyi@819
   281
      /// Sets the starting node of the path. Edge added to the path
hegyi@819
   282
      /// afterwards have to be incident to this node.
alpar@900
   283
      /// It should be called if and only if
alpar@900
   284
      /// the path is empty and before any call to
hegyi@819
   285
      /// \ref pushFront() or \ref pushBack()
hegyi@819
   286
      void setStartNode(const GraphNode &) {}
hegyi@819
   287
hegyi@819
   288
      ///Push a new edge to the front of the path
hegyi@819
   289
hegyi@819
   290
      ///Push a new edge to the front of the path.
hegyi@819
   291
      ///\sa setStartNode
hegyi@819
   292
      void pushFront(const GraphEdge& e) {
hegyi@819
   293
	front.push_back(e);
hegyi@819
   294
      }
hegyi@819
   295
hegyi@819
   296
      ///Push a new edge to the back of the path
hegyi@819
   297
hegyi@819
   298
      ///Push a new edge to the back of the path.
hegyi@819
   299
      ///\sa setStartNode
hegyi@819
   300
      void pushBack(const GraphEdge& e) {
hegyi@819
   301
	back.push_back(e);
hegyi@819
   302
      }
hegyi@819
   303
hegyi@819
   304
      ///Commit the changes to the path.
hegyi@819
   305
      void commit() {
hegyi@837
   306
	if( !front.empty() || !back.empty() ) {
hegyi@819
   307
	  Container tmp;
hegyi@819
   308
	  tmp.reserve(front.size()+back.size()+P.length());
hegyi@819
   309
	  tmp.insert(tmp.end(), front.rbegin(), front.rend());
hegyi@819
   310
	  tmp.insert(tmp.end(), P.edges.begin(), P.edges.end());
hegyi@819
   311
	  tmp.insert(tmp.end(), back.begin(), back.end());
hegyi@819
   312
	  P.edges.swap(tmp);
hegyi@819
   313
	  front.clear();
hegyi@819
   314
	  back.clear();
hegyi@819
   315
	}
hegyi@819
   316
      }
hegyi@819
   317
hegyi@819
   318
      ///Reserve storage for the builder in advance.
hegyi@819
   319
hegyi@837
   320
      ///If you know a reasonable upper bound of the number of the edges
hegyi@837
   321
      ///to add to the front, using this function you can speed up the building.
hegyi@819
   322
hegyi@837
   323
      void reserveFront(size_t r) {front.reserve(r);}
hegyi@837
   324
hegyi@837
   325
      ///Reserve storage for the builder in advance.
hegyi@837
   326
hegyi@837
   327
      ///If you know a reasonable upper bound of the number of the edges
hegyi@837
   328
      ///to add to the back, using this function you can speed up the building.
hegyi@837
   329
hegyi@837
   330
      void reserveBack(size_t r) {back.reserve(r);}
hegyi@831
   331
hegyi@819
   332
    private:
hegyi@819
   333
      bool empty() {
hegyi@819
   334
	return front.empty() && back.empty() && P.empty();
hegyi@819
   335
      }
hegyi@819
   336
alpar@986
   337
      GraphNode source() const {
hegyi@819
   338
	if( ! front.empty() )
alpar@986
   339
	  return P.gr->source(front[front.size()-1]);
hegyi@819
   340
	else if( ! P.empty() )
alpar@986
   341
	  return P.gr->source(P.edges[0]);
hegyi@819
   342
	else if( ! back.empty() )
alpar@986
   343
	  return P.gr->source(back[0]);
hegyi@819
   344
	else
hegyi@819
   345
	  return INVALID;
hegyi@819
   346
      }
alpar@986
   347
      GraphNode target() const {
hegyi@819
   348
	if( ! back.empty() )
alpar@986
   349
	  return P.gr->target(back[back.size()-1]);
hegyi@819
   350
	else if( ! P.empty() )
alpar@986
   351
	  return P.gr->target(P.edges[P.length()-1]);
hegyi@819
   352
	else if( ! front.empty() )
alpar@986
   353
	  return P.gr->target(front[0]);
hegyi@819
   354
	else
hegyi@819
   355
	  return INVALID;
hegyi@819
   356
      }
hegyi@819
   357
hegyi@819
   358
    };
hegyi@819
   359
hegyi@819
   360
  };
hegyi@819
   361
hegyi@819
   362
hegyi@819
   363
hegyi@819
   364
hegyi@819
   365
hegyi@819
   366
hegyi@819
   367
hegyi@819
   368
hegyi@819
   369
hegyi@819
   370
hegyi@819
   371
  /**********************************************************************/
hegyi@819
   372
hegyi@819
   373
hegyi@819
   374
  //! \brief A structure for representing undirected path in a graph.
hegyi@819
   375
  //!
hegyi@819
   376
  //! A structure for representing undirected path in a graph. Ie. this is
hegyi@819
   377
  //! a path in a \e directed graph but the edges should not be directed
hegyi@819
   378
  //! forward.
hegyi@819
   379
  //!
hegyi@819
   380
  //! \param Graph The graph type in which the path is.
hegyi@819
   381
  //! \param DM DebugMode, defaults to DefaultDebugMode.
hegyi@837
   382
  //!
hegyi@819
   383
  //! In a sense, the path can be treated as a graph, for is has \c NodeIt
hegyi@819
   384
  //! and \c EdgeIt with the same usage. These types converts to the \c Node
hegyi@819
   385
  //! and \c Edge of the original graph.
hegyi@819
   386
  //!
hegyi@819
   387
  //! \todo Thoroughfully check all the range and consistency tests.
deba@1730
   388
  /// \todo May we need just path for undirected graph instead of this.
hegyi@831
   389
  template<typename Graph>
klao@1909
   390
  class UPath {
hegyi@819
   391
  public:
hegyi@819
   392
    /// Edge type of the underlying graph.
hegyi@819
   393
    typedef typename Graph::Edge GraphEdge;
hegyi@819
   394
     /// Node type of the underlying graph.
hegyi@819
   395
   typedef typename Graph::Node GraphNode;
hegyi@819
   396
    class NodeIt;
hegyi@819
   397
    class EdgeIt;
hegyi@819
   398
hegyi@819
   399
  protected:
hegyi@819
   400
    const Graph *gr;
hegyi@819
   401
    typedef std::vector<GraphEdge> Container;
hegyi@819
   402
    Container edges;
hegyi@819
   403
hegyi@819
   404
  public:
hegyi@819
   405
hegyi@819
   406
    /// \param _G The graph in which the path is.
hegyi@819
   407
    ///
klao@1909
   408
    UPath(const Graph &_G) : gr(&_G) {}
hegyi@819
   409
hegyi@819
   410
    /// \brief Subpath constructor.
hegyi@819
   411
    ///
hegyi@819
   412
    /// Subpath defined by two nodes.
hegyi@819
   413
    /// \warning It is an error if the two edges are not in order!
klao@1909
   414
    UPath(const UPath &P, const NodeIt &a, const NodeIt &b) {
hegyi@819
   415
      gr = P.gr;
hegyi@819
   416
      edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
hegyi@819
   417
    }
hegyi@819
   418
hegyi@819
   419
    /// \brief Subpath constructor.
hegyi@819
   420
    ///
hegyi@819
   421
    /// Subpath defined by two edges. Contains edges in [a,b)
hegyi@819
   422
    /// \warning It is an error if the two edges are not in order!
klao@1909
   423
    UPath(const UPath &P, const EdgeIt &a, const EdgeIt &b) {
hegyi@819
   424
      gr = P.gr;
hegyi@819
   425
      edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
hegyi@819
   426
    }
hegyi@819
   427
hegyi@819
   428
    /// Length of the path.
hegyi@819
   429
    size_t length() const { return edges.size(); }
hegyi@819
   430
    /// Returns whether the path is empty.
hegyi@819
   431
    bool empty() const { return edges.empty(); }
hegyi@819
   432
hegyi@819
   433
    /// Resets the path to an empty path.
hegyi@819
   434
    void clear() { edges.clear(); }
hegyi@819
   435
hegyi@819
   436
    /// \brief Starting point of the path.
hegyi@819
   437
    ///
hegyi@819
   438
    /// Starting point of the path.
hegyi@819
   439
    /// Returns INVALID if the path is empty.
alpar@986
   440
    GraphNode source() const {
alpar@986
   441
      return empty() ? INVALID : gr->source(edges[0]);
hegyi@819
   442
    }
hegyi@819
   443
    /// \brief End point of the path.
hegyi@819
   444
    ///
hegyi@819
   445
    /// End point of the path.
hegyi@819
   446
    /// Returns INVALID if the path is empty.
alpar@986
   447
    GraphNode target() const {
alpar@986
   448
      return empty() ? INVALID : gr->target(edges[length()-1]);
hegyi@819
   449
    }
hegyi@819
   450
hegyi@819
   451
    /// \brief Initializes node or edge iterator to point to the first
hegyi@819
   452
    /// node or edge.
hegyi@819
   453
    ///
hegyi@819
   454
    /// \sa nth
hegyi@819
   455
    template<typename It>
hegyi@819
   456
    It& first(It &i) const { return i=It(*this); }
hegyi@819
   457
hegyi@819
   458
    /// \brief Initializes node iterator to point to the node of a given index.
hegyi@819
   459
    NodeIt& nth(NodeIt &i, int n) const {
hegyi@819
   460
      return i=NodeIt(*this, n);
hegyi@819
   461
    }
hegyi@819
   462
hegyi@819
   463
    /// \brief Initializes edge iterator to point to the edge of a given index.
hegyi@819
   464
    EdgeIt& nth(EdgeIt &i, int n) const {
hegyi@819
   465
      return i=EdgeIt(*this, n);
hegyi@819
   466
    }
hegyi@819
   467
hegyi@819
   468
    /// Checks validity of a node or edge iterator.
hegyi@819
   469
    template<typename It>
hegyi@819
   470
    static
hegyi@819
   471
    bool valid(const It &i) { return i.valid(); }
hegyi@819
   472
hegyi@819
   473
    /// Steps the given node or edge iterator.
hegyi@819
   474
    template<typename It>
hegyi@819
   475
    static
hegyi@819
   476
    It& next(It &e) {
hegyi@819
   477
      return ++e;
hegyi@819
   478
    }
hegyi@819
   479
alpar@986
   480
    /// \brief Returns node iterator pointing to the target node of the
hegyi@819
   481
    /// given edge iterator.
alpar@986
   482
    NodeIt target(const EdgeIt& e) const {
hegyi@819
   483
      return NodeIt(*this, e.idx+1);
hegyi@819
   484
    }
hegyi@819
   485
alpar@986
   486
    /// \brief Returns node iterator pointing to the source node of the
hegyi@819
   487
    /// given edge iterator.
alpar@986
   488
    NodeIt source(const EdgeIt& e) const {
hegyi@819
   489
      return NodeIt(*this, e.idx);
hegyi@819
   490
    }
hegyi@819
   491
hegyi@819
   492
hegyi@819
   493
hegyi@819
   494
    /**
hegyi@819
   495
     * \brief Iterator class to iterate on the edges of the paths
hegyi@837
   496
     *
hegyi@819
   497
     * This class is used to iterate on the edges of the paths
hegyi@819
   498
     *
hegyi@819
   499
     * Of course it converts to Graph::Edge
hegyi@837
   500
     *
hegyi@819
   501
     * \todo Its interface differs from the standard edge iterator.
hegyi@819
   502
     * Yes, it shouldn't.
hegyi@819
   503
     */
hegyi@819
   504
    class EdgeIt {
klao@1909
   505
      friend class UPath;
hegyi@819
   506
hegyi@819
   507
      int idx;
klao@1909
   508
      const UPath *p;
hegyi@819
   509
    public:
hegyi@819
   510
      /// Default constructor
hegyi@819
   511
      EdgeIt() {}
hegyi@819
   512
      /// Invalid constructor
hegyi@819
   513
      EdgeIt(Invalid) : idx(-1), p(0) {}
hegyi@819
   514
      /// Constructor with starting point
klao@1909
   515
      EdgeIt(const UPath &_p, int _idx = 0) :
hegyi@819
   516
	idx(_idx), p(&_p) { validate(); }
hegyi@819
   517
hegyi@819
   518
      ///Validity check
hegyi@819
   519
      bool valid() const { return idx!=-1; }
hegyi@819
   520
hegyi@819
   521
      ///Conversion to Graph::Edge
hegyi@819
   522
      operator GraphEdge () const {
hegyi@819
   523
	return valid() ? p->edges[idx] : INVALID;
hegyi@819
   524
      }
hegyi@819
   525
      /// Next edge
hegyi@819
   526
     EdgeIt& operator++() { ++idx; validate(); return *this; }
hegyi@819
   527
hegyi@819
   528
      /// Comparison operator
hegyi@819
   529
      bool operator==(const EdgeIt& e) const { return idx==e.idx; }
hegyi@819
   530
      /// Comparison operator
hegyi@819
   531
      bool operator!=(const EdgeIt& e) const { return idx!=e.idx; }
hegyi@819
   532
      /// Comparison operator
hegyi@819
   533
      bool operator<(const EdgeIt& e) const { return idx<e.idx; }
hegyi@819
   534
hegyi@819
   535
    private:
hegyi@819
   536
      // FIXME: comparison between signed and unsigned...
hegyi@819
   537
      // Jo ez igy? Vagy esetleg legyen a length() int?
hegyi@819
   538
      void validate() { if( size_t(idx) >= p->length() ) idx=-1; }
hegyi@819
   539
    };
hegyi@819
   540
hegyi@819
   541
    /**
hegyi@819
   542
     * \brief Iterator class to iterate on the nodes of the paths
hegyi@837
   543
     *
hegyi@819
   544
     * This class is used to iterate on the nodes of the paths
hegyi@819
   545
     *
hegyi@819
   546
     * Of course it converts to Graph::Node
hegyi@837
   547
     *
hegyi@819
   548
     * \todo Its interface differs from the standard node iterator.
hegyi@819
   549
     * Yes, it shouldn't.
hegyi@819
   550
     */
hegyi@819
   551
    class NodeIt {
klao@1909
   552
      friend class UPath;
hegyi@819
   553
hegyi@819
   554
      int idx;
klao@1909
   555
      const UPath *p;
hegyi@819
   556
    public:
hegyi@819
   557
      /// Default constructor
hegyi@819
   558
      NodeIt() {}
hegyi@819
   559
      /// Invalid constructor
hegyi@819
   560
      NodeIt(Invalid) : idx(-1), p(0) {}
hegyi@819
   561
      /// Constructor with starting point
klao@1909
   562
      NodeIt(const UPath &_p, int _idx = 0) :
hegyi@819
   563
	idx(_idx), p(&_p) { validate(); }
hegyi@819
   564
hegyi@819
   565
      ///Validity check
hegyi@819
   566
      bool valid() const { return idx!=-1; }
hegyi@819
   567
hegyi@819
   568
      ///Conversion to Graph::Node
hegyi@819
   569
      operator const GraphNode& () const {
hegyi@819
   570
	if(idx >= p->length())
alpar@986
   571
	  return p->target();
hegyi@819
   572
	else if(idx >= 0)
alpar@986
   573
	  return p->gr->source(p->edges[idx]);
hegyi@819
   574
	else
hegyi@819
   575
	  return INVALID;
hegyi@819
   576
      }
hegyi@819
   577
      /// Next node
hegyi@819
   578
      NodeIt& operator++() { ++idx; validate(); return *this; }
hegyi@819
   579
hegyi@819
   580
      /// Comparison operator
hegyi@819
   581
      bool operator==(const NodeIt& e) const { return idx==e.idx; }
hegyi@819
   582
      /// Comparison operator
hegyi@819
   583
      bool operator!=(const NodeIt& e) const { return idx!=e.idx; }
hegyi@819
   584
       /// Comparison operator
hegyi@819
   585
     bool operator<(const NodeIt& e) const { return idx<e.idx; }
hegyi@819
   586
hegyi@819
   587
    private:
hegyi@819
   588
      void validate() { if( size_t(idx) > p->length() ) idx=-1; }
hegyi@819
   589
    };
hegyi@819
   590
hegyi@837
   591
    friend class Builder;
hegyi@819
   592
hegyi@819
   593
    /**
hegyi@819
   594
     * \brief Class to build paths
hegyi@837
   595
     *
hegyi@819
   596
     * This class is used to fill a path with edges.
hegyi@819
   597
     *
hegyi@819
   598
     * You can push new edges to the front and to the back of the path in
hegyi@819
   599
     * arbitrary order then you should commit these changes to the graph.
hegyi@819
   600
     *
hegyi@819
   601
     * Fundamentally, for most "Paths" (classes fulfilling the
hegyi@819
   602
     * PathConcept) while the builder is active (after the first modifying
hegyi@819
   603
     * operation and until the commit()) the original Path is in a
hegyi@819
   604
     * "transitional" state (operations ot it have undefined result). But
klao@1909
   605
     * in the case of UPath the original path is unchanged until the
hegyi@819
   606
     * commit. However we don't recomend that you use this feature.
hegyi@819
   607
     */
hegyi@819
   608
    class Builder {
klao@1909
   609
      UPath &P;
hegyi@819
   610
      Container front, back;
hegyi@819
   611
hegyi@819
   612
    public:
alpar@1228
   613
      ///\param _p the path you want to fill in.
hegyi@819
   614
      ///
klao@1909
   615
      Builder(UPath &_p) : P(_p) {}
hegyi@819
   616
hegyi@819
   617
      /// Sets the starting node of the path.
hegyi@837
   618
hegyi@819
   619
      /// Sets the starting node of the path. Edge added to the path
hegyi@819
   620
      /// afterwards have to be incident to this node.
alpar@900
   621
      /// It should be called if and only if
alpar@900
   622
      /// the path is empty and before any call to
hegyi@819
   623
      /// \ref pushFront() or \ref pushBack()
hegyi@819
   624
      void setStartNode(const GraphNode &) {}
hegyi@819
   625
hegyi@819
   626
      ///Push a new edge to the front of the path
hegyi@819
   627
hegyi@819
   628
      ///Push a new edge to the front of the path.
hegyi@819
   629
      ///\sa setStartNode
hegyi@819
   630
      void pushFront(const GraphEdge& e) {
hegyi@819
   631
	front.push_back(e);
hegyi@819
   632
      }
hegyi@819
   633
hegyi@819
   634
      ///Push a new edge to the back of the path
hegyi@819
   635
hegyi@819
   636
      ///Push a new edge to the back of the path.
hegyi@819
   637
      ///\sa setStartNode
hegyi@819
   638
      void pushBack(const GraphEdge& e) {
hegyi@819
   639
	back.push_back(e);
hegyi@819
   640
      }
hegyi@819
   641
hegyi@819
   642
      ///Commit the changes to the path.
hegyi@819
   643
      void commit() {
hegyi@819
   644
	if( !(front.empty() && back.empty()) ) {
hegyi@819
   645
	  Container tmp;
hegyi@819
   646
	  tmp.reserve(front.size()+back.size()+P.length());
hegyi@819
   647
	  tmp.insert(tmp.end(), front.rbegin(), front.rend());
hegyi@819
   648
	  tmp.insert(tmp.end(), P.edges.begin(), P.edges.end());
hegyi@819
   649
	  tmp.insert(tmp.end(), back.begin(), back.end());
hegyi@819
   650
	  P.edges.swap(tmp);
hegyi@819
   651
	  front.clear();
hegyi@819
   652
	  back.clear();
hegyi@819
   653
	}
hegyi@819
   654
      }
hegyi@819
   655
hegyi@819
   656
hegyi@819
   657
      ///Reserve storage for the builder in advance.
hegyi@819
   658
hegyi@837
   659
      ///If you know a reasonable upper bound of the number of the edges
hegyi@837
   660
      ///to add to the front, using this function you can speed up the building.
hegyi@819
   661
hegyi@837
   662
      void reserveFront(size_t r) {front.reserve(r);}
hegyi@837
   663
hegyi@837
   664
      ///Reserve storage for the builder in advance.
hegyi@837
   665
hegyi@837
   666
      ///If you know a reasonable upper bound of the number of the edges
hegyi@837
   667
      ///to add to the back, using this function you can speed up the building.
hegyi@837
   668
hegyi@837
   669
      void reserveBack(size_t r) {back.reserve(r);}
hegyi@831
   670
hegyi@819
   671
    private:
hegyi@819
   672
      bool empty() {
hegyi@819
   673
	return front.empty() && back.empty() && P.empty();
hegyi@819
   674
      }
hegyi@819
   675
alpar@986
   676
      GraphNode source() const {
hegyi@819
   677
	if( ! front.empty() )
alpar@986
   678
	  return P.gr->source(front[front.size()-1]);
hegyi@819
   679
	else if( ! P.empty() )
alpar@986
   680
	  return P.gr->source(P.edges[0]);
hegyi@819
   681
	else if( ! back.empty() )
alpar@986
   682
	  return P.gr->source(back[0]);
hegyi@819
   683
	else
hegyi@819
   684
	  return INVALID;
hegyi@819
   685
      }
alpar@986
   686
      GraphNode target() const {
hegyi@819
   687
	if( ! back.empty() )
alpar@986
   688
	  return P.gr->target(back[back.size()-1]);
hegyi@819
   689
	else if( ! P.empty() )
alpar@986
   690
	  return P.gr->target(P.edges[P.length()-1]);
hegyi@819
   691
	else if( ! front.empty() )
alpar@986
   692
	  return P.gr->target(front[0]);
hegyi@819
   693
	else
hegyi@819
   694
	  return INVALID;
hegyi@819
   695
      }
hegyi@819
   696
hegyi@819
   697
    };
hegyi@819
   698
hegyi@819
   699
  };
hegyi@819
   700
hegyi@819
   701
hegyi@819
   702
  ///@}
hegyi@819
   703
alpar@921
   704
} // namespace lemon
hegyi@819
   705
alpar@921
   706
#endif // LEMON_PATH_H