filter_iterator_ref.rst 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. .. Copyright David Abrahams, Jeremy Siek, and Thomas Witt
  2. .. 2004. Use, modification and distribution is subject to the Boost
  3. .. Software License, Version 1.0. (See accompanying file
  4. .. LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ::
  6. template <class Predicate, class Iterator>
  7. class filter_iterator
  8. {
  9. public:
  10. typedef iterator_traits<Iterator>::value_type value_type;
  11. typedef iterator_traits<Iterator>::reference reference;
  12. typedef iterator_traits<Iterator>::pointer pointer;
  13. typedef iterator_traits<Iterator>::difference_type difference_type;
  14. typedef /* see below */ iterator_category;
  15. filter_iterator();
  16. filter_iterator(Predicate f, Iterator x, Iterator end = Iterator());
  17. filter_iterator(Iterator x, Iterator end = Iterator());
  18. template<class OtherIterator>
  19. filter_iterator(
  20. filter_iterator<Predicate, OtherIterator> const& t
  21. , typename enable_if_convertible<OtherIterator, Iterator>::type* = 0 // exposition
  22. );
  23. Predicate predicate() const;
  24. Iterator end() const;
  25. Iterator const& base() const;
  26. reference operator*() const;
  27. filter_iterator& operator++();
  28. private:
  29. Predicate m_pred; // exposition only
  30. Iterator m_iter; // exposition only
  31. Iterator m_end; // exposition only
  32. };
  33. If ``Iterator`` models Readable Lvalue Iterator and Bidirectional Traversal
  34. Iterator then ``iterator_category`` is convertible to
  35. ``std::bidirectional_iterator_tag``.
  36. Otherwise, if ``Iterator`` models Readable Lvalue Iterator and Forward Traversal
  37. Iterator then ``iterator_category`` is convertible to
  38. ``std::forward_iterator_tag``.
  39. Otherwise ``iterator_category`` is
  40. convertible to ``std::input_iterator_tag``.
  41. ``filter_iterator`` requirements
  42. ................................
  43. The ``Iterator`` argument shall meet the requirements of Readable
  44. Iterator and Single Pass Iterator or it shall meet the requirements of
  45. Input Iterator.
  46. The ``Predicate`` argument must be Assignable, Copy Constructible, and
  47. the expression ``p(x)`` must be valid where ``p`` is an object of type
  48. ``Predicate``, ``x`` is an object of type
  49. ``iterator_traits<Iterator>::value_type``, and where the type of
  50. ``p(x)`` must be convertible to ``bool``.
  51. ``filter_iterator`` models
  52. ..........................
  53. The concepts that ``filter_iterator`` models are dependent on which
  54. concepts the ``Iterator`` argument models, as specified in the
  55. following tables.
  56. +---------------------------------+------------------------------------------+
  57. |If ``Iterator`` models |then ``filter_iterator`` models |
  58. +=================================+==========================================+
  59. |Single Pass Iterator |Single Pass Iterator |
  60. +---------------------------------+------------------------------------------+
  61. |Forward Traversal Iterator |Forward Traversal Iterator |
  62. +---------------------------------+------------------------------------------+
  63. |Bidirectional Traversal Iterator |Bidirectional Traversal Iterator |
  64. +---------------------------------+------------------------------------------+
  65. +--------------------------------+----------------------------------------------+
  66. | If ``Iterator`` models | then ``filter_iterator`` models |
  67. +================================+==============================================+
  68. | Readable Iterator | Readable Iterator |
  69. +--------------------------------+----------------------------------------------+
  70. | Writable Iterator | Writable Iterator |
  71. +--------------------------------+----------------------------------------------+
  72. | Lvalue Iterator | Lvalue Iterator |
  73. +--------------------------------+----------------------------------------------+
  74. +-------------------------------------------------------+---------------------------------+
  75. |If ``Iterator`` models | then ``filter_iterator`` models |
  76. +=======================================================+=================================+
  77. |Readable Iterator, Single Pass Iterator | Input Iterator |
  78. +-------------------------------------------------------+---------------------------------+
  79. |Readable Lvalue Iterator, Forward Traversal Iterator | Forward Iterator |
  80. +-------------------------------------------------------+---------------------------------+
  81. |Writable Lvalue Iterator, Forward Traversal Iterator | Mutable Forward Iterator |
  82. +-------------------------------------------------------+---------------------------------+
  83. |Writable Lvalue Iterator, Bidirectional Iterator | Mutable Bidirectional Iterator |
  84. +-------------------------------------------------------+---------------------------------+
  85. ``filter_iterator<P1, X>`` is interoperable with ``filter_iterator<P2, Y>``
  86. if and only if ``X`` is interoperable with ``Y``.
  87. ``filter_iterator`` operations
  88. ..............................
  89. In addition to those operations required by the concepts that
  90. ``filter_iterator`` models, ``filter_iterator`` provides the following
  91. operations.
  92. ``filter_iterator();``
  93. :Requires: ``Predicate`` and ``Iterator`` must be Default Constructible.
  94. :Effects: Constructs a ``filter_iterator`` whose``m_pred``, ``m_iter``, and ``m_end``
  95. members are a default constructed.
  96. ``filter_iterator(Predicate f, Iterator x, Iterator end = Iterator());``
  97. :Effects: Constructs a ``filter_iterator`` where ``m_iter`` is either
  98. the first position in the range ``[x,end)`` such that ``f(*m_iter) == true``
  99. or else``m_iter == end``. The member ``m_pred`` is constructed from
  100. ``f`` and ``m_end`` from ``end``.
  101. ``filter_iterator(Iterator x, Iterator end = Iterator());``
  102. :Requires: ``Predicate`` must be Default Constructible and
  103. ``Predicate`` is a class type (not a function pointer).
  104. :Effects: Constructs a ``filter_iterator`` where ``m_iter`` is either
  105. the first position in the range ``[x,end)`` such that ``m_pred(*m_iter) == true``
  106. or else``m_iter == end``. The member ``m_pred`` is default constructed.
  107. ::
  108. template <class OtherIterator>
  109. filter_iterator(
  110. filter_iterator<Predicate, OtherIterator> const& t
  111. , typename enable_if_convertible<OtherIterator, Iterator>::type* = 0 // exposition
  112. );``
  113. :Requires: ``OtherIterator`` is implicitly convertible to ``Iterator``.
  114. :Effects: Constructs a filter iterator whose members are copied from ``t``.
  115. ``Predicate predicate() const;``
  116. :Returns: ``m_pred``
  117. ``Iterator end() const;``
  118. :Returns: ``m_end``
  119. ``Iterator const& base() const;``
  120. :Returns: ``m_iterator``
  121. ``reference operator*() const;``
  122. :Returns: ``*m_iter``
  123. ``filter_iterator& operator++();``
  124. :Effects: Increments ``m_iter`` and then continues to
  125. increment ``m_iter`` until either ``m_iter == m_end``
  126. or ``m_pred(*m_iter) == true``.
  127. :Returns: ``*this``