diff --git a/apply/test/apply_test.cpp b/apply/test/apply_test.cpp new file mode 100644 index 0000000..01bca88 --- /dev/null +++ b/apply/test/apply_test.cpp @@ -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 + +#include + +#include +#include +#include +#include + +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 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(), 10))); + BOOST_CHECK(20 == accumulate(v.begin(), v.end(), 0) && 0 == v[1] && 3 == v.size()); + } + + // Basic range test: + { + vector v(128); + + generate(v.begin(), v.end(), rand); + + BOOST_CHECK(apply(v, make_zero)); + + BOOST_CHECK(0 == accumulate(v.begin(), v.end(), 0)); + } + + { + list 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; +} diff --git a/include/boost/algorithm/apply.hpp b/include/boost/algorithm/apply.hpp new file mode 100644 index 0000000..d2da77e --- /dev/null +++ b/include/boost/algorithm/apply.hpp @@ -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 + +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 +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 +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 +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 +Mutator apply(ForwardReadableRange& R, Mutator m) +{ + return apply(boost::begin(R), boost::end(R), m); +} + +}}} // namespace boost::algorithm::sequence + +#endif // BOOST_ALGORITHM_APPLY_HPP