2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_NUMERIC
11 #define _LIBCPP_NUMERIC
19 template <class InputIterator, class T>
20 constexpr T // constexpr since C++20
21 accumulate(InputIterator first, InputIterator last, T init);
23 template <class InputIterator, class T, class BinaryOperation>
24 constexpr T // constexpr since C++20
25 accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);
27 template<class InputIterator>
28 constexpr typename iterator_traits<InputIterator>::value_type // constexpr since C++20
29 reduce(InputIterator first, InputIterator last); // C++17
31 template<class InputIterator, class T>
32 constexpr T // constexpr since C++20
33 reduce(InputIterator first, InputIterator last, T init); // C++17
35 template<class InputIterator, class T, class BinaryOperation>
36 constexpr T // constexpr since C++20
37 reduce(InputIterator first, InputIterator last, T init, BinaryOperation binary_op); // C++17
39 template <class InputIterator1, class InputIterator2, class T>
40 constexpr T // constexpr since C++20
41 inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init);
43 template <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
44 constexpr T // constexpr since C++20
45 inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
46 T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);
49 template<class InputIterator1, class InputIterator2, class T>
50 constexpr T // constexpr since C++20
51 transform_reduce(InputIterator1 first1, InputIterator1 last1,
52 InputIterator2 first2, T init); // C++17
54 template<class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
55 constexpr T // constexpr since C++20
56 transform_reduce(InputIterator1 first1, InputIterator1 last1,
57 InputIterator2 first2, T init,
58 BinaryOperation1 binary_op1, BinaryOperation2 binary_op2); // C++17
60 template<class InputIterator, class T, class BinaryOperation, class UnaryOperation>
61 constexpr T // constexpr since C++20
62 transform_reduce(InputIterator first, InputIterator last, T init,
63 BinaryOperation binary_op, UnaryOperation unary_op); // C++17
65 template <class InputIterator, class OutputIterator>
66 constexpr OutputIterator // constexpr since C++20
67 partial_sum(InputIterator first, InputIterator last, OutputIterator result);
69 template <class InputIterator, class OutputIterator, class BinaryOperation>
70 constexpr OutputIterator // constexpr since C++20
71 partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
73 template<class InputIterator, class OutputIterator, class T>
74 constexpr OutputIterator // constexpr since C++20
75 exclusive_scan(InputIterator first, InputIterator last,
76 OutputIterator result, T init); // C++17
78 template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
79 constexpr OutputIterator // constexpr since C++20
80 exclusive_scan(InputIterator first, InputIterator last,
81 OutputIterator result, T init, BinaryOperation binary_op); // C++17
83 template<class InputIterator, class OutputIterator>
84 constexpr OutputIterator // constexpr since C++20
85 inclusive_scan(InputIterator first, InputIterator last, OutputIterator result); // C++17
87 template<class InputIterator, class OutputIterator, class BinaryOperation>
88 constexpr OutputIterator // constexpr since C++20
89 inclusive_scan(InputIterator first, InputIterator last,
90 OutputIterator result, BinaryOperation binary_op); // C++17
92 template<class InputIterator, class OutputIterator, class BinaryOperation, class T>
93 constexpr OutputIterator // constexpr since C++20
94 inclusive_scan(InputIterator first, InputIterator last,
95 OutputIterator result, BinaryOperation binary_op, T init); // C++17
97 template<class InputIterator, class OutputIterator, class T,
98 class BinaryOperation, class UnaryOperation>
99 constexpr OutputIterator // constexpr since C++20
100 transform_exclusive_scan(InputIterator first, InputIterator last,
101 OutputIterator result, T init,
102 BinaryOperation binary_op, UnaryOperation unary_op); // C++17
104 template<class InputIterator, class OutputIterator,
105 class BinaryOperation, class UnaryOperation>
106 constexpr OutputIterator // constexpr since C++20
107 transform_inclusive_scan(InputIterator first, InputIterator last,
108 OutputIterator result,
109 BinaryOperation binary_op, UnaryOperation unary_op); // C++17
111 template<class InputIterator, class OutputIterator,
112 class BinaryOperation, class UnaryOperation, class T>
113 constexpr OutputIterator // constexpr since C++20
114 transform_inclusive_scan(InputIterator first, InputIterator last,
115 OutputIterator result,
116 BinaryOperation binary_op, UnaryOperation unary_op,
119 template <class InputIterator, class OutputIterator>
120 constexpr OutputIterator // constexpr since C++20
121 adjacent_difference(InputIterator first, InputIterator last, OutputIterator result);
123 template <class InputIterator, class OutputIterator, class BinaryOperation>
124 constexpr OutputIterator // constexpr since C++20
125 adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
127 template <class ForwardIterator, class T>
128 constexpr void // constexpr since C++20
129 iota(ForwardIterator first, ForwardIterator last, T value);
131 template <class M, class N>
132 constexpr common_type_t<M,N> gcd(M m, N n); // C++17
134 template <class M, class N>
135 constexpr common_type_t<M,N> lcm(M m, N n); // C++17
138 constexpr T midpoint(T a, T b) noexcept; // C++20
141 constexpr T* midpoint(T* a, T* b); // C++20
143 // [numeric.sat], saturation arithmetic
145 constexpr T add_sat(T x, T y) noexcept; // freestanding, Since C++26
147 constexpr T sub_sat(T x, T y) noexcept; // freestanding, Since C++26
149 constexpr T mul_sat(T x, T y) noexcept; // freestanding, Since C++26
151 constexpr T div_sat(T x, T y) noexcept; // freestanding, Since C++26
152 template<class T, class U>
153 constexpr T saturate_cast(U x) noexcept; // freestanding, Since C++26
161 #include <__numeric/accumulate.h>
162 #include <__numeric/adjacent_difference.h>
163 #include <__numeric/inner_product.h>
164 #include <__numeric/iota.h>
165 #include <__numeric/partial_sum.h>
167 #if _LIBCPP_STD_VER >= 17
168 # include <__numeric/exclusive_scan.h>
169 # include <__numeric/gcd_lcm.h>
170 # include <__numeric/inclusive_scan.h>
171 # include <__numeric/pstl.h>
172 # include <__numeric/reduce.h>
173 # include <__numeric/transform_exclusive_scan.h>
174 # include <__numeric/transform_inclusive_scan.h>
175 # include <__numeric/transform_reduce.h>
178 #if _LIBCPP_STD_VER >= 20
179 # include <__numeric/midpoint.h>
180 # include <__numeric/saturation_arithmetic.h>
185 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
186 # pragma GCC system_header
189 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 14
190 # include <initializer_list>
194 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
199 # include <execution>
200 # include <functional>
204 # include <type_traits>
207 #endif // _LIBCPP_NUMERIC