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