]> git.lyx.org Git - lyx.git/blob - boost/boost/iterator/filter_iterator.hpp
Boost 1.31.0
[lyx.git] / boost / boost / iterator / filter_iterator.hpp
1 // (C) Copyright David Abrahams 2002.
2 // (C) Copyright Jeremy Siek    2002.
3 // (C) Copyright Thomas Witt    2002.
4 // Permission to copy, use, modify,
5 // sell and distribute this software is granted provided this
6 // copyright notice appears in all copies. This software is provided
7 // "as is" without express or implied warranty, and with no claim as
8 // to its suitability for any purpose.
9 #ifndef BOOST_FILTER_ITERATOR_23022003THW_HPP
10 #define BOOST_FILTER_ITERATOR_23022003THW_HPP
11
12 #include <boost/iterator.hpp>
13 #include <boost/iterator/iterator_adaptor.hpp>
14 #include <boost/iterator/iterator_categories.hpp>
15
16 #include <boost/type_traits/is_class.hpp>
17 #include <boost/static_assert.hpp>
18
19 namespace boost
20 {
21   template <class Predicate, class Iterator>
22   class filter_iterator;
23
24   namespace detail
25   {
26     template <class Predicate, class Iterator>
27     struct filter_iterator_base
28     {
29         typedef iterator_adaptor<
30             filter_iterator<Predicate, Iterator>
31           , Iterator
32           , use_default
33           , typename mpl::if_<
34                 is_convertible<
35                     typename iterator_traversal<Iterator>::type
36                   , bidirectional_traversal_tag
37                 >
38               , forward_traversal_tag
39               , use_default
40             >::type
41         > type;
42     };
43   }
44   
45   template <class Predicate, class Iterator>
46   class filter_iterator
47     : public detail::filter_iterator_base<Predicate, Iterator>::type
48   {
49       typedef typename detail::filter_iterator_base<
50           Predicate, Iterator
51       >::type super_t;
52
53       friend class iterator_core_access;
54
55    public:
56       filter_iterator() { }
57
58       filter_iterator(Predicate f, Iterator x, Iterator end = Iterator())
59           : super_t(x), m_predicate(f), m_end(end)
60       {
61           satisfy_predicate();
62       }
63
64       filter_iterator(Iterator x, Iterator end = Iterator())
65         : super_t(x), m_predicate(), m_end(end)
66       {
67         // Pro8 is a little too aggressive about instantiating the
68         // body of this function.
69 #if !BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
70           // Don't allow use of this constructor if Predicate is a
71           // function pointer type, since it will be 0.
72           BOOST_STATIC_ASSERT(is_class<Predicate>::value);
73 #endif 
74           satisfy_predicate();
75       }
76
77       template<class OtherIterator>
78       filter_iterator(
79           filter_iterator<Predicate, OtherIterator> const& t
80           , typename enable_if_convertible<OtherIterator, Iterator>::type* = 0
81           )
82           : super_t(t.base()), m_predicate(t.predicate()), m_end(t.end()) {}
83
84       Predicate predicate() const { return m_predicate; }
85
86       Iterator end() const { return m_end; }
87
88    private:
89       void increment()
90       {
91           ++(this->base_reference());
92           satisfy_predicate();
93       }
94
95       void decrement()
96       {
97         while(!this->m_predicate(*--(this->base_reference()))){};
98       }
99
100       void satisfy_predicate()
101       {
102           while (this->base() != this->m_end && !this->m_predicate(*this->base()))
103               ++(this->base_reference());
104       }
105
106       // Probably should be the initial base class so it can be
107       // optimized away via EBO if it is an empty class.
108       Predicate m_predicate;
109       Iterator m_end;
110   };
111
112   template <class Predicate, class Iterator>
113   filter_iterator<Predicate,Iterator>
114   make_filter_iterator(Predicate f, Iterator x, Iterator end = Iterator())
115   {
116       return filter_iterator<Predicate,Iterator>(f,x,end);
117   }
118
119   template <class Predicate, class Iterator>
120   filter_iterator<Predicate,Iterator>
121   make_filter_iterator(
122       typename iterators::enable_if<
123           is_class<Predicate>
124         , Iterator
125       >::type x
126     , Iterator end = Iterator()
127 #if BOOST_WORKAROUND(BOOST_MSVC, == 1200)
128     , Predicate* = 0
129 #endif 
130   )
131   {
132       return filter_iterator<Predicate,Iterator>(x,end);
133   }
134
135 } // namespace boost
136
137 #endif // BOOST_FILTER_ITERATOR_23022003THW_HPP