forked from boostorg/algorithm
		
	Implement the C++17 new algorithms
This commit is contained in:
		
							
								
								
									
										52
									
								
								include/boost/algorithm/cxx17/exclusive_scan.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								include/boost/algorithm/cxx17/exclusive_scan.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,52 @@ | ||||
| /* | ||||
|    Copyright (c) Marshall Clow 2017. | ||||
|  | ||||
|    Distributed under the Boost Software License, Version 1.0. (See accompanying | ||||
|    file LICENSE10.txt or copy at http://www.boost.org/LICENSE10.txt) | ||||
| */ | ||||
|  | ||||
| /// \file  exclusive_scan.hpp | ||||
| /// \brief ??? | ||||
| /// \author Marshall Clow | ||||
|  | ||||
| #ifndef BOOST_ALGORITHM_EXCLUSIVE_SCAN_HPP | ||||
| #define BOOST_ALGORITHM_EXCLUSIVE_SCAN_HPP | ||||
|  | ||||
| #include <functional>     // for std::plus | ||||
| #include <iterator>       // for std::iterator_traits | ||||
|  | ||||
| #include <boost/range/begin.hpp> | ||||
| #include <boost/range/end.hpp> | ||||
| #include <boost/range/value_type.hpp> | ||||
|  | ||||
| namespace boost { namespace algorithm { | ||||
|  | ||||
| template<class InputIterator, class OutputIterator, class T, class BinaryOperation> | ||||
| OutputIterator exclusive_scan(InputIterator first, InputIterator last, | ||||
|                               OutputIterator result, T init, BinaryOperation bOp) | ||||
| { | ||||
|     if (first != last) | ||||
|     { | ||||
|         T saved = init; | ||||
|         do | ||||
|         { | ||||
|             init = bOp(init, *first); | ||||
|             *result = saved; | ||||
|             saved = init; | ||||
|             ++result; | ||||
|         } while (++first != last); | ||||
|     } | ||||
|     return result; | ||||
| } | ||||
|  | ||||
| template<class InputIterator, class OutputIterator, class T> | ||||
| OutputIterator exclusive_scan(InputIterator first, InputIterator last, | ||||
|                               OutputIterator result, T init) | ||||
| { | ||||
| 	typedef typename std::iterator_traits<InputIterator>::value_type VT; | ||||
|     return exclusive_scan(first, last, result, init, std::plus<VT>()); | ||||
| } | ||||
|  | ||||
| }} // namespace boost and algorithm | ||||
|  | ||||
| #endif // BOOST_ALGORITHM_EXCLUSIVE_SCAN_HPP | ||||
							
								
								
									
										37
									
								
								include/boost/algorithm/cxx17/for_each_n.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								include/boost/algorithm/cxx17/for_each_n.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
| /* | ||||
|    Copyright (c) Marshall Clow 2017. | ||||
|  | ||||
|    Distributed under the Boost Software License, Version 1.0. (See accompanying | ||||
|    file LICENSE10.txt or copy at http://www.boost.org/LICENSE10.txt) | ||||
| */ | ||||
|  | ||||
| /// \file  for_each_n.hpp | ||||
| /// \brief Apply a functor to the elements of a sequence | ||||
| /// \author Marshall Clow | ||||
|  | ||||
| #ifndef BOOST_ALGORITHM_FOR_EACH_N_HPP | ||||
| #define BOOST_ALGORITHM_FOR_EACH_N_HPP | ||||
|  | ||||
| #include <utility>      // for std::pair | ||||
|  | ||||
| namespace boost { namespace algorithm { | ||||
|  | ||||
| /// \fn for_each_n(InputIterator first, Size n, Function f); | ||||
| /// \return first + n | ||||
| /// | ||||
| /// \param first    The start of the first range. | ||||
| /// \param n        One past the end of the first range. | ||||
| /// \param f        A functor to apply to the elements of the sequence | ||||
| /// \note           If f returns a result, the result is ignored. | ||||
| template<class InputIterator, class Size, class Function> | ||||
| InputIterator for_each_n(InputIterator first, Size n, Function f) | ||||
| { | ||||
|     for ( ; n > 0; --n, ++first ) | ||||
|         f(*first); | ||||
|  | ||||
|     return first; | ||||
| } | ||||
|  | ||||
| }} // namespace boost and algorithm | ||||
|  | ||||
| #endif // BOOST_ALGORITHM_FOR_EACH_N_HPP | ||||
							
								
								
									
										60
									
								
								include/boost/algorithm/cxx17/inclusive_scan.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								include/boost/algorithm/cxx17/inclusive_scan.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,60 @@ | ||||
