lemon/bits/stl_iterators.h
author Gabor Gevay <ggab90@gmail.com>
Sun, 05 Jan 2014 22:24:56 +0100
changeset 1130 0759d974de81
child 1133 93d455c647be
permissions -rw-r--r--
STL style iterators (#325)

For
* graph types,
* graph adaptors,
* paths,
* iterable maps,
* LP rows/cols and
* active nodes is BellmanFord
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
    const P &_p;
ggab90@1130
    59
    typedef LemonItWrapper<LIT> It;
ggab90@1130
    60
  public:
ggab90@1130
    61
    LemonRangeWrapper1(const P &p) : _p(p) {}
ggab90@1130
    62
    It begin() const {
ggab90@1130
    63
      return It(LIT(_p));
ggab90@1130
    64
    }
ggab90@1130
    65
    It end() const {
ggab90@1130
    66
      return It(lemon::INVALID);
ggab90@1130
    67
    }
ggab90@1130
    68
  };
ggab90@1130
    69
ggab90@1130
    70
ggab90@1130
    71
  /// \brief A generic wrapper for Lemon iterators for range-based for loops.
ggab90@1130
    72
  ///
ggab90@1130
    73
  /// This template can be used to create a class
ggab90@1130
    74
  /// that has begin() and end() from a Lemon iterator
ggab90@1130
    75
  /// (with a 2-parameter constructor)
ggab90@1130
    76
  /// to make range-based for loops and STL algorithms work.
ggab90@1130
    77
  ///
ggab90@1130
    78
  /// \c LIT is the Lemon iterator that will be wrapped
ggab90@1130
    79
  /// \c P1 and \c P2 are the types of the parameters
ggab90@1130
    80
  /// of the constructor of \c LIT.
ggab90@1130
    81
  template<class LIT, class P1, class P2>
ggab90@1130
    82
  class LemonRangeWrapper2 {
ggab90@1130
    83
    const P1 &_p1;
ggab90@1130
    84
    const P2 &_p2;
ggab90@1130
    85
    typedef LemonItWrapper<LIT> It;
ggab90@1130
    86
  public:
ggab90@1130
    87
    LemonRangeWrapper2(const P1 &p1, const P2 &p2) : _p1(p1), _p2(p2) {}
ggab90@1130
    88
    It begin() const {
ggab90@1130
    89
      return It(LIT(_p1, _p2));
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_ */