Add range-based overload of find_not().

This commit is contained in:
Zach Laine
2018-05-12 15:25:37 -05:00
parent 8c9d5e858c
commit 6c68cf8624
3 changed files with 40 additions and 1 deletions

View File

@ -42,6 +42,9 @@ our find condition.
template<typename InputIter, typename Sentinel, typename T> template<typename InputIter, typename Sentinel, typename T>
InputIter find_not(InputIter first, Sentinel last, T const & x); InputIter find_not(InputIter first, Sentinel last, T const & x);
template<typename Range, typename T>
typename boost::range_iterator<Range>::type find_not(Range & r, T const & x);
The function `find_not` returns the first value in the sequence `[first, The function `find_not` returns the first value in the sequence `[first,
last)` that is not equal to `x`. last)` that is not equal to `x`.

View File

@ -8,13 +8,17 @@
#define BOOST_ALGORITHM_FIND_NOT_HPP #define BOOST_ALGORITHM_FIND_NOT_HPP
#include <boost/config.hpp> #include <boost/config.hpp>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <utility> #include <utility>
namespace boost { namespace algorithm { namespace boost { namespace algorithm {
template<typename InputIter, typename Sentinel, typename T> template<typename InputIter, typename Sentinel, typename T>
BOOST_CXX14_CONSTEXPR InputIter find_not(InputIter first, Sentinel last, T const & x) BOOST_CXX14_CONSTEXPR
InputIter find_not(InputIter first, Sentinel last, T const & x)
{ {
for (; first != last; ++first) { for (; first != last; ++first) {
if (*first != x) if (*first != x)
@ -23,6 +27,13 @@ BOOST_CXX14_CONSTEXPR InputIter find_not(InputIter first, Sentinel last, T const
return first; return first;
} }
template<typename Range, typename T>
BOOST_CXX14_CONSTEXPR
typename boost::range_iterator<Range>::type find_not(Range & r, T const & x)
{
return ::boost::algorithm::find_not(boost::begin(r), boost::end(r), x);
}
}} // namespace boost and algorithm }} // namespace boost and algorithm
#endif // BOOST_ALGORITHM_FIND_NOT_HPP #endif // BOOST_ALGORITHM_FIND_NOT_HPP

View File

@ -43,13 +43,22 @@ BOOST_CXX14_CONSTEXPR bool check_constexpr()
const int* start = ba::find_not(from, to, 1); // stops on first const int* start = ba::find_not(from, to, 1); // stops on first
res = (res && start == from); res = (res && start == from);
start = ba::find_not(in_data, 1); // stops on first
res = (res && start == from);
int in_data_2[] = {6, 6, 6, 6, 6}; int in_data_2[] = {6, 6, 6, 6, 6};
const int* end = ba::find_not(in_data_2, in_data_2 + 5, 6); // stops on the end const int* end = ba::find_not(in_data_2, in_data_2 + 5, 6); // stops on the end
res = (res && end == in_data_2 + 5); res = (res && end == in_data_2 + 5);
end = ba::find_not(in_data_2, 6); // stops on the end
res = (res && end == in_data_2 + 5);
const int* three = ba::find_not(from, to, 2); // stops on third element const int* three = ba::find_not(from, to, 2); // stops on third element
res = (res && three == in_data + 2); res = (res && three == in_data + 2);
three = ba::find_not(in_data, 2); // stops on third element
res = (res && three == in_data + 2);
return res; return res;
} }
@ -67,12 +76,20 @@ void test_sequence()
BOOST_CHECK_EQUAL( BOOST_CHECK_EQUAL(
dist(ba::find_not(v1.begin(), v1.end(), v1.front())), 1); dist(ba::find_not(v1.begin(), v1.end(), v1.front())), 1);
BOOST_CHECK_EQUAL(dist(ba::find_not(v1, 0)), 0);
BOOST_CHECK_EQUAL(dist(ba::find_not(v1, v1.back())), 0);
BOOST_CHECK_EQUAL(dist(ba::find_not(v1, v1.front())), 1);
v1 = std::vector<int>(10, 2); v1 = std::vector<int>(10, 2);
BOOST_CHECK_EQUAL(dist(ba::find_not(v1.begin(), v1.end(), 0)), 0); BOOST_CHECK_EQUAL(dist(ba::find_not(v1.begin(), v1.end(), 0)), 0);
BOOST_CHECK_EQUAL( BOOST_CHECK_EQUAL(
dist(ba::find_not(v1.begin(), v1.end(), v1.back())), v1.size()); dist(ba::find_not(v1.begin(), v1.end(), v1.back())), v1.size());
BOOST_CHECK_EQUAL( BOOST_CHECK_EQUAL(
dist(ba::find_not(v1.begin(), v1.end(), v1.front())), v1.size()); dist(ba::find_not(v1.begin(), v1.end(), v1.front())), v1.size());
BOOST_CHECK_EQUAL(dist(ba::find_not(v1, 0)), 0);
BOOST_CHECK_EQUAL(dist(ba::find_not(v1, v1.back())), v1.size());
BOOST_CHECK_EQUAL(dist(ba::find_not(v1, v1.front())), v1.size());
} }
// With bidirectional iterators. // With bidirectional iterators.
@ -88,6 +105,10 @@ void test_sequence()
BOOST_CHECK_EQUAL( BOOST_CHECK_EQUAL(
dist(ba::find_not(l1.begin(), l1.end(), l1.front())), 1); dist(ba::find_not(l1.begin(), l1.end(), l1.front())), 1);
BOOST_CHECK_EQUAL(dist(ba::find_not(l1, 0)), 0);
BOOST_CHECK_EQUAL(dist(ba::find_not(l1, l1.back())), 0);
BOOST_CHECK_EQUAL(dist(ba::find_not(l1, l1.front())), 1);
l1.clear(); l1.clear();
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
l1.push_back(2); l1.push_back(2);
@ -96,6 +117,10 @@ void test_sequence()
dist(ba::find_not(l1.begin(), l1.end(), l1.back())), l1.size()); dist(ba::find_not(l1.begin(), l1.end(), l1.back())), l1.size());
BOOST_CHECK_EQUAL( BOOST_CHECK_EQUAL(
dist(ba::find_not(l1.begin(), l1.end(), l1.front())), l1.size()); dist(ba::find_not(l1.begin(), l1.end(), l1.front())), l1.size());
BOOST_CHECK_EQUAL(dist(ba::find_not(l1, 0)), 0);
BOOST_CHECK_EQUAL(dist(ba::find_not(l1, l1.back())), l1.size());
BOOST_CHECK_EQUAL(dist(ba::find_not(l1, l1.front())), l1.size());
} }
BOOST_CXX14_CONSTEXPR bool ce_result = check_constexpr(); BOOST_CXX14_CONSTEXPR bool ce_result = check_constexpr();