-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator.h
290 lines (237 loc) · 8.91 KB
/
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
#pragma once
#include <cstddef>
namespace mystl {
//five iterator type
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 {};
//iterator class tempalte
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;
};
//iterator_traits(tool)
template <class T>
struct iterator_traits {
typedef typename T::iterator_category iterator_category;
typedef typename T::value_type value_type;
typedef typename T::difference_type difference_type;
typedef typename T::pointer pointer;
typedef typename T::reference reference;
};
//template partial specialization, deal with native pointers
template <class T>
struct iterator_traits<T *> {
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef random_access_iterator_tag iterator_category;
typedef T* pointer;
typedef T& refernece;
};
template <class T>
struct iterator_traits<const T *> {
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef random_access_iterator_tag iterator_category;
typedef const T* pointer;
typedef const T& refernece;
};
//global function template:extraction of iterator_category
template <class Iterator>
typename iterator_traits<Iterator>::iterator_category
iterator_category(const Iterator &) {
typedef typename iterator_traits<Iterator>::iterator_category category;
return category();
}
//global function template:extraction of value_type
template <class Iterator>
typename iterator_traits<Iterator>::value_type *
value_type(const Iterator &) {
return static_cast<typename iterator_traits<Iterator>::value_type*>(0);
}
//global function template:extraction of differnece_type
template <class Iterator>
typename iterator_traits<Iterator>::difference_type *
difference_type(const Iterator &) {
return static_cast<typename iterator_traits<Iterator>::difference_type*>(0);
}
//assist function of <iterator>
//distance(): calculate the diatance of two iterator
template <class InputIterator>
typename iterator_traits<InputIterator>::difference_type
_distance(InputIterator first, InputIterator last, input_iterator_tag) {
typename iterator_traits<InputIterator>::difference_type n = 0;
while (first != last) {
++first;
++n;
}
return n;
}
template <class RandomAccessIterator>
typename iterator_traits<RandomAccessIterator>::difference_type
_distance(RandomAccessIterator first, RandomAccessIterator last, random_access_iterator_tag) {
return last - first;
}
template <class Iterator>
typename iterator_traits<Iterator>::difference_type
distance(Iterator first, Iterator last) {
typedef typename iterator_traits<Iterator>::iterator_category category;
return _distance(first, last, category());
}
//advance() : Forward or Backward of iterator
template <class InputIterator, class Distance>
void _advance(InputIterator& i, Distance n, input_iterator_tag) {
while (n--) ++i;
}
template <class BidirectionalIterator, class Distance>
void _advance(BidirectionalIterator& i, Distance n, bidirectional_iterator_tag) {
if (n >= 0) {
while (n--) ++i;
}
else
while (n++) --i;
}
template <class RandomAccessIterator, class Distance>
void _advance(RandomAccessIterator& i, Distance n, random_access_iterator_tag) {
i += n;
}
template <class Iterator, class Distance>
void advance(Iterator& i, Distance n) {
_advance(i, n, iterator_category(i));
}
//next() : forward of iterator
template<class ForwardIterator>
typename iterator_traits<ForwardIterator>::difference_type
_next(ForwardIterator& i, forward_iterator_tag) {
return i += 1;
return i;
}
template <class ForwardIterator, class Distance>
typename iterator_traits<ForwardIterator>::difference_type
_next(ForwardIterator& i, Distance n, forward_iterator_tag) {
_advance(i, n, iterator_category(i));
return i;
}
template<class BidirectionalIterator>
typename iterator_traits<BidirectionalIterator>::difference_type
_next(BidirectionalIterator& i, bidirectional_iterator_tag) {
i += 1;
return i;
}
template <class BidirectionalIterator, class Distance>
typename iterator_traits<BidirectionalIterator>::difference_type
_next(BidirectionalIterator& i, Distance n, bidirectional_iterator_tag) {
_advance(i, n, bidirectional_iterator_tag);
return i;
}
template<class RandomAccessIteratr>
typename iterator_traits<RandomAccessIteratr>::difference_type
_next(RandomAccessIteratr& i, random_access_iterator_tag) {
i += 1;
return i;
}
template <class RandomAccessIteratr, class Distance>
typename iterator_traits<RandomAccessIteratr>::difference_type
_next(RandomAccessIteratr& i, Distance n, random_access_iterator_tag) {
_advance(i, n, random_access_iterator_tag);
return i;
}
template <class Iterator>
typename iterator_traits<Iterator>::difference_type
next(Iterator& i) {
_next(i, iterator_category(i));
}
template <class Iterator, class Distance>
typename iterator_traits<Iterator>::difference
next(Iterator& i, Distance n) {
_next(i, n, iterator_category(i));
}
//prev() not be realized
/**************************************************************
*Realize Iterator Adapter :(adapter of noramal iterator)
*reverse iterator && inserter iterator && stream iterator && move iterator
***************************************************************/
template <class Iterator>
class reverse_iterator {
protected:
Iterator current;
public:
typedef typename iterator_traits<Iterator>::iterator_category iterator_category;
typedef typename iterator_traits<Iterator>::value_type value_type;
typedef typename iterator_traits<Iterator>::difference_type difference_type;
typedef typename iterator_traits<Iterator>::pointer pointer;
typedef typename iterator_traits<Iterator>::reference reference;
typedef Iterator iterator_type; //represent the normal iterator
typedef reverse_iterator<Iterator> self; //represent the reverse iterator
public:
explicit reverse_iterator(iterator_type i) :current(i) {}
reverse_iterator(const self& rhs) :current(rhs.current) {}
iterator_type base() const { return current; }
reference operator*() const { Iterator tmp = current; return *--tmp } //*(current - 1 ).value == reverse_iterator.value
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; }
self operator+(difference_type n) { return self(current - n); }
self operator-(difference_type n) { return self(current + n); }
reference operator[](difference_type n) { return *(*this + n); } //this is a pointer of reverse_iterator
};
//overload of operator-
template <class Iterator>
typename reverse_iterator<Iterator>::difference_type
operator-(const reverse_iterator<Iterator>& lhs,
const reverse_iterator<Iterator>& rhs)
{
return rhs.base() - lhs.base();
}
//overload of operator==
template <class Iterator>
bool operator==(const reverse_iterator<Iterator>& lhs,
const reverse_iterator<Iterator>& rhs)
{
return lhs.base() == rhs.base();
}
//overload of operator<
template <class Iterator>
bool operator<(const reverse_iterator<Iterator>& lhs,
const reverse_iterator<Iterator>& rhs)
{
return rhs.base() < lhs.base();
}
//overload of operator!=
template <class Iterator>
bool operator!=(const reverse_iterator<Iterator>& lhs,
const reverse_iterator<Iterator>& rhs)
{
return !(lhs == rhs);
}
//overload of operator>
template <class Iterator>
bool operator>(const reverse_iterator<Iterator>& lhs,
const reverse_iterator<Iterator>& rhs)
{
return rhs < lhs;
}
//overload of operator<=
template <class Iterator>
bool operator<=(const reverse_iterator<Iterator>& lhs,
const reverse_iterator<Iterator>& rhs)
{
return !(rhs < lhs);
}
//overload of operator>=
template <class Iterator>
bool operator>=(const reverse_iterator<Iterator>& lhs,
const reverse_iterator<Iterator>& rhs)
{
return !(lhs < rhs);
}
}