filter_iterator.qbk 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. [section:filter Filter Iterator]
  2. The filter iterator adaptor creates a view of an iterator range in
  3. which some elements of the range are skipped. A predicate function
  4. object controls which elements are skipped. When the predicate is
  5. applied to an element, if it returns `true` then the element is
  6. retained and if it returns `false` then the element is skipped
  7. over. When skipping over elements, it is necessary for the filter
  8. adaptor to know when to stop so as to avoid going past the end of the
  9. underlying range. A filter iterator is therefore constructed with pair
  10. of iterators indicating the range of elements in the unfiltered
  11. sequence to be traversed.
  12. [h2 Example]
  13. This example uses `filter_iterator` and then
  14. `make_filter_iterator` to output only the positive integers from an
  15. array of integers. Then `make_filter_iterator` is is used to output
  16. the integers greater than `-2`.
  17. struct is_positive_number {
  18. bool operator()(int x) { return 0 < x; }
  19. };
  20. int main()
  21. {
  22. int numbers_[] = { 0, -1, 4, -3, 5, 8, -2 };
  23. const int N = sizeof(numbers_)/sizeof(int);
  24. typedef int* base_iterator;
  25. base_iterator numbers(numbers_);
  26. // Example using filter_iterator
  27. typedef boost::filter_iterator<is_positive_number, base_iterator>
  28. FilterIter;
  29. is_positive_number predicate;
  30. FilterIter filter_iter_first(predicate, numbers, numbers + N);
  31. FilterIter filter_iter_last(predicate, numbers + N, numbers + N);
  32. std::copy(filter_iter_first, filter_iter_last, std::ostream_iterator<int>(std::cout, " "));
  33. std::cout << std::endl;
  34. // Example using make_filter_iterator()
  35. std::copy(boost::make_filter_iterator<is_positive_number>(numbers, numbers + N),
  36. boost::make_filter_iterator<is_positive_number>(numbers + N, numbers + N),
  37. std::ostream_iterator<int>(std::cout, " "));
  38. std::cout << std::endl;
  39. // Another example using make_filter_iterator()
  40. std::copy(
  41. boost::make_filter_iterator(
  42. std::bind2nd(std::greater<int>(), -2)
  43. , numbers, numbers + N)
  44. , boost::make_filter_iterator(
  45. std::bind2nd(std::greater<int>(), -2)
  46. , numbers + N, numbers + N)
  47. , std::ostream_iterator<int>(std::cout, " ")
  48. );
  49. std::cout << std::endl;
  50. return boost::exit_success;
  51. }
  52. The output is:
  53. 4 5 8
  54. 4 5 8
  55. 0 -1 4 5 8
  56. The source code for this example can be found [@../example/filter_iterator_example.cpp here].
  57. [h2 Reference]
  58. [h3 Synopsis]
  59. template <class Predicate, class Iterator>
  60. class filter_iterator
  61. {
  62. public:
  63. typedef iterator_traits<Iterator>::value_type value_type;
  64. typedef iterator_traits<Iterator>::reference reference;
  65. typedef iterator_traits<Iterator>::pointer pointer;
  66. typedef iterator_traits<Iterator>::difference_type difference_type;
  67. typedef /* see below */ iterator_category;
  68. filter_iterator();
  69. filter_iterator(Predicate f, Iterator x, Iterator end = Iterator());
  70. filter_iterator(Iterator x, Iterator end = Iterator());
  71. template<class OtherIterator>
  72. filter_iterator(
  73. filter_iterator<Predicate, OtherIterator> const& t
  74. , typename enable_if_convertible<OtherIterator, Iterator>::type* = 0 // exposition
  75. );
  76. Predicate predicate() const;
  77. Iterator end() const;
  78. Iterator const& base() const;
  79. reference operator*() const;
  80. filter_iterator& operator++();
  81. private:
  82. Predicate m_pred; // exposition only
  83. Iterator m_iter; // exposition only
  84. Iterator m_end; // exposition only
  85. };
  86. If `Iterator` models Readable Lvalue Iterator and Bidirectional Traversal
  87. Iterator then `iterator_category` is convertible to
  88. `std::bidirectional_iterator_tag`.
  89. Otherwise, if `Iterator` models Readable Lvalue Iterator and Forward Traversal
  90. Iterator then `iterator_category` is convertible to
  91. `std::forward_iterator_tag`.
  92. Otherwise `iterator_category` is
  93. convertible to `std::input_iterator_tag`.
  94. [h3 Requirements]
  95. The `Iterator` argument shall meet the requirements of Readable
  96. Iterator and Single Pass Iterator or it shall meet the requirements of
  97. Input Iterator.
  98. The `Predicate` argument must be Assignable, Copy Constructible, and
  99. the expression `p(x)` must be valid where `p` is an object of type
  100. `Predicate`, `x` is an object of type
  101. `iterator_traits<Iterator>::value_type`, and where the type of
  102. `p(x)` must be convertible to `bool`.
  103. [h3 Concepts]
  104. The concepts that `filter_iterator` models are dependent on which
  105. concepts the `Iterator` argument models, as specified in the
  106. following tables.
  107. [table Traversal
  108. [[If `Iterator` models ][then `filter_iterator` models ]]
  109. [[Single Pass Iterator ][Single Pass Iterator ]]
  110. [[Forward Traversal Iterator ][Forward Traversal Iterator ]]
  111. [[Bidirectional Traversal Iterator ][Bidirectional Traversal Iterator]]
  112. ]
  113. [table Access
  114. [[If `Iterator` models ][then `filter_iterator` models ]]
  115. [[Readable Iterator][Readable Iterator]]
  116. [[Writable Iterator][Writable Iterator]]
  117. [[Lvalue Iterator ][Lvalue Iterator ]]
  118. ]
  119. [table C++03
  120. [[If `Iterator` models ][then `filter_iterator` models ]]
  121. [[Readable Iterator, Single Pass Iterator ][Input Iterator ]]
  122. [[Readable Lvalue Iterator, Forward Traversal Iterator][Forward Iterator ]]
  123. [[Writable Lvalue Iterator, Forward Traversal Iterator][Mutable Forward Iterator ]]
  124. [[Writable Lvalue Iterator, Bidirectional Iterator ][Mutable Bidirectional Iterator]]
  125. ]
  126. `filter_iterator<P1, X>` is interoperable with `filter_iterator<P2, Y>`
  127. if and only if `X` is interoperable with `Y`.
  128. [h3 Operations]
  129. In addition to those operations required by the concepts that
  130. `filter_iterator` models, `filter_iterator` provides the following
  131. operations.
  132. filter_iterator();
  133. [*Requires: ]`Predicate` and `Iterator` must be Default Constructible.[br]
  134. [*Effects: ] Constructs a `filter_iterator` whose`m_pred`, `m_iter`, and `m_end`
  135. members are a default constructed.
  136. filter_iterator(Predicate f, Iterator x, Iterator end = Iterator());
  137. [*Effects: ] Constructs a `filter_iterator` where `m_iter` is either
  138. the first position in the range `[x,end)` such that `f(*m_iter) == true`
  139. or else`m_iter == end`. The member `m_pred` is constructed from
  140. `f` and `m_end` from `end`.
  141. filter_iterator(Iterator x, Iterator end = Iterator());
  142. [*Requires: ] `Predicate` must be Default Constructible and
  143. `Predicate` is a class type (not a function pointer).[br]
  144. [*Effects: ] Constructs a `filter_iterator` where `m_iter` is either
  145. the first position in the range `[x,end)` such that `m_pred(*m_iter) == true`
  146. or else`m_iter == end`. The member `m_pred` is default constructed.
  147. template <class OtherIterator>
  148. filter_iterator(
  149. filter_iterator<Predicate, OtherIterator> const& t
  150. , typename enable_if_convertible<OtherIterator, Iterator>::type* = 0 // exposition
  151. );
  152. [*Requires: ] `OtherIterator` is implicitly convertible to `Iterator`.[br]
  153. [*Effects: ] Constructs a filter iterator whose members are copied from `t`.
  154. Predicate predicate() const;
  155. [*Returns: ] `m_pred`
  156. Ierator end() const;
  157. [*Returns: ] `m_end`
  158. Iterator const& base() const;
  159. [*Returns: ] `m_iterator`
  160. reference operator*() const;
  161. [*Returns: ] `*m_iter`
  162. filter_iterator& operator++();
  163. [*Effects: ] Increments `m_iter` and then continues to
  164. increment `m_iter` until either `m_iter == m_end`
  165. or `m_pred(*m_iter) == true`.[br]
  166. [*Returns: ] `*this`
  167. [endsect]