This repository was archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalgorithm
327 lines (285 loc) · 7.18 KB
/
algorithm
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <utility>
namespace std
{
template <typename InputIt, typename OutputIt>
OutputIt copy(InputIt first, InputIt last, OutputIt result)
{
while (first != last)
{
*result = *first;
++result;
++first;
}
return result;
}
template <class BidirIt1, class BidirIt2>
BidirIt2 copy_backward(BidirIt1 first, BidirIt1 last, BidirIt2 d_last)
{
while (first != last)
{
*(--d_last) = *(--last);
}
return d_last;
}
template <typename InputIt, typename OutputIt, typename UnaryOperation>
OutputIt transform(InputIt first, InputIt last, OutputIt result, UnaryOperation op)
{
while (first != last)
{
*result = op(*first);
++first;
++result;
}
return result;
};
template <class T>
void swap(T &a, T &b) noexcept(std::is_nothrow_move_constructible<T>::value && std::is_nothrow_move_assignable<T>::value)
{
T temp = std::move(a);
a = std::move(b);
b = std::move(temp);
}
template <class T2, std::size_t N>
void swap(T2 (&a)[N], T2 (&b)[N]) noexcept(std::is_nothrow_swappable_v<T2>)
{
for (std::size_t i = 0; i < N; ++i)
std::swap(a[i], b[i]);
}
template <typename T>
constexpr const T &min(const T &a, const T &b)
{
return (a < b) ? a : b;
}
template <typename T>
constexpr const T &max(const T &a, const T &b)
{
return (a > b) ? a : b;
}
template <typename T>
constexpr const T &clamp(const T &v, const T &lo, const T &hi)
{
return std::max(lo, std::min(v, hi));
}
template <class T, class Compare>
constexpr const T &clamp(const T &v, const T &lo, const T &hi, Compare comp)
{
return comp(v, lo) ? lo : comp(hi, v) ? hi
: v;
}
template <class ForwardIt1, class ForwardIt2>
ForwardIt1 search(ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last)
{
for (; first != last; ++first)
{
ForwardIt1 it = first;
for (ForwardIt2 s_it = s_first;; ++it, ++s_it)
{
if (s_it == s_last)
return first;
if (it == last)
return last;
if (!(*it == *s_it))
break;
}
}
return last;
}
template <class InputIt, class T>
constexpr InputIt find(InputIt first, InputIt last, const T &value)
{
for (; first != last; ++first)
if (*first == value)
return first;
return last;
}
template <class InputIt, class UnaryPred>
constexpr InputIt find_if(InputIt first, InputIt last, UnaryPred p)
{
for (; first != last; ++first)
if (p(*first))
return first;
return last;
}
template <class InputIt, class UnaryPred>
constexpr InputIt find_if_not(InputIt first, InputIt last, UnaryPred q)
{
for (; first != last; ++first)
if (!q(*first))
return first;
return last;
}
template <class ForwardIt1, class ForwardIt2>
ForwardIt1 find_end(ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last)
{
if (s_first == s_last)
return last;
ForwardIt1 result = last;
while (true)
{
ForwardIt1 new_result = std::search(first, last, s_first, s_last);
if (new_result == last)
return result;
result = new_result;
first = new_result;
++first;
}
}
template <class InputIt, class ForwardIt>
InputIt find_first_of(InputIt first, InputIt last,
ForwardIt s_first, ForwardIt s_last)
{
for (; first != last; ++first)
for (ForwardIt it = s_first; it != s_last; ++it)
if (*first == *it)
return first;
return last;
}
template <class ForwardIt, class T>
ForwardIt remove(ForwardIt first, ForwardIt last, const T &value)
{
first = std::find(first, last, value);
if (first != last)
for (ForwardIt i = first; ++i != last;)
if (!(*i == value))
*first++ = std::move(*i);
return first;
}
template <class ForwardIt, class UnaryPred>
ForwardIt remove_if(ForwardIt first, ForwardIt last, UnaryPred p)
{
first = std::find_if(first, last, p);
if (first != last)
for (ForwardIt i = first; ++i != last;)
if (!p(*i))
*first++ = std::move(*i);
return first;
}
template <class ForwardIt, class T>
void fill(ForwardIt first, ForwardIt last, const T &value)
{
for (; first != last; ++first)
*first = value;
}
template <class OutputIt, class Size, class T>
OutputIt fill_n(OutputIt first, Size count, const T &value)
{
for (Size i = 0; i < count; ++i)
{
*first = value;
++first;
}
return first;
}
template <class InputIt1, class InputIt2>
constexpr bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2)
{
while (first1 != last1)
{
if (!(*first1 == *first2))
return false;
++first1;
++first2;
}
return true;
}
template <class ExecutionPolicy, class ForwardIt1, class ForwardIt2>
bool equal(ExecutionPolicy &&policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2)
{
while (first1 != last1)
{
if (!(*first1 == *first2))
return false;
++first1;
++first2;
}
return true;
}
template <class InputIt1, class InputIt2, class BinaryPred>
constexpr bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPred p)
{
while (first1 != last1)
{
if (!p(*first1, *first2))
return false;
++first1;
++first2;
}
return true;
}
template <class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPred>
bool equal(ExecutionPolicy &&policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, BinaryPred p)
{
while (first1 != last1)
{
if (!p(*first1, *first2))
return false;
++first1;
++first2;
}
return true;
}
template <class InputIt1, class InputIt2>
constexpr bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
{
while (first1 != last1)
{
if (!(*first1 == *first2))
return false;
++first1;
++first2;
}
return true;
}
template <class ExecutionPolicy, class ForwardIt1, class ForwardIt2>
bool equal(ExecutionPolicy &&policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2)
{
while (first1 != last1)
{
if (!(*first1 == *first2))
return false;
++first1;
++first2;
}
return true;
}
template <class InputIt1, class InputIt2, class BinaryPred>
constexpr bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, BinaryPred p)
{
while (first1 != last1)
{
if (!p(*first1, *first2))
return false;
++first1;
++first2;
}
return true;
}
template <class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPred>
bool equal(ExecutionPolicy &&policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2, BinaryPred p)
{
while (first1 != last1)
{
if (!p(*first1, *first2))
return false;
++first1;
++first2;
}
return true;
}
}