-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterator.h
More file actions
463 lines (426 loc) · 11 KB
/
Copy pathIterator.h
File metadata and controls
463 lines (426 loc) · 11 KB
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
//#pragma once
//
////
//// 迭代器的型别
////
//struct InputIteratorTag {};
//struct OutputIteratorTag {};
//struct ForwardIteratorTag : public InputIteratorTag {};
//struct BidirectionalIteratorTag : public ForwardIteratorTag {};
//struct RandomAccessIteratorTag : public BidirectionalIteratorTag {};
//
////
//// Traits 就像一台“特性萃取机”,榨取各个迭代器的特性(对应的型别)
////
//template <class Iterator>
//struct IteratorTraits
//{
// typedef typename Iterator:: IteratorCategory IteratorCategory ;
// typedef typename Iterator:: ValueType ValueType;
// typedef typename Iterator:: DifferenceType DifferenceType ;
// typedef typename Iterator:: Pointer Pointer;
// typedef typename Iterator:: Reference Reference;
//};
//
////
//// 偏特化原生指针类型
////
//template <class T>
//struct IteratorTraits< T*>
//{
// typedef RandomAccessIteratorTag IteratorCategory ;
// typedef T ValueType;
// typedef ptrdiff_t DifferenceType ;
// typedef T * Pointer;
// typedef T & Reference;
//};
//
////
//// 偏特化const原生指针类型
////
//template <class T>
//struct IteratorTraits< const T *>
//{
// typedef RandomAccessIteratorTag IteratorCategory ;
// typedef T ValueType;
// typedef ptrdiff_t DifferenceType ;
// typedef const T* Pointer;
// typedef const T& Reference;
//};
//
//
//
//// O(1)
//template <class InputIterator>
//inline size_t _Distance (InputIterator first, InputIterator last, RandomAccessIteratorTag)
//{
// return last-first;
//}
//
//// O(N)
//template <class InputIterator>
//inline size_t _Distance (InputIterator first, InputIterator last, InputIteratorTag)
//{
// size_t n = 0;
// while (first != last)
// {
// n++;
// first++;
// }
//
// return n;
//}
//
//
//template <class InputIterator>
//inline size_t Distance (InputIterator first, InputIterator last)
//{
// return _Distance(first, last, IteratorTraits<InputIterator>::IteratorCategory());
//}
//
//template <class InputIterator, class Distance>
//inline void __Advance(InputIterator & i, Distance n, ForwardIteratorTag)
//{
// while (n--)
// {
// ++i;
// }
//}
//
//template <class InputIterator, class Distance>
//inline void __Advance(InputIterator & i, Distance n, BidirectionalIteratorTag)
//{
// if (n > 0)
// {
// while (n--)
// ++i;
// }
// else
// {
// while (n++)
// --i;
// }
//}
//
//template <class InputIterator, class Distance>
//inline void __Advance(InputIterator & i, Distance n, RandomAccessIteratorTag)
//{
// i += n;
//}
//
//template <class InputIterator, class Distance>
//inline void Advance(InputIterator & i, Distance n)
//{
// __Advance(i, n, IteratorTraits <InputIterator>:: IteratorCategory());
//}
//
//
/////////////////////////////////////////////////////////////////
//// 反向迭代器
//
//template <class Iterator>
//class ReverseIterator
//{
//public:
// // 通过迭代器萃取器,萃取出正向迭代器中定义的基本类型
// typedef typename IteratorTraits<Iterator>::IteratorCategory
// IteratorCategory;
// typedef typename IteratorTraits<Iterator>::ValueType
// ValueType;
// typedef typename IteratorTraits<Iterator>::DifferenceType
// DifferenceType;
// typedef typename IteratorTraits<Iterator>::Pointer
// Pointer;
// typedef typename IteratorTraits<Iterator>::Reference
// Reference;
//
// Iterator _current;
//
// typedef ReverseIterator<Iterator> Self;
//
// ReverseIterator(){};
//
// // ?
// explicit ReverseIterator(const Iterator& it)
// :_current(it)
// {}
//
// Reference operator*() const
// {
// Iterator tmp = _current;
// return *--tmp;
// }
//
// Pointer operator->()const
// {
// return &(operator*());
// }
//
// Self& operator++()
// {
// --_current;
// return *this;
// }
//
// Self operator++(int)
// {
// Self tmp = *this;
// --_current;
// return tmp;
// }
//
// Self& operator--()
// {
// ++_current;
// return *this;
// }
//
// Self operator--(int)
// {
// Self tmp = *this;
// ++_current;
// return tmp;
// }
//
// bool operator==(const Self& s) const
// {
// return _current == s._current;
// }
//
// bool operator!=(const Self& s) const
// {
// return _current != s._current;
// }
//};
//
////ReverseIterator rIt = ReverseIterator(it);
//
//
#pragma once
//
// 迭代器的型别
//
struct InputIteratorTag {};
struct OutputIteratorTag {};
struct ForwardIteratorTag : public InputIteratorTag {};
struct BidirectionalIteratorTag : public ForwardIteratorTag {};
struct RandomAccessIteratorTag : public BidirectionalIteratorTag {};
template <class T, class Distance>
struct InputIterator {
typedef InputIteratorTag IteratorCategory;
typedef T ValueType;
typedef Distance DifferenceType;
typedef T* Pointer;
typedef T& Reference;
};
struct OutputIterator {
typedef OutputIteratorTag IteratorCategory;
typedef void ValueType;
typedef void DifferenceType;
typedef void Pointer;
typedef void Reference;
};
template <class T, class Distance>
struct ForwardIterator {
typedef ForwardIteratorTag IteratorCategory;
typedef T ValueType;
typedef Distance DifferenceType;
typedef T* Pointer;
typedef T& Reference;
};
template <class T, class Distance>
struct BidirectionalIterator {
typedef BidirectionalIteratorTag IteratorCategory;
typedef T ValueType;
typedef Distance DifferenceType;
typedef T* Pointer;
typedef T& Reference;
};
template <class T, class Distance>
struct RandomAccessIterator {
typedef RandomAccessIteratorTag IteratorCategory;
typedef T ValueType;
typedef Distance DifferenceType;
typedef T* Pointer;
typedef T& Reference;
};
//
// 迭代器内嵌包含的5种相应的型别
// Iterator Category、Value Type、Difference Type、Pointer、Reference
// 这5种内嵌的型别定义,确保了能够更方便的跟STL融合。
// 且方便Iterator Traits的类型萃取
//
template <class Category, class T, class Distance = ptrdiff_t,
class Pointer = T*, class Reference = T&>
struct Iterator
{
typedef Category IteratorCategory; // 迭代器类型
typedef T ValueType; // 迭代器所指对象类型
typedef Distance DifferenceType; // 两个迭代器之间的距离
typedef Pointer Pointer; // 迭代器所指对象类型的指针
typedef Reference Reference; // 迭代器所指对象类型的引用
};
//
// Traits就像一台“特性萃取机”,榨取各个迭代器的特性(对应的型别)
//
template <class Iterator>
struct IteratorTraits
{
typedef typename Iterator::IteratorCategory IteratorCategory;
typedef typename Iterator::ValueType ValueType;
typedef typename Iterator::DifferenceType DifferenceType;
typedef typename Iterator::Pointer Pointer;
typedef typename Iterator::Reference Reference;
};
//
// 偏特化原生指针类型
//
template <class T>
struct IteratorTraits<T*>
{
typedef RandomAccessIteratorTag IteratorCategory;
typedef T ValueType;
typedef ptrdiff_t DifferenceType;
typedef T* Pointer;
typedef T& Reference;
};
//
// 偏特化const原生指针类型
//
template <class T>
struct IteratorTraits<const T*>
{
typedef RandomAccessIteratorTag IteratorCategory;
typedef T ValueType;
typedef ptrdiff_t DifferenceType;
typedef const T* Pointer;
typedef const T& Reference;
};
//////////////////////////////////////////////////////////////
// Distance的实现
template <class InputIterator>
inline typename IteratorTraits<InputIterator>::DifferenceType
__Distance(InputIterator first, InputIterator last, InputIteratorTag)
{
IteratorTraits<InputIterator>::DifferenceType n = 0;
while (first != last) {
++first; ++n;
}
return n;
}
template <class RandomAccessIterator>
inline typename IteratorTraits<RandomAccessIterator>::DifferenceType
__Distance(RandomAccessIterator first, RandomAccessIterator last,
RandomAccessIteratorTag)
{
return last - first;
}
template <class InputIterator>
inline typename IteratorTraits<InputIterator>::DifferenceType
Distance(InputIterator first, InputIterator last)
{
return __Distance(first, last, IteratorTraits<InputIterator>::IteratorCategory());
}
///////////////////////////////////////////////////////////
// Advance的实现
template <class InputIterator, class Distance>
inline void __Advance(InputIterator& i, Distance n, InputIteratorTag)
{
while (n--) ++i;
}
template <class BidirectionalIterator, class Distance>
inline void __Advance(BidirectionalIterator& i, Distance n,
BidirectionalIteratorTag)
{
if (n >= 0)
while (n--) ++i;
else
while (n++) --i;
}
template <class RandomAccessIterator, class Distance>
inline void __Advance(RandomAccessIterator& i, Distance n,
RandomAccessIteratorTag)
{
i += n;
}
template <class InputIterator, class Distance>
inline void Advance(InputIterator& i, Distance n)
{
__Advance(i, n, IteratorTraits<InputIterator>::IteratorCategory());
}
//////////////////////////////////////////////////////////////////////
//
template <class T, class Distance>
inline T* ValueType(const InputIterator<T, Distance>&) {
return (T*)(0);
}
template <class T, class Distance>
inline T* ValueType(const ForwardIterator<T, Distance>&) {
return (T*)(0);
}
template <class T, class Distance>
inline T* ValueType(const BidirectionalIterator<T, Distance>&) {
return (T*)(0);
}
template <class T, class Distance>
inline T* ValueType(const RandomAccessIterator<T, Distance>&) {
return (T*)(0);
}
template <class T>
inline T* ValueType(const T*) { return (T*)(0); }
//////////////////////////////////////////////////////////////////////
// 逆置迭代器的定义,反向迭代器是正向迭代一层封装
template <class Iterator>
class ReverseIterator
{
public:
// 通过迭代器萃取器,萃取出正向迭代器中定义的基本类型
typedef typename IteratorTraits<Iterator>::IteratorCategory
IteratorCategory;
typedef typename IteratorTraits<Iterator>::ValueType
ValueType;
typedef typename IteratorTraits<Iterator>::DifferenceType
DifferenceType;
typedef typename IteratorTraits<Iterator>::Pointer
Pointer;
typedef typename IteratorTraits<Iterator>::Reference
Reference;
typedef Iterator IteratorType;
typedef ReverseIterator<Iterator> Self;
public:
ReverseIterator() {}
explicit ReverseIterator(IteratorType x)
: _current(x) {}
Reference operator*() const
{
// 注意这里解引用时取的是当前位置的前一个数据。
// 以为RBegin()==End() REnd()==Begin()
Iterator tmp = _current;
return *--tmp;
}
Pointer operator->() const
{ return &(operator*()); }
Self& operator++() {
--_current;
return *this;
}
Self operator++(int) {
Self tmp = *this;
--_current;
return tmp;
}
Self& operator--() {
++_current;
return *this;
}
Self operator--(int) {
Self tmp = *this;
++_current;
return tmp;
}
bool operator != (const Self& x) {
return _current != x._current;
}
protected:
Iterator _current;
};