-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_iterator.hpp
244 lines (193 loc) · 4.98 KB
/
reverse_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* reverse_iterator.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: badam <badam@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/07/22 00:47:07 by badam #+# #+# */
/* Updated: 2021/10/25 14:35:18 by badam ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef REVERSE_ITERATOR_HPP
# define REVERSE_ITERATOR_HPP
namespace ft
{
template< class Iter >
class reverse_iterator
{
typedef reverse_iterator<Iter> _self;
protected:
typedef typename Iter::_item_ptr _item_ptr;
typedef typename Iter::_core _core;
public:
Iter *current;
typedef typename Iter::iterator_category iterator_category;
typedef typename Iter::value_type value_type;
typedef typename Iter::difference_type difference_type;
typedef typename Iter::pointer pointer;
typedef typename Iter::reference reference;
public:
reverse_iterator(void)
{
current = new Iter();
};
reverse_iterator(const Iter &src)
{
current = new Iter(src);
++(*this);
};
template < class Other >
reverse_iterator(const reverse_iterator<Other> &src)
{
current = new Iter(*(src.current));
};
reverse_iterator(const _self &src)
{
current = new Iter(*(src.current));
};
reverse_iterator(_item_ptr front, _item_ptr back, _item_ptr elem)
{
current = new Iter(front, back, elem);
};
_self &operator=(const _self &ref)
{
if (this != &ref)
{
if (current)
delete current;
current = new Iter(*(ref.current));
}
return (*this);
};
virtual ~reverse_iterator(void)
{
delete current;
};
Iter base(void) const
{
return (*current + 1);
}
reference operator*(void) const
{
return (*(*current));
};
pointer operator->(void) const
{
return ((*current).operator->());
};
reference operator[](difference_type n) const
{
Iter copy(current->getFront(), current->getBack(), current->getBack());
copy -= n;
return (*copy);
}
_self &operator++(void)
{
--(*current);
return (*this);
};
_self operator++(int)
{
_self tmp(*this);
++(*this);
return (tmp);
};
_self &operator--(void)
{
++(*current);
return (*this);
};
_self operator--(int)
{
_self tmp(*this);
--(*this);
return (tmp);
};
_self &operator+=(difference_type n)
{
*current -= n;
return (*this);
};
_self &operator-=(difference_type n)
{
*current += n;
return (*this);
};
_self operator+(difference_type n) const
{
_self tmp(*this);
return (tmp += n);
};
_self operator-(difference_type n) const
{
_self tmp(*this);
return (tmp -= n);
};
_item_ptr getElem(void) const
{
return (current->getElem());
};
_item_ptr getFront(void) const
{
return (current->getFront());
};
_item_ptr getBack(void) const
{
return (current->getBack());
};
};
template< class L_I, class R_I >
bool operator==(const reverse_iterator< L_I > &lhs, const reverse_iterator< R_I > &rhs)
{
return (lhs.getElem() == rhs.getElem());
};
template< class L_I, class R_I >
bool operator!=(const reverse_iterator< L_I > &lhs, const reverse_iterator< R_I > &rhs)
{
return (lhs.getElem() != rhs.getElem());
};
template< class L_I, class R_I >
bool operator<=(const reverse_iterator< L_I > &lhs, const reverse_iterator< R_I > &rhs)
{
if (lhs.getElem() == rhs.getElem())
return (true);
return (lhs < rhs);
};
template< class L_I, class R_I >
bool operator<(const reverse_iterator< L_I > &lhs, const reverse_iterator< R_I > &rhs)
{
if (!lhs.getElem())
return (false);
if (!rhs.getElem())
return (true);
return (*(lhs.current) > *(rhs.current));
};
template< class L_I, class R_I >
bool operator>=(const reverse_iterator< L_I > &lhs, const reverse_iterator< R_I > &rhs)
{
return (!(lhs < rhs));
};
template< class L_I, class R_I >
bool operator>(const reverse_iterator< L_I > &lhs, const reverse_iterator< R_I > &rhs)
{
return (!(lhs <= rhs));
};
template< class I >
reverse_iterator< I > operator+(const int nb, const reverse_iterator< I > &ref)
{
return (ref + nb);
};
template< class L_I, class R_I >
typename reverse_iterator< L_I >::difference_type operator-(const reverse_iterator< L_I > &lit, const reverse_iterator< R_I > &rit)
{
if (rit.getElem() == lit.getElem())
return (0);
if (!rit.getElem())
return lit.getFront() - 1 - lit.getElem();
if (!lit.getElem())
return rit.getElem() - rit.getFront() + 1;
return rit.getElem() - lit.getElem();
}
}
#endif