Author: | David Abrahams, Jeremy Siek, Thomas Witt |
---|---|
Contact: | dave@boost-consulting.com, jsiek@osl.iu.edu, witt@ive.uni-hannover.de |
Organization: | Boost Consulting, Indiana University Open Systems Lab, University of Hanover Institute for Transport Railway Operation and Construction |
Date: | 2003-09-14 |
Copyright: | Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003. All rights reserved |
abstract: |
---|
The filter iterator adaptor creates a view of an iterator range in which some elements of the range are skipped over. A predicate function object controls which elements are skipped. When the predicate is applied to an element, if it returns true then the element is retained and if it returns false then the element is skipped over. When skipping over elements, it is necessary for the filter adaptor to know when to stop so as to avoid going past the end of the underlying range. Therefore the constructor of the filter iterator takes two iterator parameters: the position for the filtered iterator and the end of the range.
template <class Predicate, class Iterator> class filter_iterator : public iterator_adaptor< filter_iterator<Predicate, Iterator>, Iterator , use_default , /* see details */ > { public: filter_iterator(); filter_iterator(Predicate f, Iterator x, Iterator end = Iterator()); filter_iterator(Iterator x, Iterator end = Iterator()); template<class OtherIterator> filter_iterator( filter_iterator<Predicate, OtherIterator> const& t , typename enable_if_convertible<OtherIterator, Iterator>::type* = 0 // exposition ); Predicate predicate() const; Iterator end() const; private: // as-if specification void increment() { ++(this->base_reference()); satisfy_predicate(); } void satisfy_predicate() { while (this->base() != this->m_end && !this->m_predicate(*this->base())) ++(this->base_reference()); } Predicate m_predicate; Iterator m_end; };
The base Iterator parameter must be a model of Readable Iterator and Single Pass Iterator. The resulting filter_iterator will be a model of Forward Traversal Iterator if Iterator is, otherwise the filter_iterator will be a model of Single Pass Iterator. The access category of the filter_iterator will be the most refined standard access category that is modeled by Iterator.
The Predicate must be Assignable, Copy Constructible, and the expression p(x) must be valid where p is an object of type Predicate, x is an object of type iterator_traits<Iterator>::value_type, and where the type of p(x) must be convertible to bool.
filter_iterator();
Requires: | Predicate and Iterator must be Default Constructible. |
---|---|
Returns: | a filter_iterator whose predicate is a default constructed Predicate and whose end is a default constructed Iterator. |
filter_iterator(Predicate f, Iterator x, Iterator end = Iterator());
Returns: | A filter_iterator at position x that filters according to predicate f and that will not increment past end. |
---|
filter_iterator(Iterator x, Iterator end = Iterator());
Requires: | Predicate must be Default Constructible. |
---|---|
Returns: | A filter_iterator at position x that filters according to a default constructed Predicate and that will not increment past end. |
template <class OtherIterator> filter_iterator( filter_iterator<Predicate, OtherIterator> const& t , typename enable_if_convertible<OtherIterator, Iterator>::type* = 0 // exposition );``
Requires: | OtherIterator is implicitly convertible to Iterator. |
---|---|
Returns: | A copy of iterator t. |
Predicate predicate() const;
Returns: | A copy of the predicate object used to construct *this. |
---|
Iterator end() const;
Returns: | The object end used to construct *this. |
---|