Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r52886 - in sandbox: boost/algorithm libs/algorithm/all/test
From: marshall_at_[hidden]
Date: 2009-05-10 19:17:01


Author: marshall
Date: 2009-05-10 19:16:59 EDT (Sun, 10 May 2009)
New Revision: 52886
URL: http://svn.boost.org/trac/boost/changeset/52886

Log:
Changed the name of the functions to match the ones in n2666 (and C++0x)
Text files modified:
   sandbox/boost/algorithm/all.hpp | 82 +++++++++++++++++++++---------------
   sandbox/libs/algorithm/all/test/all_test.cpp | 88 +++++++++++++++++++++++++++++++++++----
   2 files changed, 127 insertions(+), 43 deletions(-)

Modified: sandbox/boost/algorithm/all.hpp
==============================================================================
--- sandbox/boost/algorithm/all.hpp (original)
+++ sandbox/boost/algorithm/all.hpp 2009-05-10 19:16:59 EDT (Sun, 10 May 2009)
@@ -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.
+/// \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
+#include <boost/range.hpp> // For boost::begin and boost::end
 
-/// \file all.hpp
-/// \brief Test ranges against predicates.
-/// \author Marshall Clow
+// 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 @@
 /// \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 @@
     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 @@
 /// \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 @@
     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 @@
 /// \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 @@
     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 @@
 /// \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 @@
     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 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 @@
 /// \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 @@
     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 @@
 /// \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 @@
     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 @@
 
 }} // namespace boost and algorithm
 
+#undef ANY_DOING_0X
 #endif // BOOST_ALGORITHM_ALL_HPP

Modified: sandbox/libs/algorithm/all/test/all_test.cpp
==============================================================================
--- sandbox/libs/algorithm/all/test/all_test.cpp (original)
+++ sandbox/libs/algorithm/all/test/all_test.cpp 2009-05-10 19:16:59 EDT (Sun, 10 May 2009)
@@ -25,22 +25,92 @@
 
 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));
+// 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!
+}
 
- BOOST_CHECK_EQUAL(true, ba::none(vc, 'z'));
- BOOST_CHECK_EQUAL(false, ba::none(vc, 'a'));
+
+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;
 }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk