lemon/bits/stl_iterators.h
author Balazs Dezso <deba@google.com>
Tue, 14 Apr 2015 16:36:45 +0200
changeset 1339 93d455c647be
parent 1336 0759d974de81
permissions -rw-r--r--
Fix problem with temporal iterator parameters (#325)
ggab90@1336
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
ggab90@1336
     2
 */
ggab90@1336
     3
ggab90@1336
     4
#ifndef STL_ITERATORS_H_
ggab90@1336
     5
#define STL_ITERATORS_H_
ggab90@1336
     6
ggab90@1336
     7
#include <lemon/core.h>
ggab90@1336
     8
ggab90@1336
     9
namespace lemon {
ggab90@1336
    10
ggab90@1336
    11
  /// \brief Template to make STL iterators from Lemon iterators.
ggab90@1336
    12
  ///
ggab90@1336
    13
  /// This template makes an STL iterator from a Lemon iterator
ggab90@1336
    14
  /// by adding the missing features.
ggab90@1336
    15
  /// It inherits from \c std::iterator to make \c iterator_concept work
ggab90@1336
    16
  /// (so that STL algorithms work).
ggab90@1336
    17
  /// \c T should be the lemon iterator to be decorated.
ggab90@1336
    18
  template<class T>
ggab90@1336
    19
  struct LemonItWrapper
ggab90@1336
    20
      : public T, public std::iterator<std::input_iterator_tag, T> {
ggab90@1336
    21
ggab90@1336
    22
    LemonItWrapper(const T &x) : T(x) {}
ggab90@1336
    23
ggab90@1336
    24
    //Lemon iterators don't have operator*, (because they rather
ggab90@1336
    25
    //inherit from their "value_type"),
ggab90@1336
    26
    //so we add one that just returns the object.
ggab90@1336
    27
    const T& operator*() const {
ggab90@1336
    28
      return static_cast<const T&>(*this);
ggab90@1336
    29
    }
ggab90@1336
    30
ggab90@1336
    31
    //I can't think of any use case for this with Lemon iterators,
ggab90@1336
    32
    //but maybe it should be included for completeness.
ggab90@1336
    33
    const T* operator->() {
ggab90@1336
    34
      return static_cast<const T*>(this);
ggab90@1336
    35
    }
ggab90@1336
    36
ggab90@1336
    37
    //Lemon iterators don't have postincrement.
ggab90@1336
    38
    void operator++(int) {
ggab90@1336
    39
      T::operator++();
ggab90@1336
    40
    }
ggab90@1336
    41
ggab90@1336
    42
    using T::operator++;
ggab90@1336
    43
ggab90@1336
    44
  };
ggab90@1336
    45
ggab90@1336
    46
ggab90@1336
    47
  /// \brief A generic wrapper for Lemon iterators for range-based for loops.
ggab90@1336
    48
  ///
ggab90@1336
    49
  /// This template can be used to create a class
ggab90@1336
    50
  /// that has begin() and end() from a Lemon iterator
ggab90@1336
    51
  /// (with a 1-parameter constructor)
ggab90@1336
    52
  /// to make range-based for loops and STL algorithms work.
ggab90@1336
    53
  ///
ggab90@1336
    54
  /// \c LIT is the Lemon iterator that will be wrapped
ggab90@1336
    55
  /// \c P is the type of the parameter of the constructor of \c LIT.
ggab90@1336
    56
  template<class LIT, class P>
ggab90@1336
    57
  class LemonRangeWrapper1 {
ggab90@1336
    58
    typedef LemonItWrapper<LIT> It;
deba@1339
    59
    It _begin;
deba@1339
    60
    
ggab90@1336
    61
  public:
deba@1339
    62
    LemonRangeWrapper1(const P &p) : _begin(LIT(p)) {}
ggab90@1336
    63
    It begin() const {
deba@1339
    64
      return _begin;
ggab90@1336
    65
    }
ggab90@1336
    66
    It end() const {
ggab90@1336
    67
      return It(lemon::INVALID);
ggab90@1336
    68
    }
ggab90@1336
    69
  };
ggab90@1336
    70
ggab90@1336
    71
ggab90@1336
    72
  /// \brief A generic wrapper for Lemon iterators for range-based for loops.
ggab90@1336
    73
  ///
ggab90@1336
    74
  /// This template can be used to create a class
ggab90@1336
    75
  /// that has begin() and end() from a Lemon iterator
ggab90@1336
    76
  /// (with a 2-parameter constructor)
ggab90@1336
    77
  /// to make range-based for loops and STL algorithms work.
ggab90@1336
    78
  ///
ggab90@1336
    79
  /// \c LIT is the Lemon iterator that will be wrapped
ggab90@1336
    80
  /// \c P1 and \c P2 are the types of the parameters
ggab90@1336
    81
  /// of the constructor of \c LIT.
ggab90@1336
    82
  template<class LIT, class P1, class P2>
ggab90@1336
    83
  class LemonRangeWrapper2 {
deba@1339
    84
    typedef LemonItWrapper<LIT> It; 
deba@1339
    85
    It _begin;
deba@1339
    86
 public:
deba@1339
    87
    LemonRangeWrapper2(const P1 &p1, const P2 &p2) : _begin(LIT(p1, p2)) {}
ggab90@1336
    88
    It begin() const {
deba@1339
    89
      return _begin;
ggab90@1336
    90
    }
ggab90@1336
    91
    It end() const {
ggab90@1336
    92
      return It(lemon::INVALID);
ggab90@1336
    93
    }
ggab90@1336
    94
  };
ggab90@1336
    95
ggab90@1336
    96
ggab90@1336
    97
}
ggab90@1336
    98
ggab90@1336
    99
#endif /* STL_ITERATORS_H_ */