From 40de115990b614928a65de407e2543bb45b7f800 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Sun, 10 May 2009 23:16:59 +0000 Subject: [PATCH] Changed the name of the functions to match the ones in n2666 (and C++0x) [SVN r52886] --- all/test/all_test.cpp | 90 +++++++++++++++++++++++++++++---- include/boost/algorithm/all.hpp | 86 ++++++++++++++++++------------- 2 files changed, 130 insertions(+), 46 deletions(-) diff --git a/all/test/all_test.cpp b/all/test/all_test.cpp index fe400a9..454d96d 100644 --- a/all/test/all_test.cpp +++ b/all/test/all_test.cpp @@ -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 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 vi(some_numbers, some_numbers + 5); + + int some_letters[] = { 'a', 'q', 'n', 'y', 'n' }; + std::vector 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 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 vi(some_numbers, some_numbers + 5); + + int some_letters[] = { 'a', 'q', 'n', 'y', 'n' }; + std::vector 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 vi(some_numbers, some_numbers + 5); + + int some_letters[] = { 'a', 'q', 'n', 'y', 'n' }; + std::vector 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; } diff --git a/include/boost/algorithm/all.hpp b/include/boost/algorithm/all.hpp index fa7d70e..18c02f5 100644 --- a/include/boost/algorithm/all.hpp +++ b/include/boost/algorithm/all.hpp @@ -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 // 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 // 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 +// use std::all_of; +// #else +// template +// 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 - 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 - 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 - 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 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 - 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 /// \param val A value to compare against /// template - 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 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 - 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 /// \param p A predicate /// template - 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 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 template 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 /// \param val A value to compare against /// template - 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 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 - 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 /// \param p A predicate /// template - 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 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 - 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 }} // namespace boost and algorithm +#undef ANY_DOING_0X #endif // BOOST_ALGORITHM_ALL_HPP