| /* | ||||
|    Copyright (c) Marshall Clow 2017. | ||||
|  | ||||
|    Distributed under the Boost Software License, Version 1.0. (See accompanying | ||||
|    file LICENSE10.txt or copy at http://www.boost.org/LICENSE10.txt) | ||||
| */ | ||||
|  | ||||
| /// \file  transform_reduce.hpp | ||||
| /// \brief Combine the (transformed) elements of a sequence (or two) into a single value. | ||||
| /// \author Marshall Clow | ||||
|  | ||||
| #ifndef BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP | ||||
| #define BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP | ||||
|  | ||||
| #include <functional>     // for std::plus | ||||
| #include <iterator>       // for std::iterator_traits | ||||
|  | ||||
| #include <boost/range/begin.hpp> | ||||
| #include <boost/range/end.hpp> | ||||
| #include <boost/range/value_type.hpp> | ||||
|  | ||||
| namespace boost { namespace algorithm { | ||||
|  | ||||
| template<class InputIterator, class OutputIterator, class T, class BinaryOperation> | ||||
| OutputIterator inclusive_scan(InputIterator first, InputIterator last, | ||||
|                               OutputIterator result, BinaryOperation bOp, T init) | ||||
| { | ||||
|     for (; first != last; ++first, (void) ++result) { | ||||
|         init = bOp(init, *first); | ||||
|         *result = init; | ||||
|         } | ||||
|     return result; | ||||
| } | ||||
|  | ||||
|  | ||||
| template<class InputIterator, class OutputIterator, class BinaryOperation> | ||||
| OutputIterator inclusive_scan(InputIterator first, InputIterator last, | ||||
|                               OutputIterator result, BinaryOperation bOp) | ||||
| { | ||||
|     if (first != last) { | ||||
|         typename std::iterator_traits<InputIterator>::value_type init = *first; | ||||
|         *result++ = init; | ||||
|         if (++first != last) | ||||
|             return inclusive_scan(first, last, result, bOp, init); | ||||
|         } | ||||
|  | ||||
|     return result; | ||||
| } | ||||
|  | ||||
| template<class InputIterator, class OutputIterator> | ||||
| OutputIterator inclusive_scan(InputIterator first, InputIterator last, | ||||
|                    OutputIterator result) | ||||
| { | ||||
|     typedef typename std::iterator_traits<InputIterator>::value_type VT; | ||||
|     return inclusive_scan(first, last, result, std::plus<VT>()); | ||||
| } | ||||
|  | ||||
| }} // namespace boost and algorithm | ||||
|  | ||||
| #endif // BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP | ||||
							
								
								
									
										72
									
								
								include/boost/algorithm/cxx17/reduce.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								include/boost/algorithm/cxx17/reduce.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,72 @@ | ||||
| /* | ||||
|    Copyright (c) Marshall Clow 2017. | ||||
|  | ||||
|    Distributed under the Boost Software License, Version 1.0. (See accompanying | ||||
|    file LICENSE10.txt or copy at http://www.boost.org/LICENSE10.txt) | ||||
| */ | ||||
|  | ||||
| /// \file  reduce.hpp | ||||
| /// \brief Combine the elements of a sequence into a single value | ||||
| /// \author Marshall Clow | ||||
|  | ||||
| #ifndef BOOST_ALGORITHM_REDUCE_HPP | ||||
| #define BOOST_ALGORITHM_REDUCE_HPP | ||||
|  | ||||
| #include <functional>     // for std::plus | ||||
| #include <iterator>       // for std::iterator_traits | ||||
|  | ||||
| #include <boost/range/begin.hpp> | ||||
| #include <boost/range/end.hpp> | ||||
| #include <boost/range/value_type.hpp> | ||||
|  | ||||
| namespace boost { namespace algorithm { | ||||
|  | ||||
| template<class InputIterator, class T, class BinaryOperation> | ||||
| T reduce(InputIterator first, InputIterator last, T init, BinaryOperation bOp) | ||||
| { | ||||
|     ; | ||||
|     for (; first != last; ++first) | ||||
|         init = bOp(init, *first); | ||||
|     return init; | ||||
| } | ||||
|  | ||||
| template<class InputIterator, class T> | ||||
| T reduce(InputIterator first, InputIterator last, T init) | ||||
| { | ||||
| 	typedef typename std::iterator_traits<InputIterator>::value_type VT; | ||||
|     return reduce(first, last, init, std::plus<VT>()); | ||||
| } | ||||
|  | ||||
| template<class InputIterator> | ||||
| typename std::iterator_traits<InputIterator>::value_type | ||||
| reduce(InputIterator first, InputIterator last) | ||||
| { | ||||
|     return reduce(first, last, | ||||
|        typename std::iterator_traits<InputIterator>::value_type()); | ||||
| } | ||||
|  | ||||
| template<class Range> | ||||
| typename boost::range_value<Range>::type | ||||
| reduce(const Range &r) | ||||
| { | ||||
|     return reduce(boost::begin(r), boost::end(r)); | ||||
| } | ||||
|  | ||||
| //	Not sure that this won't be ambiguous (1) | ||||
| template<class Range, class T> | ||||
| T reduce(const Range &r, T init) | ||||
| { | ||||
|     return reduce(boost::begin (r), boost::end (r), init); | ||||
| } | ||||
|  | ||||
|  | ||||
| //	Not sure that this won't be ambiguous (2) | ||||
| template<class Range, class T, class BinaryOperation> | ||||
| T reduce(const Range &r, T init, BinaryOperation bOp) | ||||
| { | ||||
|     return reduce(boost::begin(r), boost::end(r), init, bOp); | ||||
| } | ||||
|  | ||||
| }} // namespace boost and algorithm | ||||
|  | ||||
| #endif // BOOST_ALGORITHM_REDUCE_HPP | ||||
							
								
								
									
										46
									
								
								include/boost/algorithm/cxx17/transform_exclusive_scan.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								include/boost/algorithm/cxx17/transform_exclusive_scan.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,46 @@ | ||||
