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
10 #include <boost/iterator.hpp>
11 #include <boost/iterator/iterator_adaptor.hpp>
12 #include <boost/iterator/iterator_categories.hpp>
14 #include <boost/type_traits/is_class.hpp>
15 #include <boost/static_assert.hpp>
19 template <class Predicate, class Iterator>
20 class filter_iterator;
24 template <class Predicate, class Iterator>
25 struct filter_iterator_base
27 typedef iterator_adaptor<
28 filter_iterator<Predicate, Iterator>
33 typename iterator_traversal<Iterator>::type
34 , random_access_traversal_tag
36 , bidirectional_traversal_tag
43 template <class Predicate, class Iterator>
45 : public detail::filter_iterator_base<Predicate, Iterator>::type
47 typedef typename detail::filter_iterator_base<
51 friend class iterator_core_access;
56 filter_iterator(Predicate f, Iterator x, Iterator end_ = Iterator())
57 : super_t(x), m_predicate(f), m_end(end_)
62 filter_iterator(Iterator x, Iterator end_ = Iterator())
63 : super_t(x), m_predicate(), m_end(end_)
65 // Pro8 is a little too aggressive about instantiating the
66 // body of this function.
67 #if !BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
68 // Don't allow use of this constructor if Predicate is a
69 // function pointer type, since it will be 0.
70 BOOST_STATIC_ASSERT(is_class<Predicate>::value);
75 template<class OtherIterator>
77 filter_iterator<Predicate, OtherIterator> const& t
78 , typename enable_if_convertible<OtherIterator, Iterator>::type* = 0
80 : super_t(t.base()), m_predicate(t.predicate()), m_end(t.end()) {}
82 Predicate predicate() const { return m_predicate; }
84 Iterator end() const { return m_end; }
89 ++(this->base_reference());
95 while(!this->m_predicate(*--(this->base_reference()))){};
98 void satisfy_predicate()
100 while (this->base() != this->m_end && !this->m_predicate(*this->base()))
101 ++(this->base_reference());
104 // Probably should be the initial base class so it can be
105 // optimized away via EBO if it is an empty class.
106 Predicate m_predicate;
110 template <class Predicate, class Iterator>
111 filter_iterator<Predicate,Iterator>
112 make_filter_iterator(Predicate f, Iterator x, Iterator end = Iterator())
114 return filter_iterator<Predicate,Iterator>(f,x,end);
117 template <class Predicate, class Iterator>
118 filter_iterator<Predicate,Iterator>
119 make_filter_iterator(
120 typename iterators::enable_if<
124 , Iterator end = Iterator()
125 #if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
130 return filter_iterator<Predicate,Iterator>(x,end);
135 #endif // BOOST_FILTER_ITERATOR_23022003THW_HPP