From c54c980b7919beaa06050483fef6d72fd5116b46 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Sat, 5 Apr 2014 21:23:26 +0900 Subject: [PATCH] add C++14 constexpr copy_while, copy_until, clamp_range --- sprout/algorithm/cxx14.hpp | 7 ++-- sprout/algorithm/cxx14/clamp_range.hpp | 55 ++++++++++++++++++++++++++ sprout/algorithm/cxx14/copy_until.hpp | 33 ++++++++++++++++ sprout/algorithm/cxx14/copy_while.hpp | 33 ++++++++++++++++ 4 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 sprout/algorithm/cxx14/clamp_range.hpp create mode 100644 sprout/algorithm/cxx14/copy_until.hpp create mode 100644 sprout/algorithm/cxx14/copy_while.hpp diff --git a/sprout/algorithm/cxx14.hpp b/sprout/algorithm/cxx14.hpp index 3602ef5f..710e471d 100644 --- a/sprout/algorithm/cxx14.hpp +++ b/sprout/algorithm/cxx14.hpp @@ -59,10 +59,9 @@ //#include //#include #include -//#include -//#include -//#include -//#include +#include +#include +#include //#include //#include //#include diff --git a/sprout/algorithm/cxx14/clamp_range.hpp b/sprout/algorithm/cxx14/clamp_range.hpp new file mode 100644 index 00000000..1acfd5ca --- /dev/null +++ b/sprout/algorithm/cxx14/clamp_range.hpp @@ -0,0 +1,55 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + 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 SPROUT_ALGORITHM_CXX14_CLAMP_RANGE_HPP +#define SPROUT_ALGORITHM_CXX14_CLAMP_RANGE_HPP + +#include +#include +#include +#include + +namespace sprout { + // + // clamp_range + // + template< + typename InputIterator, typename OutputIterator, typename Compare, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CXX14_CONSTEXPR OutputIterator + clamp_range( + InputIterator first, InputIterator last, OutputIterator result, + typename std::iterator_traits::value_type const& low, + typename std::iterator_traits::value_type const& high, + Compare comp + ) + { + while (first != last) { + *result++ = sprout::clamp(*first++, low, high, comp); + } + return result; + } + template< + typename InputIterator, typename OutputIterator, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CXX14_CONSTEXPR OutputIterator + clamp_range( + InputIterator first, InputIterator last, OutputIterator result, + typename std::iterator_traits::value_type const& low, + typename std::iterator_traits::value_type const& high + ) + { + while (first != last) { + *result++ = sprout::clamp(*first++, low, high); + } + return result; + } +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_CXX14_CLAMP_RANGE_HPP diff --git a/sprout/algorithm/cxx14/copy_until.hpp b/sprout/algorithm/cxx14/copy_until.hpp new file mode 100644 index 00000000..32f08786 --- /dev/null +++ b/sprout/algorithm/cxx14/copy_until.hpp @@ -0,0 +1,33 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + 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 SPROUT_ALGORITHM_CXX14_COPY_UNTIL_HPP +#define SPROUT_ALGORITHM_CXX14_COPY_UNTIL_HPP + +#include +#include +#include +#include + +namespace sprout { + // + // copy_until + // + template< + typename InputIterator, typename OutputIterator, typename Predicate, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CXX14_CONSTEXPR sprout::pair + copy_until(InputIterator first, InputIterator last, OutputIterator result, Predicate pred) { + for (; first != last && !pred(*first); ++first) { + *result++ = *first; + } + return std::make_pair(first, result); + } +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_CXX14_COPY_UNTIL_HPP diff --git a/sprout/algorithm/cxx14/copy_while.hpp b/sprout/algorithm/cxx14/copy_while.hpp new file mode 100644 index 00000000..4052608d --- /dev/null +++ b/sprout/algorithm/cxx14/copy_while.hpp @@ -0,0 +1,33 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + 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 SPROUT_ALGORITHM_CXX14_COPY_WHILE_HPP +#define SPROUT_ALGORITHM_CXX14_COPY_WHILE_HPP + +#include +#include +#include +#include + +namespace sprout { + // + // copy_while + // + template< + typename InputIterator, typename OutputIterator, typename Predicate, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CXX14_CONSTEXPR sprout::pair + copy_while(InputIterator first, InputIterator last, OutputIterator result, Predicate pred) { + for (; first != last && pred(*first); ++first) { + *result++ = *first; + } + return std::make_pair(first, result); + } +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_CXX14_COPY_WHILE_HPP