search_n.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Boost.Range library
  2. //
  3. // Copyright Neil Groves 2009. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. //
  9. // For more information, see http://www.boost.org/libs/range/
  10. //
  11. #include <boost/range/algorithm/search_n.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/assign.hpp>
  15. #include <algorithm>
  16. #include <list>
  17. #include <set>
  18. #include <vector>
  19. namespace
  20. {
  21. template<typename ForwardIterator, typename Integer, typename Value>
  22. inline ForwardIterator
  23. reference_search_n(ForwardIterator first, ForwardIterator last,
  24. Integer count, const Value& value)
  25. {
  26. if (count <= 0)
  27. return first;
  28. else if (count == 1)
  29. return std::find(first, last, value);
  30. else
  31. {
  32. first = std::find(first, last, value);
  33. while (first != last)
  34. {
  35. typename std::iterator_traits<ForwardIterator>::difference_type n = count;
  36. ForwardIterator i = first;
  37. ++i;
  38. while (i != last && n != 1 && *i==value)
  39. {
  40. ++i;
  41. --n;
  42. }
  43. if (n == 1)
  44. return first;
  45. if (i == last)
  46. return last;
  47. first = std::find(++i, last, value);
  48. }
  49. }
  50. return last;
  51. }
  52. template<typename ForwardIterator, typename Integer, typename Value,
  53. typename BinaryPredicate>
  54. inline ForwardIterator
  55. reference_search_n(ForwardIterator first, ForwardIterator last,
  56. Integer count, const Value& value,
  57. BinaryPredicate pred)
  58. {
  59. typedef typename std::iterator_traits<
  60. ForwardIterator
  61. >::iterator_category cat_t BOOST_RANGE_UNUSED;
  62. if (count <= 0)
  63. return first;
  64. if (count == 1)
  65. {
  66. while (first != last && !static_cast<bool>(pred(*first, value)))
  67. ++first;
  68. return first;
  69. }
  70. else
  71. {
  72. typedef typename std::iterator_traits<ForwardIterator>::difference_type difference_t;
  73. while (first != last && !static_cast<bool>(pred(*first, value)))
  74. ++first;
  75. while (first != last)
  76. {
  77. difference_t n = count;
  78. ForwardIterator i = first;
  79. ++i;
  80. while (i != last && n != 1 && static_cast<bool>(pred(*i, value)))
  81. {
  82. ++i;
  83. --n;
  84. }
  85. if (n == 1)
  86. return first;
  87. if (i == last)
  88. return last;
  89. first = ++i;
  90. while (first != last && !static_cast<bool>(pred(*first, value)))
  91. ++first;
  92. }
  93. }
  94. return last;
  95. }
  96. template< class Container1, class Value, class Pred >
  97. void test_search_n_pred_impl(Container1& cont1, Value value, Pred pred)
  98. {
  99. typedef BOOST_DEDUCED_TYPENAME Container1::const_iterator const_iterator1_t;
  100. typedef BOOST_DEDUCED_TYPENAME Container1::iterator iterator1_t;
  101. const Container1& ccont1 = cont1;
  102. for (std::size_t n = 0; n < cont1.size(); ++n)
  103. {
  104. iterator1_t it = boost::search_n(cont1, n, value, pred);
  105. BOOST_CHECK( it == boost::search_n(boost::make_iterator_range(cont1), n, value, pred) );
  106. BOOST_CHECK( it == reference_search_n(cont1.begin(), cont1.end(), n, value, pred) );
  107. const_iterator1_t cit = boost::search_n(ccont1, n, value, pred);
  108. BOOST_CHECK( cit == boost::search_n(boost::make_iterator_range(ccont1), n, value, pred) );
  109. BOOST_CHECK( cit == reference_search_n(ccont1.begin(), ccont1.end(), n, value, pred) );
  110. }
  111. }
  112. template< class Container1, class Value >
  113. void test_search_n_impl(Container1& cont1, Value value)
  114. {
  115. typedef BOOST_DEDUCED_TYPENAME Container1::const_iterator const_iterator1_t;
  116. typedef BOOST_DEDUCED_TYPENAME Container1::iterator iterator1_t;
  117. const Container1& ccont1 = cont1;
  118. for (std::size_t n = 0; n < cont1.size(); ++n)
  119. {
  120. iterator1_t it = boost::search_n(cont1, n, value);
  121. BOOST_CHECK( it == boost::search_n(boost::make_iterator_range(cont1), n, value) );
  122. BOOST_CHECK( it == reference_search_n(cont1.begin(), cont1.end(), n, value) );
  123. const_iterator1_t cit = boost::search_n(ccont1, n, value);
  124. BOOST_CHECK( cit == boost::search_n(boost::make_iterator_range(ccont1), n, value) );
  125. BOOST_CHECK( cit == reference_search_n(ccont1.begin(), ccont1.end(), n, value) );
  126. }
  127. test_search_n_pred_impl(cont1, value, std::less<int>());
  128. test_search_n_pred_impl(cont1, value, std::greater<int>());
  129. test_search_n_pred_impl(cont1, value, std::equal_to<int>());
  130. test_search_n_pred_impl(cont1, value, std::not_equal_to<int>());
  131. }
  132. template< class Container1, class Container2 >
  133. void test_search_n_impl()
  134. {
  135. using namespace boost::assign;
  136. Container1 cont1;
  137. test_search_n_impl(cont1, 1);
  138. cont1 += 1;
  139. test_search_n_impl(cont1, 1);
  140. test_search_n_impl(cont1, 0);
  141. cont1.clear();
  142. cont1 += 1,1;
  143. test_search_n_impl(cont1, 1);
  144. test_search_n_impl(cont1, 0);
  145. cont1 += 1,1,1;
  146. test_search_n_impl(cont1, 1);
  147. test_search_n_impl(cont1, 0);
  148. cont1.clear();
  149. cont1 += 1,2,3,4,5,6,7,8,9;
  150. test_search_n_impl(cont1, 1);
  151. test_search_n_impl(cont1, 2);
  152. test_search_n_impl(cont1, 5);
  153. test_search_n_impl(cont1, 9);
  154. }
  155. void test_search_n()
  156. {
  157. test_search_n_impl< std::list<int>, std::list<int> >();
  158. test_search_n_impl< std::vector<int>, std::vector<int> >();
  159. test_search_n_impl< std::set<int>, std::set<int> >();
  160. test_search_n_impl< std::list<int>, std::vector<int> >();
  161. test_search_n_impl< std::vector<int>, std::list<int> >();
  162. }
  163. }
  164. boost::unit_test::test_suite*
  165. init_unit_test_suite(int argc, char* argv[])
  166. {
  167. boost::unit_test::test_suite* test
  168. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.search_n" );
  169. test->add( BOOST_TEST_CASE( &test_search_n ) );
  170. return test;
  171. }