diff -r 39b6e65574c6 -r 0759d974de81 lemon/bits/stl_iterators.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lemon/bits/stl_iterators.h Sun Jan 05 22:24:56 2014 +0100 @@ -0,0 +1,99 @@ +/* -*- mode: C++; indent-tabs-mode: nil; -*- + */ + +#ifndef STL_ITERATORS_H_ +#define STL_ITERATORS_H_ + +#include + +namespace lemon { + + /// \brief Template to make STL iterators from Lemon iterators. + /// + /// This template makes an STL iterator from a Lemon iterator + /// by adding the missing features. + /// It inherits from \c std::iterator to make \c iterator_concept work + /// (so that STL algorithms work). + /// \c T should be the lemon iterator to be decorated. + template + struct LemonItWrapper + : public T, public std::iterator { + + LemonItWrapper(const T &x) : T(x) {} + + //Lemon iterators don't have operator*, (because they rather + //inherit from their "value_type"), + //so we add one that just returns the object. + const T& operator*() const { + return static_cast(*this); + } + + //I can't think of any use case for this with Lemon iterators, + //but maybe it should be included for completeness. + const T* operator->() { + return static_cast(this); + } + + //Lemon iterators don't have postincrement. + void operator++(int) { + T::operator++(); + } + + using T::operator++; + + }; + + + /// \brief A generic wrapper for Lemon iterators for range-based for loops. + /// + /// This template can be used to create a class + /// that has begin() and end() from a Lemon iterator + /// (with a 1-parameter constructor) + /// to make range-based for loops and STL algorithms work. + /// + /// \c LIT is the Lemon iterator that will be wrapped + /// \c P is the type of the parameter of the constructor of \c LIT. + template + class LemonRangeWrapper1 { + const P &_p; + typedef LemonItWrapper It; + public: + LemonRangeWrapper1(const P &p) : _p(p) {} + It begin() const { + return It(LIT(_p)); + } + It end() const { + return It(lemon::INVALID); + } + }; + + + /// \brief A generic wrapper for Lemon iterators for range-based for loops. + /// + /// This template can be used to create a class + /// that has begin() and end() from a Lemon iterator + /// (with a 2-parameter constructor) + /// to make range-based for loops and STL algorithms work. + /// + /// \c LIT is the Lemon iterator that will be wrapped + /// \c P1 and \c P2 are the types of the parameters + /// of the constructor of \c LIT. + template + class LemonRangeWrapper2 { + const P1 &_p1; + const P2 &_p2; + typedef LemonItWrapper It; + public: + LemonRangeWrapper2(const P1 &p1, const P2 &p2) : _p1(p1), _p2(p2) {} + It begin() const { + return It(LIT(_p1, _p2)); + } + It end() const { + return It(lemon::INVALID); + } + }; + + +} + +#endif /* STL_ITERATORS_H_ */