forked from czHappy/MiniSTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstl_iterator.h
426 lines (351 loc) · 12.8 KB
/
stl_iterator.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#pragma once
#include <cstddef> //ptrdiff_t定义
#include <iostream>
namespace MiniSTL {
//五种迭代器类型
struct input_iterator_tag {};
struct output_iterator_tag {};
struct forward_iterator_tag : public input_iterator_tag {};
struct bidirectional_iterator_tag : public forward_iterator_tag {};
struct random_access_iterator_tag : public bidirectional_iterator_tag {};
//为了避免遗忘记录traits,此基类以供自定义iterator继承之
template <class Category, class T, class Distance = ptrdiff_t,
class Pointer = T *, class Reference = T &>
struct iterator {
typedef Category iterator_category ;
typedef T value_type ;
typedef Distance difference_type ;
typedef Pointer pointer;
typedef Reference reference;
};
// traits 展现Iterator所有特性
template <class Iterator>
struct iterator_traits {
typedef typename Iterator::iterator_category iterator_category;
typedef typename Iterator::value_type value_type;
typedef typename Iterator::difference_type difference_type;
typedef typename Iterator::pointer pointer;
typedef typename Iterator::reference reference;
};
//针对raw pointer设计的偏特化版本
template <class T>
struct iterator_traits<T *> {
using iterator_category = random_access_iterator_tag;
using value_type = T;
using difference_type = ptrdiff_t;
using pointer = T *;
using reference = T &;
};
//针对pointer-to-const设计的偏特化版本
template <class T>
struct iterator_traits<const T *> {
using iterator_category = random_access_iterator_tag;
using value_type = T;
using difference_type = ptrdiff_t;
using pointer = const T *;
using reference = const T &;
};
//书上这里写了value_type(const Iterator& )函数 返回的是value_type
// 感觉也没什么用 就不写了
//以下是别名 省的写好长的类型名
template <class Iterator>
using iterator_category_t =
typename iterator_traits<Iterator>::iterator_category;
template <class Iterator>
using value_type_t = typename iterator_traits<Iterator>::value_type;
template <class Iterator>
using difference_type_t = typename iterator_traits<Iterator>::difference_type;
template <class Iterator>
using pointer_t = typename iterator_traits<Iterator>::pointer;
template <class Iterator>
using reference_t = typename iterator_traits<Iterator>::reference;
//以下为整组distance函数
template <class InputIterator>
inline difference_type_t<InputIterator> __distance(InputIterator first,
InputIterator last,
input_iterator_tag) {
difference_type_t<InputIterator> n = 0;
while (first != last) ++first, ++n;
return n;
}
// 随机迭代器 可以直接减求距离
template <class InputIterator>
inline difference_type_t<InputIterator> __distance(InputIterator first,
InputIterator last,
random_access_iterator_tag) {
return last - first;
}
// 利用函数重载 编译期就知道该调用哪个具体函数 根据tag
template <class InputIterator>
inline difference_type_t<InputIterator> distance(InputIterator first,
InputIterator last) {
return __distance(first, last, iterator_category_t<InputIterator>());
}
//以下为整组advance函数
template <class InputIterator, class Distance>
inline void __advance(InputIterator &i, Distance n, input_iterator_tag) {
while (n--) ++i;
}
template <class InputIterator, class Distance>
inline void __advance(InputIterator &i, Distance n,
bidirectional_iterator_tag) {
if (n >= 0)
while (n--) ++i;
else
while (n++) --i;
}
template <class InputIterator, class Distance>
inline void __advance(InputIterator &i, Distance n,
random_access_iterator_tag) {
i += n;
}
template <class InputIterator, class Distance>
inline void advance(InputIterator &i, Distance n) {
__advance(i, n, iterator_category_t<InputIterator>());
}
//以下为三种迭代器适配器
// insert
//reverse
// stream
// insert:back_insert,fornt_insert,insert
template <class Container>
class back_insert_iterator {
protected:
Container *container; //底层容器
public:
using iterator_category = output_iterator_tag;
using value_tyep = void;
using difference_type = void;
using pointer = void;
using reference = void;
public:
explicit back_insert_iterator(Container &value) : container(value) {}
back_insert_iterator &operator=(
const typename Container::value_type &value) {
container->push_back(value); //本质上是调用了push_back
return *this;
}
//以下三个接口对back_insert_iterator无用,关闭功能(为什么不直接设为private?)
back_insert_iterator operator*() { return *this; }
back_insert_iterator operator++() { return *this; }
back_insert_iterator operator++(int) { return *this; }
};
//以下是一个辅助函数,便于快速使用back_insert_iterator
template <class Container>
inline back_insert_iterator<Container> back_inserter(Container &x) {
return back_insert_iterator<Container>(x);
}
// front_insert_iterator无法用于vector,因为后者不具备push_front
template <class Container>
class front_insert_iterator {
protected:
Container *container; //底层容器
public:
using iterator_category = output_iterator_tag;
using value_tyep = void;
using difference_type = void;
using pointer = void;
using reference = void;
public:
explicit front_insert_iterator(Container &value) : container(value) {}
front_insert_iterator &operator=(
const typename Container::value_type &value) {
container->push_front(value);
return *this;
}
//以下三个接口对back_insert_iterator无用,关闭功能(为什么不直接设为private?)
front_insert_iterator operator*() { return *this; }
front_insert_iterator operator++() { return *this; }
front_insert_iterator operator++(int) { return *this; }
};
template <class Container>
inline front_insert_iterator<Container> front_inserter(Container &x) {
return front_insert_iterator<Container>(x);
}
template <class Container>
class insert_iterator {
protected:
Container *container; //底层容器
typename Container::iterator iter;
public:
using iterator_category = output_iterator_tag;
using value_tyep = void;
using difference_type = void;
using pointer = void;
using reference = void;
public:
insert_iterator(Container &value, typename Container::iterator i)
: container(value), iter(i) {}
insert_iterator &operator=(const typename Container::value_type &value) {
container->insert(iter, value); //调用insert
++iter; //保证insert_iterator永远与目标贴合
return *this;
}
//以下三个接口对back_insert_iterator无用,关闭功能(为什么不直接设为private?)
insert_iterator operator*() { return *this; }
insert_iterator operator++() { return *this; }
insert_iterator operator++(int) { return *this; }
};
template <class Container, class Iterator>
inline insert_iterator<Container> inserter(Container &x, Iterator i) {
return insert_iterator<Container>(x, i);
}
template <class Iterator>
class __reverse_iterator {
template <class It>
friend bool operator==(const __reverse_iterator<It> &,
const __reverse_iterator<It> &);
template <class It>
friend bool operator!=(const __reverse_iterator<It> &,
const __reverse_iterator<It> &);
protected:
Iterator current; //与之对应的正向迭代器
public:
using iterator_category = iterator_category_t<Iterator>;
using value_type = value_type_t<Iterator>;
using difference_type = difference_type_t<Iterator>;
using pointer = pointer_t<Iterator>;
using reference = reference_t<Iterator>;
using iterator_type = Iterator; //正向迭代器
using self = __reverse_iterator; //反向迭代器
public:
__reverse_iterator() {}
explicit __reverse_iterator(iterator_type value) : current(value) {}
__reverse_iterator(const self &value) : current(value.current) {}
iterator_type base() const { return current; }
reference operator*() const {
Iterator temp = current;
return *--temp; //对逆向迭代器的取值等价于对应的正向迭代器后退一个单位取值
}
pointer operator->() const { return &(operator*()); }
self &operator++() {
--current;
return *this;
}
self operator++(int) {
self temp = *this;
--current;
return temp;
}
self &operator--() {
++current;
return *this;
}
self operator--(int) {
self temp = *this;
++current;
return temp;
}
self operator+(difference_type n) const { return self(current - n); }
self &operator+=(difference_type n) const {
current -= n;
return *this;
}
self operator-(difference_type n) const { return self(current + n); }
self &operator-=(difference_type n) const {
current += n;
return *this;
}
reference operator[](difference_type n) const { return *(*this + n); }
bool operator==(const self &rhs) const { return current == rhs.current; }
bool operator!=(const self &rhs) const { return !((*this) == rhs); }
};
template <class Iterator>
inline bool operator==(const __reverse_iterator<Iterator> &lhs,
const __reverse_iterator<Iterator> &rhs) {
return lhs.operator==(rhs);
}
template <class Iterator>
inline bool operator!=(const __reverse_iterator<Iterator> &lhs,
const __reverse_iterator<Iterator> &rhs) {
return !(lhs == rhs);
}
template <class Iterator>
inline bool operator<(const __reverse_iterator<Iterator> &lhs,
const __reverse_iterator<Iterator> &rhs) {
return rhs.base() < lhs.base();
}
template <class Iterator>
inline bool operator>(const __reverse_iterator<Iterator> &lhs,
const __reverse_iterator<Iterator> &rhs) {
return rhs < lhs;
}
template <class Iterator>
inline bool operator<=(const __reverse_iterator<Iterator> &lhs,
const __reverse_iterator<Iterator> &rhs) {
return !(rhs < lhs);
}
template <class Iterator>
inline bool operator>=(const __reverse_iterator<Iterator> &lhs,
const __reverse_iterator<Iterator> &rhs) {
return !(lhs < rhs);
}
template <class Iterator>
inline typename __reverse_iterator<Iterator>::difference_type operator-(
const __reverse_iterator<Iterator> &lhs,
const __reverse_iterator<Iterator> &rhs) {
return rhs.base() - lhs.base();
}
template <class Iterator>
inline __reverse_iterator<Iterator> operator+(
typename __reverse_iterator<Iterator>::difference_type n,
const __reverse_iterator<Iterator> &x) {
return __reverse_iterator<Iterator>(x.base() - n);
}
// stream:input_stream,output_stream
template <class T, class Distance = ptrdiff_t>
class istream_iterator {
protected:
std::istream *stream;
T value;
bool end_marker;
void read() {
end_marker = (*stream) ? true : false;
if (end_marker) *stream >> value;
//完成输入后,stream状态可能发生了改变,再次判定
end_marker = (*stream) ? true : false;
}
public:
using iterator_category = input_iterator_tag;
using value_tyep = T;
using difference_type = Distance;
using pointer = const T *; //由于身为input_iterator,采用const较为保险
using reference = const T &;
istream_iterator() : stream(&std::cin), end_marker(false) {}
istream_iterator(std::istream &s) : stream(&s) { read(); }
reference operator*() const { return value; }
pointer operator->() const { return &(operator*()); }
istream_iterator &operator++() {
read();
return *this;
}
istream_iterator operator++(int) {
istream_iterator temp = *this;
read();
return temp;
}
};
template <class T>
class ostream_iterator {
protected:
std::ostream *stream;
const char *interval; //输出间隔符
public:
using iterator_category = output_iterator_tag;
using value_tyep = void;
using difference_type = void;
using pointer = void;
using reference = void;
ostream_iterator() : stream(&std::cout), interval(nullptr) {}
ostream_iterator(std::ostream &s, const char *c)
: stream(&s), interval(c) {}
ostream_iterator &operator=(const T &value) {
*stream << value;
if (interval) *stream << interval;
return *this;
}
ostream_iterator &operator*() { return *this; }
ostream_iterator &operator++() { return *this; }
ostream_iterator operator++(int) { return *this; }
};
} // namespace MiniSTL