forked from boostorg/algorithm
Add range-based overload of find_not().
This commit is contained in:
@ -42,6 +42,9 @@ our find condition.
|
||||
template<typename InputIter, typename Sentinel, typename T>
|
||||
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,
|
||||
last)` that is not equal to `x`.
|
||||
|
||||
|
@ -8,13 +8,17 @@
|
||||
#define BOOST_ALGORITHM_FIND_NOT_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/range/begin.hpp>
|
||||
#include <boost/range/end.hpp>
|
||||
|
||||
#include <utility>
|
||||
|
||||
|
||||
namespace boost { namespace algorithm {
|
||||
|
||||
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) {
|
||||
if (*first != x)
|
||||
@ -23,6 +27,13 @@ BOOST_CXX14_CONSTEXPR InputIter find_not(InputIter first, Sentinel last, T const
|
||||
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
|
||||
|
||||
#endif // BOOST_ALGORITHM_FIND_NOT_HPP
|
||||
|
@ -43,13 +43,22 @@ BOOST_CXX14_CONSTEXPR bool check_constexpr()
|
||||
const int* start = ba::find_not(from, to, 1); // stops on first
|
||||
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};
|
||||
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);
|
||||
|
||||
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
|
||||
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;
|
||||
}
|
||||
|
||||
@ -67,12 +76,20 @@ void test_sequence()
|
||||
BOOST_CHECK_EQUAL(
|
||||
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);
|
||||
BOOST_CHECK_EQUAL(dist(ba::find_not(v1.begin(), v1.end(), 0)), 0);
|
||||
BOOST_CHECK_EQUAL(
|
||||
dist(ba::find_not(v1.begin(), v1.end(), v1.back())), v1.size());
|
||||
BOOST_CHECK_EQUAL(
|
||||
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.
|
||||
@ -88,6 +105,10 @@ void test_sequence()
|
||||
BOOST_CHECK_EQUAL(
|
||||
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();
|
||||
for (int i = 0; i < 10; ++i)
|
||||
l1.push_back(2);
|
||||
@ -96,6 +117,10 @@ void test_sequence()
|
||||
dist(ba::find_not(l1.begin(), l1.end(), l1.back())), l1.size());
|
||||
BOOST_CHECK_EQUAL(
|
||||
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();
|
||||
|
Reference in New Issue
Block a user