|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r83064 - in trunk: boost/algorithm/cxx11 libs/algorithm/test
From: marshall_at_[hidden]
Date: 2013-02-21 11:18:52
Author: marshall
Date: 2013-02-21 11:18:51 EST (Thu, 21 Feb 2013)
New Revision: 83064
URL: http://svn.boost.org/trac/boost/changeset/83064
Log:
Changed the return type for boost::algorithm::copy_while and copy_until to return a pair<InputIterator, OutputIterator> instead of just an OutputIterator
Text files modified:
trunk/boost/algorithm/cxx11/copy_if.hpp | 21 ++++---
trunk/libs/algorithm/test/copy_if_test1.cpp | 101 ++++++++++++++++++++++++++++++++++++++-
2 files changed, 108 insertions(+), 14 deletions(-)
Modified: trunk/boost/algorithm/cxx11/copy_if.hpp
==============================================================================
--- trunk/boost/algorithm/cxx11/copy_if.hpp (original)
+++ trunk/boost/algorithm/cxx11/copy_if.hpp 2013-02-21 11:18:51 EST (Thu, 21 Feb 2013)
@@ -63,7 +63,7 @@
/// \fn copy_while ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
/// \brief Copies all the elements at the start of the input range that
/// satisfy the predicate to the output range.
-/// \return The updated output iterator
+/// \return The updated input and output iterators
///
/// \param first The start of the input sequence
/// \param last One past the end of the input sequence
@@ -71,25 +71,26 @@
/// \param p A predicate for testing the elements of the range
///
template<typename InputIterator, typename OutputIterator, typename Predicate>
-OutputIterator copy_while ( InputIterator first, InputIterator last,
- OutputIterator result, Predicate p )
+std::pair<InputIterator, OutputIterator>
+copy_while ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
{
for ( ; first != last && p(*first); ++first )
*result++ = *first;
- return result;
+ return std::make_pair(first, result);
}
/// \fn copy_while ( const Range &r, OutputIterator result, Predicate p )
/// \brief Copies all the elements at the start of the input range that
/// satisfy the predicate to the output range.
-/// \return The updated output iterator
+/// \return The updated input and output iterators
///
/// \param r The input range
/// \param result An output iterator to write the results into
/// \param p A predicate for testing the elements of the range
///
template<typename Range, typename OutputIterator, typename Predicate>
-OutputIterator copy_while ( const Range &r, OutputIterator result, Predicate p )
+std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
+copy_while ( const Range &r, OutputIterator result, Predicate p )
{
return boost::algorithm::copy_while (boost::begin (r), boost::end(r), result, p);
}
@@ -106,11 +107,12 @@
/// \param p A predicate for testing the elements of the range
///
template<typename InputIterator, typename OutputIterator, typename Predicate>
-OutputIterator copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
+std::pair<InputIterator, OutputIterator>
+copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
{
for ( ; first != last && !p(*first); ++first )
*result++ = *first;
- return result;
+ return std::make_pair(first, result);
}
/// \fn copy_until ( const Range &r, OutputIterator result, Predicate p )
@@ -123,7 +125,8 @@
/// \param p A predicate for testing the elements of the range
///
template<typename Range, typename OutputIterator, typename Predicate>
-OutputIterator copy_until ( const Range &r, OutputIterator result, Predicate p )
+std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
+copy_until ( const Range &r, OutputIterator result, Predicate p )
{
return boost::algorithm::copy_until (boost::begin (r), boost::end(r), result, p);
}
Modified: trunk/libs/algorithm/test/copy_if_test1.cpp
==============================================================================
--- trunk/libs/algorithm/test/copy_if_test1.cpp (original)
+++ trunk/libs/algorithm/test/copy_if_test1.cpp 2013-02-21 11:18:51 EST (Thu, 21 Feb 2013)
@@ -20,6 +20,7 @@
#include <list>
#include <boost/algorithm/cxx11/all_of.hpp>
+#include <boost/algorithm/cxx11/none_of.hpp>
namespace ba = boost::algorithm;
// namespace ba = boost;
@@ -30,7 +31,7 @@
bool is_odd ( int v ) { return v % 2 == 1; }
template <typename Container>
-void test_sequence ( Container const &c ) {
+void test_copy_if ( Container const &c ) {
typedef typename Container::value_type value_type;
std::vector<value_type> v;
@@ -48,13 +49,13 @@
v.clear ();
ba::copy_if ( c.begin (), c.end (), back_inserter ( v ), is_true);
BOOST_CHECK ( v.size () == c.size ());
- BOOST_CHECK ( std::equal ( c.begin (), c.end (), v.begin ()));
+ BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
v.clear ();
ba::copy_if ( c, back_inserter ( v ), is_true);
BOOST_CHECK ( v.size () == c.size ());
BOOST_CHECK ( v.size () == c.size ());
- BOOST_CHECK ( std::equal ( c.begin (), c.end (), v.begin ()));
+ BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
// Some of the elements
v.clear ();
@@ -69,16 +70,106 @@
}
+template <typename Container>
+void test_copy_while ( Container const &c ) {
+
+ typedef typename Container::value_type value_type;
+ typename Container::const_iterator it;
+ std::vector<value_type> v;
+
+// None of the elements
+ v.clear ();
+ ba::copy_while ( c.begin (), c.end (), back_inserter ( v ), is_false);
+ BOOST_CHECK ( v.size () == 0 );
+
+ v.clear ();
+ ba::copy_while ( c, back_inserter ( v ), is_false);
+ BOOST_CHECK ( v.size () == 0 );
+
+// All the elements
+ v.clear ();
+ ba::copy_while ( c.begin (), c.end (), back_inserter ( v ), is_true);
+ BOOST_CHECK ( v.size () == c.size ());
+ BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
+
+ v.clear ();
+ ba::copy_while ( c, back_inserter ( v ), is_true);
+ BOOST_CHECK ( v.size () == c.size ());
+ BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
+
+// Some of the elements
+ v.clear ();
+ it = ba::copy_while ( c.begin (), c.end (), back_inserter ( v ), is_even ).first;
+ BOOST_CHECK ( v.size () == (size_t) std::distance ( c.begin (), it ));
+ BOOST_CHECK ( it == c.end () || !is_even ( *it ));
+ BOOST_CHECK ( ba::all_of ( v.begin (), v.end (), is_even ));
+ BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
+
+ v.clear ();
+ it = ba::copy_while ( c, back_inserter ( v ), is_even ).first;
+ BOOST_CHECK ( v.size () == (size_t) std::distance ( c.begin (), it ));
+ BOOST_CHECK ( it == c.end () || !is_even ( *it ));
+ BOOST_CHECK ( ba::all_of ( v.begin (), v.end (), is_even ));
+ BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
+ }
+
+template <typename Container>
+void test_copy_until ( Container const &c ) {
+
+ typedef typename Container::value_type value_type;
+ typename Container::const_iterator it;
+ std::vector<value_type> v;
+
+// None of the elements
+ v.clear ();
+ ba::copy_until ( c.begin (), c.end (), back_inserter ( v ), is_true);
+ BOOST_CHECK ( v.size () == 0 );
+
+ v.clear ();
+ ba::copy_until ( c, back_inserter ( v ), is_true);
+ BOOST_CHECK ( v.size () == 0 );
+
+// All the elements
+ v.clear ();
+ ba::copy_until ( c.begin (), c.end (), back_inserter ( v ), is_false);
+ BOOST_CHECK ( v.size () == c.size ());
+ BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
+
+ v.clear ();
+ ba::copy_until ( c, back_inserter ( v ), is_false);
+ BOOST_CHECK ( v.size () == c.size ());
+ BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
+
+// Some of the elements
+ v.clear ();
+ it = ba::copy_until ( c.begin (), c.end (), back_inserter ( v ), is_even ).first;
+ BOOST_CHECK ( v.size () == (size_t) std::distance ( c.begin (), it ));
+ BOOST_CHECK ( it == c.end () || is_even ( *it ));
+ BOOST_CHECK ( ba::none_of ( v.begin (), v.end (), is_even ));
+ BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
+
+ v.clear ();
+ it = ba::copy_until ( c, back_inserter ( v ), is_even ).first;
+ BOOST_CHECK ( v.size () == (size_t) std::distance ( c.begin (), it ));
+ BOOST_CHECK ( it == c.end () || is_even ( *it ));
+ BOOST_CHECK ( ba::none_of ( v.begin (), v.end (), is_even ));
+ BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
+ }
+
void test_sequence1 () {
std::vector<int> v;
for ( int i = 5; i < 15; ++i )
v.push_back ( i );
- test_sequence ( v );
+ test_copy_if ( v );
+ test_copy_while ( v );
+ test_copy_until ( v );
std::list<int> l;
for ( int i = 25; i > 15; --i )
l.push_back ( i );
- test_sequence ( l );
+ test_copy_if ( l );
+ test_copy_while ( l );
+ test_copy_until ( l );
}
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