Changed the name of the functions to match the ones in n2666 (and C++0x)

[SVN r52886]
This commit is contained in:
Marshall Clow
2009-05-10 23:16:59 +00:00
parent e9c9685fe9
commit 40de115990
2 changed files with 130 additions and 46 deletions

View File

@ -25,22 +25,92 @@ namespace ba = boost::algorithm;
void test_none()
{
// Note: The literal values here are tested against directly, careful if you change them:
int some_numbers[] = { 1, 5, 0, 18, 1 };
std::vector<int> vi(some_numbers, some_numbers + 5);
// Note: The literal values here are tested against directly, careful if you change them:
int some_numbers[] = { 1, 5, 0, 18, 1 };
std::vector<int> vi(some_numbers, some_numbers + 5);
int some_letters[] = { 'a', 'q', 'n', 'y', 'n' };
std::vector<char> vc(some_letters, some_letters + 5);
BOOST_CHECK_EQUAL ( true, ba::none_of ( vi, 100 ));
BOOST_CHECK_EQUAL ( true, ba::none_of ( vi.begin(), vi.end (), 100 ));
BOOST_CHECK_EQUAL ( false, ba::none_of ( vi, 1 ));
BOOST_CHECK_EQUAL ( false, ba::none_of ( vi.begin (), vi.end(), 1 ));
int some_letters[] = { 'a', 'q', 'n', 'y', 'n' };
std::vector<char> vc(some_letters, some_letters + 5);
BOOST_CHECK_EQUAL ( true, ba::none_of ( vi.end (), vi.end (), 0 ));
BOOST_CHECK_EQUAL(true, ba::none(vi, 100));
BOOST_CHECK_EQUAL(false, ba::none(vi, 1));
BOOST_CHECK_EQUAL(true, ba::none(vc, 'z'));
BOOST_CHECK_EQUAL(false, ba::none(vc, 'a'));
// 5 is not in { 0, 18, 1 }, but 1 is
BOOST_CHECK_EQUAL ( true, ba::none_of ( vi.begin () + 2, vi.end(), 5 ));
BOOST_CHECK_EQUAL ( false, ba::none_of ( vi.begin () + 2, vi.end(), 1 ));
// 18 is not in { 1, 5, 0 }, but 5 is
BOOST_CHECK_EQUAL ( true, ba::none_of ( vi.begin (), vi.begin() + 3, 18 ));
BOOST_CHECK_EQUAL ( false, ba::none_of ( vi.begin (), vi.begin() + 3, 5 ));
BOOST_CHECK_EQUAL ( true, ba::none_of ( vc, 'z' ));
BOOST_CHECK_EQUAL ( false, ba::none_of ( vc, 'a' ));
// BOOST_CHECK_EQUAL ( true, ba::none_of ( vc, 'n' )); // Better fail!
}
void test_any ()
{
// Note: The literal values here are tested against directly, careful if you change them:
int some_numbers[] = { 1, 5, 0, 18, 10 };
std::vector<int> vi(some_numbers, some_numbers + 5);
int some_letters[] = { 'a', 'q', 'n', 'y', 'n' };
std::vector<char> vc(some_letters, some_letters + 5);
BOOST_CHECK_EQUAL ( true, ba::any_of ( vi, 1 ));
BOOST_CHECK_EQUAL ( true, ba::any_of ( vi.begin(), vi.end (), 1 ));
BOOST_CHECK_EQUAL ( false, ba::any_of ( vi, 9 ));
BOOST_CHECK_EQUAL ( false, ba::any_of ( vi.begin (), vi.end(), 9 ));
BOOST_CHECK_EQUAL ( true, ba::any_of ( vi, 10 ));
BOOST_CHECK_EQUAL ( false, ba::any_of ( vi, 4 ));
BOOST_CHECK_EQUAL ( false, ba::any_of ( vi.end (), vi.end (), 0 ));
// 5 is not in { 0, 18, 10 }, but 10 is
BOOST_CHECK_EQUAL ( true, ba::any_of ( vi.begin () + 2, vi.end(), 10 ));
BOOST_CHECK_EQUAL ( false, ba::any_of ( vi.begin () + 2, vi.end(), 5 ));
// 18 is not in { 1, 5, 0 }, but 5 is
BOOST_CHECK_EQUAL ( true, ba::any_of ( vi.begin (), vi.begin() + 3, 5 ));
BOOST_CHECK_EQUAL ( false, ba::any_of ( vi.begin (), vi.begin() + 3, 18 ));
BOOST_CHECK_EQUAL ( true, ba::any_of (vc, 'q' ));
BOOST_CHECK_EQUAL ( false, ba::any_of (vc, '!' ));
// BOOST_CHECK_EQUAL ( false, ba::any_of (vc, 'n' )); // Better fail!
}
void test_all ()
{
// Note: The literal values here are tested against directly, careful if you change them:
int some_numbers[] = { 1, 1, 1, 18, 10 };
std::vector<int> vi(some_numbers, some_numbers + 5);
int some_letters[] = { 'a', 'q', 'n', 'y', 'n' };
std::vector<char> vc(some_letters, some_letters + 5);
BOOST_CHECK_EQUAL ( false, ba::all_of ( vi, 1 ));
BOOST_CHECK_EQUAL ( false, ba::all_of ( vi.begin(), vi.end (), 1 ));
BOOST_CHECK_EQUAL ( false, ba::all_of ( vi, 0 ));
BOOST_CHECK_EQUAL ( false, ba::all_of ( vi.begin(), vi.end (), 0 ));
BOOST_CHECK_EQUAL ( true, ba::all_of ( vi.end (), vi.end (), 0));
BOOST_CHECK_EQUAL ( true, ba::all_of ( vi.begin (), vi.begin () + 3, 1));
BOOST_CHECK_EQUAL ( true, ba::all_of ( vc.begin () + 1, vc.begin () + 2, 'q' ));
BOOST_CHECK_EQUAL ( false, ba::all_of(vc, '!' ));
// BOOST_CHECK_EQUAL ( true, ba::all_of(vc, 'n' )); // Better fail!
}
int test_main( int , char* [] )
{
test_none();
test_any();
test_all();
return 0;
}

