From 1419ba6c1e8676e01547de624f0992077f98530c Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Mon, 7 Jul 2008 22:09:00 +0000 Subject: [PATCH] More Algorithms [SVN r47198] --- include/boost/algorithm/copy.hpp | 74 +++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 6 deletions(-) diff --git a/include/boost/algorithm/copy.hpp b/include/boost/algorithm/copy.hpp index c18e9f2..01ee254 100644 --- a/include/boost/algorithm/copy.hpp +++ b/include/boost/algorithm/copy.hpp @@ -6,6 +6,9 @@ Revision history: 05 May 2008 mtc First version - as part of BoostCon 2008 + 07 Jul 2008 mtc Added more algorithms proposed by Matt Austern as part of C++Ox + + */ #ifndef BOOST_ALGORITHM_SEQUENCE_COPY_HPP @@ -197,16 +200,33 @@ template /// \param res An output iterator to copy into /// \return The (modified) output iterator /// -// template -// O copy_n ( I first, Size count, O res ) - template - O copy_n ( I first, typename std::iterator_traits::difference_type count, O res ) + template + O copy_n ( I first, Size count, O res ) +// template +// O copy_n ( I first, typename std::iterator_traits::difference_type count, O res ) { - while ( count-- > 0 ) - *res++ = *first++; + for ( ; count > 0; ++res, ++first, --count ) + *res = *first; return res; } +/// \fn uninitialized_copy_n ( I first, Size count, O res ) +/// \brief xxx +/// +/// \param first The start of the input sequence +/// \param count The number of elements to copy +/// \param res An output iterator to copy into +/// \return The (modified) output iterator +/// + template + O uninitialized_copy_n ( I first, Size count, O res ) + { + typedef typename std::iterator_traits::value_type vt; + for ( ; count > 0; ++res, ++first, --count ) + new ( static_cast ( &*res )) vt (*first); + return res; + } + // Range-based versions of copy and copy_backwards. /// \fn copy ( Range range, O res ) @@ -240,6 +260,48 @@ template return std::copy_backward ( boost::begin ( range ), boost::end ( range ), res ); } + +/// \fn partition_copy ( I first, I last, O1 out_true, O2 out_false, Pred pred ) +/// \brief Copies each element from [first, last) into one of the two output sequences, +/// depending on the result of the predicate +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param out_true An output iterator to copy into +/// \param out_false An output iterator to copy into +/// \param p A predicate to determine which output sequence to copy into. +/// +/// + template + std::pair partition_copy ( I first, I last, O1 out_true, O2 out_false, Pred pred ) + { + while (first != last) + { + if (p(*first)) + *out_true++ = *first++; + else + *out_false++ = *first++; + } + + return std::make_pair ( out_true, out_false ); + } + + +/// \fn partition_copy ( I first, I last, O1 out_true, O2 out_false, Pred pred ) +/// \brief Copies each element from the range into one of the two output sequences, +/// depending on the result of the predicate +/// +/// \param range The input range +/// \param out_true An output iterator to copy into +/// \param out_false An output iterator to copy into +/// \param p A predicate to determine which output sequence to copy into. +/// + template + std::pair partition_copy ( Range range, O1 out_true, O2 out_false, Pred pred ) + { + return partition_copy ( boost::begin ( range ), boost::end ( range ), out_true, out_false, pred ); + } + }}} // namespace boost & algorithm & sequence #endif // BOOST_ALGORITHM_SEQUENCE_COPY_HPP