1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
4 #ifndef STL_ITERATORS_H_
5 #define STL_ITERATORS_H_
7 #include <lemon/core.h>
11 /// \brief Template to make STL iterators from Lemon iterators.
13 /// This template makes an STL iterator from a Lemon iterator
14 /// by adding the missing features.
15 /// It inherits from \c std::iterator to make \c iterator_concept work
16 /// (so that STL algorithms work).
17 /// \c T should be the lemon iterator to be decorated.
20 : public T, public std::iterator<std::input_iterator_tag, T> {
22 LemonItWrapper(const T &x) : T(x) {}
24 //Lemon iterators don't have operator*, (because they rather
25 //inherit from their "value_type"),
26 //so we add one that just returns the object.
27 const T& operator*() const {
28 return static_cast<const T&>(*this);
31 //I can't think of any use case for this with Lemon iterators,
32 //but maybe it should be included for completeness.
33 const T* operator->() {
34 return static_cast<const T*>(this);
37 //Lemon iterators don't have postincrement.
38 void operator++(int) {
47 /// \brief A generic wrapper for Lemon iterators for range-based for loops.
49 /// This template can be used to create a class
50 /// that has begin() and end() from a Lemon iterator
51 /// (with a 1-parameter constructor)
52 /// to make range-based for loops and STL algorithms work.
54 /// \c LIT is the Lemon iterator that will be wrapped
55 /// \c P is the type of the parameter of the constructor of \c LIT.
56 template<class LIT, class P>
57 class LemonRangeWrapper1 {
58 typedef LemonItWrapper<LIT> It;
62 LemonRangeWrapper1(const P &p) : _begin(LIT(p)) {}
67 return It(lemon::INVALID);
72 /// \brief A generic wrapper for Lemon iterators for range-based for loops.
74 /// This template can be used to create a class
75 /// that has begin() and end() from a Lemon iterator
76 /// (with a 2-parameter constructor)
77 /// to make range-based for loops and STL algorithms work.
79 /// \c LIT is the Lemon iterator that will be wrapped
80 /// \c P1 and \c P2 are the types of the parameters
81 /// of the constructor of \c LIT.
82 template<class LIT, class P1, class P2>
83 class LemonRangeWrapper2 {
84 typedef LemonItWrapper<LIT> It;
87 LemonRangeWrapper2(const P1 &p1, const P2 &p2) : _begin(LIT(p1, p2)) {}
92 return It(lemon::INVALID);
99 #endif /* STL_ITERATORS_H_ */