| /* | ||||
|    Copyright (c) Marshall Clow 2017. | ||||
|  | ||||
|    Distributed under the Boost Software License, Version 1.0. (See accompanying | ||||
|    file LICENSE10.txt or copy at http://www.boost.org/LICENSE10.txt) | ||||
| */ | ||||
|  | ||||
| /// \file  transform_exclusive_scan.hpp | ||||
| /// \brief ???? | ||||
| /// \author Marshall Clow | ||||
|  | ||||
| #ifndef BOOST_ALGORITHM_TRANSFORM_EXCLUSIVE_SCAN_HPP | ||||
| #define BOOST_ALGORITHM_TRANSFORM_EXCLUSIVE_SCAN_HPP | ||||
|  | ||||
| #include <functional>     // for std::plus | ||||
| #include <iterator>       // for std::iterator_traits | ||||
|  | ||||
| #include <boost/range/begin.hpp> | ||||
| #include <boost/range/end.hpp> | ||||
| #include <boost/range/value_type.hpp> | ||||
|  | ||||
| namespace boost { namespace algorithm { | ||||
|  | ||||
| template<class InputIterator, class OutputIterator, class T, | ||||
|          class BinaryOperation, class UnaryOperation> | ||||
| OutputIterator transform_exclusive_scan(InputIterator first, InputIterator last, | ||||
|                                         OutputIterator result, T init, | ||||
|                                         BinaryOperation bOp, UnaryOperation uOp) | ||||
| { | ||||
|     if (first != last) | ||||
|     { | ||||
|         T saved = init; | ||||
|         do | ||||
|         { | ||||
|             init = bOp(init, uOp(*first)); | ||||
|             *result = saved; | ||||
|             saved = init; | ||||
|             ++result; | ||||
|         } while (++first != last); | ||||
|     } | ||||
|     return result; | ||||
| } | ||||
|  | ||||
| }} // namespace boost and algorithm | ||||
|  | ||||
| #endif // BOOST_ALGORITHM_TRANSFORM_EXCLUSIVE_SCAN_HPP | ||||
							
								
								
									
										58
									
								
								include/boost/algorithm/cxx17/transform_inclusive_scan.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								include/boost/algorithm/cxx17/transform_inclusive_scan.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,58 @@ | ||||
