forked from czHappy/MiniSTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstl_algobase.h
285 lines (249 loc) · 10.3 KB
/
stl_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
#pragma once
#include <string.h> // memcmp
#include <cstddef>
#include "stl_function.h"
#include "stl_iterator.h"
#include "typeTraits.h"
namespace MiniSTL {
// 不能保证长度相等
template <class InputIterator1, class InputIterator2>
inline bool equal(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2) {
for (; first1 != last1; ++first1, ++first2)
if (*first1 != *first2) return false;
return true;
}
// 接受自定义比较器
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
inline bool equal(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, BinaryPredicate binary_pred) {
for (; first1 != last1; ++first1, ++first2)
if (!binary_pred(*first1, *first2)) return false;
return true;
}
template <class ForwardIterator, class T>
void fill(ForwardIterator first, ForwardIterator last, const T &value) {
for (; first != last; ++first) *first = value;
}
// 显然,本算法执行覆写操作,因此通常配合inserter完成
template <class OutputIterator, class Size, class T>
OutputIterator fill_n(OutputIterator first, Size n, const T &value) {
for (; n > 0; --n, ++first) *first = value;
return first;
}
template <class ForwardIterator1, class ForwardIterator2>
inline void iter_swap(ForwardIterator1 a, ForwardIterator2 b) {
return __iter_swap(a, b, value_type_t<ForwardIterator1>());
}
// 必须要知道迭代器指向的对象类型,才能够构造对象,因此本处使用了value_type
template <class ForwardIterator1, class ForwardIterator2, class T>
inline void iter_swap(ForwardIterator1 a, ForwardIterator2 b, T) {
T temp = *a;
*a = *b;
*b = temp;
}
template <class InputIterator1, class InputIterator2>
bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2) {
for (; first1 != last1 && first2 != last2; ++first1, ++first2) {
if (*first1 < *first2)
return true;
else if (*first1 > *first2)
return false;
}
return first1 == last1 &&
first2 != last2; // 若第二序列有余,返回true,否则false
}
template <class T>
inline const T &max(const T &a, const T &b) {
return a < b ? b : a;
}
template <class T, class Compare>
inline const T &max(const T &a, const T &b, Compare comp) {
return comp(a, b) ? b : a;
}
template <class T>
inline const T &min(const T &a, const T &b) {
return b < a ? b : a;
}
template <class T, class Compare>
inline const T &min(const T &a, const T &b, Compare comp) {
return comp(b, a) ? b : a;
}
template <class InputIterator1, class InputIterator2, class Compare>
bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
Compare comp) {
for (; first1 != last1 && first2 != last2; ++first1, ++first2) {
if (comp(*first1, *first2))
return true;
else if (comp(*first2, *first1))
return false;
}
return first1 == last1 &&
first2 != last2; // 若第二序列有余,返回true,否则false
}
// 针对原始指针const unsigned char*的全特化版本
inline bool lexicographical_compare(const unsigned char *first1,
const unsigned char *last1,
const unsigned char *first2,
const unsigned char *last2) {
const size_t len1 = last1 - first1;
const size_t len2 = last2 - first2;
// 先比较长度相同的段落
const int result = memcmp(first1, first2, min(len1, len2));
return result != 0 ? result < 0 : len1 < len2;
}
// 显然要求序列1长于序列2
template <class InputIterator1, class InputIterator2>
pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1,
InputIterator1 last1,
InputIterator2 first2,
InputIterator2 last2) {
while (first1 != last1 && *first1 == *first2) {
++first1, ++first2;
}
return pair<InputIterator1, InputIterator2>(first1, first2);
}
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1,
InputIterator1 last1,
InputIterator2 first2,
InputIterator2 last2,
BinaryPredicate binary_pred) {
while (first1 != last1 && binary_pred(*first1, *first2)) {
++first1, ++first2;
}
return pair<InputIterator1, InputIterator2>(first1, first2);
}
template <class T>
inline void swap(T &a, T &b) {
T temp = a;
a = b;
b = temp;
}
template <class InputIterator, class OutputIterator>
struct __copy_dispatch { // 仿函数对象
OutputIterator operator()(InputIterator first, InputIterator last,
OutputIterator result) {
return __copy(first, last, result,
iterator_category_t<InputIterator>());
}
};
// 偏特化处理
template <class T>
struct __copy_dispatch<T *, T *> {
T *operator()(T *first, T *last, T *result) {
using t = typename _type_traits<T>::has_trivial_assignment_operator;
return __copy_t(first, last, result, t());
}
};
// 偏特化处理
template <class T>
struct __copy_dispatch<const T *, T *> {
T *operator()(const T *first, const T *last, T *result) {
using t = typename _type_traits<T>::has_trivial_assignment_operator;
return __copy_t(first, last, result, t());
}
};
template <class InputIterator, class OutputIterator>
inline OutputIterator copy(InputIterator first, InputIterator last,
OutputIterator result) {
return __copy_dispatch<InputIterator, OutputIterator>()(
first, last, result); // __copy_dispatch是一个仿函数对象
}
// 针对指针的偏特化
inline char *copy(const char *first, const char *last, char *result) {
memmove(result, first, last - first);
return result + (last - first);
}
inline wchar_t *copy(const wchar_t *first, const wchar_t *last,
wchar_t *result) {
memmove(result, first, sizeof(wchar_t) * (last - first));
return result + (last - first);
}
// InputIterator
template <class InputIterator, class OutputIterator>
inline OutputIterator __copy(InputIterator first, InputIterator last,
OutputIterator result, input_iterator_tag) {
for (; first != last; ++first, ++result) // 迭代器判同,速度较慢
*result = *first;
return result;
}
// RandomIterator
template <class InputIterator, class OutputIterator>
inline OutputIterator __copy(InputIterator first, InputIterator last,
OutputIterator result,
random_access_iterator_tag) {
return __copy_d(first, last, result,
difference_type_t<InputIterator>()); // 再细分函数以便复用
}
template <class InputIterator, class OutputIterator, class Distance>
inline OutputIterator __copy_d(InputIterator first, InputIterator last,
OutputIterator result, Distance) {
for (Distance n = last - first; n > 0;
--n, ++first, ++result) // 以n决定循环次数,速度较快
*result = *first;
return result;
}
// 具备trivial copy assignment operator,可执行memmove
template <class T>
inline T *__copy_t(const T *first, const T *last, T *result, _true_type) {
memmove(result, first, sizeof(T) * (last - first));
return result + (last - first);
}
// 原始指针是一种random_access_iterator
template <class T>
inline T *__copy_t(const T *first, const T *last, T *result, _false_type) {
return __copy_d(first, last, result, ptrdiff_t());
}
template <class BidirectionalIter1, class BidirectionalIter2, class Distance>
inline BidirectionalIter2 __copy_backward(BidirectionalIter1 first,
BidirectionalIter1 last,
BidirectionalIter2 result,
bidirectional_iterator_tag,
Distance) {
while (first != last) *--result = *--last;
return result;
}
template <class RandomAccessIter, class BidirectionalIter2, class Distance>
inline BidirectionalIter2 __copy_backward(RandomAccessIter first,
RandomAccessIter last,
BidirectionalIter2 result,
random_access_iterator_tag,
Distance) {
for (Distance n = last - first; n > 0; --n) *--result = *--last;
return result;
}
template <class BidirectionalIter1, class BidirectionalIter2, class BoolType>
struct __copy_backward_dispatch {
BidirectionalIter2 operator()(BidirectionalIter1 first,
BidirectionalIter1 last,
BidirectionalIter2 result) {
return __copy_backward(first, last, result,
iterator_category_t<BidirectionalIter1>(),
difference_type_t<BidirectionalIter1>());
}
};
template <class T>
struct __copy_backward_dispatch<T *, T *, _true_type> {
T *operator()(const T *first, const T *last, T *result) {
const ptrdiff_t n = last - first;
memmove(result - n, first, sizeof(T) * n);
return result - n;
}
};
template <class T>
struct __copy_backward_dispatch<const T *, T *, _true_type> {
T *operator()(const T *first, const T *last, T *result) {
return __copy_backward_dispatch<T *, T *, _true_type>()(first, last,
result);
}
};
template <class BI1, class BI2>
inline BI2 copy_backward(BI1 first, BI1 last, BI2 result) {
using Trivial = typename _type_traits<
value_type_t<BI2>>::has_trivial_assignment_operator;
return __copy_backward_dispatch<BI1, BI2, Trivial>()(first, last, result);
}
} // namespace MiniSTL