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