-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumeric.h
154 lines (143 loc) · 4.98 KB
/
numeric.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// Created by gy gao on 1/10/23.
//
#ifndef MYOWNSTL_NUMERIC_H
#define MYOWNSTL_NUMERIC_H
// 这个头文件包含了 myownstl 的数值算法
#include "iterator.h"
namespace myownstl
{
/*****************************************************************************************/
// accumulate
// 版本1:以初值 init 对每个元素进行累加
// 版本2:以初值 init 对每个元素进行二元操作
/*****************************************************************************************/
// 版本1
template<class InputIter, class T>
T accumulate(InputIter first, InputIter last, T init)
{
for (; first != last; ++first)
{
init += *first;
}
return init;
}
// 版本2
template<class InputIter, class T, class BinaryOp>
T accumulate(InputIter first, InputIter last, T init, BinaryOp binary_op)
{
for (; first != last; ++first)
{
init = binary_op(init, *first);
}
return init;
}
/*****************************************************************************************/
// adjacent_difference
// 版本1:计算相邻元素的差值,结果保存到以 result 为起始的区间上
// 版本2:自定义相邻元素的二元操作
/*****************************************************************************************/
// 版本1
template<class InputIter, class OutputIter>
OutputIter adjacent_difference(InputIter first, InputIter last, OutputIter result)
{
if (first == last) return result;
*result = *first;
while (++first != last)
{
auto tmp = *first;
*++result = tmp - value;
value = tmp;
}
return ++result;
}
// 版本2
template<class InputIter, class OutputIter, class BinaryOp>
OutputIter adjacent_difference(InputIter first, InputIter last, OutputIter result, BinaryOp binary_op)
{
if (first == last) return result;
*result = *first;
auto value = *first;
while (++first != last)
{
auto tmp = *first;
*++result = binary_op(tmp, value);
value = tmp;
}
return ++result;
}
/*****************************************************************************************/
// inner_product
// 版本1:以 init 为初值,计算两个区间的内积
// 版本2:自定义 operator+ 和 operator*
/*****************************************************************************************/
// 版本1
template<class InputIter1, class InputIter2, class T>
T inner_product(InputIter1 first1, InputIter1 last1, InputIter2 first2, T init)
{
for (; first1 != last1; ++first1, ++first2)
{
init = init + ((*first) * (*first2));
}
return init;
}
// 版本2
template<class InputIter1, class InputIter2, class T, class BinaryOp1, class BinaryOp2>
T inner_product(InputIter1 first1, InputIter1 last1, InputIter2 first2, T init,
BinaryOp1 binary_op1, BinaryOp2 binary_op2)
{
for (; first1 != last1; ++first1, ++first2)
{
init = binary_op1(init, binary_op2(*first1, *first2));
}
return init;
}
/*****************************************************************************************/
// iota
// 填充[first, last),以 value 为初值开始递增
/*****************************************************************************************/
template<class ForwardIter, class T>
void iota(ForwardIter first, ForwardIter last, T value)
{
while (first != last)
{
*first++ = value;
++value;
}
}
/*****************************************************************************************/
// partial_sum
// 版本1:计算局部累计求和,结果保存到以 result 为起始的区间上
// 版本2:进行局部进行自定义二元操作
/*****************************************************************************************/
// 版本1
template<class InputIter, class OutputIter>
OutputIter partial_sum(InputIter first, InputIter last, OutputIter result)
{
if (first == last) return result;
*result = *first;
auto value = *first;
while (++first != last)
{
value = value + *first;
*++result = value;
}
return ++result;
}
// 版本2
template<class InputIter, class OutputIter, class BinaryOp>
OutputIter partial_sum(InputIter first, InputIter last, OutputIter result,
BinaryOp binary_op)
{
if (first == last) return result;
*result = *first;
auto value = *first;
while (++first != last)
{
value = binary_op(value, *first);
*++result = value;
}
return ++result;
}
}
#endif //MYOWNSTL_NUMERIC_H