-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalgorithm.hh
301 lines (222 loc) · 11.3 KB
/
algorithm.hh
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
// Copyright Stephan T. Lavavej, http://nuwen.net .
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at
// http://boost.org/LICENSE_1_0.txt .
#ifndef PHAM_ALGORITHM_HH
#define PHAM_ALGORITHM_HH
#include "compiler.hh"
#ifdef NUWEN_PLATFORM_MSVC
#pragma once
#endif
#include "external_begin.hh"
#include <algorithm>
#include <iterator>
#include <list>
#include <boost/mem_fn.hpp>
#include "external_end.hh"
namespace nuwen {
template <typename InIt, typename OutIt, typename Pred> OutIt copy_if(InIt first, InIt last, OutIt result, Pred pred);
template <typename C> C& universal_sort(C& container);
template <typename T> std::list<T>& universal_sort(std::list<T>& l);
template <typename C, typename F> C& universal_sort(C& container, F comparator);
template <typename T, typename F> std::list<T>& universal_sort(std::list<T>& l, F comparator);
template <typename C> C universal_sort_copy(const C& container);
template <typename C, typename F> C universal_sort_copy(const C& container, F comparator);
template <typename C> C& universal_stable_sort(C& container);
template <typename T> std::list<T>& universal_stable_sort(std::list<T>& l);
template <typename C, typename F> C& universal_stable_sort(C& container, F comparator);
template <typename T, typename F> std::list<T>& universal_stable_sort(std::list<T>& l, F comparator);
template <typename C> C universal_stable_sort_copy(const C& container);
template <typename C, typename F> C universal_stable_sort_copy(const C& container, F comparator);
template <typename C, typename T> C& nuke ( C& container, const T& value);
template <typename C, typename T> C nuke_copy(const C& container, const T& value);
template <typename C, typename P> C& nuke_if ( C& container, P pred);
template <typename C, typename P> C nuke_copy_if(const C& container, P pred);
template <typename C> C& nuke_dupes(C& container);
template <typename C, typename F, typename B> C& nuke_dupes(C& container, F comparator, B binary_pred);
template <typename C> C nuke_dupes_copy(const C& container);
template <typename C, typename F, typename B> C nuke_dupes_copy(const C& container, F comparator, B binary_pred);
template <typename C> C& stable_nuke_dupes(C& container);
template <typename C, typename F, typename B> C& stable_nuke_dupes(C& container, F comparator, B binary_pred);
template <typename C> C stable_nuke_dupes_copy(const C& container);
template <typename C, typename F, typename B> C stable_nuke_dupes_copy(const C& container, F comparator, B binary_pred);
template <typename InIt, typename OutIt, typename MemR, typename MemT>
OutIt copy_if(InIt first, InIt last, OutIt result, MemR (MemT::* pred)() const);
template <typename C, typename MemR, typename MemT>
C& nuke_if(C& container, MemR (MemT::* pred)() const);
template <typename C, typename MemR, typename MemT>
C nuke_copy_if(const C& container, MemR (MemT::* pred)() const);
template <typename InIt, typename MemR, typename MemT>
void for_each(InIt first, InIt last, MemR (MemT::* fxn)());
template <typename InIt, typename MemR, typename MemT>
InIt find_if(InIt first, InIt last, MemR (MemT::* pred)() const);
template <typename InIt, typename MemR, typename MemT>
typename std::iterator_traits<InIt>::difference_type
count_if(InIt first, InIt last, MemR (MemT::* pred)() const);
template <typename InIt, typename OutIt, typename MemR, typename MemT>
OutIt transform(InIt first, InIt last, OutIt result, MemR (MemT::* op)() const);
template <typename FwdIt, typename MemR, typename MemT, typename T>
void replace_if(FwdIt first, FwdIt last, MemR (MemT::* pred)() const, const T& new_value);
template <typename It, typename OutIt, typename MemR, typename MemT, typename T>
OutIt replace_copy_if(It first, It last, OutIt result, MemR (MemT::* pred)() const, const T& new_value);
template <typename BiIt, typename MemR, typename MemT>
BiIt partition(BiIt first, BiIt last, MemR (MemT::* pred)() const);
template <typename BiIt, typename MemR, typename MemT>
BiIt stable_partition(BiIt first, BiIt last, MemR (MemT::* pred)() const);
}
template <typename InIt, typename OutIt, typename Pred> OutIt nuwen::copy_if(InIt first, const InIt last, OutIt result, const Pred pred) {
while (first != last) {
if (pred(*first)) {
*result++ = *first++;
} else {
++first;
}
}
return result;
}
template <typename C> C& nuwen::universal_sort(C& container) {
std::sort(container.begin(), container.end());
return container;
}
template <typename T> std::list<T>& nuwen::universal_sort(std::list<T>& l) {
l.sort();
return l;
}
template <typename C, typename F> C& nuwen::universal_sort(C& container, const F comparator) {
std::sort(container.begin(), container.end(), comparator);
return container;
}
template <typename T, typename F> std::list<T>& nuwen::universal_sort(std::list<T>& l, const F comparator) {
l.sort(comparator);
return l;
}
template <typename C> C nuwen::universal_sort_copy(const C& container) {
C ret(container);
return universal_sort(ret);
}
template <typename C, typename F> C nuwen::universal_sort_copy(const C& container, const F comparator) {
C ret(container);
return universal_sort(ret, comparator);
}
template <typename C> C& nuwen::universal_stable_sort(C& container) {
std::stable_sort(container.begin(), container.end());
return container;
}
template <typename T> std::list<T>& nuwen::universal_stable_sort(std::list<T>& l) {
l.sort();
return l;
}
template <typename C, typename F> C& nuwen::universal_stable_sort(C& container, const F comparator) {
std::stable_sort(container.begin(), container.end(), comparator);
return container;
}
template <typename T, typename F> std::list<T>& nuwen::universal_stable_sort(std::list<T>& l, const F comparator) {
l.sort(comparator);
return l;
}
template <typename C> C nuwen::universal_stable_sort_copy(const C& container) {
C ret(container);
return universal_stable_sort(ret);
}
template <typename C, typename F> C nuwen::universal_stable_sort_copy(const C& container, const F comparator) {
C ret(container);
return universal_stable_sort(ret, comparator);
}
template <typename C, typename T> C& nuwen::nuke(C& container, const T& value) {
container.erase(std::remove(container.begin(), container.end(), value), container.end());
return container;
}
template <typename C, typename T> C nuwen::nuke_copy(const C& container, const T& value) {
C ret(container);
return nuke(ret, value);
}
template <typename C, typename P> C& nuwen::nuke_if(C& container, const P pred) {
container.erase(std::remove_if(container.begin(), container.end(), pred), container.end());
return container;
}
template <typename C, typename P> C nuwen::nuke_copy_if(const C& container, const P pred) {
C ret(container);
return nuke_if(ret, pred);
}
template <typename C> C& nuwen::nuke_dupes(C& container) {
universal_sort(container);
container.erase(std::unique(container.begin(), container.end()), container.end());
return container;
}
template <typename C, typename F, typename B> C& nuwen::nuke_dupes(C& container, const F comparator, const B binary_pred) {
universal_sort(container, comparator);
container.erase(std::unique(container.begin(), container.end(), binary_pred), container.end());
return container;
}
template <typename C> C nuwen::nuke_dupes_copy(const C& container) {
C ret(container);
return nuke_dupes(ret);
}
template <typename C, typename F, typename B> C nuwen::nuke_dupes_copy(const C& container, const F comparator, const B binary_pred) {
C ret(container);
return nuke_dupes(ret, comparator, binary_pred);
}
template <typename C> C& nuwen::stable_nuke_dupes(C& container) {
universal_stable_sort(container);
container.erase(std::unique(container.begin(), container.end()), container.end());
return container;
}
template <typename C, typename F, typename B> C& nuwen::stable_nuke_dupes(C& container, const F comparator, const B binary_pred) {
universal_stable_sort(container, comparator);
container.erase(std::unique(container.begin(), container.end(), binary_pred), container.end());
return container;
}
template <typename C> C nuwen::stable_nuke_dupes_copy(const C& container) {
C ret(container);
return stable_nuke_dupes(ret);
}
template <typename C, typename F, typename B> C nuwen::stable_nuke_dupes_copy(const C& container, const F comparator, const B binary_pred) {
C ret(container);
return stable_nuke_dupes(ret, comparator, binary_pred);
}
template <typename InIt, typename OutIt, typename MemR, typename MemT>
OutIt nuwen::copy_if(InIt first, InIt last, OutIt result, MemR (MemT::* const pred)() const) {
return copy_if(first, last, result, boost::mem_fn(pred));
}
template <typename C, typename MemR, typename MemT>
C& nuwen::nuke_if(C& container, MemR (MemT::* const pred)() const) {
return nuke_if(container, boost::mem_fn(pred));
}
template <typename C, typename MemR, typename MemT>
C nuwen::nuke_copy_if(const C& container, MemR (MemT::* const pred)() const) {
return nuke_copy_if(container, boost::mem_fn(pred));
}
template <typename InIt, typename MemR, typename MemT>
void nuwen::for_each(InIt first, InIt last, MemR (MemT::* const fxn)()) {
std::for_each(first, last, boost::mem_fn(fxn));
}
template <typename InIt, typename MemR, typename MemT>
InIt nuwen::find_if(InIt first, InIt last, MemR (MemT::* const pred)() const) {
return std::find_if(first, last, boost::mem_fn(pred));
}
template <typename InIt, typename MemR, typename MemT>
typename std::iterator_traits<InIt>::difference_type
nuwen::count_if(InIt first, InIt last, MemR (MemT::* const pred)() const) {
return std::count_if(first, last, boost::mem_fn(pred));
}
template <typename InIt, typename OutIt, typename MemR, typename MemT>
OutIt nuwen::transform(InIt first, InIt last, OutIt result, MemR (MemT::* const op)() const) {
return std::transform(first, last, result, boost::mem_fn(op));
}
template <typename FwdIt, typename MemR, typename MemT, typename T>
void nuwen::replace_if(FwdIt first, FwdIt last, MemR (MemT::* const pred)() const, const T& new_value) {
std::replace_if(first, last, boost::mem_fn(pred), new_value);
}
template <typename It, typename OutIt, typename MemR, typename MemT, typename T>
OutIt nuwen::replace_copy_if(It first, It last, OutIt result, MemR (MemT::* const pred)() const, const T& new_value) {
return std::replace_copy_if(first, last, result, boost::mem_fn(pred), new_value);
}
template <typename BiIt, typename MemR, typename MemT>
BiIt nuwen::partition(BiIt first, BiIt last, MemR (MemT::* const pred)() const) {
return std::partition(first, last, boost::mem_fn(pred));
}
template <typename BiIt, typename MemR, typename MemT>
BiIt nuwen::stable_partition(BiIt first, BiIt last, MemR (MemT::* const pred)() const) {
return std::stable_partition(first, last, boost::mem_fn(pred));
}
#endif // Idempotency