src/work/klao/path.h
author alpar
Mon, 19 Jul 2004 13:29:32 +0000
changeset 706 0fe42b8ec5a6
parent 683 3cbf51510180
child 803 c3d832275e69
permissions -rw-r--r--
Temporal change: public Edge constructor with given ID.
klao@225
     1
// -*- c++ -*- //
klao@225
     2
alpar@686
     3
/**
alpar@686
     4
@defgroup paths Path Structures
alpar@686
     5
@ingroup datas
alpar@686
     6
\brief Path structures implemented in Hugo.
alpar@686
     7
alpar@686
     8
Hugolib provides flexible data structures
alpar@686
     9
to work with paths.
alpar@686
    10
alpar@686
    11
All of them have the same interface, especially they can be built or extended
alpar@686
    12
using a standard Builder subclass. This make is easy to have e.g. the Dijkstra
alpar@686
    13
algorithm to store its result in any kind of path structure.
alpar@686
    14
alpar@686
    15
*/
alpar@686
    16
alpar@686
    17
///\ingroup paths
alpar@434
    18
///\file
klao@493
    19
///\brief Classes for representing paths in graphs.
klao@225
    20
klao@225
    21
#ifndef HUGO_PATH_H
klao@225
    22
#define HUGO_PATH_H
klao@225
    23
klao@225
    24
#include <deque>
klao@369
    25
#include <vector>
klao@226
    26
#include <algorithm>
klao@225
    27
athos@607
    28
#include <hugo/invalid.h>
athos@607
    29
#include <hugo/error.h>
klao@493
    30
#include <debug.h>
klao@225
    31
klao@225
    32
