-
Notifications
You must be signed in to change notification settings - Fork 0
/
RBT_iterator.hpp
159 lines (130 loc) · 4.33 KB
/
RBT_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* RBT_iterator.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: graja <graja@student.42wolfsburg.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/26 08:02:45 by graja #+# #+# */
/* Updated: 2022/05/10 08:17:13 by graja ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef RBT_ITERATOR_H
# define RBT_ITERATOR_H
# include "iterator.hpp"
# include "RBtree.hpp"
# include "utility.hpp"
namespace ft
{
template <typename Key, typename T, bool is_const>
class RBT_iterator : public ft::iterator<std::bidirectional_iterator_tag, T>
{
public:
typedef Key key_type;
typedef T mapped_type;
typedef pair<const key_type, mapped_type> value_type;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef const value_type & const_reference;
private:
typedef typename RBtree<key_type, mapped_type>::iter RBnode;
typedef typename RBtree<key_type, mapped_type>::const_iter cRBnode;
typedef typename choose<is_const, cRBnode , RBnode >::type RB_type;
RB_type _p;
RB_type _first;
RB_type _last;
public:
RBT_iterator(void) :_p(NULL), _first(NULL), _last(NULL){}
RBT_iterator(RB_type in, RB_type f = NULL, RB_type l = NULL) :
_p(in), _first(f), _last(l) {}
~RBT_iterator(void) {}
template<typename fst, typename U, bool cval>
RBT_iterator(const RBT_iterator<fst, U, cval> & r) : _p(r.getPtr()) {}
RB_type getPtr(void) const {return _p;}
RB_type getf(void) const {return _first;}
RB_type getl(void) const {return _last;}
RBT_iterator(const RBT_iterator & cpy) : _p(cpy._p), _first(cpy._first), _last(cpy._last) {}
RBT_iterator & operator=(const RBT_iterator & right)
{
this->_p = right._p;
this->_first = right._first;
this->_last = right._last;
return (*this);
}
RBT_iterator & operator++()
{
RB_type tmp;
if (!_p)
_p = _first;
else if (!_p->right_child)
{
tmp = _p->parent;
while (tmp && tmp->data->first < _p->data->first)
tmp = tmp->parent;
_p = tmp;
}
else if (!_p->right_child->left_child)
_p = (_p->right_child);
else if (_p->right_child->left_child)
{
tmp = _p->right_child->left_child;
while (tmp->left_child)
tmp = tmp->left_child;
_p = tmp;
}
return (*this);
}
RBT_iterator operator++(int)
{RBT_iterator tmp(*this); operator++(); return tmp;}
RBT_iterator& operator--()
{
RB_type tmp;
if (!_p)
_p = _last;
else if (!_p->left_child)
{
tmp = _p->parent;
while (tmp && tmp->data->first > _p->data->first)
tmp = tmp->parent;
_p = tmp;
}
else if (!_p->left_child->right_child)
_p = (_p->left_child);
else if (_p->left_child->right_child)
{
tmp = _p->left_child->right_child;
while (tmp->right_child)
tmp = tmp->right_child;
_p = tmp;
}
return (*this);
}
RBT_iterator operator--(int)
{RBT_iterator tmp(*this); operator--(); return tmp;}
value_type & operator*()
{
if (_p == NULL)
{
RBT_iterator tmp(*this);
tmp++;
return *tmp;
}
return (*(_p->data));
}
value_type * operator->()
{
return (&(operator*()));
}
//This has to be a template, because we have two iterators in
//one, only differing by the bool is_c which is actually pointing
//out if it is const or not. So we need this to compare one with
//the other or compiler will hit us straight in the face !
template <typename L, typename U, bool is_c>
bool operator==(const RBT_iterator<L, U, is_c> & lhs) const
{return lhs.getPtr() == this->getPtr();}
template <typename L, typename U, bool is_c>
bool operator!=(const RBT_iterator<L, U, is_c> & lhs) const
{return lhs.getPtr() != this->getPtr();}
};
} //end namespace
#endif