-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml_algobase.h
337 lines (246 loc) · 8.43 KB
/
ml_algobase.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
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
328
329
330
331
332
333
334
335
/**
* @file ml_algobase.h
* @brief
* @author malin malinbupt@gmail.com
* @date 2015年04月02日 星期四 16时47分22秒
* @version
* @note
*/
#ifndef __ML_INTERNAL_ALGOBASE_H
#define __ML_INTERNAL_ALGOBASE_H
#include "ml_config.h"
#include "ml_pair.h"
#include "type_traits.h"
#include "ml_iterator_base.h"
__ML_BEGIN_NAMESPACE
template<typename InputIterator1, typename InputIterator2>
bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)
{
for (; first1 != last1; ++first1, ++first2)
{
if (*first1 != *first2) return false;
}
return true;
}
template<typename InputIterator1, typename InputIterator2, typename Predict>
bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, Predict pred)
{
for (; first1 != last1; ++first1, ++first2)
{
if (!pred(*first1, *first2)) return false;
}
return true;
}
template<typename ForwardIterator, typename T>
void fill(ForwardIterator first, ForwardIterator last, const T &x)
{
while (first != last) *first++ = x;
}
template<typename ForwardIterator, typename Size, typename T>
ForwardIterator fill_n(ForwardIterator first, Size n, const T &x)
{
while (n-- > 0) *first++ = x;
return first;
}
template<typename ForwardIterator1, typename ForwardIterator2>
inline void iter_swap(ForwardIterator1 a, ForwardIterator2 b)
{
typename iterator_traits<ForwardIterator1>::value_type tmp;
tmp = *a; *a = *b; *b = tmp;
}
template<typename InputIterator1, typename InputIterator2>
bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2)
{
for (; first1 != last1, first2 != last2; ++first1, ++first2)
{
if (*first1 < *first2) return true;
if (*first2 < *first1) return false;
}
return first1 == last1 && first2 != last2;
}
template<typename InputIterator1, typename InputIterator2, typename Compare>
bool lexicographical_comapre(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare cmp)
{
for (; first1 != last1, first2 != last2; ++first1, ++first2)
{
if (cmp(*first1, *first2)) return true;
if (cmp(*first2, *first1)) return false;
}
return first1 == last1 && first2 != last2;
}
template<typename T>
const T &max(const T &a, const T &b)
{
return a < b ? b : a;
}
template<typename T, typename Compare>
const T &max(const T &a, const T &b, Compare comp)
{
return comp(a, b) ? b : a;
}
template<typename T>
const T &min(const T &a, const T &b)
{
return a < b ? a : b;
}
template<typename T, typename Compare>
const T &min(const T &a, const T &b, Compare comp)
{
return comp(a, b) ? a : b;
}
template<typename InputIterator1, typename InputIterator2>
pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator2 last1, InputIterator2 first2)
{
for (; first1 != last1 && *first1 == *first2; ++first1, ++last1);
return make_pair(first1, first2);
}
template<typename T>
inline void swap(T &a, T &b)
{
T tmp = a; a = b; b = tmp;
}
// copy
// special for iterators which support for arithmetic operations(random_access_iterator and pointer)
template<typename RandomIterator, typename OutputIterator, typename Distance>
OutputIterator __copy_d(RandomIterator first, RandomIterator last, OutputIterator dest, Distance*)
{
for (Distance n = last - first; n > 0; --n, ++first, ++dest) *dest = *first;
return dest;
}
// special for bidirectional_iterator
template<typename InputIterator, typename OutputIterator>
OutputIterator __copy(InputIterator first, InputIterator last, OutputIterator dest, input_iterator_tag)
{
for (; first != last; ++first, ++dest) *first = *dest;
return dest;
}
// special for random_access_iterator_tag
template<typename RandomAccessIterator, typename OutputIterator>
OutputIterator __copy(RandomAccessIterator first, RandomAccessIterator last, OutputIterator dest, random_access_iterator_tag)
{
return __copy_d(first, last, dest, distance_type(first));
}
// dipatch function template
template<typename InputIterator, typename OutputIterator>
struct __copy_dispatch
{
OutputIterator operator () (InputIterator first, InputIterator last, OutputIterator dest)
{
return __copy(first, last, dest, iterator_category(dest));
}
};
// special for pointer which has tivial assignment operator. mememove is the best solution
template<typename T>
T *__copy_t(const T *first, const T *last, T *dest, __true_type)
{
memmove(dest, first, (last - first) * sizeof(T));
return dest + (last - first);
}
// special for pointer which has nontrivial assignment operator. invoke the random_iterator_tag version
template<typename T>
T *__copy_t(const T *first, const T *last, T *dest, __false_type)
{
return __copy_d(first, last, dest, (ptrdiff_t*)(0));
}
// dipatch function template, special for pointer
template<typename T>
struct __copy_dispatch<T*, T*>
{
T* operator () (T *first, T *last, T *dest)
{
typename __type_traits<T>::has_trivial_assignment_operator t;
return __copy_t(first, last, dest, t);
}
};
template<typename T>
struct __copy_dispatch<const T*, T*>
{
T* operator () (const T *first, const T *last, T *dest)
{
typename __type_traits<T>::has_trivial_assignment_operator t;
return __copy_t(first, last, dest, t);
}
};
// common interface
template<typename InputIterator, typename OutputIterator>
OutputIterator copy(InputIterator first, InputIterator last, OutputIterator dest)
{
return __copy_dispatch<InputIterator, OutputIterator>()(first, last, dest);
}
// copy_backward
// special for iterators which support for arithmetic operations(random_access_iterator and pointer)
template<typename RandomAccessIterator, typename BidirectionalIterator, typename Distance>
BidirectionalIterator __copy_backward_d(RandomAccessIterator first, RandomAccessIterator last, BidirectionalIterator dest,
Distance*)
{
for (Distance n = last - first; n > 0; --n) *--dest = *--last;
return dest;
}
// special for bidirectional_iterator
template<typename BidirectionalIterator1, typename BidirectionalIterator2>
BidirectionalIterator2 __copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 dest,
bidirectional_iterator_tag)
{
while (first != last) *--dest = *--last;
return dest;
}
// special for random_access_iterator
template<typename RandomAccessIterator, typename BidirectionalIterator>
inline BidirectionalIterator __copy_backward(RandomAccessIterator first, RandomAccessIterator last, BidirectionalIterator dest,
random_access_iterator_tag)
{
typename iterator_traits<RandomAccessIterator>::difference_type *dis(0);
return __copy_backward_d(first, last, dest, dis);
}
// dispatch function template, used for iterator
template<typename BidirectionalIterator1, typename BidirectionalIterator2>
struct __copy_backward_dispatch
{
inline BidirectionalIterator2 operator () (BidirectionalIterator1 first, BidirectionalIterator1 last,
BidirectionalIterator2 dest)
{
typename iterator_traits<BidirectionalIterator1>::iterator_category category;
return __copy_backward(first, last, dest, category);
}
};
// special for pointer which has tivial assignment operator. mememove is the best solution
template<typename T>
inline T *__copy_backward_t(const T *first, const T *last, T *dest, __true_type)
{
ptrdiff_t n = last - first;
memmove(dest - n, first, n * sizeof(T));
return dest + n;
}
// special for pointer which has nontrivial assignment operator. invoke the random_iterator_tag version
template<typename T>
inline T *__copy_backward_t(const T *first, const T *last, const T *dest, __false_type)
{
return __copy_backward_d(first, last, dest, (ptrdiff_t*)(0));
}
// dispatch function tenplate, used for pointer
template<typename T>
struct __copy_backward_dispatch<T*, T*>
{
inline T* operator () (T *first, T *last, T *dest)
{
typename __type_traits<T>::has_trivial_assignment_operator t;
return __copy_backward_t(first, last, dest, t);
}
};
template<typename T>
struct __copy_backward_dispatch<const T*, T*>
{
inline T* operator () (const T *first, T *last, T *dest)
{
typename __type_traits<T>::has_trivial_assignment_operator t;
return __copy_backward_t(first, last, dest, t);
}
};
// common interface
template<typename BidirectionalIterator1, typename BidirectionalIterator2>
inline BidirectionalIterator2 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 dest)
{
return __copy_backward_dispatch<BidirectionalIterator1, BidirectionalIterator2>()(first, last, dest);
}
__ML_END_NAMESPACE
#endif // __ML_INTERNAL_ALGOBASE_H