From 79ea4f0885fdfbacdc6ca3b5fb9db3056150a05c Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Sat, 13 Oct 2012 21:06:32 +0900 Subject: [PATCH] add algorithm clamp, clamp_range_copy, clamp_range --- libs/algorithm/test/clamp_range.cpp | 143 ++++++++++++ libs/algorithm/test/clamp_range_copy.cpp | 221 ++++++++++++++++++ libs/algorithm/test/modifying.cpp | 4 + sprout/algorithm/adjacent_find.hpp | 2 +- sprout/algorithm/all_of.hpp | 2 +- sprout/algorithm/any_of.hpp | 2 +- sprout/algorithm/clamp.hpp | 30 +++ sprout/algorithm/clamp_range.hpp | 8 + sprout/algorithm/clamp_range_copy.hpp | 8 + sprout/algorithm/count_if.hpp | 2 +- sprout/algorithm/equal.hpp | 2 +- sprout/algorithm/find_if.hpp | 2 +- sprout/algorithm/find_if_not.hpp | 2 +- sprout/algorithm/fit.hpp | 2 + sprout/algorithm/fit/clamp_range.hpp | 79 +++++++ sprout/algorithm/fit/clamp_range_copy.hpp | 81 +++++++ sprout/algorithm/fixed.hpp | 2 + sprout/algorithm/fixed/clamp_range.hpp | 41 ++++ sprout/algorithm/fixed/clamp_range_copy.hpp | 145 ++++++++++++ sprout/algorithm/is_sorted_until.hpp | 2 +- sprout/algorithm/mismatch.hpp | 2 +- sprout/algorithm/non_modifying.hpp | 1 + sprout/algorithm/none_of.hpp | 2 +- sprout/iterator/filter_iterator.hpp | 2 +- sprout/range/algorithm/clamp_range_copy.hpp | 8 + sprout/range/algorithm/fit.hpp | 1 + .../range/algorithm/fit/clamp_range_copy.hpp | 41 ++++ sprout/range/algorithm/fixed.hpp | 1 + .../algorithm/fixed/clamp_range_copy.hpp | 43 ++++ 29 files changed, 870 insertions(+), 11 deletions(-) create mode 100644 libs/algorithm/test/clamp_range.cpp create mode 100644 libs/algorithm/test/clamp_range_copy.cpp create mode 100644 sprout/algorithm/clamp.hpp create mode 100644 sprout/algorithm/clamp_range.hpp create mode 100644 sprout/algorithm/clamp_range_copy.hpp create mode 100644 sprout/algorithm/fit/clamp_range.hpp create mode 100644 sprout/algorithm/fit/clamp_range_copy.hpp create mode 100644 sprout/algorithm/fixed/clamp_range.hpp create mode 100644 sprout/algorithm/fixed/clamp_range_copy.hpp create mode 100644 sprout/range/algorithm/clamp_range_copy.hpp create mode 100644 sprout/range/algorithm/fit/clamp_range_copy.hpp create mode 100644 sprout/range/algorithm/fixed/clamp_range_copy.hpp diff --git a/libs/algorithm/test/clamp_range.cpp b/libs/algorithm/test/clamp_range.cpp new file mode 100644 index 00000000..ccb85a58 --- /dev/null +++ b/libs/algorithm/test/clamp_range.cpp @@ -0,0 +1,143 @@ +#ifndef SPROUT_LIBS_ALGORITHM_TEST_CLAMP_RANGE_CPP +#define SPROUT_LIBS_ALGORITHM_TEST_CLAMP_RANGE_CPP + +#include +#include +#include +#include +#include + +namespace testspr { + static void algorithm_clamp_range_test() { + using namespace sprout; + { + SPROUT_STATIC_CONSTEXPR auto arr1 = array{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}; + + // クランプ (4 <= x <= 7) + { + SPROUT_STATIC_CONSTEXPR auto clamped = sprout::clamp_range( + arr1, + 4, + 7 + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + clamped, + array{{4, 4, 4, 4, 5, 6, 7, 7, 7, 7}} + )); + } + { + SPROUT_STATIC_CONSTEXPR auto clamped = sprout::fit::clamp_range( + arr1, + 4, + 7 + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + clamped, + array{{4, 4, 4, 4, 5, 6, 7, 7, 7, 7}} + )); + } + // クランプ (4 <= x <= 7) + // 範囲の切り出し + { + SPROUT_STATIC_CONSTEXPR auto clamped = sprout::clamp_range( + sprout::sub(arr1, 2, 8), + 4, + 7 + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + clamped, + array{{4, 4, 5, 6, 7, 7}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(clamped), + array{{1, 2, 4, 4, 5, 6, 7, 7, 9, 10}} + )); + } + { + SPROUT_STATIC_CONSTEXPR auto clamped = sprout::fit::clamp_range( + sprout::sub(arr1, 2, 8), + 4, + 7 + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + clamped, + array{{4, 4, 5, 6, 7, 7}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(clamped), + array{{1, 2, 4, 4, 5, 6, 7, 7, 9, 10}} + )); + } + } + { + SPROUT_STATIC_CONSTEXPR auto arr1 = array{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}; + + // クランプ (4 <= x <= 7) + { + SPROUT_STATIC_CONSTEXPR auto clamped = sprout::clamp_range( + arr1, + 4, + 7, + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + clamped, + array{{4, 4, 4, 4, 5, 6, 7, 7, 7, 7}} + )); + } + { + SPROUT_STATIC_CONSTEXPR auto clamped = sprout::fit::clamp_range( + arr1, + 4, + 7, + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + clamped, + array{{4, 4, 4, 4, 5, 6, 7, 7, 7, 7}} + )); + } + // クランプ (4 <= x <= 7) + // 範囲の切り出し + { + SPROUT_STATIC_CONSTEXPR auto clamped = sprout::clamp_range( + sprout::sub(arr1, 2, 8), + 4, + 7, + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + clamped, + array{{4, 4, 5, 6, 7, 7}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(clamped), + array{{1, 2, 4, 4, 5, 6, 7, 7, 9, 10}} + )); + } + { + SPROUT_STATIC_CONSTEXPR auto clamped = sprout::fit::clamp_range( + sprout::sub(arr1, 2, 8), + 4, + 7, + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + clamped, + array{{4, 4, 5, 6, 7, 7}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(clamped), + array{{1, 2, 4, 4, 5, 6, 7, 7, 9, 10}} + )); + } + } + } +} // namespace testspr + +#ifndef TESTSPR_CPP_INCLUDE +# define TESTSPR_TEST_FUNCTION testspr::algorithm_clamp_range_test +# include +#endif + +#endif // #ifndef SPROUT_LIBS_ALGORITHM_TEST_CLAMP_RANGE_CPP diff --git a/libs/algorithm/test/clamp_range_copy.cpp b/libs/algorithm/test/clamp_range_copy.cpp new file mode 100644 index 00000000..61b45055 --- /dev/null +++ b/libs/algorithm/test/clamp_range_copy.cpp @@ -0,0 +1,221 @@ +#ifndef SPROUT_LIBS_ALGORITHM_TEST_CLAMP_RANGE_COPY_CPP +#define SPROUT_LIBS_ALGORITHM_TEST_CLAMP_RANGE_COPY_CPP + +#include +#include +#include +#include +#include + +namespace testspr { + static void algorithm_clamp_range_copy_test() { + using namespace sprout; + { + SPROUT_STATIC_CONSTEXPR auto arr1 = array{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}; + SPROUT_STATIC_CONSTEXPR auto arr2 = array{}; + SPROUT_STATIC_CONSTEXPR auto arr3 = array{}; + + // [2 .. 8) の範囲をクランプ (4 <= x <= 7) + { + SPROUT_STATIC_CONSTEXPR auto clamped = sprout::clamp_range_copy( + sprout::begin(arr1) + 2, + sprout::begin(arr1) + 8, + arr2, + 4, + 7 + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + clamped, + array{{4, 4, 5, 6, 7, 7, 0, 0, 0, 0}} + )); + } + { + SPROUT_STATIC_CONSTEXPR auto clamped = sprout::fit::clamp_range_copy( + sprout::begin(arr1) + 2, + sprout::begin(arr1) + 8, + arr2, + 4, + 7 + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + clamped, + array{{4, 4, 5, 6, 7, 7}} + )); + } + // [2 .. 8) の範囲をクランプ (4 <= x <= 7) + // 出力範囲をオーバーする場合 + { + SPROUT_STATIC_CONSTEXPR auto clamped = sprout::clamp_range_copy( + sprout::begin(arr1) + 2, + sprout::begin(arr1) + 8, + arr3, + 4, + 7 + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + clamped, + array{{4, 4, 5, 6}} + )); + } + { + SPROUT_STATIC_CONSTEXPR auto clamped = sprout::fit::clamp_range_copy( + sprout::begin(arr1) + 2, + sprout::begin(arr1) + 8, + arr3, + 4, + 7 + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + clamped, + array{{4, 4, 5, 6}} + )); + } + // [2 .. 8) の範囲をクランプ (4 <= x <= 7) + // 出力範囲の切り出し + { + SPROUT_STATIC_CONSTEXPR auto clamped = sprout::clamp_range_copy( + sprout::begin(arr1) + 2, + sprout::begin(arr1) + 8, + sprout::sub(arr2, 2, 8), + 4, + 7 + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + clamped, + array{{4, 4, 5, 6, 7, 7}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(clamped), + array{{0, 0, 4, 4, 5, 6, 7, 7, 0, 0}} + )); + } + { + SPROUT_STATIC_CONSTEXPR auto clamped = sprout::fit::clamp_range_copy( + sprout::begin(arr1) + 2, + sprout::begin(arr1) + 8, + sprout::sub(arr2, 2, 8), + 4, + 7 + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + clamped, + array{{4, 4, 5, 6, 7, 7}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(clamped), + array{{0, 0, 4, 4, 5, 6, 7, 7, 0, 0}} + )); + } + } + { + SPROUT_STATIC_CONSTEXPR auto arr1 = array{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}; + SPROUT_STATIC_CONSTEXPR auto arr2 = array{}; + SPROUT_STATIC_CONSTEXPR auto arr3 = array{}; + + // [2 .. 8) の範囲をクランプ (4 <= x <= 7) + { + SPROUT_STATIC_CONSTEXPR auto clamped = sprout::clamp_range_copy( + sprout::begin(arr1) + 2, + sprout::begin(arr1) + 8, + arr2, + 4, + 7, + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + clamped, + array{{4, 4, 5, 6, 7, 7, 0, 0, 0, 0}} + )); + } + { + SPROUT_STATIC_CONSTEXPR auto clamped = sprout::fit::clamp_range_copy( + sprout::begin(arr1) + 2, + sprout::begin(arr1) + 8, + arr2, + 4, + 7, + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + clamped, + array{{4, 4, 5, 6, 7, 7}} + )); + } + // [2 .. 8) の範囲をクランプ (4 <= x <= 7) + // 出力範囲をオーバーする場合 + { + SPROUT_STATIC_CONSTEXPR auto clamped = sprout::clamp_range_copy( + sprout::begin(arr1) + 2, + sprout::begin(arr1) + 8, + arr3, + 4, + 7, + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + clamped, + array{{4, 4, 5, 6}} + )); + } + { + SPROUT_STATIC_CONSTEXPR auto clamped = sprout::fit::clamp_range_copy( + sprout::begin(arr1) + 2, + sprout::begin(arr1) + 8, + arr3, + 4, + 7, + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + clamped, + array{{4, 4, 5, 6}} + )); + } + // [2 .. 8) の範囲をクランプ (4 <= x <= 7) + // 出力範囲の切り出し + { + SPROUT_STATIC_CONSTEXPR auto clamped = sprout::clamp_range_copy( + sprout::begin(arr1) + 2, + sprout::begin(arr1) + 8, + sprout::sub(arr2, 2, 8), + 4, + 7, + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + clamped, + array{{4, 4, 5, 6, 7, 7}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(clamped), + array{{0, 0, 4, 4, 5, 6, 7, 7, 0, 0}} + )); + } + { + SPROUT_STATIC_CONSTEXPR auto clamped = sprout::fit::clamp_range_copy( + sprout::begin(arr1) + 2, + sprout::begin(arr1) + 8, + sprout::sub(arr2, 2, 8), + 4, + 7, + testspr::less() + ); + TESTSPR_BOTH_ASSERT(testspr::equal( + clamped, + array{{4, 4, 5, 6, 7, 7}} + )); + TESTSPR_BOTH_ASSERT(testspr::equal( + sprout::get_internal(clamped), + array{{0, 0, 4, 4, 5, 6, 7, 7, 0, 0}} + )); + } + } + } +} // namespace testspr + +#ifndef TESTSPR_CPP_INCLUDE +# define TESTSPR_TEST_FUNCTION testspr::algorithm_clamp_range_copy_test +# include +#endif + +#endif // #ifndef SPROUT_LIBS_ALGORITHM_TEST_CLAMP_RANGE_COPY_CPP diff --git a/libs/algorithm/test/modifying.cpp b/libs/algorithm/test/modifying.cpp index a9dea379..e7d703e6 100644 --- a/libs/algorithm/test/modifying.cpp +++ b/libs/algorithm/test/modifying.cpp @@ -56,6 +56,8 @@ #include "./sort_heap.cpp" #include "./next_permutation.cpp" #include "./prev_permutation.cpp" +#include "./clamp_range_copy.cpp" +#include "./clamp_range.cpp" #include "./swap_element.cpp" #include "./swap_element_copy.cpp" #include "./random_swap.cpp" @@ -121,6 +123,8 @@ namespace testspr { testspr::algorithm_sort_heap_test(); testspr::algorithm_next_permutation_test(); testspr::algorithm_prev_permutation_test(); + testspr::algorithm_clamp_range_copy_test(); + testspr::algorithm_clamp_range_test(); testspr::algorithm_swap_element_test(); testspr::algorithm_swap_element_copy_test(); testspr::algorithm_random_swap_test(); diff --git a/sprout/algorithm/adjacent_find.hpp b/sprout/algorithm/adjacent_find.hpp index ca768306..80bc78ca 100644 --- a/sprout/algorithm/adjacent_find.hpp +++ b/sprout/algorithm/adjacent_find.hpp @@ -14,7 +14,7 @@ namespace sprout { inline SPROUT_CONSTEXPR ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred) { return first == last || sprout::next(first) == last ? last - : pred(*first, *(sprout::next(first))) != false ? first + : pred(*first, *(sprout::next(first))) ? first : sprout::adjacent_find(sprout::next(first), last, pred) ; } diff --git a/sprout/algorithm/all_of.hpp b/sprout/algorithm/all_of.hpp index 33d48f6b..ba8dc912 100644 --- a/sprout/algorithm/all_of.hpp +++ b/sprout/algorithm/all_of.hpp @@ -12,7 +12,7 @@ namespace sprout { inline SPROUT_CONSTEXPR bool all_of(InputIterator first, InputIterator last, Predicate pred) { return first == last ? true - : pred(*first) == true && sprout::all_of(sprout::next(first), last, pred) + : pred(*first) && sprout::all_of(sprout::next(first), last, pred) ; } } // namespace sprout diff --git a/sprout/algorithm/any_of.hpp b/sprout/algorithm/any_of.hpp index 112c32d4..5c4cccb1 100644 --- a/sprout/algorithm/any_of.hpp +++ b/sprout/algorithm/any_of.hpp @@ -12,7 +12,7 @@ namespace sprout { inline SPROUT_CONSTEXPR bool any_of(InputIterator first, InputIterator last, Predicate pred) { return first == last ? false - : pred(*first) == true || sprout::any_of(sprout::next(first), last, pred) + : pred(*first) || sprout::any_of(sprout::next(first), last, pred) ; } } // namespace sprout diff --git a/sprout/algorithm/clamp.hpp b/sprout/algorithm/clamp.hpp new file mode 100644 index 00000000..81188e80 --- /dev/null +++ b/sprout/algorithm/clamp.hpp @@ -0,0 +1,30 @@ +#ifndef SPROUT_ALGORITHM_CLAMP_HPP +#define SPROUT_ALGORITHM_CLAMP_HPP + +#include +#include +#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT + +namespace sprout { + // + // clamp + // + template + inline SPROUT_CONSTEXPR T const& + clamp(T const& value, typename std::common_type::type const& low, typename std::common_type::type const& high, Compare comp) { + return comp(value, low) ? low + : comp(high, value) ? high + : value + ; + } + template + inline SPROUT_CONSTEXPR T const& + clamp(T const& value, typename std::common_type::type const& low, typename std::common_type::type const& high) { + return sprout::clamp( + value, low, high, + NS_SSCRISK_CEL_OR_SPROUT::less() + ); + } +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_CLAMP_HPP diff --git a/sprout/algorithm/clamp_range.hpp b/sprout/algorithm/clamp_range.hpp new file mode 100644 index 00000000..43eb1e73 --- /dev/null +++ b/sprout/algorithm/clamp_range.hpp @@ -0,0 +1,8 @@ +#ifndef SPROUT_ALGORITHM_CLAMP_RANGE_HPP +#define SPROUT_ALGORITHM_CLAMP_RANGE_HPP + +#include +#include +#include + +#endif // #ifndef SPROUT_ALGORITHM_CLAMP_RANGE_HPP diff --git a/sprout/algorithm/clamp_range_copy.hpp b/sprout/algorithm/clamp_range_copy.hpp new file mode 100644 index 00000000..e3c92133 --- /dev/null +++ b/sprout/algorithm/clamp_range_copy.hpp @@ -0,0 +1,8 @@ +#ifndef SPROUT_ALGORITHM_CLAMP_RANGE_COPY_HPP +#define SPROUT_ALGORITHM_CLAMP_RANGE_COPY_HPP + +#include +#include +#include + +#endif // #ifndef SPROUT_ALGORITHM_CLAMP_RANGE_COPY_HPP diff --git a/sprout/algorithm/count_if.hpp b/sprout/algorithm/count_if.hpp index 25bd2059..01aaf721 100644 --- a/sprout/algorithm/count_if.hpp +++ b/sprout/algorithm/count_if.hpp @@ -13,7 +13,7 @@ namespace sprout { inline SPROUT_CONSTEXPR typename std::iterator_traits::difference_type count_if(InputIterator first, InputIterator last, Predicate pred) { return first == last ? 0 - : (pred(*first) != false ? 1 : 0) + sprout::count_if(sprout::next(first), last, pred) + : (pred(*first) ? 1 : 0) + sprout::count_if(sprout::next(first), last, pred) ; } } // namespace sprout diff --git a/sprout/algorithm/equal.hpp b/sprout/algorithm/equal.hpp index 89461468..2b98ff57 100644 --- a/sprout/algorithm/equal.hpp +++ b/sprout/algorithm/equal.hpp @@ -20,7 +20,7 @@ namespace sprout { inline SPROUT_CONSTEXPR bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred) { return first1 == last1 ? true - : pred(*first1, *first2) != false && sprout::equal(sprout::next(first1), last1, sprout::next(first2), pred) + : pred(*first1, *first2) && sprout::equal(sprout::next(first1), last1, sprout::next(first2), pred) ; } } // namespace sprout diff --git a/sprout/algorithm/find_if.hpp b/sprout/algorithm/find_if.hpp index e09be602..8c5baef4 100644 --- a/sprout/algorithm/find_if.hpp +++ b/sprout/algorithm/find_if.hpp @@ -11,7 +11,7 @@ namespace sprout { template inline SPROUT_CONSTEXPR InputIterator find_if(InputIterator first, InputIterator last, Predicate pred) { - return first == last || pred(*first) != false ? first + return first == last || pred(*first) ? first : sprout::find_if(sprout::next(first), last, pred) ; } diff --git a/sprout/algorithm/find_if_not.hpp b/sprout/algorithm/find_if_not.hpp index d201eaef..60237f6e 100644 --- a/sprout/algorithm/find_if_not.hpp +++ b/sprout/algorithm/find_if_not.hpp @@ -11,7 +11,7 @@ namespace sprout { template inline SPROUT_CONSTEXPR InputIterator find_if_not(InputIterator first, InputIterator last, Predicate pred) { - return first == last || pred(*first) == false ? first + return first == last || !pred(*first) ? first : sprout::find_if_not(sprout::next(first), last, pred) ; } diff --git a/sprout/algorithm/fit.hpp b/sprout/algorithm/fit.hpp index 0d1d207e..2a2a83f1 100644 --- a/sprout/algorithm/fit.hpp +++ b/sprout/algorithm/fit.hpp @@ -52,6 +52,8 @@ #include #include #include +#include +#include #include #include #include diff --git a/sprout/algorithm/fit/clamp_range.hpp b/sprout/algorithm/fit/clamp_range.hpp new file mode 100644 index 00000000..e144541b --- /dev/null +++ b/sprout/algorithm/fit/clamp_range.hpp @@ -0,0 +1,79 @@ +#ifndef SPROUT_ALGORITHM_FIT_CLAMP_RANGE_HPP +#define SPROUT_ALGORITHM_FIT_CLAMP_RANGE_HPP + +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace fit { + namespace detail { + template + inline SPROUT_CONSTEXPR typename sprout::fit::result_of::algorithm::type + clamp_range_impl( + Container const& cont, + typename sprout::container_traits::value_type const& low, + typename sprout::container_traits::value_type const& high, + Compare comp, + typename sprout::container_traits::difference_type offset + ) + { + return sprout::sub_copy( + sprout::get_internal(sprout::fixed::clamp_range(cont, low, high, comp)), + offset, + offset + sprout::size(cont) + ); + } + } // namespace detail + // + // clamp_range + // + template + inline SPROUT_CONSTEXPR typename sprout::fit::result_of::algorithm::type + clamp_range( + Container const& cont, + typename sprout::container_traits::value_type const& low, + typename sprout::container_traits::value_type const& high, + Compare comp + ) + { + return sprout::fit::detail::clamp_range_impl(cont, low, high, comp, sprout::internal_begin_offset(cont)); + } + + namespace detail { + template + inline SPROUT_CONSTEXPR typename sprout::fit::result_of::algorithm::type + clamp_range_impl( + Container const& cont, + typename sprout::container_traits::value_type const& low, + typename sprout::container_traits::value_type const& high, + typename sprout::container_traits::difference_type offset + ) + { + return sprout::sub_copy( + sprout::get_internal(sprout::fixed::clamp_range(cont, low, high)), + offset, + offset + sprout::size(cont) + ); + } + } // namespace detail + // + // clamp_range + // + template + inline SPROUT_CONSTEXPR typename sprout::fit::result_of::algorithm::type + clamp_range( + Container const& cont, + typename sprout::container_traits::value_type const& low, + typename sprout::container_traits::value_type const& high + ) + { + return sprout::fit::detail::clamp_range_impl(cont, low, high, sprout::internal_begin_offset(cont)); + } + } // namespace fit +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_FIT_CLAMP_RANGE_HPP diff --git a/sprout/algorithm/fit/clamp_range_copy.hpp b/sprout/algorithm/fit/clamp_range_copy.hpp new file mode 100644 index 00000000..19eea653 --- /dev/null +++ b/sprout/algorithm/fit/clamp_range_copy.hpp @@ -0,0 +1,81 @@ +#ifndef SPROUT_ALGORITHM_FIT_CLAMP_RANGE_COPY_HPP +#define SPROUT_ALGORITHM_FIT_CLAMP_RANGE_COPY_HPP + +#include +#include +#include +#include +#include +#include +#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT +#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT + +namespace sprout { + namespace fit { + namespace detail { + template + inline SPROUT_CONSTEXPR typename sprout::fit::result_of::algorithm::type + clamp_range_copy_impl( + InputIterator first, InputIterator last, Result const& result, + typename std::iterator_traits::value_type const& low, + typename std::iterator_traits::value_type const& high, + Compare comp, + typename sprout::container_traits::difference_type offset + ) + { + return sprout::sub_copy( + sprout::get_internal(sprout::fixed::clamp_range_copy(first, last, result, low, high, comp)), + offset, + offset + NS_SSCRISK_CEL_OR_SPROUT::min(NS_SSCRISK_CEL_OR_SPROUT::distance(first, last), sprout::size(result)) + ); + } + } // namespace detail + // + // clamp_range_copy + // + template + inline SPROUT_CONSTEXPR typename sprout::fit::result_of::algorithm::type + clamp_range_copy( + InputIterator first, InputIterator last, Result const& result, + typename std::iterator_traits::value_type const& low, + typename std::iterator_traits::value_type const& high, + Compare comp + ) + { + return sprout::fit::detail::clamp_range_copy_impl(first, last, result, low, high, comp, sprout::internal_begin_offset(result)); + } + + namespace detail { + template + inline SPROUT_CONSTEXPR typename sprout::fit::result_of::algorithm::type + clamp_range_copy_impl( + InputIterator first, InputIterator last, Result const& result, + typename std::iterator_traits::value_type const& low, + typename std::iterator_traits::value_type const& high, + typename sprout::container_traits::difference_type offset + ) + { + return sprout::sub_copy( + sprout::get_internal(sprout::fixed::clamp_range_copy(first, last, result, low, high)), + offset, + offset + NS_SSCRISK_CEL_OR_SPROUT::min(NS_SSCRISK_CEL_OR_SPROUT::distance(first, last), sprout::size(result)) + ); + } + } // namespace detail + // + // clamp_range_copy + // + template + inline SPROUT_CONSTEXPR typename sprout::fit::result_of::algorithm::type + clamp_range_copy( + InputIterator first, InputIterator last, Result const& result, + typename std::iterator_traits::value_type const& low, + typename std::iterator_traits::value_type const& high + ) + { + return sprout::fit::detail::clamp_range_copy_impl(first, last, result, low, high, sprout::internal_begin_offset(result)); + } + } // namespace fit +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_FIT_CLAMP_RANGE_COPY_HPP diff --git a/sprout/algorithm/fixed.hpp b/sprout/algorithm/fixed.hpp index 860bc195..30d0dde1 100644 --- a/sprout/algorithm/fixed.hpp +++ b/sprout/algorithm/fixed.hpp @@ -52,6 +52,8 @@ #include #include #include +#include +#include #include #include #include diff --git a/sprout/algorithm/fixed/clamp_range.hpp b/sprout/algorithm/fixed/clamp_range.hpp new file mode 100644 index 00000000..e69663d0 --- /dev/null +++ b/sprout/algorithm/fixed/clamp_range.hpp @@ -0,0 +1,41 @@ +#ifndef SPROUT_ALGORITHM_FIXED_CLAMP_RANGE_HPP +#define SPROUT_ALGORITHM_FIXED_CLAMP_RANGE_HPP + +#include +#include +#include +#include +#include + +namespace sprout { + namespace fixed { + // + // clamp_range + // + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type + clamp_range( + Container const& cont, + typename sprout::container_traits::value_type const& low, + typename sprout::container_traits::value_type const& high, + Compare comp + ) + { + return sprout::fixed::clamp_range_copy(sprout::begin(cont), sprout::end(cont), cont, low, high, comp); + } + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type + clamp_range( + Container const& cont, + typename sprout::container_traits::value_type const& low, + typename sprout::container_traits::value_type const& high + ) + { + return sprout::fixed::clamp_range_copy(sprout::begin(cont), sprout::end(cont), cont, low, high); + } + } // namespace fixed + + using sprout::fixed::clamp_range; +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_FIXED_CLAMP_RANGE_HPP diff --git a/sprout/algorithm/fixed/clamp_range_copy.hpp b/sprout/algorithm/fixed/clamp_range_copy.hpp new file mode 100644 index 00000000..ac555865 --- /dev/null +++ b/sprout/algorithm/fixed/clamp_range_copy.hpp @@ -0,0 +1,145 @@ +#ifndef SPROUT_ALGORITHM_FIXED_CLAMP_RANGE_COPY_HPP +#define SPROUT_ALGORITHM_FIXED_CLAMP_RANGE_COPY_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT +#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT + +namespace sprout { + namespace fixed { + namespace detail { + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type + clamp_range_copy_impl_ra( + RandomAccessIterator first, RandomAccessIterator last, Result const& result, + typename std::iterator_traits::value_type const& low, + typename std::iterator_traits::value_type const& high, + Compare comp, + sprout::index_tuple, + typename sprout::container_traits::difference_type offset, + typename sprout::container_traits::size_type size, + typename sprout::container_traits::size_type input_size + ) + { + return sprout::remake( + result, + sprout::size(result), + (Indexes >= offset && Indexes < offset + size && Indexes < offset + input_size + ? sprout::clamp(first[Indexes - offset], low, high, comp) + : *sprout::next(sprout::internal_begin(result), Indexes) + )... + ); + } + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type + clamp_range_copy( + RandomAccessIterator first, RandomAccessIterator last, Result const& result, + typename std::iterator_traits::value_type const& low, + typename std::iterator_traits::value_type const& high, + Compare comp, + std::random_access_iterator_tag* + ) + { + return sprout::fixed::detail::clamp_range_copy_impl_ra( + first, last, result, low, high, comp, + sprout::index_range<0, sprout::container_traits::static_size>::make(), + sprout::internal_begin_offset(result), + sprout::size(result), + NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) + ); + } + template + inline SPROUT_CONSTEXPR typename std::enable_if< + sprout::container_traits::static_size == sizeof...(Args), + typename sprout::fixed::result_of::algorithm::type + >::type + clamp_range_copy_impl( + InputIterator first, InputIterator last, Result const& result, + typename std::iterator_traits::value_type const& low, + typename std::iterator_traits::value_type const& high, + Compare comp, + typename sprout::container_traits::size_type size, + Args const&... args + ) + { + return sprout::remake(result, sprout::size(result), args...); + } + template + inline SPROUT_CONSTEXPR typename std::enable_if< + sprout::container_traits::static_size != sizeof...(Args), + typename sprout::fixed::result_of::algorithm::type + >::type + clamp_range_copy_impl( + InputIterator first, InputIterator last, Result const& result, + typename std::iterator_traits::value_type const& low, + typename std::iterator_traits::value_type const& high, + Compare comp, + typename sprout::container_traits::size_type size, + Args const&... args + ) + { + return first != last && sizeof...(Args) < size + ? clamp_range_copy_impl( + sprout::next(first), last, result, low, high, comp, + size, + args..., sprout::clamp(*first, low, high, comp) + ) + : sprout::detail::container_complate(result, args...) + ; + } + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type + clamp_range_copy( + InputIterator first, InputIterator last, Result const& result, + typename std::iterator_traits::value_type const& low, + typename std::iterator_traits::value_type const& high, + Compare comp, + void* + ) + { + return sprout::fixed::detail::clamp_range_copy_impl(first, last, result, low, high, comp, sprout::size(result)); + } + } // namespace detail + // + // clamp_range_copy + // + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type + clamp_range_copy( + InputIterator first, InputIterator last, Result const& result, + typename std::iterator_traits::value_type const& low, + typename std::iterator_traits::value_type const& high, + Compare comp + ) + { + typedef typename std::iterator_traits::iterator_category* category; + return sprout::fixed::detail::clamp_range_copy(first, last, result, low, high, comp, category()); + } + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type + clamp_range_copy( + InputIterator first, InputIterator last, Result const& result, + typename std::iterator_traits::value_type const& low, + typename std::iterator_traits::value_type const& high + ) + { + return sprout::fixed::clamp_range_copy( + first, last, result, low, high, + NS_SSCRISK_CEL_OR_SPROUT::less::value_type>() + ); + } + } // namespace fixed + + using sprout::fixed::clamp_range_copy; +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_FIXED_CLAMP_RANGE_COPY_HPP diff --git a/sprout/algorithm/is_sorted_until.hpp b/sprout/algorithm/is_sorted_until.hpp index 840fb00d..e5d03b79 100644 --- a/sprout/algorithm/is_sorted_until.hpp +++ b/sprout/algorithm/is_sorted_until.hpp @@ -21,7 +21,7 @@ namespace sprout { inline SPROUT_CONSTEXPR ForwardIterator is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp) { return first == last || sprout::next(first) == last ? last - : comp(*(sprout::next(first)), *first) != false ? sprout::next(first) + : comp(*(sprout::next(first)), *first) ? sprout::next(first) : sprout::is_sorted_until(sprout::next(first), last) ; } diff --git a/sprout/algorithm/mismatch.hpp b/sprout/algorithm/mismatch.hpp index c40e8175..888cbf4e 100644 --- a/sprout/algorithm/mismatch.hpp +++ b/sprout/algorithm/mismatch.hpp @@ -21,7 +21,7 @@ namespace sprout { template inline SPROUT_CONSTEXPR sprout::pair mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred) { - return first1 == last1 || pred(*first1, *first2) == false + return first1 == last1 || !pred(*first1, *first2) ? sprout::pair{first1, first2} : sprout::mismatch(sprout::next(first1), last1, sprout::next(first2)) ; diff --git a/sprout/algorithm/non_modifying.hpp b/sprout/algorithm/non_modifying.hpp index 60a55d07..bf06ff8b 100644 --- a/sprout/algorithm/non_modifying.hpp +++ b/sprout/algorithm/non_modifying.hpp @@ -45,5 +45,6 @@ #include #include #include +#include #endif // #ifndef SPROUT_ALGORITHM_NON_MODIFYIING_HPP diff --git a/sprout/algorithm/none_of.hpp b/sprout/algorithm/none_of.hpp index 5fd74b1b..e4b00b10 100644 --- a/sprout/algorithm/none_of.hpp +++ b/sprout/algorithm/none_of.hpp @@ -12,7 +12,7 @@ namespace sprout { inline SPROUT_CONSTEXPR bool none_of(InputIterator first, InputIterator last, Predicate pred) { return first == last ? true - : pred(*first) == false && sprout::none_of(sprout::next(first), last, pred) + : !pred(*first) && sprout::none_of(sprout::next(first), last, pred) ; } } // namespace sprout diff --git a/sprout/iterator/filter_iterator.hpp b/sprout/iterator/filter_iterator.hpp index 9b59c72a..9f269f15 100644 --- a/sprout/iterator/filter_iterator.hpp +++ b/sprout/iterator/filter_iterator.hpp @@ -47,7 +47,7 @@ namespace sprout { return sprout::find_if(first, last, pred); } static SPROUT_CONSTEXPR iterator_type find_prev(iterator_type first, predicate_type pred) { - return pred(*first) != false ? first + return pred(*first) ? first : find_prev(sprout::prev(first), pred) ; } diff --git a/sprout/range/algorithm/clamp_range_copy.hpp b/sprout/range/algorithm/clamp_range_copy.hpp new file mode 100644 index 00000000..f26e641e --- /dev/null +++ b/sprout/range/algorithm/clamp_range_copy.hpp @@ -0,0 +1,8 @@ +#ifndef SPROUT_RANGE_ALGORITHM_CLAMP_RANGE_COPY_HPP +#define SPROUT_RANGE_ALGORITHM_CLAMP_RANGE_COPY_HPP + +#include +#include +#include + +#endif // #ifndef SPROUT_RANGE_ALGORITHM_CLAMP_RANGE_COPY_HPP diff --git a/sprout/range/algorithm/fit.hpp b/sprout/range/algorithm/fit.hpp index c3d2bf05..39bd5a72 100644 --- a/sprout/range/algorithm/fit.hpp +++ b/sprout/range/algorithm/fit.hpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #endif // #ifndef SPROUT_RANGE_ALGORITHM_FIT_HPP diff --git a/sprout/range/algorithm/fit/clamp_range_copy.hpp b/sprout/range/algorithm/fit/clamp_range_copy.hpp new file mode 100644 index 00000000..f5ea0e5f --- /dev/null +++ b/sprout/range/algorithm/fit/clamp_range_copy.hpp @@ -0,0 +1,41 @@ +#ifndef SPROUT_RANGE_ALGORITHM_FIT_CLAMP_RANGE_COPY_HPP +#define SPROUT_RANGE_ALGORITHM_FIT_CLAMP_RANGE_COPY_HPP + +#include +#include +#include +#include +#include + +namespace sprout { + namespace range { + namespace fit { + // + // clamp_range_copy + // + template + inline SPROUT_CONSTEXPR typename sprout::fit::result_of::algorithm::type + clamp_range_copy( + Input const& input, Result const& result, + typename sprout::container_traits::value_type const& low, + typename sprout::container_traits::value_type const& high, + Compare comp + ) + { + return sprout::fit::clamp_range_copy(sprout::begin(input), sprout::end(input), result, low, high, comp); + } + template + inline SPROUT_CONSTEXPR typename sprout::fit::result_of::algorithm::type + clamp_range_copy( + Input const& input, Result const& result, + typename sprout::container_traits::value_type const& low, + typename sprout::container_traits::value_type const& high + ) + { + return sprout::fit::clamp_range_copy(sprout::begin(input), sprout::end(input), result, low, high); + } + } // namespace fit + } // namespace range +} // namespace sprout + +#endif // #ifndef SPROUT_RANGE_ALGORITHM_FIT_CLAMP_RANGE_COPY_HPP diff --git a/sprout/range/algorithm/fixed.hpp b/sprout/range/algorithm/fixed.hpp index 0fc34326..dbda6ff9 100644 --- a/sprout/range/algorithm/fixed.hpp +++ b/sprout/range/algorithm/fixed.hpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #endif // #ifndef SPROUT_RANGE_ALGORITHM_FIXED_HPP diff --git a/sprout/range/algorithm/fixed/clamp_range_copy.hpp b/sprout/range/algorithm/fixed/clamp_range_copy.hpp new file mode 100644 index 00000000..302004a8 --- /dev/null +++ b/sprout/range/algorithm/fixed/clamp_range_copy.hpp @@ -0,0 +1,43 @@ +#ifndef SPROUT_RANGE_ALGORITHM_FIXED_CLAMP_RANGE_COPY_HPP +#define SPROUT_RANGE_ALGORITHM_FIXED_CLAMP_RANGE_COPY_HPP + +#include +#include +#include +#include +#include + +namespace sprout { + namespace range { + namespace fixed { + // + // clamp_range_copy + // + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type + clamp_range_copy( + Input const& input, Result const& result, + typename sprout::container_traits::value_type const& low, + typename sprout::container_traits::value_type const& high, + Compare comp + ) + { + return sprout::fixed::clamp_range_copy(sprout::begin(input), sprout::end(input), result, low, high, comp); + } + template + inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm::type + clamp_range_copy( + Input const& input, Result const& result, + typename sprout::container_traits::value_type const& low, + typename sprout::container_traits::value_type const& high + ) + { + return sprout::fixed::clamp_range_copy(sprout::begin(input), sprout::end(input), result, low, high); + } + } // namespace fixed + + using sprout::range::fixed::clamp_range_copy; + } // namespace range +} // namespace sprout + +#endif // #ifndef SPROUT_RANGE_ALGORITHM_FIXED_CLAMP_RANGE_COPY_HPP