filter_iterator.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // (C) Copyright David Abrahams 2002.
  2. // (C) Copyright Jeremy Siek 2002.
  3. // (C) Copyright Thomas Witt 2002.
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_FILTER_ITERATOR_23022003THW_HPP
  8. #define BOOST_FILTER_ITERATOR_23022003THW_HPP
  9. #include <boost/iterator/iterator_adaptor.hpp>
  10. #include <boost/iterator/iterator_categories.hpp>
  11. #include <boost/type_traits/is_class.hpp>
  12. #include <boost/static_assert.hpp>
  13. namespace boost {
  14. namespace iterators {
  15. template <class Predicate, class Iterator>
  16. class filter_iterator;
  17. namespace detail
  18. {
  19. template <class Predicate, class Iterator>
  20. struct filter_iterator_base
  21. {
  22. typedef iterator_adaptor<
  23. filter_iterator<Predicate, Iterator>
  24. , Iterator
  25. , use_default
  26. , typename mpl::if_<
  27. is_convertible<
  28. typename iterator_traversal<Iterator>::type
  29. , random_access_traversal_tag
  30. >
  31. , bidirectional_traversal_tag
  32. , use_default
  33. >::type
  34. > type;
  35. };
  36. }
  37. template <class Predicate, class Iterator>
  38. class filter_iterator
  39. : public detail::filter_iterator_base<Predicate, Iterator>::type
  40. {
  41. typedef typename detail::filter_iterator_base<
  42. Predicate, Iterator
  43. >::type super_t;
  44. friend class iterator_core_access;
  45. public:
  46. filter_iterator() { }
  47. filter_iterator(Predicate f, Iterator x, Iterator end_ = Iterator())
  48. : super_t(x), m_predicate(f), m_end(end_)
  49. {
  50. satisfy_predicate();
  51. }
  52. filter_iterator(Iterator x, Iterator end_ = Iterator())
  53. : super_t(x), m_predicate(), m_end(end_)
  54. {
  55. // Pro8 is a little too aggressive about instantiating the
  56. // body of this function.
  57. #if !BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
  58. // Don't allow use of this constructor if Predicate is a
  59. // function pointer type, since it will be 0.
  60. BOOST_STATIC_ASSERT(is_class<Predicate>::value);
  61. #endif
  62. satisfy_predicate();
  63. }
  64. template<class OtherIterator>
  65. filter_iterator(
  66. filter_iterator<Predicate, OtherIterator> const& t
  67. , typename enable_if_convertible<OtherIterator, Iterator>::type* = 0
  68. )
  69. : super_t(t.base()), m_predicate(t.predicate()), m_end(t.end()) {}
  70. Predicate predicate() const { return m_predicate; }
  71. Iterator end() const { return m_end; }
  72. private:
  73. void increment()
  74. {
  75. ++(this->base_reference());
  76. satisfy_predicate();
  77. }
  78. void decrement()
  79. {
  80. while(!this->m_predicate(*--(this->base_reference()))){};
  81. }
  82. void satisfy_predicate()
  83. {
  84. while (this->base() != this->m_end && !this->m_predicate(*this->base()))
  85. ++(this->base_reference());
  86. }
  87. // Probably should be the initial base class so it can be
  88. // optimized away via EBO if it is an empty class.
  89. Predicate m_predicate;
  90. Iterator m_end;
  91. };
  92. template <class Predicate, class Iterator>
  93. inline filter_iterator<Predicate,Iterator>
  94. make_filter_iterator(Predicate f, Iterator x, Iterator end = Iterator())
  95. {
  96. return filter_iterator<Predicate,Iterator>(f,x,end);
  97. }
  98. template <class Predicate, class Iterator>
  99. inline filter_iterator<Predicate,Iterator>
  100. make_filter_iterator(
  101. typename iterators::enable_if<
  102. is_class<Predicate>
  103. , Iterator
  104. >::type x
  105. , Iterator end = Iterator())
  106. {
  107. return filter_iterator<Predicate,Iterator>(x,end);
  108. }
  109. } // namespace iterators
  110. using iterators::filter_iterator;
  111. using iterators::make_filter_iterator;
  112. } // namespace boost
  113. #endif // BOOST_FILTER_ITERATOR_23022003THW_HPP