From 5ccbc4e903cf58190b865ee159c883c4ae308c18 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Sun, 14 Dec 2014 12:59:51 +0900 Subject: [PATCH] add tuples::apply --- sprout/algorithm/abs_diff.hpp | 34 +++++++++ sprout/algorithm/cxx14/sample.hpp | 30 ++++---- sprout/algorithm/non_modifying.hpp | 1 + sprout/algorithm/search.hpp | 7 ++ sprout/algorithm/searching.hpp | 14 ++++ .../algorithm/searching/default_searcher.hpp | 40 +++++++++++ sprout/functional/bind/bind.hpp | 5 ++ sprout/functional/bind/placeholder.hpp | 15 ++++ sprout/range/algorithm/search.hpp | 27 +++++++ sprout/tuple.hpp | 1 + sprout/tuple/apply.hpp | 70 +++++++++++++++++++ sprout/tuple/fused.hpp | 34 +-------- sprout/type_traits/is_null_pointer.hpp | 46 ++++++++++++ sprout/type_traits/std_type_traits.hpp | 5 +- sprout/type_traits/std_value_variables.hpp | 2 - testspr/header_all.hpp | 1 + 16 files changed, 280 insertions(+), 52 deletions(-) create mode 100644 sprout/algorithm/abs_diff.hpp create mode 100644 sprout/algorithm/searching.hpp create mode 100644 sprout/algorithm/searching/default_searcher.hpp create mode 100644 sprout/tuple/apply.hpp create mode 100644 sprout/type_traits/is_null_pointer.hpp diff --git a/sprout/algorithm/abs_diff.hpp b/sprout/algorithm/abs_diff.hpp new file mode 100644 index 00000000..42a58d08 --- /dev/null +++ b/sprout/algorithm/abs_diff.hpp @@ -0,0 +1,34 @@ +/*============================================================================= + 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_ABS_DIFF_HPP +#define SPROUT_ALGORITHM_ABS_DIFF_HPP + +#include + +namespace sprout { + // + // abs_diff + // + template + inline SPROUT_CONSTEXPR T + abs_diff(T const& a, T const& b) { + return (a < b) ? b - a : a - b; + } + template + inline SPROUT_CONSTEXPR T + abs_diff(T const& a, T const& b, Compare comp) { + return comp(a, b) ? b - a : a - b; + } + template + inline SPROUT_CONSTEXPR T + abs_diff(T const& a, T const& b, Compare comp, Difference diff) { + return comp(a, b) ? diff(b, a) : diff(a, b); + } +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_ABS_DIFF_HPP diff --git a/sprout/algorithm/cxx14/sample.hpp b/sprout/algorithm/cxx14/sample.hpp index 08e27ba2..21d34e80 100644 --- a/sprout/algorithm/cxx14/sample.hpp +++ b/sprout/algorithm/cxx14/sample.hpp @@ -17,42 +17,42 @@ namespace sprout { namespace detail { - template + template inline SPROUT_CXX14_CONSTEXPR SampleIterator sample_impl( - PopIterator first, PopIterator last, std::input_iterator_tag*, + PopulationIterator first, PopulationIterator last, std::input_iterator_tag*, SampleIterator out, std::random_access_iterator_tag*, - Size n, URNG&& g + Distance n, URNG&& g ) { - typedef SPROUT_WORKAROUND_DETAIL_UNIFORM_INT_DISTRIBUTION distribution_type; + typedef SPROUT_WORKAROUND_DETAIL_UNIFORM_INT_DISTRIBUTION distribution_type; typedef typename distribution_type::param_type param_type; distribution_type dist = {}; - Size sample_size = 0; + Distance sample_size = 0; while (first != last && sample_size != n) { out[sample_size++] = *first++; } - for (Size pop_size = sample_size; first != last; ++first, ++pop_size) { + for (Distance pop_size = sample_size; first != last; ++first, ++pop_size) { param_type const p(0, pop_size); - Size const k = dist(g, p); + Distance const k = dist(g, p); if (k < n) { out[k] = *first; } } return out + sample_size; } - template + template inline SPROUT_CXX14_CONSTEXPR SampleIterator sample_impl( - PopIterator first, PopIterator last, std::forward_iterator_tag*, + PopulationIterator first, PopulationIterator last, std::forward_iterator_tag*, SampleIterator out, std::output_iterator_tag*, - Size n, URNG&& g + Distance n, URNG&& g ) { - typedef SPROUT_WORKAROUND_DETAIL_UNIFORM_INT_DISTRIBUTION distribution_type; + typedef SPROUT_WORKAROUND_DETAIL_UNIFORM_INT_DISTRIBUTION distribution_type; typedef typename distribution_type::param_type param_type; distribution_type dist = {}; - Size unsampled_size = sprout::distance(first, last); + Distance unsampled_size = sprout::distance(first, last); for (n = NS_SSCRISK_CEL_OR_SPROUT::min(n, unsampled_size); n != 0; ++first ) { param_type const p(0, --unsampled_size); if (dist(g, p) < n) { @@ -66,10 +66,10 @@ namespace sprout { // // sample // - template + template inline SPROUT_CXX14_CONSTEXPR SampleIterator - sample(PopIterator first, PopIterator last, SampleIterator out, Size n, URNG&& g) { - typedef typename std::iterator_traits::iterator_category* pop_category; + sample(PopulationIterator first, PopulationIterator last, SampleIterator out, Distance n, URNG&& g) { + typedef typename std::iterator_traits::iterator_category* pop_category; typedef typename std::iterator_traits::iterator_category* sample_category; return sprout::detail::sample_impl( first, last, pop_category(), diff --git a/sprout/algorithm/non_modifying.hpp b/sprout/algorithm/non_modifying.hpp index 702cf658..2220aeeb 100644 --- a/sprout/algorithm/non_modifying.hpp +++ b/sprout/algorithm/non_modifying.hpp @@ -61,5 +61,6 @@ #include #include #include +#include #endif // #ifndef SPROUT_ALGORITHM_NON_MODIFYIING_HPP diff --git a/sprout/algorithm/search.hpp b/sprout/algorithm/search.hpp index 6c71fa27..59936622 100644 --- a/sprout/algorithm/search.hpp +++ b/sprout/algorithm/search.hpp @@ -149,6 +149,13 @@ namespace sprout { { return sprout::search(first1, last1, first2, last2, sprout::equal_to<>()); } + + // + template + inline SPROUT_CONSTEXPR ForwardIterator + search(ForwardIterator first, ForwardIterator last, Searcher const& searcher) { + return searcher(first, last); + } } // namespace sprout #endif // #ifndef SPROUT_ALGORITHM_SEARCH_HPP diff --git a/sprout/algorithm/searching.hpp b/sprout/algorithm/searching.hpp new file mode 100644 index 00000000..7f5a8758 --- /dev/null +++ b/sprout/algorithm/searching.hpp @@ -0,0 +1,14 @@ +/*============================================================================= + 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_SEARCHING_HPP +#define SPROUT_ALGORITHM_SEARCHING_HPP + +#include +#include + +#endif // #ifndef SPROUT_ALGORITHM_SEARCHING_HPP diff --git a/sprout/algorithm/searching/default_searcher.hpp b/sprout/algorithm/searching/default_searcher.hpp new file mode 100644 index 00000000..1193ff51 --- /dev/null +++ b/sprout/algorithm/searching/default_searcher.hpp @@ -0,0 +1,40 @@ +/*============================================================================= + 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_SEARCHING_DEFAULT_SEARCHER_HPP +#define SPROUT_ALGORITHM_SEARCHING_DEFAULT_SEARCHER_HPP + +#include +#include +#include + +namespace sprout { + // + // default_searcher + // + template > + class default_searcher { + private: + ForwardIterator2 first2_; + ForwardIterator2 last2_; + BinaryPredicate pred_; + public: + SPROUT_CONSTEXPR default_searcher(ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred) + : first2_(first2), last2_(last2), pred_(pred) + {} + SPROUT_CONSTEXPR default_searcher(ForwardIterator2 first2, ForwardIterator2 last2) + : first2_(first2), last2_(last2), pred_() + {} + template + SPROUT_CONSTEXPR ForwardIterator1 + operator()(ForwardIterator1 first1, ForwardIterator1 last1) const { + return sprout::search(first1, last1, first2_, last2_, pred_); + } + }; +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_SEARCHING_DEFAULT_SEARCHER_HPP diff --git a/sprout/functional/bind/bind.hpp b/sprout/functional/bind/bind.hpp index d61f7cfa..808af904 100644 --- a/sprout/functional/bind/bind.hpp +++ b/sprout/functional/bind/bind.hpp @@ -54,6 +54,11 @@ namespace sprout { : public sprout::is_bind_expression {}; +#if SPROUT_USE_VARIABLE_TEMPLATES + template + SPROUT_STATIC_CONSTEXPR bool is_bind_expression_v = sprout::is_bind_expression::value; +#endif // #if SPROUT_USE_VARIABLE_TEMPLATES + namespace detail { struct no_tuple_element; diff --git a/sprout/functional/bind/placeholder.hpp b/sprout/functional/bind/placeholder.hpp index 5be3b4a1..adc0084b 100644 --- a/sprout/functional/bind/placeholder.hpp +++ b/sprout/functional/bind/placeholder.hpp @@ -190,6 +190,11 @@ namespace sprout { : public sprout::integral_constant {}; +#if SPROUT_USE_VARIABLE_TEMPLATES + template + SPROUT_STATIC_CONSTEXPR bool is_placeholder_v = sprout::is_placeholder::value; +#endif // #if SPROUT_USE_VARIABLE_TEMPLATES + // // is_positional_placeholder // @@ -214,6 +219,11 @@ namespace sprout { : public sprout::integral_constant {}; +#if SPROUT_USE_VARIABLE_TEMPLATES + template + SPROUT_STATIC_CONSTEXPR bool is_positional_placeholder_v = sprout::is_positional_placeholder::value; +#endif // #if SPROUT_USE_VARIABLE_TEMPLATES + // // is_variadic_placeholder // @@ -237,6 +247,11 @@ namespace sprout { struct is_variadic_placeholder > : public sprout::integral_constant {}; + +#if SPROUT_USE_VARIABLE_TEMPLATES + template + SPROUT_STATIC_CONSTEXPR bool is_variadic_placeholder_v = sprout::is_variadic_placeholder::value; +#endif // #if SPROUT_USE_VARIABLE_TEMPLATES } // namespace sprout #endif // #ifndef SPROUT_FUNCTIONAL_BIND_PLACEHOLDER_HPP diff --git a/sprout/range/algorithm/search.hpp b/sprout/range/algorithm/search.hpp index 52582fd4..509c7d99 100644 --- a/sprout/range/algorithm/search.hpp +++ b/sprout/range/algorithm/search.hpp @@ -76,6 +76,33 @@ namespace sprout { SPROUT_FORWARD(ForwardRange1, range1) ); } + + // + template + inline SPROUT_CONSTEXPR typename sprout::range::range_return::type + search(ForwardRange&& range, Searcher const& searcher) { + return sprout::range::range_return::pack( + sprout::search( + sprout::begin(SPROUT_FORWARD(ForwardRange, range)), + sprout::end(SPROUT_FORWARD(ForwardRange, range)), + searcher + ), + SPROUT_FORWARD(ForwardRange, range) + ); + } + + template + inline SPROUT_CONSTEXPR typename sprout::range::range_return::type + search(ForwardRange&& range, Searcher const& searcher) { + return sprout::range::range_return::pack( + sprout::search( + sprout::begin(SPROUT_FORWARD(ForwardRange, range)), + sprout::end(SPROUT_FORWARD(ForwardRange, range)), + searcher + ), + SPROUT_FORWARD(ForwardRange, range) + ); + } } // namespace range } // namespace sprout diff --git a/sprout/tuple.hpp b/sprout/tuple.hpp index 890659bd..88c2ea01 100644 --- a/sprout/tuple.hpp +++ b/sprout/tuple.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include diff --git a/sprout/tuple/apply.hpp b/sprout/tuple/apply.hpp new file mode 100644 index 00000000..668a5578 --- /dev/null +++ b/sprout/tuple/apply.hpp @@ -0,0 +1,70 @@ +/*============================================================================= + 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_TUPLE_APPLY_HPP +#define SPROUT_TUPLE_APPLY_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace tuples { + // + // apply_result + // + namespace detail { + template + struct apply_result_impl; + template + struct apply_result_impl > { + public: + typedef decltype( + std::declval()( + sprout::tuples::get(std::declval())... + ) + ) type; + }; + } // namespace detail + template + struct apply_result + : public sprout::tuples::detail::apply_result_impl< + F, Tuple, + typename sprout::tuple_indexes::type>::type + > + {}; + + // + // apply + // + namespace detail { + template + inline SPROUT_CONSTEXPR Result + apply_impl(F&& f, Tuple&& t, sprout::index_tuple) { + return SPROUT_FORWARD(F, f)(sprout::tuples::get(SPROUT_FORWARD(Tuple, t))...); + } + } // namespace detail + template + inline SPROUT_CONSTEXPR typename sprout::tuples::apply_result::type + apply(F&& f, Tuple&& t) { + return sprout::tuples::detail::apply_impl::type>( + SPROUT_FORWARD(F, f), SPROUT_FORWARD(Tuple, t), + sprout::tuple_indexes::type>::make() + ); + } + } // namespace tuples + + using sprout::tuples::apply_result; + using sprout::tuples::apply; +} // namespace sprout + +#endif // #ifndef SPROUT_TUPLE_APPLY_HPP diff --git a/sprout/tuple/fused.hpp b/sprout/tuple/fused.hpp index 4bbbbedb..e679b620 100644 --- a/sprout/tuple/fused.hpp +++ b/sprout/tuple/fused.hpp @@ -8,13 +8,9 @@ #ifndef SPROUT_TUPLE_FUSED_HPP #define SPROUT_TUPLE_FUSED_HPP -#include -#include #include -#include #include -#include -#include +#include #include namespace sprout { @@ -26,34 +22,13 @@ namespace sprout { class fused { public: typedef F functor_type; - private: - template - struct result_impl; - template - struct result_impl > { - public: - typedef decltype( - std::declval()( - sprout::tuples::get(std::declval())... - ) - ) type; - }; public: template struct result - : public result_impl< - Tuple, - typename sprout::tuple_indexes::type>::type - > + : public sprout::tuples::apply_result {}; private: functor_type f_; - private: - template - SPROUT_CONSTEXPR Result - call(Tuple&& t, sprout::index_tuple) const { - return f_(sprout::tuples::get(SPROUT_FORWARD(Tuple, t))...); - } public: SPROUT_CONSTEXPR fused() SPROUT_DEFAULTED_DEFAULT_CONSTRUCTOR_DECL fused(fused const&) = default; @@ -66,10 +41,7 @@ namespace sprout { template SPROUT_CONSTEXPR typename result::type operator()(Tuple&& t) const { - return call::type>( - SPROUT_FORWARD(Tuple, t), - sprout::tuple_indexes::type>::make() - ); + return sprout::tuples::apply(f_, SPROUT_FORWARD(Tuple, t)); } }; diff --git a/sprout/type_traits/is_null_pointer.hpp b/sprout/type_traits/is_null_pointer.hpp new file mode 100644 index 00000000..9b487f7b --- /dev/null +++ b/sprout/type_traits/is_null_pointer.hpp @@ -0,0 +1,46 @@ +/*============================================================================= + 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_TYPE_TRAITS_IS_NULL_POINTER_HPP +#define SPROUT_TYPE_TRAITS_IS_NULL_POINTER_HPP + +#include +#include +#include + +namespace sprout { + // + // is_null_pointer + // + template + struct is_null_pointer + : public sprout::false_type + {}; + template + struct is_null_pointer + : public sprout::is_null_pointer + {}; + template + struct is_null_pointer + : public sprout::is_null_pointer + {}; + template + struct is_null_pointer + : public sprout::is_null_pointer + {}; + template<> + struct is_null_pointer + : public sprout::true_type + {}; + +#if SPROUT_USE_VARIABLE_TEMPLATES + template + SPROUT_STATIC_CONSTEXPR bool is_null_pointer_v = sprout::is_null_pointer::value; +#endif // #if SPROUT_USE_VARIABLE_TEMPLATES +} // namespace sprout + +#endif // #ifndef SPROUT_TYPE_TRAITS_IS_NULL_POINTER_HPP diff --git a/sprout/type_traits/std_type_traits.hpp b/sprout/type_traits/std_type_traits.hpp index 8d6c2445..357a7f9d 100644 --- a/sprout/type_traits/std_type_traits.hpp +++ b/sprout/type_traits/std_type_traits.hpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #if !defined(_LIBCPP_VERSION) || (_LIBCPP_VERSION < 1101) # include @@ -28,10 +29,6 @@ namespace sprout { : public sprout::detail::type_traits_wrapper > {}; template - struct is_null_pointer - : public sprout::detail::type_traits_wrapper::type, std::nullptr_t> > - {}; - template struct is_integral : public sprout::detail::type_traits_wrapper > {}; diff --git a/sprout/type_traits/std_value_variables.hpp b/sprout/type_traits/std_value_variables.hpp index 95f4e8cd..0f5c001f 100644 --- a/sprout/type_traits/std_value_variables.hpp +++ b/sprout/type_traits/std_value_variables.hpp @@ -21,8 +21,6 @@ namespace sprout { template SPROUT_STATIC_CONSTEXPR bool is_void_v = sprout::is_void::value; template - SPROUT_STATIC_CONSTEXPR bool is_null_pointer_v = sprout::is_null_pointer::value; - template SPROUT_STATIC_CONSTEXPR bool is_integral_v = sprout::is_integral::value; template SPROUT_STATIC_CONSTEXPR bool is_floating_point_v = sprout::is_floating_point::value; diff --git a/testspr/header_all.hpp b/testspr/header_all.hpp index a5d3b17d..3a0f78e7 100644 --- a/testspr/header_all.hpp +++ b/testspr/header_all.hpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include