source: mainline/uspace/lib/cpp/include/__bits/numeric.hpp@ e49d0ac

Last change on this file since e49d0ac was b57ba05, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Update headers in C++ files

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/*
2 * SPDX-FileCopyrightText: 2018 Jaroslav Jindrak
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef LIBCPP_BITS_NUMERIC
8#define LIBCPP_BITS_NUMERIC
9
10#include <utility>
11
12namespace std
13{
14
15 /**
16 * 26.7.2, accumulate:
17 */
18
19 template<class InputIterator, class T>
20 T accumulate(InputIterator first, InputIterator last, T init)
21 {
22 auto acc{init};
23 while (first != last)
24 acc += *first++;
25
26 return acc;
27 }
28
29 template<class InputIterator, class T, class BinaryOperation>
30 T accumulate(InputIterator first, InputIterator last, T init,
31 BinaryOperation op)
32 {
33 auto acc{init};
34 while (first != last)
35 acc = op(acc, *first++);
36
37 return acc;
38 }
39
40 /**
41 * 26.7.3, inner product:
42 */
43
44 template<class InputIterator1, class InputIterator2, class T>
45 T inner_product(InputIterator1 first1, InputIterator1 last1,
46 InputIterator2 first2, T init)
47 {
48 auto res{init};
49 while (first1 != last1)
50 res += (*first1++) * (*first2++);
51
52 return res;
53 }
54
55 template<class InputIterator1, class InputIterator2, class T,
56 class BinaryOperation1, class BinaryOperation2>
57 T inner_product(InputIterator1 first1, InputIterator1 last1,
58 InputIterator2 first2, T init,
59 BinaryOperation1 op1, BinaryOperation2 op2)
60 {
61 auto res{init};
62 while (first1 != last1)
63 res = op1(res, op2(*first1++, *first2++));
64
65 return res;
66 }
67
68 /**
69 * 26.7.4, partial sum:
70 */
71
72 template<class InputIterator, class OutputIterator>
73 OutputIterator partial_sum(InputIterator first, InputIterator last,
74 OutputIterator result)
75 {
76 if (first == last)
77 return result;
78
79 auto acc{*first++};
80 *result++ = acc;
81
82 while (first != last)
83 *result++ = acc = acc + *first++;
84
85 return result;
86 }
87
88 template<class InputIterator, class OutputIterator, class BinaryOperation>
89 OutputIterator partial_sum(InputIterator first, InputIterator last,
90 OutputIterator result, BinaryOperation op)
91 {
92 if (first == last)
93 return result;
94
95 auto acc{*first++};
96 *result++ = acc;
97
98 while (first != last)
99 *result++ = acc = op(acc, *first++);
100
101 return result;
102 }
103
104 /**
105 * 26.7.5, adjacent difference:
106 */
107
108 template<class InputIterator, class OutputIterator>
109 OutputIterator adjacent_difference(InputIterator first, InputIterator last,
110 OutputIterator result)
111 {
112 if (first == last)
113 return result;
114
115 auto acc{*first++};
116 *result++ = acc;
117
118 while (first != last)
119 {
120 auto val = *first++;
121 *result++ = val - acc;
122 acc = move(val);
123 }
124
125 return result;
126 }
127
128 template<class InputIterator, class OutputIterator, class BinaryOperation>
129 OutputIterator adjacent_difference(InputIterator first, InputIterator last,
130 OutputIterator result, BinaryOperation op)
131 {
132 if (first == last)
133 return result;
134
135 auto acc{*first++};
136 *result++ = acc;
137
138 while (first != last)
139 {
140 auto val = *first++;
141 *result++ = op(val, acc);
142 acc = move(val);
143 }
144
145 return result;
146 }
147
148 /**
149 * 26.7.6, iota:
150 */
151
152 template<class ForwardIterator, class T>
153 void iota(ForwardIterator first, ForwardIterator last, T value)
154 {
155 while (first != last)
156 *first++ = value++;
157 }
158}
159
160#endif
Note: See TracBrowser for help on using the repository browser.