| /* | ||||
|    Copyright (c) Marshall Clow 2017. | ||||
|  | ||||
|    Distributed under the Boost Software License, Version 1.0. (See accompanying | ||||
|    file LICENSE10.txt or copy at http://www.boost.org/LICENSE10.txt) | ||||
| */ | ||||
|  | ||||
| /// \file  transform_reduce.hpp | ||||
| /// \brief Combine the (transformed) elements of a sequence (or two) into a single value. | ||||
| /// \author Marshall Clow | ||||
|  | ||||
| #ifndef BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP | ||||
| #define BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP | ||||
|  | ||||
| #include <functional>     // for std::plus | ||||
| #include <iterator>       // for std::iterator_traits | ||||
|  | ||||
| #include <boost/range/begin.hpp> | ||||
| #include <boost/range/end.hpp> | ||||
| #include <boost/range/value_type.hpp> | ||||
|  | ||||
| namespace boost { namespace algorithm { | ||||
|  | ||||
| template<class InputIterator, class OutputIterator, | ||||
|          class BinaryOperation, class UnaryOperation, class T> | ||||
| OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last, | ||||
|                                         OutputIterator result, | ||||
|                                         BinaryOperation bOp, UnaryOperation uOp, | ||||
|                                         T init) | ||||
| { | ||||
|     for (; first != last; ++first, (void) ++result) { | ||||
|         init = bOp(init, uOp(*first)); | ||||
|         *result = init; | ||||
|         } | ||||
|  | ||||
|     return result; | ||||
| } | ||||
|  | ||||
| template<class InputIterator, class OutputIterator, | ||||
|          class BinaryOperation, class UnaryOperation> | ||||
| OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last, | ||||
|                                         OutputIterator result, | ||||
|                                         BinaryOperation bOp, UnaryOperation uOp) | ||||
| { | ||||
|     if (first != last) { | ||||
|         typename std::iterator_traits<InputIterator>::value_type init = uOp(*first); | ||||
|         *result++ = init; | ||||
|         if (++first != last) | ||||
|             return transform_inclusive_scan(first, last, result, bOp, uOp, init); | ||||
|         } | ||||
|  | ||||
|     return result; | ||||
| } | ||||
|  | ||||
|  | ||||
| }} // namespace boost and algorithm | ||||
|  | ||||
| #endif // BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP | ||||
							
								
								
									
										55
									
								
								include/boost/algorithm/cxx17/transform_reduce.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								include/boost/algorithm/cxx17/transform_reduce.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,55 @@ | ||||
| /* | ||||
|    Copyright (c) Marshall Clow 2017. | ||||
|  | ||||
|    Distributed under the Boost Software License, Version 1.0. (See accompanying | ||||
|    file LICENSE10.txt or copy at http://www.boost.org/LICENSE10.txt) | ||||
| */ | ||||
|  | ||||
| /// \file  transform_reduce.hpp | ||||
| /// \brief Combine the (transformed) elements of a sequence (or two) into a single value. | ||||
| /// \author Marshall Clow | ||||
|  | ||||
| #ifndef BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP | ||||
| #define BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP | ||||
|  | ||||
| #include <functional>     // for std::plus | ||||
| #include <iterator>       // for std::iterator_traits | ||||
|  | ||||
| #include <boost/range/begin.hpp> | ||||
| #include <boost/range/end.hpp> | ||||
| #include <boost/range/value_type.hpp> | ||||
|  | ||||
| namespace boost { namespace algorithm { | ||||
|  | ||||
| template<class InputIterator1, class InputIterator2, class T, | ||||
|          class BinaryOperation1, class BinaryOperation2> | ||||
| T transform_reduce(InputIterator1 first1, InputIterator1 last1, | ||||
|                    InputIterator2 first2, T init, | ||||
|                  BinaryOperation1 bOp1, BinaryOperation2 bOp2) | ||||
| { | ||||
|     for (; first1 != last1; ++first1, (void) ++first2) | ||||
|         init = bOp1(init, bOp2(*first1, *first2)); | ||||
|     return init; | ||||
| } | ||||
|  | ||||
| template<class InputIterator, class T, | ||||
|          class BinaryOperation, class UnaryOperation> | ||||
| T transform_reduce(InputIterator first, InputIterator last, | ||||
|                    T init, BinaryOperation bOp, UnaryOperation uOp) | ||||
| { | ||||
|     for (; first != last; ++first) | ||||
|         init = bOp(init, uOp(*first)); | ||||
|     return init; | ||||
| } | ||||
|  | ||||
| template<class InputIterator1, class InputIterator2, class T> | ||||
| T transform_reduce(InputIterator1 first1, InputIterator1 last1, | ||||
|                    InputIterator2 first2, T init) | ||||
| { | ||||
|     return transform_reduce(first1, last1, first2, init, | ||||
|                             std::plus<T>(), std::multiplies<T>()); | ||||
| } | ||||
|  | ||||
| }} // namespace boost and algorithm | ||||
|  | ||||
| #endif // BOOST_ALGORITHM_TRANSFORM_REDUCE_HPP | ||||
		Reference in New Issue
	
	Block a user