mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2025-07-04 14:14:09 +00:00
add c++14 constexpr numeric algorithms
This commit is contained in:
parent
fd608b74e2
commit
f1555cb917
65 changed files with 433 additions and 150 deletions
|
@ -9,8 +9,8 @@
|
|||
#define SPROUT_NUMERIC_CXX14_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
//#include <sprout/numeric/cxx14/partial_sum.hpp>
|
||||
//#include <sprout/numeric/cxx14/adjacent_difference.hpp>
|
||||
//#include <sprout/numeric/cxx14/iota.hpp>
|
||||
#include <sprout/numeric/cxx14/partial_sum.hpp>
|
||||
#include <sprout/numeric/cxx14/adjacent_difference.hpp>
|
||||
#include <sprout/numeric/cxx14/iota.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_NUMERIC_CXX14_HPP
|
||||
|
|
54
sprout/numeric/cxx14/adjacent_difference.hpp
Normal file
54
sprout/numeric/cxx14/adjacent_difference.hpp
Normal file
|
@ -0,0 +1,54 @@
|
|||
/*=============================================================================
|
||||
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_NUMERIC_CXX14_ADJACENT_DIFFERENCE_HPP
|
||||
#define SPROUT_NUMERIC_CXX14_ADJACENT_DIFFERENCE_HPP
|
||||
|
||||
#include <iterator>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/utility/move.hpp>
|
||||
#include <sprout/iterator/type_traits/is_iterator_of.hpp>
|
||||
#include <sprout/type_traits/enabler_if.hpp>
|
||||
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// 26.7.5 Adjacent difference
|
||||
//
|
||||
template<
|
||||
typename InputIterator, typename OutputIterator, typename BinaryOperation,
|
||||
typename sprout::enabler_if<sprout::is_iterator_outputable<OutputIterator>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CXX14_CONSTEXPR OutputIterator
|
||||
adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op) {
|
||||
typedef typename std::iterator_traits<InputIterator>::value_type value_type;
|
||||
if (first == last) {
|
||||
return result;
|
||||
}
|
||||
value_type acc = *first;
|
||||
*result = acc;
|
||||
while (++first != last) {
|
||||
value_type val = *first;
|
||||
*++result = binary_op(val, acc);
|
||||
acc = sprout::move(val);
|
||||
}
|
||||
return ++result;
|
||||
}
|
||||
template<
|
||||
typename InputIterator, typename OutputIterator,
|
||||
typename sprout::enabler_if<sprout::is_iterator_outputable<OutputIterator>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CXX14_CONSTEXPR OutputIterator
|
||||
adjacent_difference(InputIterator first, InputIterator last, OutputIterator result) {
|
||||
return sprout::adjacent_difference(
|
||||
first, last, result,
|
||||
NS_SSCRISK_CEL_OR_SPROUT::minus<typename std::iterator_traits<InputIterator>::value_type>()
|
||||
);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_NUMERIC_CXX14_ADJACENT_DIFFERENCE_HPP
|
27
sprout/numeric/cxx14/iota.hpp
Normal file
27
sprout/numeric/cxx14/iota.hpp
Normal file
|
@ -0,0 +1,27 @@
|
|||
/*=============================================================================
|
||||
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_NUMERIC_CXX14_IOTA_HPP
|
||||
#define SPROUT_NUMERIC_CXX14_IOTA_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// 26.7.6 Iota
|
||||
//
|
||||
template<typename ForwardIterator, typename T>
|
||||
inline SPROUT_CXX14_CONSTEXPR void
|
||||
iota(ForwardIterator first, ForwardIterator last, T value) {
|
||||
while (first != last) {
|
||||
*first++ = value;
|
||||
++value;
|
||||
}
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_NUMERIC_CXX14_IOTA_HPP
|
52
sprout/numeric/cxx14/partial_sum.hpp
Normal file
52
sprout/numeric/cxx14/partial_sum.hpp
Normal file
|
@ -0,0 +1,52 @@
|
|||
/*=============================================================================
|
||||
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_NUMERIC_CXX14_PARTIAL_SUM_HPP
|
||||
#define SPROUT_NUMERIC_CXX14_PARTIAL_SUM_HPP
|
||||
|
||||
#include <iterator>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/iterator/type_traits/is_iterator_of.hpp>
|
||||
#include <sprout/type_traits/enabler_if.hpp>
|
||||
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// 26.7.4 Partial sum
|
||||
//
|
||||
template<
|
||||
typename InputIterator, typename OutputIterator, typename BinaryOperation,
|
||||
typename sprout::enabler_if<sprout::is_iterator_outputable<OutputIterator>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CXX14_CONSTEXPR OutputIterator
|
||||
partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op) {
|
||||
typedef typename std::iterator_traits<InputIterator>::value_type value_type;
|
||||
if (first == last) {
|
||||
return result;
|
||||
}
|
||||
value_type sum = *first;
|
||||
*result = sum;
|
||||
while (++first != last) {
|
||||
sum = binary_op(sum, *first);
|
||||
*++result = sum;
|
||||
}
|
||||
return ++result;
|
||||
}
|
||||
template<
|
||||
typename InputIterator, typename OutputIterator,
|
||||
typename sprout::enabler_if<sprout::is_iterator_outputable<OutputIterator>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CXX14_CONSTEXPR OutputIterator
|
||||
partial_sum(InputIterator first, InputIterator last, OutputIterator result) {
|
||||
return sprout::partial_sum(
|
||||
first, last, result,
|
||||
NS_SSCRISK_CEL_OR_SPROUT::plus<typename std::iterator_traits<InputIterator>::value_type>()
|
||||
);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_NUMERIC_CXX14_PARTIAL_SUM_HPP
|
Loading…
Add table
Add a link
Reference in a new issue