From a90bbd5f380c632ed76138ec790bed45dc2ffb2e Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Sat, 5 Oct 2013 13:35:26 +0900 Subject: [PATCH] add advance C++14 constexpr version --- sprout/algorithm/detail/max.hpp | 31 ++++++++++ sprout/algorithm/detail/min.hpp | 31 ++++++++++ sprout/algorithm/fixed/copy.hpp | 2 +- sprout/algorithm/fixed/copy_backward.hpp | 2 +- sprout/algorithm/fixed/copy_n.hpp | 2 +- sprout/algorithm/max.hpp | 13 +---- sprout/algorithm/max_element.hpp | 3 +- sprout/algorithm/min.hpp | 13 +---- sprout/algorithm/min_element.hpp | 3 +- sprout/compost/utility/rosenberg.hpp | 1 - sprout/config/compiler/clang.hpp | 4 +- sprout/config/compiler/gcc.hpp | 4 +- sprout/iterator/advance.hpp | 56 +++++++++++++++++++ sprout/iterator/next.hpp | 3 +- sprout/iterator/operation.hpp | 1 + sprout/math/equal_to.hpp | 2 +- .../numeric/dft/fixed/amplitude_spectrum.hpp | 2 +- sprout/numeric/dft/fixed/dft.hpp | 2 +- sprout/numeric/dft/fixed/idft.hpp | 2 +- sprout/numeric/dft/fixed/phase_spectrum.hpp | 2 +- sprout/numeric/dft/fixed/sawtooth.hpp | 1 - sprout/numeric/dft/fixed/sinusoid.hpp | 1 - sprout/numeric/dft/fixed/square.hpp | 1 - sprout/numeric/dft/fixed/triangle.hpp | 1 - sprout/random/inversive_congruential.hpp | 2 +- sprout/random/linear_congruential.hpp | 2 +- sprout/string/string.hpp | 2 +- sprout/variant/variant.hpp | 2 +- 28 files changed, 145 insertions(+), 46 deletions(-) create mode 100644 sprout/algorithm/detail/max.hpp create mode 100644 sprout/algorithm/detail/min.hpp create mode 100644 sprout/iterator/advance.hpp diff --git a/sprout/algorithm/detail/max.hpp b/sprout/algorithm/detail/max.hpp new file mode 100644 index 00000000..256682ce --- /dev/null +++ b/sprout/algorithm/detail/max.hpp @@ -0,0 +1,31 @@ +/*============================================================================= + Copyright (c) 2011-2013 Bolero MURAKAMI + Copyright (C) 2011 RiSK (sscrisk) + 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_DETAIL_MAX_HPP +#define SPROUT_ALGORITHM_DETAIL_MAX_HPP + +#include +#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT + +namespace sprout { + + // 25.4.7 Minimum and maximum + template + inline SPROUT_CONSTEXPR T const& + max(T const& a, T const& b, Compare comp) { + return comp(a, b) ? b : a; + } + + template + inline SPROUT_CONSTEXPR T const& + max(T const& a, T const& b) { + return sprout::max(a, b, NS_SSCRISK_CEL_OR_SPROUT::less()); + } +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_DETAIL_MAX_HPP diff --git a/sprout/algorithm/detail/min.hpp b/sprout/algorithm/detail/min.hpp new file mode 100644 index 00000000..972e5a63 --- /dev/null +++ b/sprout/algorithm/detail/min.hpp @@ -0,0 +1,31 @@ +/*============================================================================= + Copyright (c) 2011-2013 Bolero MURAKAMI + Copyright (C) 2011 RiSK (sscrisk) + 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_DETAIL_MIN_HPP +#define SPROUT_ALGORITHM_DETAIL_MIN_HPP + +#include +#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT + +namespace sprout { + + // 25.4.7 Minimum and maximum + template + inline SPROUT_CONSTEXPR T const& + min(T const& a, T const& b, Compare comp) { + return comp(b, a) ? b : a; + } + + template + inline SPROUT_CONSTEXPR T const& + min(T const& a, T const& b) { + return sprout::min(a, b, NS_SSCRISK_CEL_OR_SPROUT::less()); + } +} // namespace sprout + +#endif // #ifndef SPROUT_ALGORITHM_DETAIL_MIN_HPP diff --git a/sprout/algorithm/fixed/copy.hpp b/sprout/algorithm/fixed/copy.hpp index dee70dde..6f257c30 100644 --- a/sprout/algorithm/fixed/copy.hpp +++ b/sprout/algorithm/fixed/copy.hpp @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include namespace sprout { diff --git a/sprout/algorithm/fixed/copy_backward.hpp b/sprout/algorithm/fixed/copy_backward.hpp index 64899697..a122f3bf 100644 --- a/sprout/algorithm/fixed/copy_backward.hpp +++ b/sprout/algorithm/fixed/copy_backward.hpp @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include namespace sprout { diff --git a/sprout/algorithm/fixed/copy_n.hpp b/sprout/algorithm/fixed/copy_n.hpp index d1c70d2b..fcf32022 100644 --- a/sprout/algorithm/fixed/copy_n.hpp +++ b/sprout/algorithm/fixed/copy_n.hpp @@ -17,7 +17,7 @@ #include #include #include -#include +#include #include #include diff --git a/sprout/algorithm/max.hpp b/sprout/algorithm/max.hpp index db3b6e7e..54a8cb6b 100644 --- a/sprout/algorithm/max.hpp +++ b/sprout/algorithm/max.hpp @@ -11,6 +11,7 @@ #include #include +#include #include #include #include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT @@ -18,18 +19,6 @@ namespace sprout { // 25.4.7 Minimum and maximum - template - inline SPROUT_CONSTEXPR T const& - max(T const& a, T const& b, Compare comp) { - return comp(a, b) ? b : a; - } - - template - inline SPROUT_CONSTEXPR T const& - max(T const& a, T const& b) { - return sprout::max(a, b, NS_SSCRISK_CEL_OR_SPROUT::less()); - } - #ifdef SPROUT_NO_CXX14_INITIALIZER_LIST template inline SPROUT_CONSTEXPR T diff --git a/sprout/algorithm/max_element.hpp b/sprout/algorithm/max_element.hpp index e6f2534e..825b6b64 100644 --- a/sprout/algorithm/max_element.hpp +++ b/sprout/algorithm/max_element.hpp @@ -11,7 +11,8 @@ #include #include #include -#include +#include +#include #include #include #include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT diff --git a/sprout/algorithm/min.hpp b/sprout/algorithm/min.hpp index 586cd8ec..bbf4636f 100644 --- a/sprout/algorithm/min.hpp +++ b/sprout/algorithm/min.hpp @@ -11,6 +11,7 @@ #include #include +#include #include #include #include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT @@ -18,18 +19,6 @@ namespace sprout { // 25.4.7 Minimum and maximum - template - inline SPROUT_CONSTEXPR T const& - min(T const& a, T const& b, Compare comp) { - return comp(b, a) ? b : a; - } - - template - inline SPROUT_CONSTEXPR T const& - min(T const& a, T const& b) { - return sprout::min(a, b, NS_SSCRISK_CEL_OR_SPROUT::less()); - } - #ifdef SPROUT_NO_CXX14_INITIALIZER_LIST template inline SPROUT_CONSTEXPR T diff --git a/sprout/algorithm/min_element.hpp b/sprout/algorithm/min_element.hpp index 7be0461d..533a9d5b 100644 --- a/sprout/algorithm/min_element.hpp +++ b/sprout/algorithm/min_element.hpp @@ -11,7 +11,8 @@ #include #include #include -#include +#include +#include #include #include #include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT diff --git a/sprout/compost/utility/rosenberg.hpp b/sprout/compost/utility/rosenberg.hpp index aabd4486..81a469d5 100644 --- a/sprout/compost/utility/rosenberg.hpp +++ b/sprout/compost/utility/rosenberg.hpp @@ -11,7 +11,6 @@ #include #include #include -#include namespace sprout { namespace compost { diff --git a/sprout/config/compiler/clang.hpp b/sprout/config/compiler/clang.hpp index 597415ed..5b19d686 100644 --- a/sprout/config/compiler/clang.hpp +++ b/sprout/config/compiler/clang.hpp @@ -44,7 +44,9 @@ # define SPROUT_NO_CXX11_UNICODE_LITERALS #endif -#define SPROUT_NO_CXX14_CONSTEXPR +#if (!__has_feature(cxx_constexpr) || __cplusplus < 201305L) +# define SPROUT_NO_CXX14_CONSTEXPR +#endif #if !defined(SPROUT_NO_CXX11_CONSTEXPR) # define SPROUT_WORKAROUND_NOT_TERMINATE_RECURSIVE_CONSTEXPR_FUNCTION_TEMPLATE diff --git a/sprout/config/compiler/gcc.hpp b/sprout/config/compiler/gcc.hpp index 3c744e55..e6294faf 100644 --- a/sprout/config/compiler/gcc.hpp +++ b/sprout/config/compiler/gcc.hpp @@ -52,6 +52,8 @@ # define SPROUT_HAS_CONSTEXPR_BIT_OPERATION #endif -#define SPROUT_NO_CXX14_CONSTEXPR +#if (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 9) || __cplusplus < 201300L) +# define SPROUT_NO_CXX14_CONSTEXPR +#endif #endif // #ifndef SPROUT_CONFIG_COMPILER_GCC_HPP diff --git a/sprout/iterator/advance.hpp b/sprout/iterator/advance.hpp new file mode 100644 index 00000000..a12c5bd6 --- /dev/null +++ b/sprout/iterator/advance.hpp @@ -0,0 +1,56 @@ +/*============================================================================= + Copyright (c) 2011-2013 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_ITERATOR_ADVANCE_HPP +#define SPROUT_ITERATOR_ADVANCE_HPP + +#include +#include +#include +#include + +namespace sprout { + namespace detail { + template + inline SPROUT_CXX14_CONSTEXPR void + advance_impl(InputIterator& it, Distance n, std::input_iterator_tag*) { + SPROUT_ASSERT(sprout::math::greater_equal(n, 0)); + for (; 0 < n; --n) { + ++it; + } + } + template + inline SPROUT_CXX14_CONSTEXPR void + advance_impl(BidirectionalIterator& it, Distance n, std::bidirectional_iterator_tag*) { + if (n > 0) { + for (; 0 < n; --n) { + ++it; + } + } else { + for (; n < 0; ++n) { + --it; + } + } + } + template + inline SPROUT_CXX14_CONSTEXPR void + advance_impl(RandomAccessIterator& it, Distance n, std::random_access_iterator_tag*) { + it += n; + } + } // namespace detail + // + // advance + // + template + inline SPROUT_CXX14_CONSTEXPR void + advance(Iterator& it, Distance n) { + typedef typename std::iterator_traits::iterator_category* category; + sprout::detail::advance_impl(it, n, category()); + } +} // namespace sprout + +#endif // #ifndef SPROUT_ITERATOR_ADVANCE_HPP diff --git a/sprout/iterator/next.hpp b/sprout/iterator/next.hpp index c750a779..b728791d 100644 --- a/sprout/iterator/next.hpp +++ b/sprout/iterator/next.hpp @@ -15,6 +15,7 @@ #include #include #include +#include namespace sprout_adl { sprout::not_found_via_adl iterator_next(...); @@ -101,7 +102,7 @@ namespace sprout { std::input_iterator_tag* ) { - return SPROUT_ASSERT(n >= 0), + return SPROUT_ASSERT(sprout::math::greater_equal(n, 0)), n == 0 ? it : sprout::iterator_detail::next_impl_2(it, n) ; diff --git a/sprout/iterator/operation.hpp b/sprout/iterator/operation.hpp index 6a8fdd22..bb852c42 100644 --- a/sprout/iterator/operation.hpp +++ b/sprout/iterator/operation.hpp @@ -12,5 +12,6 @@ #include #include #include +#include #endif // #ifndef SPROUT_ITERATOR_OPERATION_HPP diff --git a/sprout/math/equal_to.hpp b/sprout/math/equal_to.hpp index 6afeaa8d..0bf6fe13 100644 --- a/sprout/math/equal_to.hpp +++ b/sprout/math/equal_to.hpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/sprout/numeric/dft/fixed/amplitude_spectrum.hpp b/sprout/numeric/dft/fixed/amplitude_spectrum.hpp index 31307d81..3d0d6a1c 100644 --- a/sprout/numeric/dft/fixed/amplitude_spectrum.hpp +++ b/sprout/numeric/dft/fixed/amplitude_spectrum.hpp @@ -19,7 +19,7 @@ #include #include #include -#include +#include #include #include diff --git a/sprout/numeric/dft/fixed/dft.hpp b/sprout/numeric/dft/fixed/dft.hpp index 091c6c90..47668ab0 100644 --- a/sprout/numeric/dft/fixed/dft.hpp +++ b/sprout/numeric/dft/fixed/dft.hpp @@ -19,7 +19,7 @@ #include #include #include -#include +#include #include #include diff --git a/sprout/numeric/dft/fixed/idft.hpp b/sprout/numeric/dft/fixed/idft.hpp index 8db98096..82e2a190 100644 --- a/sprout/numeric/dft/fixed/idft.hpp +++ b/sprout/numeric/dft/fixed/idft.hpp @@ -19,7 +19,7 @@ #include #include #include -#include +#include #include #include diff --git a/sprout/numeric/dft/fixed/phase_spectrum.hpp b/sprout/numeric/dft/fixed/phase_spectrum.hpp index dd6cdce6..6fdd2280 100644 --- a/sprout/numeric/dft/fixed/phase_spectrum.hpp +++ b/sprout/numeric/dft/fixed/phase_spectrum.hpp @@ -19,7 +19,7 @@ #include #include #include -#include +#include #include #include diff --git a/sprout/numeric/dft/fixed/sawtooth.hpp b/sprout/numeric/dft/fixed/sawtooth.hpp index ca0705fc..e537c942 100644 --- a/sprout/numeric/dft/fixed/sawtooth.hpp +++ b/sprout/numeric/dft/fixed/sawtooth.hpp @@ -15,7 +15,6 @@ #include #include #include -#include #include #include diff --git a/sprout/numeric/dft/fixed/sinusoid.hpp b/sprout/numeric/dft/fixed/sinusoid.hpp index b8413e74..09063cf1 100644 --- a/sprout/numeric/dft/fixed/sinusoid.hpp +++ b/sprout/numeric/dft/fixed/sinusoid.hpp @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include diff --git a/sprout/numeric/dft/fixed/square.hpp b/sprout/numeric/dft/fixed/square.hpp index dc74bb82..0bf3a8e5 100644 --- a/sprout/numeric/dft/fixed/square.hpp +++ b/sprout/numeric/dft/fixed/square.hpp @@ -15,7 +15,6 @@ #include #include #include -#include #include #include diff --git a/sprout/numeric/dft/fixed/triangle.hpp b/sprout/numeric/dft/fixed/triangle.hpp index 9bbe0036..45c1f51c 100644 --- a/sprout/numeric/dft/fixed/triangle.hpp +++ b/sprout/numeric/dft/fixed/triangle.hpp @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include diff --git a/sprout/random/inversive_congruential.hpp b/sprout/random/inversive_congruential.hpp index 2f3c64f1..9a2987b0 100644 --- a/sprout/random/inversive_congruential.hpp +++ b/sprout/random/inversive_congruential.hpp @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include namespace sprout { diff --git a/sprout/random/linear_congruential.hpp b/sprout/random/linear_congruential.hpp index 8ce5a269..3e59e476 100644 --- a/sprout/random/linear_congruential.hpp +++ b/sprout/random/linear_congruential.hpp @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include namespace sprout { diff --git a/sprout/string/string.hpp b/sprout/string/string.hpp index 75f07ab0..55f8382a 100644 --- a/sprout/string/string.hpp +++ b/sprout/string/string.hpp @@ -29,7 +29,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/sprout/variant/variant.hpp b/sprout/variant/variant.hpp index 61646f15..729c7c21 100644 --- a/sprout/variant/variant.hpp +++ b/sprout/variant/variant.hpp @@ -27,7 +27,7 @@ #include #include #include -#include +#include namespace sprout { namespace detail {