Initial checkin of apply() and apply_if() / BoostCon 2008

library-in-a-week


[SVN r45589]
This commit is contained in:
Jesse Williamson
2008-05-20 20:56:13 +00:00
parent 003a58264f
commit e9f8e116d3
2 changed files with 171 additions and 0 deletions

84
apply/test/apply_test.cpp Normal file
View File

@ -0,0 +1,84 @@
/*** show that you have a forward iterator
* more types
* more containers*/
/*
Copyright (C) 2008 Jesse Williamson
Use, modification and distribution are subject to the
Boost Software License, Version 1.0. (See accompanying file
LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#include <boost/test/included/test_exec_monitor.hpp>
#include <boost/algorithm/apply.hpp>
#include <list>
#include <vector>
#include <numeric>
#include <iostream>
using namespace std;
using namespace boost::algorithm::sequence;
int make_zero(int x)
{
return 0;
}
string make_z(string x)
{
return "z";
}
int test_main(int, char **)
{
// Basic test:
{
vector<int> v(3);
// Should work on a single element:
BOOST_CHECK(apply(v.begin(), v.begin(), make_zero));
BOOST_CHECK(0 == accumulate(v.begin(), v.end(), 0));
v[0] = 1;
v[1] = 2;
v[2] = 3;
BOOST_CHECK(apply(v.begin(), v.end(), make_zero));
BOOST_CHECK(0 == accumulate(v.begin(), v.end(), 0));
// Use a conditional function (apply_if()), and do not modify the sequence:
v[0] = 10;
v[1] = 2;
v[2] = 10;
BOOST_CHECK(apply_if(v.begin(), v.end(), make_zero, std::bind1st(std::not_equal_to<int>(), 10)));
BOOST_CHECK(20 == accumulate(v.begin(), v.end(), 0) && 0 == v[1] && 3 == v.size());
}
// Basic range test:
{
vector<int> v(128);
generate(v.begin(), v.end(), rand);
BOOST_CHECK(apply(v, make_zero));
BOOST_CHECK(0 == accumulate(v.begin(), v.end(), 0));
}
{
list<string> ls;
const char *s[] = { "hello", "there", "world", "!\n" };
copy(s, s + 4, back_inserter(ls));
apply(ls.begin(), ls.end(), make_z);
BOOST_CHECK(accumulate(ls.begin(), ls.end(), string()) == "zzzz");
}
return 0;
}

View File

@ -0,0 +1,87 @@
/*
Copyright (C) Jesse Williamson 2008.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_ALGORITHM_APPLY_HPP
#define BOOST_ALGORITHM_APPLY_HPP
/// \file apply.hpp
/// \brief Boost implementation of sequence-modifying algorithms.
/// \author Jesse Williamson
#include <boost/range.hpp>
namespace boost { namespace algorithm { namespace sequence {
/// \fn apply_if (ForwardIter begin, ForwardIter end, Mutator m, ConditionPredicate c)
/// \brief Applies m to all elements from (begin, end) for which c is true.
///
/// \param begin The start of the sequence to modify
/// \param last One past the end of the input sequence
/// \param m Unary mutator function object
/// \param c Unary condition function object
/// \return The mutator function object
///
template <class ForwardIter, class Mutator, class ConditionPredicate>
Mutator apply_if(ForwardIter begin, ForwardIter end, Mutator m, ConditionPredicate c)
{
for (; begin != end; begin++) {
if (c(*begin))
*begin = m(*begin);
}
return m;
}
/// \fn apply_if (Range r, Mutator m, ConditionPredicate c)
/// \brief Applies m to all elements in the range r for which c is true.
///
/// \param r The range to modify
/// \param m Unary mutator function object
/// \param c Unary condition function object
/// \return The mutator function object
///
template <class ForwardReadableRange, class Mutator, class ConditionPredicate>
Mutator apply_if(ForwardReadableRange& R, Mutator m, ConditionPredicate c)
{
return apply_if(boost::begin(R), boost::end(R), m, c);
}
/// \fn apply (ForwardIter begin, ForwardIter end, Mutator m)
/// \brief Applies m to all elements from (begin, end).
///
/// \param begin The start of the sequence to modify
/// \param last One past the end of the input sequence
/// \param m Unary mutator function object
/// \return The mutator function object
///
template <class ForwardIter, class Mutator>
Mutator apply(ForwardIter begin, ForwardIter end, Mutator m)
{
for (; begin != end; begin++)
*begin = m(*begin);
return m;
}
/// \fn apply (ForwardReadableRange r, Mutator m)
/// \brief Applies m to all elements in the range r
///
/// \param r The range to modify
/// \param m Unary mutator function object
/// \return The mutator function object
///
template <class ForwardReadableRange, class Mutator>
Mutator apply(ForwardReadableRange& R, Mutator m)
{
return apply(boost::begin(R), boost::end(R), m);
}
}}} // namespace boost::algorithm::sequence
#endif // BOOST_ALGORITHM_APPLY_HPP