lemon/bits/stl_iterators.h
changeset 1130 0759d974de81
child 1133 93d455c647be
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/bits/stl_iterators.h	Sun Jan 05 22:24:56 2014 +0100
     1.3 @@ -0,0 +1,99 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + */
     1.6 +
     1.7 +#ifndef STL_ITERATORS_H_
     1.8 +#define STL_ITERATORS_H_
     1.9 +
    1.10 +#include <lemon/core.h>
    1.11 +
    1.12 +namespace lemon {
    1.13 +
    1.14 +  /// \brief Template to make STL iterators from Lemon iterators.
    1.15 +  ///
    1.16 +  /// This template makes an STL iterator from a Lemon iterator
    1.17 +  /// by adding the missing features.
    1.18 +  /// It inherits from \c std::iterator to make \c iterator_concept work
    1.19 +  /// (so that STL algorithms work).
    1.20 +  /// \c T should be the lemon iterator to be decorated.
    1.21 +  template<class T>
    1.22 +  struct LemonItWrapper
    1.23 +      : public T, public std::iterator<std::input_iterator_tag, T> {
    1.24 +
    1.25 +    LemonItWrapper(const T &x) : T(x) {}
    1.26 +
    1.27 +    //Lemon iterators don't have operator*, (because they rather
    1.28 +    //inherit from their "value_type"),
    1.29 +    //so we add one that just returns the object.
    1.30 +    const T& operator*() const {
    1.31 +      return static_cast<const T&>(*this);
    1.32 +    }
    1.33 +
    1.34 +    //I can't think of any use case for this with Lemon iterators,
    1.35 +    //but maybe it should be included for completeness.
    1.36 +    const T* operator->() {
    1.37 +      return static_cast<const T*>(this);
    1.38 +    }
    1.39 +
    1.40 +    //Lemon iterators don't have postincrement.
    1.41 +    void operator++(int) {
    1.42 +      T::operator++();
    1.43 +    }
    1.44 +
    1.45 +    using T::operator++;
    1.46 +
    1.47 +  };
    1.48 +
    1.49 +
    1.50 +  /// \brief A generic wrapper for Lemon iterators for range-based for loops.
    1.51 +  ///
    1.52 +  /// This template can be used to create a class
    1.53 +  /// that has begin() and end() from a Lemon iterator
    1.54 +  /// (with a 1-parameter constructor)
    1.55 +  /// to make range-based for loops and STL algorithms work.
    1.56 +  ///
    1.57 +  /// \c LIT is the Lemon iterator that will be wrapped
    1.58 +  /// \c P is the type of the parameter of the constructor of \c LIT.
    1.59 +  template<class LIT, class P>
    1.60 +  class LemonRangeWrapper1 {
    1.61 +    const P &_p;
    1.62 +    typedef LemonItWrapper<LIT> It;
    1.63 +  public:
    1.64 +    LemonRangeWrapper1(const P &p) : _p(p) {}
    1.65 +    It begin() const {
    1.66 +      return It(LIT(_p));
    1.67 +    }
    1.68 +    It end() const {
    1.69 +      return It(lemon::INVALID);
    1.70 +    }
    1.71 +  };
    1.72 +
    1.73 +
    1.74 +  /// \brief A generic wrapper for Lemon iterators for range-based for loops.
    1.75 +  ///
    1.76 +  /// This template can be used to create a class
    1.77 +  /// that has begin() and end() from a Lemon iterator
    1.78 +  /// (with a 2-parameter constructor)
    1.79 +  /// to make range-based for loops and STL algorithms work.
    1.80 +  ///
    1.81 +  /// \c LIT is the Lemon iterator that will be wrapped
    1.82 +  /// \c P1 and \c P2 are the types of the parameters
    1.83 +  /// of the constructor of \c LIT.
    1.84 +  template<class LIT, class P1, class P2>
    1.85 +  class LemonRangeWrapper2 {
    1.86 +    const P1 &_p1;
    1.87 +    const P2 &_p2;
    1.88 +    typedef LemonItWrapper<LIT> It;
    1.89 +  public:
    1.90 +    LemonRangeWrapper2(const P1 &p1, const P2 &p2) : _p1(p1), _p2(p2) {}
    1.91 +    It begin() const {
    1.92 +      return It(LIT(_p1, _p2));
    1.93 +    }
    1.94 +    It end() const {
    1.95 +      return It(lemon::INVALID);
    1.96 +    }
    1.97 +  };
    1.98 +
    1.99 +
   1.100 +}
   1.101 +
   1.102 +#endif /* STL_ITERATORS_H_ */