View File

@ -6,23 +6,35 @@
Revision history:
05 May 2008 mtc First version - as part of BoostCon 2008
07 May 2009 mtc Changed names to match n2666
*/
// Returns true iff all of the elements in [ first, last ) satisfy the predicate.
#ifndef BOOST_ALGORITHM_ALL_HPP
#define BOOST_ALGORITHM_ALL_HPP
#include <boost/range.hpp> // For boost::begin and boost::end
/// \file all.hpp
/// \brief Test ranges against predicates.
/// \author Marshall Clow
#ifndef BOOST_ALGORITHM_ALL_HPP
#define BOOST_ALGORITHM_ALL_HPP
#include <boost/range.hpp> // For boost::begin and boost::end
// I would love to use the all_of, any_of and none_of that are in the C++0x
// standard library if they are available, but I don't know how to do that.
// -- mtc 11-May-2009
// Something like:
// #ifdef <something>
// use std::all_of;
// #else
// template<typename I, typename V>
// bool all_of ( I first, I last, const V &val )
// ... and so on.
// #endif
namespace boost { namespace algorithm {
/// \fn all ( I first, I last, const V &val )
/// \fn all_of ( I first, I last, const V &val )
/// \brief Returns true if all elements in [first, last) are equal to 'val'
///
/// \param first The start of the input sequence
@ -30,7 +42,7 @@ namespace boost { namespace algorithm {
/// \param val A value to compare against
///
template<typename I, typename V>
bool all ( I first, I last, const V &val )
bool all_of ( I first, I last, const V &val )
{
while (first != last) {
if ( *first++ != val )
@ -39,20 +51,21 @@ namespace boost { namespace algorithm {
return true;
}
/// \fn all ( Range range, const V &val )
/// \fn all_of ( Range range, const V &val )
/// \brief Returns true if all elements in the range are equal to 'val'
///
/// \param range The input range
/// \param val A value to compare against
///
template<typename Range, typename V>
bool all ( Range range, const V &val )
bool all_of ( Range range, const V &val )
{
return all ( boost::begin ( range ), boost::end ( range ), val );
return all_of ( boost::begin ( range ), boost::end ( range ), val );
}
/// \fn all_if ( I first, I last, Pred p )
/// \fn all_of_if ( I first, I last, Pred p )
/// \brief Returns true if all elements in [first, last) satisfy the predicate
///
/// \param first The start of the input sequence
@ -60,7 +73,7 @@ namespace boost { namespace algorithm {
/// \param p A predicate
///
template<typename I, typename Pred>
bool all_if ( I first, I last, Pred p )
bool all_of_if ( I first, I last, Pred p )
{
while (first != last) {
if ( !p(*first++))
@ -69,19 +82,19 @@ template<typename I, typename Pred>
return true;
}
/// \fn all_if ( Range range, Pred p )
/// \fn all_of_if ( Range range, Pred p )
/// \brief Returns true if all elements in the range satisfy the predicate
///
/// \param range The input range
/// \param p A predicate to test the elements
///
template<typename Range, typename Pred>
bool all_if ( Range range, Pred p )
bool all_of_if ( Range range, Pred p )
{
return all_if ( boost::begin ( range ), boost::end ( range ), p );
return all_of_if ( boost::begin ( range ), boost::end ( range ), p );
}
/// \fn none ( I first, I last, const V &val )
/// \fn none_of ( I first, I last, const V &val )
/// \brief Returns true if none of the elements in [first, last) are equal to 'val'
///
/// \param first The start of the input sequence
@ -89,7 +102,7 @@ template<typename I, typename Pred>
/// \param val A value to compare against
///
template<typename I, typename V>
bool none ( I first, I last, const V &val )
bool none_of ( I first, I last, const V &val )
{
while (first != last) {
if ( *first++ == val )
@ -98,20 +111,20 @@ template<typename I, typename Pred>
return true;
}
/// \fn none ( Range range, const V &val )
/// \fn none_of ( Range range, const V &val )
/// \brief Returns true if none of the elements in the range are equal to 'val'
///
/// \param range The input range
/// \param val A value to compare against
///
template<typename Range, typename V>
bool none ( Range range, const V & val )
bool none_of ( Range range, const V & val )
{
return none ( boost::begin ( range ), boost::end ( range ), val );
return none_of ( boost::begin ( range ), boost::end ( range ), val );
}
/// \fn none_if ( I first, I last, Pred p )
/// \fn none_of_if ( I first, I last, Pred p )
/// \brief Returns true if none of the elements in [first, last) satisfy the predicate
///
/// \param first The start of the input sequence
@ -119,7 +132,7 @@ template<typename I, typename Pred>
/// \param p A predicate
///
template<typename I, typename Pred>
bool none_if ( I first, I last, Pred p )
bool none_of_if ( I first, I last, Pred p )
{
while (first != last) {
if ( p(*first++))
@ -128,7 +141,7 @@ template<typename I, typename Pred>
return true;
}
/// \fn none_if ( Range range, Pred p )
/// \fn none_of_if ( Range range, Pred p )
/// \brief Returns true if none of the elements in the range satisfy the predicate
///
/// \param range The input range
@ -137,10 +150,10 @@ template<typename I, typename Pred>
template<typename Range, typename Pred>
bool none_if ( Range range, Pred p )
{
return none_if ( boost::begin ( range ), boost::end ( range ), p );
return none_of_if ( boost::begin ( range ), boost::end ( range ), p );
}
/// \fn any ( I first, I last, const V &val )
/// \fn any_of ( I first, I last, const V &val )
/// \brief Returns true if any of the elements in [first, last) are equal to 'val'
///
/// \param first The start of the input sequence
@ -148,7 +161,7 @@ template<typename I, typename Pred>
/// \param val A value to compare against
///
template<typename I, typename V>
bool any ( I first, I last, const V &val )
bool any_of ( I first, I last, const V &val )
{
while (first != last) {
if ( *first++ == val )
@ -157,19 +170,19 @@ template<typename I, typename Pred>
return false;
}
/// \fn any ( Range range, const V &val )
/// \fn any_of ( Range range, const V &val )
/// \brief Returns true if any of the elements in the range are equal to 'val'
///
/// \param range The input range
/// \param val A value to compare against
///
template<typename Range, typename V>
bool any ( Range range, const V &val )
bool any_of ( Range range, const V &val )
{
return any ( boost::begin ( range ), boost::end ( range ), val );
return any_of ( boost::begin ( range ), boost::end ( range ), val );
}
/// \fn any_if ( I first, I last, Pred p )
/// \fn any_of_if ( I first, I last, Pred p )
/// \brief Returns true if any of the elements in [first, last) satisfy the predicate
///
/// \param first The start of the input sequence
@ -177,7 +190,7 @@ template<typename I, typename Pred>
/// \param p A predicate
///
template<typename I, typename Pred>
bool any_if ( I first, I last, Pred p)
bool any_of_if ( I first, I last, Pred p)
{
while (first != last) {
if ( p(*first++))
@ -186,16 +199,16 @@ template<typename I, typename Pred>
return false;
}
/// \fn any_if ( Range range, Pred p )
/// \fn any_of_if ( Range range, Pred p )
/// \brief Returns true if any elements in the range satisfy the predicate
///
/// \param range The input range
/// \param p A predicate to test the elements
///
template<typename Range, typename Pred>
bool any_if ( Range range, Pred p )
bool any_of_if ( Range range, Pred p )
{
return any_if ( boost::begin ( range ), boost::end ( range ), p );
return any_of_if ( boost::begin ( range ), boost::end ( range ), p );
}
/// \fn exists_and_only ( I first, I last, const V &val )
@ -257,4 +270,5 @@ template<typename I, typename Pred>
}} // namespace boost and algorithm
#undef ANY_DOING_0X
#endif // BOOST_ALGORITHM_ALL_HPP