namespace hugo {
klao@225
    33
alpar@686
    34
  /// \addtogroup paths
alpar@434
    35
  /// @{
alpar@434
    36
alpar@434
    37
klao@493
    38
  //! \brief A structure for representing directed path in a graph.
klao@493
    39
  //!
klao@619
    40
  //! A structure for representing directed path in a graph.
klao@493
    41
  //! \param Graph The graph type in which the path is.
klao@493
    42
  //! \param DM DebugMode, defaults to DefaultDebugMode.
klao@493
    43
  //! 
klao@493
    44
  //! In a sense, the path can be treated as a graph, for is has \c NodeIt
klao@493
    45
  //! and \c EdgeIt with the same usage. These types converts to the \c Node
klao@493
    46
  //! and \c Edge of the original graph.
klao@493
    47
  //!
klao@493
    48
  //! \todo Thoroughfully check all the range and consistency tests.
klao@493
    49
  template<typename Graph, typename DM = DefaultDebugMode>
klao@369
    50
  class DirPath {
klao@369
    51
  public:
alpar@686
    52
    /// Edge type of the underlying graph.
alpar@686
    53
    typedef typename Graph::Edge GraphEdge; 
alpar@686
    54
    /// Node type of the underlying graph.
klao@369
    55
    typedef typename Graph::Node GraphNode;
klao@369
    56
    class NodeIt;
klao@369
    57
    class EdgeIt;
klao@369
    58
klao@369
    59
  protected:
klao@369
    60
    const Graph *gr;
klao@369
    61
    typedef std::vector<GraphEdge> Container;
klao@369
    62
    Container edges;
klao@369
    63
klao@369
    64
  public:
klao@369
    65
alpar@434
    66
    /// \param _G The graph in which the path is.
alpar@434
    67
    ///
klao@369
    68
    DirPath(const Graph &_G) : gr(&_G) {}
klao@369
    69
klao@493
    70
    /// \brief Subpath constructor.
klao@493
    71
    ///
klao@369
    72
    /// Subpath defined by two nodes.
alpar@434
    73
    /// \warning It is an error if the two edges are not in order!
klao@619
    74
    DirPath(const DirPath &P, const NodeIt &a, const NodeIt &b) {
klao@619
    75
      if( DM::range_check && (!a.valid() || !b.valid) ) {
klao@619
    76
	// FIXME: this check should be more elaborate...
klao@619
    77
	fault("DirPath, subpath ctor: invalid bounding nodes");
klao@619
    78
      }
klao@619
    79
      gr = P.gr;
klao@619
    80
      edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
klao@619
    81
    }
klao@619
    82
klao@493
    83
    /// \brief Subpath constructor.
klao@493
    84
    ///
klao@369
    85
    /// Subpath defined by two edges. Contains edges in [a,b)
alpar@434
    86
    /// \warning It is an error if the two edges are not in order!
klao@619
    87
    DirPath(const DirPath &P, const EdgeIt &a, const EdgeIt &b) {
klao@619
    88
      if( DM::range_check && (!a.valid() || !b.valid) ) {
klao@619
    89
	// FIXME: this check should be more elaborate...
klao@619
    90
	fault("DirPath, subpath ctor: invalid bounding nodes");
klao@619
    91
      }
klao@619
    92
      gr = P.gr;
klao@619
    93
      edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
klao@619
    94
    }
klao@369
    95
klao@493
    96
    /// Length of the path.
klao@369
    97
    size_t length() const { return edges.size(); }
klao@493
    98
    /// Returns whether the path is empty.
klao@369
    99
    bool empty() const { return edges.empty(); }
klao@493
   100
klao@493
   101
    /// Resets the path to an empty path.
klao@493
   102
    void clear() { edges.clear(); }
klao@493
   103
klao@493
   104
    /// \brief Starting point of the path.
klao@493
   105
    ///
klao@493
   106
    /// Starting point of the path.
klao@493
   107
    /// Returns INVALID if the path is empty.
klao@369
   108
    GraphNode from() const {
klao@369
   109
      return empty() ? INVALID : gr->tail(edges[0]);
klao@369
   110
    }
klao@493
   111
    /// \brief End point of the path.
klao@493
   112
    ///
klao@493
   113
    /// End point of the path.
klao@493
   114
    /// Returns INVALID if the path is empty.
klao@369
   115
    GraphNode to() const {
klao@369
   116
      return empty() ? INVALID : gr->head(edges[length()-1]);
klao@369
   117
    }
klao@369
   118
klao@493
   119
    /// \brief Initializes node or edge iterator to point to the first
klao@493
   120
    /// node or edge.
klao@493
   121
    ///
klao@493
   122
    /// \sa nth
klao@369
   123
    template<typename It>
klao@369
   124
    It& first(It &i) const { return i=It(*this); }
klao@369
   125
klao@619
   126
    /// \brief Initializes node iterator to point to the node of a given index.
klao@619
   127
    NodeIt& nth(NodeIt &i, int n) const {
klao@493
   128
      if( DM::range_check && (n<0 || n>int(length())) )
klao@493
   129
	fault("DirPath::nth: index out of range");
klao@619
   130
      return i=NodeIt(*this, n);
klao@619
   131
    }
klao@619
   132
klao@619
   133
    /// \brief Initializes edge iterator to point to the edge of a given index.
klao@619
   134
    EdgeIt& nth(EdgeIt &i, int n) const {
klao@619
   135
      if( DM::range_check && (n<0 || n>=int(length())) )
klao@619
   136
	fault("DirPath::nth: index out of range");
klao@619
   137
      return i=EdgeIt(*this, n);
klao@493
   138
    }
klao@369
   139
klao@493
   140
    /// Checks validity of a node or edge iterator.
klao@369
   141
    template<typename It>
klao@619
   142
    static
klao@619
   143
    bool valid(const It &i) { return i.valid(); }
klao@369
   144
klao@493
   145
    /// Steps the given node or edge iterator.
klao@369
   146
    template<typename It>
klao@619
   147
    static
klao@619
   148
    It& next(It &e) {
klao@493
   149
      if( DM::range_check && !e.valid() )
klao@493
   150
	fault("DirPath::next() on invalid iterator");
klao@493
   151
      return ++e;
klao@493
   152
    }
klao@369
   153
klao@493
   154
    /// \brief Returns node iterator pointing to the head node of the
klao@493
   155
    /// given edge iterator.
klao@493
   156
    NodeIt head(const EdgeIt& e) const {
klao@619
   157
      if( DM::range_check && !e.valid() )
klao@619
   158
	fault("DirPath::head() on invalid iterator");
klao@493
   159
      return NodeIt(*this, e.idx+1);
klao@493
   160
    }
klao@493
   161
klao@493
   162
    /// \brief Returns node iterator pointing to the tail node of the
klao@493
   163
    /// given edge iterator.
klao@493
   164
    NodeIt tail(const EdgeIt& e) const {
klao@619
   165
      if( DM::range_check && !e.valid() )
klao@619
   166
	fault("DirPath::tail() on invalid iterator");
klao@493
   167
      return NodeIt(*this, e.idx);
klao@493
   168
    }
klao@369
   169
klao@369
   170
alpar@686
   171
    /* Iterator classes */
alpar@686
   172
alpar@686
   173
    /**
alpar@686
   174
     * \brief Iterator class to iterate on the edges of the paths
alpar@686
   175
     * 
alpar@686
   176
     * \ingroup paths
alpar@686
   177
     * This class is used to iterate on the edges of the paths
alpar@686
   178
     *
alpar@686
   179
     * Of course it converts to Graph::Edge
alpar@686
   180
     * 
alpar@686
   181
     * \todo Its interface differs from the standard edge iterator.
alpar@686
   182
     * Yes, it shouldn't.
alpar@686
   183
     */
klao@369
   184
    class EdgeIt {
klao@369
   185
      friend class DirPath;
klao@369
   186
klao@369
   187
      int idx;
klao@369
   188
      const DirPath *p;
klao@369
   189
    public:
alpar@686
   190
      /// Default constructor
klao@369
   191
      EdgeIt() {}
alpar@686
   192
      /// Invalid constructor
klao@369
   193
      EdgeIt(Invalid) : idx(-1), p(0) {}
alpar@686
   194
      /// Constructor with starting point
klao@369
   195
      EdgeIt(const DirPath &_p, int _idx = 0) :
klao@369
   196
	idx(_idx), p(&_p) { validate(); }
klao@369
   197
alpar@686
   198
      ///Validity check
klao@369
   199
      bool valid() const { return idx!=-1; }
klao@369
   200
alpar@686
   201
      ///Conversion to Graph::Edge
klao@369
   202
      operator GraphEdge () const {
klao@369
   203
	return valid() ? p->edges[idx] : INVALID;
klao@369
   204
      }
alpar@686
   205
alpar@686
   206
      /// Next edge
klao@369
   207
      EdgeIt& operator++() { ++idx; validate(); return *this; }
klao@369
   208
alpar@686
   209
      /// Comparison operator
klao@369
   210
      bool operator==(const EdgeIt& e) const { return idx==e.idx; }
alpar@686
   211
      /// Comparison operator
klao@369
   212
      bool operator!=(const EdgeIt& e) const { return idx!=e.idx; }
alpar@686
   213
      /// Comparison operator
klao@369
   214
      bool operator<(const EdgeIt& e) const { return idx<e.idx; }
klao@369
   215
klao@369
   216
    private:
klao@369
   217
      // FIXME: comparison between signed and unsigned...
klao@369
   218
      // Jo ez igy? Vagy esetleg legyen a length() int?
klao@369
   219
      void validate() { if( size_t(idx) >= p->length() ) idx=-1; }
klao@369
   220
    };
klao@369
   221
alpar@686
   222
    /**
alpar@686
   223
     * \brief Iterator class to iterate on the nodes of the paths
alpar@686
   224
     * 
alpar@686
   225
     * \ingroup paths
alpar@686
   226
     * This class is used to iterate on the nodes of the paths
alpar@686
   227
     *
alpar@686
   228
     * Of course it converts to Graph::Node
alpar@686
   229
     * 
alpar@686
   230
     * \todo Its interface differs from the standard node iterator.
alpar@686
   231
     * Yes, it shouldn't.
alpar@686
   232
     */
klao@369
   233
    class NodeIt {
klao@369
   234
      friend class DirPath;
klao@369
   235
klao@369
   236
      int idx;
klao@369
   237
      const DirPath *p;
klao@369
   238
    public:
alpar@686
   239
      /// Default constructor
klao@369
   240
      NodeIt() {}
alpar@686
   241
      /// Invalid constructor
klao@369
   242
      NodeIt(Invalid) : idx(-1), p(0) {}
alpar@686
   243
      /// Constructor with starting point
klao@369
   244
      NodeIt(const DirPath &_p, int _idx = 0) :
klao@369
   245
	idx(_idx), p(&_p) { validate(); }
klao@369
   246
alpar@686
   247
      ///Validity check
klao@369
   248
      bool valid() const { return idx!=-1; }
klao@369
   249
alpar@686
   250
      ///Conversion to Graph::Node
klao@619
   251
      operator const GraphNode& () const {
klao@369
   252
	if(idx >= p->length())
klao@369
   253
	  return p->to();
klao@369
   254
	else if(idx >= 0)
klao@369
   255
	  return p->gr->tail(p->edges[idx]);
klao@369
   256
	else
klao@369
   257
	  return INVALID;
klao@369
   258
      }
alpar@686
   259
      /// Next node
klao@369
   260
      NodeIt& operator++() { ++idx; validate(); return *this; }
klao@369
   261
alpar@686
   262
      /// Comparison operator
klao@369
   263
      bool operator==(const NodeIt& e) const { return idx==e.idx; }
alpar@686
   264
      /// Comparison operator
klao@369
   265
      bool operator!=(const NodeIt& e) const { return idx!=e.idx; }
alpar@686
   266
      /// Comparison operator
klao@369
   267
      bool operator<(const NodeIt& e) const { return idx<e.idx; }
klao@369
   268
klao@369
   269
    private:
klao@369
   270
      void validate() { if( size_t(idx) > p->length() ) idx=-1; }
klao@369
   271
    };
klao@369
   272
klao@369
   273
    friend class Builder;    
alpar@434
   274
klao@493
   275
    /**
klao@493
   276
     * \brief Class to build paths
klao@493
   277
     * 
alpar@686
   278
     * \ingroup paths
klao@493
   279
     * This class is used to fill a path with edges.
klao@493
   280
     *
klao@493
   281
     * You can push new edges to the front and to the back of the path in
klao@619
   282
     * arbitrary order then you should commit these changes to the graph.
klao@493
   283
     *
klao@493
   284
     * Fundamentally, for most "Paths" (classes fulfilling the
klao@493
   285
     * PathConcept) while the builder is active (after the first modifying
klao@493
   286
     * operation and until the commit()) the original Path is in a
alpar@683
   287
     * "transitional" state (operations on it have undefined result). But
alpar@682
   288
     * in the case of DirPath the original path remains unchanged until the
klao@493
   289
     * commit. However we don't recomend that you use this feature.
klao@493
   290
     */
klao@369
   291
    class Builder {
klao@369
   292
      DirPath &P;
klao@493
   293
      Container front, back;
klao@369
   294
klao@369
   295
    public:
klao@493
   296
      ///\param _P the path you want to fill in.
alpar@434
   297
      ///
klao@369
   298
      Builder(DirPath &_P) : P(_P) {}
klao@369
   299
klao@619
   300
      /// Sets the starting node of the path.
alpar@434
   301
      
klao@619
   302
      /// Sets the starting node of the path. Edge added to the path
klao@619
   303
      /// afterwards have to be incident to this node.
klao@619
   304
      /// It should be called iff the path is empty and before any call to
klao@619
   305
      /// \ref pushFront() or \ref pushBack()
klao@619
   306
      void setStart(const GraphNode &) {}
klao@619
   307
alpar@434
   308
      ///Push a new edge to the front of the path
alpar@434
   309
alpar@434
   310
      ///Push a new edge to the front of the path.
klao@619
   311
      ///\sa setStart
klao@493
   312
      void pushFront(const GraphEdge& e) {
klao@493
   313
	if( DM::consistensy_check && !empty() && P.gr->head(e)!=from() ) {
klao@493
   314
	  fault("DirPath::Builder::pushFront: nonincident edge");
klao@369
   315
	}
klao@493
   316
	front.push_back(e);
klao@369
   317
      }
klao@493
   318
alpar@434
   319
      ///Push a new edge to the back of the path
alpar@434
   320
alpar@434
   321
      ///Push a new edge to the back of the path.
klao@619
   322
      ///\sa setStart
klao@493
   323
      void pushBack(const GraphEdge& e) {
klao@493
   324
	if( DM::consistensy_check && !empty() && P.gr->tail(e)!=to() ) {
klao@493
   325
	  fault("DirPath::Builder::pushBack: nonincident edge");
klao@369
   326
	}
klao@493
   327
	back.push_back(e);
klao@369
   328
      }
klao@369
   329
alpar@434
   330
      ///Commit the changes to the path.
klao@369
   331
      void commit() {
klao@493
   332
	if( !(front.empty() && back.empty()) ) {
klao@493
   333
	  Container tmp;
klao@493
   334
	  tmp.reserve(front.size()+back.size()+P.length());
klao@493
   335
	  tmp.insert(tmp.end(), front.rbegin(), front.rend());
klao@493
   336
	  tmp.insert(tmp.end(), P.edges.begin(), P.edges.end());
klao@493
   337
	  tmp.insert(tmp.end(), back.begin(), back.end());
klao@493
   338
	  P.edges.swap(tmp);
klao@493
   339
	  front.clear();
klao@493
   340
	  back.clear();
klao@369
   341
	}
klao@369
   342
      }
klao@369
   343
klao@619
   344
      // FIXME: Hmm, pontosan hogy is kene ezt csinalni?
klao@619
   345
      // Hogy kenyelmes egy ilyet hasznalni?
alpar@686
   346
  
alpar@686
   347
      ///Reserve storage in advance for the builder
alpar@686
   348
alpar@686
   349
      ///If you know an reasonable upper bound of the number of the edges
alpar@686
   350
      ///to add, using this function you can speed up the building.
klao@619
   351
      void reserve(size_t r) {
klao@619
   352
	front.reserve(r);
klao@619
   353
	back.reserve(r);
klao@619
   354
      }
alpar@434
   355
klao@619
   356
    private:
klao@619
   357
      bool empty() {
klao@619
   358
	return front.empty() && back.empty() && P.empty();
klao@619
   359
      }
klao@619
   360
klao@619
   361
      GraphNode from() const {
klao@619
   362
	if( ! front.empty() )
klao@619
   363
	  return P.gr->tail(front[front.size()-1]);
klao@619
   364
	else if( ! P.empty() )
klao@619
   365
	  return P.gr->tail(P.edges[0]);
klao@619
   366
	else if( ! back.empty() )
klao@619
   367
	  return P.gr->tail(back[0]);
klao@619
   368
	else
klao@619
   369
	  return INVALID;
klao@619
   370
      }
klao@619
   371
      GraphNode to() const {
klao@619
   372
	if( ! back.empty() )
klao@619
   373
	  return P.gr->head(back[back.size()-1]);
klao@619
   374
	else if( ! P.empty() )
klao@619
   375
	  return P.gr->head(P.edges[P.length()-1]);
klao@619
   376
	else if( ! front.empty() )
klao@619
   377
	  return P.gr->head(front[0]);
klao@619
   378
	else
klao@619
   379
	  return INVALID;
klao@619
   380
      }
klao@619
   381
klao@619
   382
    };
klao@619
   383
klao@619
   384
  };
klao@619
   385
klao@619
   386
klao@619
   387
klao@619
   388
klao@619
   389
klao@619
   390
klao@619
   391
klao@619
   392
klao@619
   393
klao@619
   394
klao@619
   395
  /**********************************************************************/
klao@619
   396
klao@619
   397
klao@619
   398
  //! \brief A structure for representing undirected path in a graph.
klao@619
   399
  //!
klao@619
   400
  //! A structure for representing undirected path in a graph. Ie. this is
klao@619
   401
  //! a path in a \e directed graph but the edges should not be directed
klao@619
   402
  //! forward.
klao@619
   403
  //!
klao@619
   404
  //! \param Graph The graph type in which the path is.
klao@619
   405
  //! \param DM DebugMode, defaults to DefaultDebugMode.
klao@619
   406
  //! 
klao@619
   407
  //! In a sense, the path can be treated as a graph, for is has \c NodeIt
klao@619
   408
  //! and \c EdgeIt with the same usage. These types converts to the \c Node
klao@619
   409
  //! and \c Edge of the original graph.
klao@619
   410
  //!
klao@619
   411
  //! \todo Thoroughfully check all the range and consistency tests.
klao@619
   412
  template<typename Graph, typename DM = DefaultDebugMode>
klao@619
   413
  class UndirPath {
klao@619
   414
  public:
alpar@686
   415
    /// Edge type of the underlying graph.
klao@619
   416
    typedef typename Graph::Edge GraphEdge;
alpar@686
   417
     /// Node type of the underlying graph.
alpar@686
   418
   typedef typename Graph::Node GraphNode;
klao@619
   419
    class NodeIt;
klao@619
   420
    class EdgeIt;
klao@619
   421
klao@619
   422
  protected:
klao@619
   423
    const Graph *gr;
klao@619
   424
    typedef std::vector<GraphEdge> Container;
klao@619
   425
    Container edges;
klao@619
   426
klao@619
   427
  public:
klao@619
   428
klao@619
   429
    /// \param _G The graph in which the path is.
klao@619
   430
    ///
klao@619
   431
    UndirPath(const Graph &_G) : gr(&_G) {}
klao@619
   432
klao@619
   433
    /// \brief Subpath constructor.
klao@619
   434
    ///
klao@619
   435
    /// Subpath defined by two nodes.
klao@619
   436
    /// \warning It is an error if the two edges are not in order!
klao@619
   437
    UndirPath(const UndirPath &P, const NodeIt &a, const NodeIt &b) {
klao@619
   438
      if( DM::range_check && (!a.valid() || !b.valid) ) {
klao@619
   439
	// FIXME: this check should be more elaborate...
klao@619
   440
	fault("UndirPath, subpath ctor: invalid bounding nodes");
klao@619
   441
      }
klao@619
   442
      gr = P.gr;
klao@619
   443
      edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
klao@619
   444
    }
klao@619
   445
klao@619
   446
    /// \brief Subpath constructor.
klao@619
   447
    ///
klao@619
   448
    /// Subpath defined by two edges. Contains edges in [a,b)
klao@619
   449
    /// \warning It is an error if the two edges are not in order!
klao@619
   450
    UndirPath(const UndirPath &P, const EdgeIt &a, const EdgeIt &b) {
klao@619
   451
      if( DM::range_check && (!a.valid() || !b.valid) ) {
klao@619
   452
	// FIXME: this check should be more elaborate...
klao@619
   453
	fault("UndirPath, subpath ctor: invalid bounding nodes");
klao@619
   454
      }
klao@619
   455
      gr = P.gr;
klao@619
   456
      edges.insert(edges.end(), P.edges.begin()+a.idx, P.edges.begin()+b.idx);
klao@619
   457
    }
klao@619
   458
klao@619
   459
    /// Length of the path.
klao@619
   460
    size_t length() const { return edges.size(); }
klao@619
   461
    /// Returns whether the path is empty.
klao@619
   462
    bool empty() const { return edges.empty(); }
klao@619
   463
klao@619
   464
    /// Resets the path to an empty path.
klao@619
   465
    void clear() { edges.clear(); }
klao@619
   466
klao@619
   467
    /// \brief Starting point of the path.
klao@619
   468
    ///
klao@619
   469
    /// Starting point of the path.
klao@619
   470
    /// Returns INVALID if the path is empty.
klao@619
   471
    GraphNode from() const {
klao@619
   472
      return empty() ? INVALID : gr->tail(edges[0]);
klao@619
   473
    }
klao@619
   474
    /// \brief End point of the path.
klao@619
   475
    ///
klao@619
   476
    /// End point of the path.
klao@619
   477
    /// Returns INVALID if the path is empty.
klao@619
   478
    GraphNode to() const {
klao@619
   479
      return empty() ? INVALID : gr->head(edges[length()-1]);
klao@619
   480
    }
klao@619
   481
klao@619
   482
    /// \brief Initializes node or edge iterator to point to the first
klao@619
   483
    /// node or edge.
klao@619
   484
    ///
klao@619
   485
    /// \sa nth
klao@619
   486
    template<typename It>
klao@619
   487
    It& first(It &i) const { return i=It(*this); }
klao@619
   488
klao@619
   489
    /// \brief Initializes node iterator to point to the node of a given index.
klao@619
   490
    NodeIt& nth(NodeIt &i, int n) const {
klao@619
   491
      if( DM::range_check && (n<0 || n>int(length())) )
klao@619
   492
	fault("UndirPath::nth: index out of range");
klao@619
   493
      return i=NodeIt(*this, n);
klao@619
   494
    }
klao@619
   495
klao@619
   496
    /// \brief Initializes edge iterator to point to the edge of a given index.
klao@619
   497
    EdgeIt& nth(EdgeIt &i, int n) const {
klao@619
   498
      if( DM::range_check && (n<0 || n>=int(length())) )
klao@619
   499
	fault("UndirPath::nth: index out of range");
klao@619
   500
      return i=EdgeIt(*this, n);
klao@619
   501
    }
klao@619
   502
klao@619
   503
    /// Checks validity of a node or edge iterator.
klao@619
   504
    template<typename It>
klao@619
   505
    static
klao@619
   506
    bool valid(const It &i) { return i.valid(); }
klao@619
   507
klao@619
   508
    /// Steps the given node or edge iterator.
klao@619
   509
    template<typename It>
klao@619
   510
    static
klao@619
   511
    It& next(It &e) {
klao@619
   512
      if( DM::range_check && !e.valid() )
klao@619
   513
	fault("UndirPath::next() on invalid iterator");
klao@619
   514
      return ++e;
klao@619
   515
    }
klao@619
   516
klao@619
   517
    /// \brief Returns node iterator pointing to the head node of the
klao@619
   518
    /// given edge iterator.
klao@619
   519
    NodeIt head(const EdgeIt& e) const {
klao@619
   520
      if( DM::range_check && !e.valid() )
klao@619
   521
	fault("UndirPath::head() on invalid iterator");
klao@619
   522
      return NodeIt(*this, e.idx+1);
klao@619
   523
    }
klao@619
   524
klao@619
   525
    /// \brief Returns node iterator pointing to the tail node of the
klao@619
   526
    /// given edge iterator.
klao@619
   527
    NodeIt tail(const EdgeIt& e) const {
klao@619
   528
      if( DM::range_check && !e.valid() )
klao@619
   529
	fault("UndirPath::tail() on invalid iterator");
klao@619
   530
      return NodeIt(*this, e.idx);
klao@619
   531
    }
klao@619
   532
klao@619
   533
alpar@686
   534
alpar@686
   535
    /**
alpar@686
   536
     * \brief Iterator class to iterate on the edges of the paths
alpar@686
   537
     * 
alpar@686
   538
     * \ingroup paths
alpar@686
   539
     * This class is used to iterate on the edges of the paths
alpar@686
   540
     *
alpar@686
   541
     * Of course it converts to Graph::Edge
alpar@686
   542
     * 
alpar@686
   543
     * \todo Its interface differs from the standard edge iterator.
alpar@686
   544
     * Yes, it shouldn't.
alpar@686
   545
     */
klao@619
   546
    class EdgeIt {
klao@619
   547
      friend class UndirPath;
klao@619
   548
klao@619
   549
      int idx;
klao@619
   550
      const UndirPath *p;
klao@619
   551
    public:
alpar@686
   552
      /// Default constructor
klao@619
   553
      EdgeIt() {}
alpar@686
   554
      /// Invalid constructor
klao@619
   555
      EdgeIt(Invalid) : idx(-1), p(0) {}
alpar@686
   556
      /// Constructor with starting point
klao@619
   557
      EdgeIt(const UndirPath &_p, int _idx = 0) :
klao@619
   558
	idx(_idx), p(&_p) { validate(); }
klao@619
   559
alpar@686
   560
      ///Validity check
klao@619
   561
      bool valid() const { return idx!=-1; }
klao@619
   562
alpar@686
   563
      ///Conversion to Graph::Edge
klao@619
   564
      operator GraphEdge () const {
klao@619
   565
	return valid() ? p->edges[idx] : INVALID;
klao@619
   566
      }
alpar@686
   567
      /// Next edge
alpar@686
   568
     EdgeIt& operator++() { ++idx; validate(); return *this; }
klao@619
   569
alpar@686
   570
      /// Comparison operator
klao@619
   571
      bool operator==(const EdgeIt& e) const { return idx==e.idx; }
alpar@686
   572
      /// Comparison operator
klao@619
   573
      bool operator!=(const EdgeIt& e) const { return idx!=e.idx; }
alpar@686
   574
      /// Comparison operator
klao@619
   575
      bool operator<(const EdgeIt& e) const { return idx<e.idx; }
klao@619
   576
klao@619
   577
    private:
klao@619
   578
      // FIXME: comparison between signed and unsigned...
klao@619
   579
      // Jo ez igy? Vagy esetleg legyen a length() int?
klao@619
   580
      void validate() { if( size_t(idx) >= p->length() ) idx=-1; }
klao@619
   581
    };
klao@619
   582
alpar@686
   583
    /**
alpar@686
   584
     * \brief Iterator class to iterate on the nodes of the paths
alpar@686
   585
     * 
alpar@686
   586
     * \ingroup paths
alpar@686
   587
     * This class is used to iterate on the nodes of the paths
alpar@686
   588
     *
alpar@686
   589
     * Of course it converts to Graph::Node
alpar@686
   590
     * 
alpar@686
   591
     * \todo Its interface differs from the standard node iterator.
alpar@686
   592
     * Yes, it shouldn't.
alpar@686
   593
     */
klao@619
   594
    class NodeIt {
klao@619
   595
      friend class UndirPath;
klao@619
   596
klao@619
   597
      int idx;
klao@619
   598
      const UndirPath *p;
klao@619
   599
    public:
alpar@686
   600
      /// Default constructor
klao@619
   601
      NodeIt() {}
alpar@686
   602
      /// Invalid constructor
klao@619
   603
      NodeIt(Invalid) : idx(-1), p(0) {}
alpar@686
   604
      /// Constructor with starting point
klao@619
   605
      NodeIt(const UndirPath &_p, int _idx = 0) :
klao@619
   606
	idx(_idx), p(&_p) { validate(); }
klao@619
   607
alpar@686
   608
      ///Validity check
klao@619
   609
      bool valid() const { return idx!=-1; }
klao@619
   610
alpar@686
   611
      ///Conversion to Graph::Node
klao@619
   612
      operator const GraphNode& () const {
klao@619
   613
	if(idx >= p->length())
klao@619
   614
	  return p->to();
klao@619
   615
	else if(idx >= 0)
klao@619
   616
	  return p->gr->tail(p->edges[idx]);
klao@619
   617
	else
klao@619
   618
	  return INVALID;
klao@619
   619
      }
alpar@686
   620
      /// Next node
klao@619
   621
      NodeIt& operator++() { ++idx; validate(); return *this; }
klao@619
   622
alpar@686
   623
      /// Comparison operator
klao@619
   624
      bool operator==(const NodeIt& e) const { return idx==e.idx; }
alpar@686
   625
      /// Comparison operator
klao@619
   626
      bool operator!=(const NodeIt& e) const { return idx!=e.idx; }
alpar@686
   627
       /// Comparison operator
alpar@686
   628
     bool operator<(const NodeIt& e) const { return idx<e.idx; }
klao@619
   629
klao@619
   630
    private:
klao@619
   631
      void validate() { if( size_t(idx) > p->length() ) idx=-1; }
klao@619
   632
    };
klao@619
   633
klao@619
   634
    friend class Builder;    
klao@619
   635
klao@619
   636
    /**
klao@619
   637
     * \brief Class to build paths
klao@619
   638
     * 
alpar@686
   639
     * \ingroup paths
klao@619
   640
     * This class is used to fill a path with edges.
klao@619
   641
     *
klao@619
   642
     * You can push new edges to the front and to the back of the path in
klao@619
   643
     * arbitrary order then you should commit these changes to the graph.
klao@619
   644
     *
klao@619
   645
     * Fundamentally, for most "Paths" (classes fulfilling the
klao@619
   646
     * PathConcept) while the builder is active (after the first modifying
klao@619
   647
     * operation and until the commit()) the original Path is in a
klao@619
   648
     * "transitional" state (operations ot it have undefined result). But
klao@619
   649
     * in the case of UndirPath the original path is unchanged until the
klao@619
   650
     * commit. However we don't recomend that you use this feature.
klao@619
   651
     */
klao@619
   652
    class Builder {
klao@619
   653
      UndirPath &P;
klao@619
   654
      Container front, back;
klao@619
   655
klao@619
   656
    public:
klao@619
   657
      ///\param _P the path you want to fill in.
klao@619
   658
      ///
klao@619
   659
      Builder(UndirPath &_P) : P(_P) {}
klao@619
   660
klao@619
   661
      /// Sets the starting node of the path.
klao@619
   662
      
klao@619
   663
      /// Sets the starting node of the path. Edge added to the path
klao@619
   664
      /// afterwards have to be incident to this node.
klao@619
   665
      /// It should be called iff the path is empty and before any call to
klao@619
   666
      /// \ref pushFront() or \ref pushBack()
klao@619
   667
      void setStart(const GraphNode &) {}
klao@619
   668
klao@619
   669
      ///Push a new edge to the front of the path
klao@619
   670
klao@619
   671
      ///Push a new edge to the front of the path.
klao@619
   672
      ///\sa setStart
klao@619
   673
      void pushFront(const GraphEdge& e) {
klao@619
   674
	if( DM::consistensy_check && !empty() && P.gr->head(e)!=from() ) {
klao@619
   675
	  fault("UndirPath::Builder::pushFront: nonincident edge");
klao@619
   676
	}
klao@619
   677
	front.push_back(e);
klao@619
   678
      }
klao@619
   679
klao@619
   680
      ///Push a new edge to the back of the path
klao@619
   681
klao@619
   682
      ///Push a new edge to the back of the path.
klao@619
   683
      ///\sa setStart
klao@619
   684
      void pushBack(const GraphEdge& e) {
klao@619
   685
	if( DM::consistensy_check && !empty() && P.gr->tail(e)!=to() ) {
klao@619
   686
	  fault("UndirPath::Builder::pushBack: nonincident edge");
klao@619
   687
	}
klao@619
   688
	back.push_back(e);
klao@619
   689
      }
klao@619
   690
klao@619
   691
      ///Commit the changes to the path.
klao@619
   692
      void commit() {
klao@619
   693
	if( !(front.empty() && back.empty()) ) {
klao@619
   694
	  Container tmp;
klao@619
   695
	  tmp.reserve(front.size()+back.size()+P.length());
klao@619
   696
	  tmp.insert(tmp.end(), front.rbegin(), front.rend());
klao@619
   697
	  tmp.insert(tmp.end(), P.edges.begin(), P.edges.end());
klao@619
   698
	  tmp.insert(tmp.end(), back.begin(), back.end());
klao@619
   699
	  P.edges.swap(tmp);
klao@619
   700
	  front.clear();
klao@619
   701
	  back.clear();
klao@619
   702
	}
klao@619
   703
      }
klao@369
   704
klao@369
   705
      // FIXME: Hmm, pontosan hogy is kene ezt csinalni?
klao@369
   706
      // Hogy kenyelmes egy ilyet hasznalni?
alpar@686
   707
alpar@686
   708
      ///Reserve storage in advance for the builder
alpar@686
   709
alpar@686
   710
      ///If you know an reasonable upper bound of the number of the edges
alpar@686
   711
      ///to add, using this function you can speed up the building.
alpar@686
   712
       void reserve(size_t r) {
klao@493
   713
	front.reserve(r);
klao@493
   714
	back.reserve(r);
klao@369
   715
      }
klao@369
   716
klao@369
   717
    private:
klao@493
   718
      bool empty() {
klao@493
   719
	return front.empty() && back.empty() && P.empty();
klao@493
   720
      }
klao@369
   721
klao@369
   722
      GraphNode from() const {
klao@493
   723
	if( ! front.empty() )
klao@493
   724
	  return P.gr->tail(front[front.size()-1]);
klao@369
   725
	else if( ! P.empty() )
klao@369
   726
	  return P.gr->tail(P.edges[0]);
klao@493
   727
	else if( ! back.empty() )
klao@493
   728
	  return P.gr->tail(back[0]);
klao@369
   729
	else
klao@369
   730
	  return INVALID;
klao@369
   731
      }
klao@369
   732
      GraphNode to() const {
klao@493
   733
	if( ! back.empty() )
klao@493
   734
	  return P.gr->head(back[back.size()-1]);
klao@493
   735
	else if( ! P.empty() )
klao@369
   736
	  return P.gr->head(P.edges[P.length()-1]);
klao@493
   737
	else if( ! front.empty() )
klao@493
   738
	  return P.gr->head(front[0]);
klao@369
   739
	else
klao@369
   740
	  return INVALID;
klao@369
   741
      }
klao@369
   742
klao@369
   743
    };
klao@369
   744
klao@369
   745
  };
klao@369
   746
klao@369
   747
klao@369
   748
klao@369
   749
klao@369
   750
klao@369
   751
klao@369
   752
klao@369
   753
klao@369
   754
klao@369
   755
klao@369
   756
  /**********************************************************************/
klao@369
   757
klao@369
   758
klao@225
   759
  /* Ennek az allocatorosdinak sokkal jobban utana kene nezni a hasznalata
klao@225
   760
     elott. Eleg bonyinak nez ki, ahogyan azokat az STL-ben hasznaljak. */
klao@225
   761
klao@225
   762
  template<typename Graph>
klao@369
   763
  class DynamicPath {
klao@225
   764
klao@225
   765
  public:
klao@225
   766
    typedef typename Graph::Edge GraphEdge;
klao@225
   767
    typedef typename Graph::Node GraphNode;
klao@225
   768
    class NodeIt;
klao@225
   769
    class EdgeIt;
klao@225
   770
klao@225
   771
  protected:
klao@225
   772
    Graph& G;
klao@225
   773
    // FIXME: ehelyett eleg lenne tarolni ket boolt: a ket szelso el
klao@225
   774
    // iranyitasat:
klao@225
   775
    GraphNode _first, _last;
klao@225
   776
    typedef std::deque<GraphEdge> Container;
klao@225
   777
    Container edges;
klao@225
   778
klao@225
   779
  public:
klao@225
   780
klao@369
   781
    DynamicPath(Graph &_G) : G(_G), _first(INVALID), _last(INVALID) {}
klao@225
   782
klao@226
   783
    /// Subpath defined by two nodes.
klao@226
   784
    /// Nodes may be in reversed order, then
klao@226
   785
    /// we contstruct the reversed path.
klao@369
   786
    DynamicPath(const DynamicPath &P, const NodeIt &a, const NodeIt &b);
klao@226
   787
    /// Subpath defined by two edges. Contains edges in [a,b)
klao@226
   788
    /// It is an error if the two edges are not in order!
klao@369
   789
    DynamicPath(const DynamicPath &P, const EdgeIt &a, const EdgeIt &b);
klao@225
   790
    
klao@225
   791
    size_t length() const { return edges.size(); }
klao@225
   792
    GraphNode from() const { return _first; }
klao@225
   793
    GraphNode to() const { return _last; }
klao@225
   794
klao@225
   795
    NodeIt& first(NodeIt &n) const { return nth(n, 0); }
klao@225
   796
    EdgeIt& first(EdgeIt &e) const { return nth(e, 0); }
klao@225
   797
    template<typename It>
klao@225
   798
    It first() const { 
klao@225
   799
      It e;
klao@225
   800
      first(e);
klao@225
   801
      return e; 
klao@225
   802
    }
klao@225
   803
klao@225
   804
    NodeIt& nth(NodeIt &, size_t) const;
klao@225
   805
    EdgeIt& nth(EdgeIt &, size_t) const;
klao@225
   806
    template<typename It>
klao@225
   807
    It nth(size_t n) const { 
klao@225
   808
      It e;
klao@225
   809
      nth(e, n);
klao@225
   810
      return e; 
klao@225
   811
    }
klao@225
   812
klao@225
   813
    bool valid(const NodeIt &n) const { return n.idx <= length(); }
klao@225
   814
    bool valid(const EdgeIt &e) const { return e.it < edges.end(); }
klao@225
   815
klao@225
   816
    bool isForward(const EdgeIt &e) const { return e.forw; }
klao@225
   817
klao@226
   818
    /// index of a node on the path. Returns length+2 for the invalid NodeIt
klao@226
   819
    int index(const NodeIt &n) const { return n.idx; }
klao@226
   820
    /// index of an edge on the path. Returns length+1 for the invalid EdgeIt
klao@226
   821
    int index(const EdgeIt &e) const { return e.it - edges.begin(); }
klao@226
   822
klao@225
   823
    EdgeIt& next(EdgeIt &e) const;
klao@225
   824
    NodeIt& next(NodeIt &n) const;
klao@225
   825
    template <typename It>
klao@225
   826
    It getNext(It it) const {
klao@225
   827
      It tmp(it); return next(tmp);
klao@225
   828
    }
klao@225
   829
klao@225
   830
    // A path is constructed using the following four functions.
klao@225
   831
    // They return false if the requested operation is inconsistent
klao@225
   832
    // with the path constructed so far.
klao@225
   833
    // If your path has only one edge you MUST set either "from" or "to"!
klao@225
   834
    // So you probably SHOULD call it in any case to be safe (and check the
klao@225
   835
    // returned value to check if your path is consistent with your idea).
klao@225
   836
    bool pushFront(const GraphEdge &e);
klao@225
   837
    bool pushBack(const GraphEdge &e);
klao@225
   838
    bool setFrom(const GraphNode &n);
klao@225
   839
    bool setTo(const GraphNode &n);
klao@225
   840
klao@225
   841
    // WARNING: these two functions return the head/tail of an edge with
klao@225
   842
    // respect to the direction of the path!
klao@225
   843
    // So G.head(P.graphEdge(e)) == P.graphNode(P.head(e)) holds only if 
klao@225
   844
    // P.forward(e) is true (or the edge is a loop)!
klao@225
   845
    NodeIt head(const EdgeIt& e) const;
klao@225
   846
    NodeIt tail(const EdgeIt& e) const;
klao@225
   847
klao@225
   848
    // FIXME: ezeknek valami jobb nev kellene!!!
klao@225
   849
    GraphEdge graphEdge(const EdgeIt& e) const;
klao@225
   850
    GraphNode graphNode(const NodeIt& n) const;
klao@225
   851
klao@225
   852
klao@225
   853
    /*** Iterator classes ***/
klao@225
   854
    class EdgeIt {
klao@369
   855
      friend class DynamicPath;
klao@225
   856
klao@225
   857
      typename Container::const_iterator it;
klao@225
   858
      bool forw;
klao@225
   859
    public:
klao@225
   860
      // FIXME: jarna neki ilyen is...
klao@225
   861
      // EdgeIt(Invalid);
klao@225
   862
klao@225
   863
      bool forward() const { return forw; }
klao@225
   864
klao@225
   865
      bool operator==(const EdgeIt& e) const { return it==e.it; }
klao@225
   866
      bool operator!=(const EdgeIt& e) const { return it!=e.it; }
klao@225
   867
      bool operator<(const EdgeIt& e) const { return it<e.it; }
klao@225
   868
    };
klao@225
   869
klao@225
   870
    class NodeIt {
klao@369
   871
      friend class DynamicPath;
klao@225
   872
klao@226
   873
      size_t idx;
klao@225
   874
      bool tail;  // Is this node the tail of the edge with same idx?
klao@225
   875
klao@225
   876
    public:
klao@225
   877
      // FIXME: jarna neki ilyen is...
klao@225
   878
      // NodeIt(Invalid);
klao@225
   879
klao@225
   880
      bool operator==(const NodeIt& n) const { return idx==n.idx; }
klao@225
   881
      bool operator!=(const NodeIt& n) const { return idx!=n.idx; }
klao@225
   882
      bool operator<(const NodeIt& n) const { return idx<n.idx; }
klao@225
   883
    };
klao@225
   884
klao@225
   885
  private:
klao@225
   886
    bool edgeIncident(const GraphEdge &e, const GraphNode &a,
klao@225
   887
		      GraphNode &b);
klao@225
   888
    bool connectTwoEdges(const GraphEdge &e, const GraphEdge &f);
klao@225
   889
  };
klao@225
   890
klao@225
   891
  template<typename Gr>
klao@369
   892
  typename DynamicPath<Gr>::EdgeIt&
klao@369
   893
  DynamicPath<Gr>::next(DynamicPath::EdgeIt &e) const {
klao@225
   894
    if( e.it == edges.end() ) 
klao@225
   895
      return e;
klao@225
   896
klao@225
   897
    GraphNode common_node = ( e.forw ? G.head(*e.it) : G.tail(*e.it) );
klao@225
   898
    ++e.it;
klao@225
   899
klao@225
   900
    // Invalid edgeit is always forward :)
klao@225
   901
    if( e.it == edges.end() ) {
klao@225
   902
      e.forw = true;
klao@225
   903
      return e;
klao@225
   904
    }
klao@225
   905
klao@225
   906
    e.forw = ( G.tail(*e.it) == common_node );
klao@225
   907
    return e;
klao@225
   908
  }
klao@225
   909
klao@225
   910
  template<typename Gr>
klao@369
   911
  typename DynamicPath<Gr>::NodeIt& DynamicPath<Gr>::next(NodeIt &n) const {
klao@225
   912
    if( n.idx >= length() ) {
klao@225
   913
      // FIXME: invalid
klao@225
   914
      n.idx = length()+1;
klao@225
   915
      return n;
klao@225
   916
    }
klao@225
   917
klao@225
   918
    
klao@225
   919
    GraphNode next_node = ( n.tail ? G.head(edges[n.idx]) :
klao@225
   920
			      G.tail(edges[n.idx]) );
klao@225
   921
    ++n.idx;
klao@225
   922
    if( n.idx < length() ) {
klao@225
   923
      n.tail = ( next_node == G.tail(edges[n.idx]) );
klao@225
   924
    }
klao@225
   925
    else {
klao@225
   926
      n.tail = true;
klao@225
   927
    }
klao@225
   928
klao@225
   929
    return n;
klao@225
   930
  }
klao@225
   931
klao@225
   932
  template<typename Gr>
klao@369
   933
  bool DynamicPath<Gr>::edgeIncident(const GraphEdge &e, const GraphNode &a,
klao@225
   934
			  GraphNode &b) {
klao@225
   935
    if( G.tail(e) == a ) {
klao@225
   936
      b=G.head(e);
klao@225
   937
      return true;
klao@225
   938
    }
klao@225
   939
    if( G.head(e) == a ) {
klao@225
   940
      b=G.tail(e);
klao@225
   941
      return true;
klao@225
   942
    }
klao@225
   943
    return false;
klao@225
   944
  }
klao@225
   945
klao@225
   946
  template<typename Gr>
klao@369
   947
  bool DynamicPath<Gr>::connectTwoEdges(const GraphEdge &e,
klao@225
   948
			     const GraphEdge &f) {
klao@225
   949
    if( edgeIncident(f, G.tail(e), _last) ) {
klao@225
   950
      _first = G.head(e);
klao@225
   951
      return true;
klao@225
   952
    }
klao@225
   953
    if( edgeIncident(f, G.head(e), _last) ) {
klao@225
   954
      _first = G.tail(e);
klao@225
   955
      return true;
klao@225
   956
    }
klao@225
   957
    return false;
klao@225
   958
  }
klao@225
   959
klao@225
   960
  template<typename Gr>
klao@369
   961
  bool DynamicPath<Gr>::pushFront(const GraphEdge &e) {
klao@225
   962
    if( G.valid(_first) ) {
klao@225
   963
	if( edgeIncident(e, _first, _first) ) {
klao@225
   964
	  edges.push_front(e);
klao@225
   965
	  return true;
klao@225
   966
	}
klao@225
   967
	else
klao@225
   968
	  return false;
klao@225
   969
    }
klao@225
   970
    else if( length() < 1 || connectTwoEdges(e, edges[0]) ) {
klao@225
   971
      edges.push_front(e);
klao@225
   972
      return true;
klao@225
   973
    }
klao@225
   974
    else
klao@225
   975
      return false;
klao@225
   976
  }
klao@225
   977
klao@225
   978
  template<typename Gr>
klao@369
   979
  bool DynamicPath<Gr>::pushBack(const GraphEdge &e) {
klao@225
   980
    if( G.valid(_last) ) {
klao@225
   981
	if( edgeIncident(e, _last, _last) ) {
klao@225
   982
	  edges.push_back(e);
klao@225
   983
	  return true;
klao@225
   984
	}
klao@225
   985
	else
klao@225
   986
	  return false;
klao@225
   987
    }
klao@225
   988
    else if( length() < 1 || connectTwoEdges(edges[0], e) ) {
klao@225
   989
      edges.push_back(e);
klao@225
   990
      return true;
klao@225
   991
    }
klao@225
   992
    else
klao@225
   993
      return false;
klao@225
   994
  }
klao@225
   995
klao@225
   996
klao@225
   997
  template<typename Gr>
klao@369
   998
  bool DynamicPath<Gr>::setFrom(const GraphNode &n) {
klao@225
   999
    if( G.valid(_first) ) {
klao@225
  1000
      return _first == n;
klao@225
  1001
    }
klao@225
  1002
    else {
klao@225
  1003
      if( length() > 0) {
klao@225
  1004
	if( edgeIncident(edges[0], n, _last) ) {
klao@225
  1005
	  _first = n;
klao@225
  1006
	  return true;
klao@225
  1007
	}
klao@225
  1008
	else return false;
klao@225
  1009
      }
klao@225
  1010
      else {
klao@225
  1011
	_first = _last = n;
klao@225
  1012
	return true;
klao@225
  1013
      }
klao@225
  1014
    }
klao@225
  1015
  }
klao@225
  1016
klao@225
  1017
  template<typename Gr>
klao@369
  1018
  bool DynamicPath<Gr>::setTo(const GraphNode &n) {
klao@225
  1019
    if( G.valid(_last) ) {
klao@225
  1020
      return _last == n;
klao@225
  1021
    }
klao@225
  1022
    else {
klao@225
  1023
      if( length() > 0) {
klao@225
  1024
	if( edgeIncident(edges[0], n, _first) ) {
klao@225
  1025
	  _last = n;
klao@225
  1026
	  return true;
klao@225
  1027
	}
klao@225
  1028
	else return false;
klao@225
  1029
      }
klao@225
  1030
      else {
klao@225
  1031
	_first = _last = n;
klao@225
  1032
	return true;
klao@225
  1033
      }
klao@225
  1034
    }
klao@225
  1035
  }
klao@225
  1036
klao@225
  1037
klao@225
  1038
  template<typename Gr>
klao@369
  1039
  typename DynamicPath<Gr>::NodeIt
klao@369
  1040
  DynamicPath<Gr>::tail(const EdgeIt& e) const {
klao@225
  1041
    NodeIt n;
klao@225
  1042
klao@225
  1043
    if( e.it == edges.end() ) {
klao@225
  1044
      // FIXME: invalid-> invalid
klao@225
  1045
      n.idx = length() + 1;
klao@225
  1046
      n.tail = true;
klao@225
  1047
      return n;
klao@225
  1048
    }
klao@225
  1049
klao@225
  1050
    n.idx = e.it-edges.begin();
klao@225
  1051
    n.tail = e.forw;
klao@226
  1052
    return n;
klao@225
  1053
  }
klao@225
  1054
klao@225
  1055
  template<typename Gr>
klao@369
  1056
  typename DynamicPath<Gr>::NodeIt
klao@369
  1057
  DynamicPath<Gr>::head(const EdgeIt& e) const {
klao@225
  1058
    if( e.it == edges.end()-1 ) {
klao@225
  1059
      return _last;
klao@225
  1060
    }
klao@225
  1061
klao@225
  1062
    EdgeIt next_edge = e;
klao@225
  1063
    next(next_edge);
klao@225
  1064
    return tail(next_edge);
klao@225
  1065
  }
klao@225
  1066
      
klao@225
  1067
  template<typename Gr>
klao@369
  1068
  typename DynamicPath<Gr>::GraphEdge
klao@369
  1069
  DynamicPath<Gr>::graphEdge(const EdgeIt& e) const {
klao@225
  1070
    if( e.it != edges.end() ) {
klao@225
  1071
      return *e.it;
klao@225
  1072
    }
klao@225
  1073
    else {
klao@225
  1074
      return INVALID;
klao@225
  1075
    }
klao@225
  1076
  }
klao@225
  1077
  
klao@225
  1078
  template<typename Gr>
klao@369
  1079
  typename DynamicPath<Gr>::GraphNode
klao@369
  1080
  DynamicPath<Gr>::graphNode(const NodeIt& n) const {
klao@225
  1081
    if( n.idx < length() ) {
klao@225
  1082
      return n.tail ? G.tail(edges[n.idx]) : G.head(edges[n.idx]);
klao@225
  1083
    }
klao@225
  1084
    else if( n.idx == length() ) {
klao@225
  1085
      return _last;
klao@225
  1086
    }
klao@225
  1087
    else {
klao@225
  1088
      return INVALID;
klao@225
  1089
    }
klao@225
  1090
  }
klao@225
  1091
klao@225
  1092
  template<typename Gr>
klao@369
  1093
  typename DynamicPath<Gr>::EdgeIt&
klao@369
  1094
  DynamicPath<Gr>::nth(EdgeIt &e, size_t k) const {
klao@450
  1095
    if( k>=length() ) {
klao@225
  1096
      // FIXME: invalid EdgeIt
klao@225
  1097
      e.it = edges.end();
klao@225
  1098
      e.forw = true;
klao@225
  1099
      return e;
klao@225
  1100
    }
klao@225
  1101
klao@225
  1102
    e.it = edges.begin()+k;
klao@225
  1103
    if(k==0) {
klao@225
  1104
      e.forw = ( G.tail(*e.it) == _first );
klao@225
  1105
    }
klao@225
  1106
    else {
klao@225
  1107
      e.forw = ( G.tail(*e.it) == G.tail(edges[k-1]) ||
klao@225
  1108
		 G.tail(*e.it) == G.head(edges[k-1]) );
klao@225
  1109
    }
klao@225
  1110
    return e;
klao@225
  1111
  }
klao@225
  1112
    
klao@225
  1113
  template<typename Gr>
klao@369
  1114
  typename DynamicPath<Gr>::NodeIt&
klao@369
  1115
  DynamicPath<Gr>::nth(NodeIt &n, size_t k) const {
klao@450
  1116
    if( k>length() ) {
klao@225
  1117
      // FIXME: invalid NodeIt
klao@225
  1118
      n.idx = length()+1;
klao@225
  1119
      n.tail = true;
klao@225
  1120
      return n;
klao@225
  1121
    }
klao@225
  1122
    if( k==length() ) {
klao@225
  1123
      n.idx = length();
klao@225
  1124
      n.tail = true;
klao@225
  1125
      return n;
klao@225
  1126
    }
klao@225
  1127
    n = tail(nth<EdgeIt>(k));
klao@225
  1128
    return n;
klao@225
  1129
  }
klao@225
  1130
klao@226
  1131
  // Reszut konstruktorok:
klao@226
  1132
klao@226
  1133
klao@226
  1134
  template<typename Gr>
klao@369
  1135
  DynamicPath<Gr>::DynamicPath(const DynamicPath &P, const EdgeIt &a,
klao@369
  1136
			       const EdgeIt &b) :
klao@226
  1137
    G(P.G), edges(a.it, b.it)    // WARNING: if b.it < a.it this will blow up! 
klao@226
  1138
  {
klao@226
  1139
    if( G.valid(P._first) && a.it < P.edges.end() ) {
klao@226
  1140
      _first = ( a.forw ? G.tail(*a.it) : G.head(*a.it) );
klao@226
  1141
      if( b.it < P.edges.end() ) {
klao@226
  1142
	_last = ( b.forw ? G.tail(*b.it) : G.head(*b.it) );
klao@226
  1143
      }
klao@226
  1144
      else {
klao@226
  1145
	_last = P._last;
klao@226
  1146
      }
klao@226
  1147
    }
klao@226
  1148
  }
klao@226
  1149
klao@226
  1150
  template<typename Gr>
klao@369
  1151
  DynamicPath<Gr>::DynamicPath(const DynamicPath &P, const NodeIt &a,
klao@369
  1152
			       const NodeIt &b) : G(P.G)
klao@226
  1153
  {
klao@226
  1154
    if( !P.valid(a) || !P.valid(b) )
klao@226
  1155
      return;
klao@226
  1156
klao@226
  1157
    int ai = a.idx, bi = b.idx;
klao@226
  1158
    if( bi<ai )
klao@450
  1159
      std::swap(ai,bi);
klao@226
  1160
    
klao@226
  1161
    edges.resize(bi-ai);
klao@226
  1162
    copy(P.edges.begin()+ai, P.edges.begin()+bi, edges.begin());
klao@226
  1163
klao@226
  1164
    _first = P.graphNode(a);
klao@226
  1165
    _last = P.graphNode(b);
klao@226
  1166
  }
klao@226
  1167
alpar@434
  1168
  ///@}
klao@225
  1169
klao@225
  1170
} // namespace hugo
klao@225
  1171
klao@225
  1172
#endif // HUGO_PATH_H