1 | /*
|
---|
2 | * Copyright (c) 2018 Jaroslav Jindrak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #ifndef LIBCPP_INTERNAL_RBTREE_ITERATORS
|
---|
30 | #define LIBCPP_INTERNAL_RBTREE_ITERATORS
|
---|
31 |
|
---|
32 | #include <internal/iterator.hpp>
|
---|
33 | #include <internal/rbtree_node.hpp>
|
---|
34 | #include <iterator>
|
---|
35 |
|
---|
36 | namespace std::aux
|
---|
37 | {
|
---|
38 | /**
|
---|
39 | * Note: In order for these iterators to be reversible,
|
---|
40 | * the end state of an iterator is represented by a flag
|
---|
41 | * which can be set from true to false in operator--
|
---|
42 | * (i.e. decrementing end iterator) or set from false to
|
---|
43 | * true in operator++ (i.e. incrementing last before end
|
---|
44 | * iterator).
|
---|
45 | */
|
---|
46 |
|
---|
47 | template<class Value, class Reference, class Pointer, class Size, class Node>
|
---|
48 | class rbtree_iterator
|
---|
49 | {
|
---|
50 | public:
|
---|
51 | using value_type = Value;
|
---|
52 | using size_type = Size;
|
---|
53 | using reference = Reference;
|
---|
54 | using pointer = Pointer;
|
---|
55 | using difference_type = ptrdiff_t;
|
---|
56 |
|
---|
57 | using iterator_category = bidirectional_iterator_tag;
|
---|
58 |
|
---|
59 | using node_type = Node;
|
---|
60 |
|
---|
61 | rbtree_iterator(node_type* current = nullptr, bool end = true)
|
---|
62 | : current_{current}, end_{end}
|
---|
63 | { /* DUMMY BODY */ }
|
---|
64 |
|
---|
65 | rbtree_iterator(const rbtree_iterator&) = default;
|
---|
66 | rbtree_iterator& operator=(const rbtree_iterator&) = default;
|
---|
67 |
|
---|
68 | reference operator*() const
|
---|
69 | {
|
---|
70 | return current_->value;
|
---|
71 | }
|
---|
72 |
|
---|
73 | pointer operator->() const
|
---|
74 | {
|
---|
75 | return ¤t_->value;
|
---|
76 | }
|
---|
77 |
|
---|
78 | rbtree_iterator& operator++()
|
---|
79 | {
|
---|
80 | if (end_)
|
---|
81 | return *this;
|
---|
82 |
|
---|
83 | if (current_)
|
---|
84 | {
|
---|
85 | auto next = current_->successor();
|
---|
86 | if (next)
|
---|
87 | current_ = next;
|
---|
88 | else
|
---|
89 | end_ = true;
|
---|
90 | }
|
---|
91 |
|
---|
92 | return *this;
|
---|
93 | }
|
---|
94 |
|
---|
95 | rbtree_iterator operator++(int)
|
---|
96 | {
|
---|
97 | auto tmp = *this;
|
---|
98 | ++(*this);
|
---|
99 |
|
---|
100 | return tmp;
|
---|
101 | }
|
---|
102 |
|
---|
103 | rbtree_iterator& operator--()
|
---|
104 | {
|
---|
105 | if (end_)
|
---|
106 | {
|
---|
107 | end_ = false;
|
---|
108 |
|
---|
109 | return *this;
|
---|
110 | }
|
---|
111 |
|
---|
112 | if (current_)
|
---|
113 | {
|
---|
114 | auto next = current_->predecessor();
|
---|
115 | if (next)
|
---|
116 | current_ = next;
|
---|
117 | else
|
---|
118 | end_ = true;
|
---|
119 | }
|
---|
120 |
|
---|
121 | return *this;
|
---|
122 | }
|
---|
123 |
|
---|
124 | rbtree_iterator operator--(int)
|
---|
125 | {
|
---|
126 | auto tmp = *this;
|
---|
127 | --(*this);
|
---|
128 |
|
---|
129 | return tmp;
|
---|
130 | }
|
---|
131 |
|
---|
132 | const node_type* node() const
|
---|
133 | {
|
---|
134 | return current_;
|
---|
135 | }
|
---|
136 |
|
---|
137 | node_type* node()
|
---|
138 | {
|
---|
139 | return current_;
|
---|
140 | }
|
---|
141 |
|
---|
142 | bool end() const
|
---|
143 | {
|
---|
144 | return end_;
|
---|
145 | }
|
---|
146 |
|
---|
147 | private:
|
---|
148 | node_type* current_;
|
---|
149 | bool end_;
|
---|
150 | };
|
---|
151 |
|
---|
152 | template<class Val, class Ref, class Ptr, class Sz, class N>
|
---|
153 | bool operator==(const rbtree_iterator<Val, Ref, Ptr, Sz, N>& lhs,
|
---|
154 | const rbtree_iterator<Val, Ref, Ptr, Sz, N>& rhs)
|
---|
155 | {
|
---|
156 | return (lhs.node() == rhs.node()) && (lhs.end() == rhs.end());
|
---|
157 | }
|
---|
158 |
|
---|
159 | template<class Val, class Ref, class Ptr, class Sz, class N>
|
---|
160 | bool operator!=(const rbtree_iterator<Val, Ref, Ptr, Sz, N>& lhs,
|
---|
161 | const rbtree_iterator<Val, Ref, Ptr, Sz, N>& rhs)
|
---|
162 | {
|
---|
163 | return !(lhs == rhs);
|
---|
164 | }
|
---|
165 |
|
---|
166 | template<class Value, class ConstReference, class ConstPointer, class Size, class Node>
|
---|
167 | class rbtree_const_iterator
|
---|
168 | {
|
---|
169 | using non_const_iterator_type = rbtree_iterator<
|
---|
170 | Value, get_non_const_ref_t<ConstReference>,
|
---|
171 | get_non_const_ptr_t<ConstPointer>, Size, Node
|
---|
172 | >;
|
---|
173 |
|
---|
174 | public:
|
---|
175 | using value_type = Value;
|
---|
176 | using size_type = Size;
|
---|
177 | using const_reference = ConstReference;
|
---|
178 | using const_pointer = ConstPointer;
|
---|
179 | using difference_type = ptrdiff_t;
|
---|
180 |
|
---|
181 | using iterator_category = bidirectional_iterator_tag;
|
---|
182 |
|
---|
183 | // For iterator_traits.
|
---|
184 | using reference = ConstReference;
|
---|
185 | using pointer = ConstPointer;
|
---|
186 |
|
---|
187 | using node_type = Node;
|
---|
188 |
|
---|
189 | rbtree_const_iterator(const node_type* current = nullptr, bool end = true)
|
---|
190 | : current_{current}, end_{end}
|
---|
191 | { /* DUMMY BODY */ }
|
---|
192 |
|
---|
193 | rbtree_const_iterator(const rbtree_const_iterator&) = default;
|
---|
194 | rbtree_const_iterator& operator=(const rbtree_const_iterator&) = default;
|
---|
195 |
|
---|
196 | rbtree_const_iterator(const non_const_iterator_type& other)
|
---|
197 | : current_{other.node()}, end_{other.end()}
|
---|
198 | { /* DUMMY BODY */ }
|
---|
199 |
|
---|
200 | rbtree_const_iterator& operator=(const non_const_iterator_type& other)
|
---|
201 | {
|
---|
202 | current_ = other.node();
|
---|
203 | end_ = other.end();
|
---|
204 |
|
---|
205 | return *this;
|
---|
206 | }
|
---|
207 |
|
---|
208 | const_reference operator*() const
|
---|
209 | {
|
---|
210 | return current_->value;
|
---|
211 | }
|
---|
212 |
|
---|
213 | const_pointer operator->() const
|
---|
214 | {
|
---|
215 | return ¤t_->value;
|
---|
216 | }
|
---|
217 |
|
---|
218 | rbtree_const_iterator& operator++()
|
---|
219 | {
|
---|
220 | if (end_)
|
---|
221 | return *this;
|
---|
222 |
|
---|
223 | if (current_)
|
---|
224 | {
|
---|
225 | auto next = current_->successor();
|
---|
226 | if (next)
|
---|
227 | current_ = next;
|
---|
228 | else
|
---|
229 | end_ = true;
|
---|
230 | }
|
---|
231 |
|
---|
232 | return *this;
|
---|
233 | }
|
---|
234 |
|
---|
235 | rbtree_const_iterator operator++(int)
|
---|
236 | {
|
---|
237 | auto tmp = *this;
|
---|
238 | ++(*this);
|
---|
239 |
|
---|
240 | return tmp;
|
---|
241 | }
|
---|
242 |
|
---|
243 | rbtree_const_iterator& operator--()
|
---|
244 | {
|
---|
245 | if (end_)
|
---|
246 | {
|
---|
247 | end_ = false;
|
---|
248 |
|
---|
249 | return *this;
|
---|
250 | }
|
---|
251 |
|
---|
252 | if (current_)
|
---|
253 | {
|
---|
254 | auto next = current_->predecessor();
|
---|
255 | if (next)
|
---|
256 | current_ = next;
|
---|
257 | else
|
---|
258 | end_ = true;
|
---|
259 | }
|
---|
260 |
|
---|
261 | return *this;
|
---|
262 | }
|
---|
263 |
|
---|
264 | rbtree_const_iterator operator--(int)
|
---|
265 | {
|
---|
266 | auto tmp = *this;
|
---|
267 | --(*this);
|
---|
268 |
|
---|
269 | return tmp;
|
---|
270 | }
|
---|
271 |
|
---|
272 | const node_type* node() const
|
---|
273 | {
|
---|
274 | return current_;
|
---|
275 | }
|
---|
276 |
|
---|
277 | bool end() const
|
---|
278 | {
|
---|
279 | return end_;
|
---|
280 | }
|
---|
281 |
|
---|
282 | private:
|
---|
283 | const node_type* current_;
|
---|
284 | bool end_;
|
---|
285 | };
|
---|
286 |
|
---|
287 | template<class Val, class CRef, class CPtr, class Sz, class N>
|
---|
288 | bool operator==(const rbtree_const_iterator<Val, CRef, CPtr, Sz, N>& lhs,
|
---|
289 | const rbtree_const_iterator<Val, CRef, CPtr, Sz, N>& rhs)
|
---|
290 | {
|
---|
291 | return (lhs.node() == rhs.node()) && (lhs.end() == rhs.end());
|
---|
292 | }
|
---|
293 |
|
---|
294 | template<class Val, class CRef, class CPtr, class Sz, class N>
|
---|
295 | bool operator!=(const rbtree_const_iterator<Val, CRef, CPtr, Sz, N>& lhs,
|
---|
296 | const rbtree_const_iterator<Val, CRef, CPtr, Sz, N>& rhs)
|
---|
297 | {
|
---|
298 | return !(lhs == rhs);
|
---|
299 | }
|
---|
300 |
|
---|
301 | template<class Val, class Ref, class Ptr, class CRef, class CPtr, class Sz, class N>
|
---|
302 | bool operator==(const rbtree_iterator<Val, Ref, Ptr, Sz, N>& lhs,
|
---|
303 | const rbtree_const_iterator<Val, CRef, CPtr, Sz, N>& rhs)
|
---|
304 | {
|
---|
305 | return (lhs.node() == rhs.node()) && (lhs.end() == rhs.end());
|
---|
306 | }
|
---|
307 |
|
---|
308 | template<class Val, class Ref, class Ptr, class CRef, class CPtr, class Sz, class N>
|
---|
309 | bool operator!=(const rbtree_iterator<Val, Ref, Ptr, Sz, N>& lhs,
|
---|
310 | const rbtree_const_iterator<Val, CRef, CPtr, Sz, N>& rhs)
|
---|
311 | {
|
---|
312 | return !(lhs == rhs);
|
---|
313 | }
|
---|
314 |
|
---|
315 | template<class Val, class CRef, class CPtr, class Ref, class Ptr, class Sz, class N>
|
---|
316 | bool operator==(const rbtree_const_iterator<Val, CRef, CPtr, Sz, N>& lhs,
|
---|
317 | const rbtree_iterator<Val, Ref, Ptr, Sz, N>& rhs)
|
---|
318 | {
|
---|
319 | return (lhs.node() == rhs.node()) && (lhs.end() == rhs.end());
|
---|
320 | }
|
---|
321 |
|
---|
322 | template<class Val, class CRef, class CPtr, class Ref, class Ptr, class Sz, class N>
|
---|
323 | bool operator!=(const rbtree_const_iterator<Val, CRef, CPtr, Sz, N>& lhs,
|
---|
324 | const rbtree_iterator<Val, Ref, Ptr, Sz, N>& rhs)
|
---|
325 | {
|
---|
326 | return !(lhs == rhs);
|
---|
327 | }
|
---|
328 | }
|
---|
329 |
|
---|
330 | #endif
|
---|