-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_iterator.hpp
217 lines (179 loc) · 5.32 KB
/
tree_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
#ifndef TREE_ITERATOR_HPP
# define TREE_ITERATOR_HPP
#include "iterator_traits.hpp"
#include <cstddef>
#include <iterator>
namespace ft{
// map uses node*
// N usually will be node*, B will be a pair<T, Y>
template <class N, class B>
class tree_iterator
{
public:
typedef B value_type;
typedef typename ft::iterator_traits<N>::pointer npointer;
typedef typename ft::iterator_traits<N>::reference nreference;
typedef value_type* pointer;
typedef value_type& reference;
typedef std::bidirectional_iterator_tag iterator_category;
typedef typename ft::iterator_traits<N>::difference_type difference_type;
typedef typename B::first_type key_type;
tree_iterator() : p(NULL), sentinel(NULL) {}
explicit tree_iterator(npointer ptr, npointer sentinel) : p(ptr), sentinel(sentinel) {}
template<typename C, typename D>
tree_iterator(const tree_iterator<C, D> &it)
{
p = it.getNode();
sentinel = it.getSentinel();
}
template<typename C, typename D>
tree_iterator& operator=(const tree_iterator<C, D> &it)
{
p = it.getNode();
sentinel = it.getSentinel();
return *this;
}
reference operator*() const { return *(p->getContent()); }
pointer operator->() const { return p->getContent(); }
npointer* base() { return (&p) ;}
npointer getNode() const { return (p) ;}
npointer getSentinel() const { return (sentinel) ;}
npointer getLowestNodeFrom(npointer node) const
{
npointer aux = NULL;
aux = node;
while (aux && aux->left && aux->left != sentinel)
aux = aux->left;
return (aux);
};
npointer getHighestNode() const
{
npointer aux = NULL;
aux = p;
while (aux && aux->parent && aux->parent != sentinel)
aux = aux->parent;
return (getHighestNodeFrom(aux));
}
npointer getHighestNodeFrom(npointer node) const
{
npointer aux = NULL;
aux = node;
while (aux && aux->right && aux->right != sentinel)
aux = aux->right;
return (aux);
}
npointer getNextElement() const
{
npointer parent;
npointer node;
node = p;
if (node && node->right && node->right != sentinel)
return (getLowestNodeFrom(node->right));
parent = node->parent;
while (parent && node == parent->right && parent != sentinel)
{
node = parent;
parent = parent->parent;
}
return (parent);
}
npointer getPrevElement() const
{
npointer parent;
npointer node;
node = p;
if (p == sentinel)
{
return (getHighestNode());
}
if (node->left && node->left != sentinel)
return (getHighestNodeFrom(node->left));
parent = node->parent;
while (parent && node == parent->left && parent != sentinel)
{
node = parent;
parent = parent->parent;
}
return (parent);
}
tree_iterator& operator++() {
p = getNextElement();
return *this;
}
tree_iterator& operator--() {
p = getPrevElement();
return *this;
}
tree_iterator operator++(int) {
tree_iterator copy(getNode(), getSentinel());
++(*this);
return (copy);
}
tree_iterator operator--(int) {
tree_iterator copy(getNode(), getSentinel());
--(*this);
return (copy);
}
protected:
npointer p;
npointer sentinel;
};
template <class N, class B>
class set_iterator : public tree_iterator<N,B>
{
public:
typedef B value_type;
typedef typename ft::iterator_traits<N>::pointer npointer;
typedef typename ft::iterator_traits<N>::reference nreference;
typedef const typename value_type::second_type* pointer;
typedef const typename value_type::second_type& reference;
typedef std::bidirectional_iterator_tag iterator_category;
typedef typename ft::iterator_traits<N>::difference_type difference_type;
set_iterator() : tree_iterator<N, B>(){};
explicit set_iterator(npointer ptr, npointer sentinel) : tree_iterator<N,B>(ptr, sentinel) {}
template<typename C, typename D>
set_iterator(const set_iterator<C, D> &it)
{
this->p = it.getNode();
this->sentinel = it.getSentinel();
}
template<typename C, typename D>
set_iterator& operator=(const set_iterator<C, D> &it)
{
this->p = it.getNode();
this->sentinel = it.getSentinel();
return *this;
}
npointer getNode() const { return (this->p) ;}
npointer getSentinel() const { return (this->sentinel) ;}
reference operator*() const {
return (this->p->getContent()->first);
}
pointer operator->() const { return &(this->p->getContent()->first); }
set_iterator& operator++() {
this->p = this->getNextElement();
return *this;
}
set_iterator& operator--() {
this->p = this->getPrevElement();
return *this;
}
set_iterator operator++(int) {
set_iterator copy(this->getNode(), this->getSentinel());
++(*this);
return (copy);
}
set_iterator operator--(int) {
set_iterator copy(this->getNode(), this->getSentinel());
--(*this);
return (copy);
}
};
template <typename N1, typename B1, typename N2, typename B2>
bool operator!=(const ft::tree_iterator<N1, B1> &a, const ft::tree_iterator<N2, B2> &b){
return (a.getNode() != b.getNode()); }
template <typename N1, typename B1, typename N2, typename B2>
bool operator==(const ft::tree_iterator<N1, B1> &a, const ft::tree_iterator<N2, B2> &b){
return (a.getNode() == b.getNode()); }
}
#endif