-
Notifications
You must be signed in to change notification settings - Fork 1
/
iterator.hpp
346 lines (286 loc) · 10.8 KB
/
iterator.hpp
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
#pragma once
#include "utils.hpp"
namespace ft {
/*
* normal_iterator passes a pointer to type T (type T is passed by vector).
* reverse_iterator passes normal_iterator<pointer> (as passed by vector).
*/
template <typename 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;
};
/*
* normal_iterator passes a pointer to type T (type T is passed by vector).
*/
template <typename Tp>
struct iterator_traits<Tp*> {
typedef std::random_access_iterator_tag iterator_category;
typedef Tp value_type;
typedef std::ptrdiff_t difference_type;
typedef Tp* pointer;
typedef Tp& reference;
};
/*
* Gets used when a normal_iterator is created with a const_pointer (i.e. cbegin() and cend())
* const Tp* is a pointer to a const T passed from vector.
*/
template <typename Tp>
struct iterator_traits<const Tp*> {
typedef std::random_access_iterator_tag iterator_category;
typedef Tp value_type;
typedef std::ptrdiff_t difference_type;
typedef const Tp* pointer;
typedef const Tp& reference;
};
/*
* Iterator_type is a pointer to the type T passed to vector.
*/
template <typename Iterator_type, typename Container>
class normal_iterator {
protected:
Iterator_type current_;
typedef iterator_traits<Iterator_type> traits_type;
public:
typedef typename traits_type::iterator_category iterator_category;
typedef typename traits_type::value_type value_type;
typedef typename traits_type::difference_type difference_type;
typedef typename traits_type::reference reference;
typedef typename traits_type::pointer pointer;
public:
normal_iterator() : current_(Iterator_type()) {}
normal_iterator(const Iterator_type& i) : current_(i) {}
template <typename Iter>
normal_iterator(const normal_iterator<Iter, typename enable_if<(is_same<Iter, typename Container::pointer>::value),
Container>::type>& i)
: current_(i.base()) {}
// Forward iterator overloads
reference operator*() const {
return *current_;
}
pointer operator->() const { return current_; }
normal_iterator& operator=(const normal_iterator& other)
{
current_ = other.current_;
return *this;
}
normal_iterator& operator++() {
++current_;
return *this;
}
normal_iterator operator++(int) { return normal_iterator(current_++); }
// Bidirectional iterator overloads
normal_iterator& operator--() {
--current_;
return *this;
}
normal_iterator operator--(int) {
return normal_iterator(current_--);
}
// Random access iterator overloads
reference operator[](difference_type n) const { return current_[n]; }
normal_iterator& operator+=(difference_type n) {
current_ += n;
return *this;
}
normal_iterator operator+(difference_type elem) const {
return normal_iterator(current_ + elem);
}
normal_iterator& operator-=(difference_type n) {
current_ -= n;
return *this;
}
normal_iterator operator-(difference_type elem) const {
return normal_iterator(current_ - elem);
}
const Iterator_type& base() const { return current_; }
};
// Non-member overloads
// Forward iterator non-member overloads
template <typename IteratorL, typename IteratorR, typename Container>
bool operator==(const normal_iterator<IteratorL, Container>& lhs
,const normal_iterator<IteratorR, Container>& rhs) {
return lhs.base() == rhs.base();
}
template <typename IteratorL, typename IteratorR, typename Container>
bool operator!=(const normal_iterator<IteratorL, Container>& lhs,
const normal_iterator<IteratorR, Container>& rhs) {
return lhs.base() != rhs.base();
}
// Random access iterator non-member overloads
template <typename IteratorL, typename IteratorR, typename Container>
bool operator>(const normal_iterator<IteratorL, Container>& lhs,
const normal_iterator<IteratorR, Container>& rhs) {
return lhs.base() > rhs.base();
}
template <typename IteratorL, typename IteratorR, typename Container>
bool operator<(const normal_iterator<IteratorL, Container>& lhs,
const normal_iterator<IteratorR, Container>& rhs) {
return lhs.base() < rhs.base();
}
template <typename IteratorL, typename IteratorR, typename Container>
bool operator>=(const normal_iterator<IteratorL, Container>& lhs,
const normal_iterator<IteratorR, Container>& rhs) {
return lhs.base() >= rhs.base();
}
template <typename IteratorL, typename IteratorR, typename Container>
bool operator<=(const normal_iterator<IteratorL, Container>& lhs,
const normal_iterator<IteratorR, Container>& rhs) {
return lhs.base() <= rhs.base();
}
template <typename Iter1, typename Iter2, typename Container>
typename normal_iterator<Iter1, Container>::difference_type operator-(
const normal_iterator<Iter1, Container>& lhs,
const normal_iterator<Iter2, Container>& rhs) {
return lhs.base() - rhs.base();
}
template <typename Iterator, typename Container>
normal_iterator<Iterator, Container> operator+(
typename normal_iterator<Iterator, Container>::difference_type lhs,
const normal_iterator<Iterator, Container>& rhs) {
return normal_iterator<Iterator, Container>(rhs.base() + lhs);
}
/*
* Iterator_type is normal_iterator<pointer> passed by vector
*/
template<typename Iterator_type>
class reverse_iterator : std::iterator<
typename iterator_traits<Iterator_type>::iterator_category,
typename iterator_traits<Iterator_type>::value_type,
typename iterator_traits<Iterator_type>::difference_type,
typename iterator_traits<Iterator_type>::pointer,
typename iterator_traits<Iterator_type>::reference
>
{
public:
Iterator_type current;
public:
typedef iterator_traits<Iterator_type> traits_type;
typedef Iterator_type Iter;
typedef typename traits_type::iterator_category iterator_category;
typedef typename traits_type::value_type value_type;
typedef typename traits_type::difference_type difference_type;
typedef typename traits_type::pointer pointer;
typedef typename traits_type::reference reference;
public:
// MEMBER FUNCTIONS
reverse_iterator() : current() {}
explicit reverse_iterator(Iterator_type x) : current(x) {}
template<typename U>
reverse_iterator(const reverse_iterator<U>& other) : current(other.current) {}
template<typename U>
reverse_iterator& operator=(const reverse_iterator<U>& other) {
current = other.current;
return *this;
}
Iterator_type base() const {
return current;
}
reference operator*() const {
Iter tmp = current;
return *--tmp;
}
pointer operator->() const {
//return current;
return &(operator*());
}
reference operator[](difference_type n) const {
return base()[-n-1];
}
reverse_iterator& operator++() {
--current;
return *this;
}
reverse_iterator& operator--() {
++current;
return *this;
}
reverse_iterator operator++(int) {
return reverse_iterator(current--);
}
reverse_iterator operator--(int) {
return reverse_iterator(current++);
}
reverse_iterator operator+(difference_type n) const {
return reverse_iterator(current - n);
}
difference_type operator+(reverse_iterator n) const {
return current - n;
}
reverse_iterator operator-(difference_type n) const {
return reverse_iterator(current + n);
}
reverse_iterator& operator+=(difference_type n) {
current -= n;
return *this;
}
reverse_iterator& operator-=(difference_type n) {
current += n;
return *this;
}
};
// REVERSE ITERATOR NON-MEMBER FUNCTIONS
template<typename Iterator1, typename Iterator2>
bool operator==(const reverse_iterator<Iterator1>& lhs,
const reverse_iterator<Iterator2>& rhs)
{
return lhs.base() == rhs.base();
}
template<typename Iterator1, typename Iterator2>
bool operator!=(const reverse_iterator<Iterator1>& lhs,
const reverse_iterator<Iterator2>& rhs)
{
return lhs.base() != rhs.base();
}
template<typename Iterator1, typename Iterator2>
bool operator<(const reverse_iterator<Iterator1>& lhs,
const reverse_iterator<Iterator2>& rhs)
{
return lhs.base() > rhs.base();
}
template<typename Iterator1, typename Iterator2>
bool operator<=(const reverse_iterator<Iterator1>& lhs,
const reverse_iterator<Iterator2>& rhs)
{
return lhs.base() >= rhs.base();
}
template<typename Iterator1, typename Iterator2>
bool operator>(const reverse_iterator<Iterator1>& lhs,
const reverse_iterator<Iterator2>& rhs)
{
return lhs.base() < rhs.base();
}
template<typename Iterator1, typename Iterator2>
bool operator>=(const reverse_iterator<Iterator1>& lhs,
const reverse_iterator<Iterator2>& rhs)
{
return lhs.base() <= rhs.base();
}
template<class Iter>
reverse_iterator<Iter> operator+(typename reverse_iterator<Iter>::difference_type n, const reverse_iterator<Iter>& it)
{
return reverse_iterator<Iter>(it.base() - n);
}
template<class Iter>
reverse_iterator<Iter> operator-(typename reverse_iterator<Iter>::difference_type n, const reverse_iterator<Iter>& it)
{
return it.base() + n;
}
template <typename Iterator>
typename reverse_iterator<Iterator>::difference_type
operator-(const reverse_iterator<Iterator>& lhs,
const reverse_iterator<Iterator>& rhs)
{
return (rhs.base() - lhs.base());
}
template <typename Iter1, typename Iter2>
typename reverse_iterator<Iter1>::difference_type
operator-(const reverse_iterator<Iter1>& lhs,
const reverse_iterator<Iter2>& rhs)
{
return (rhs.base() - lhs.base());
}
}