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