C++: resolve ambiguous naming
[helenos.git] / uspace / lib / cpp / include / __bits / adt / rbtree.hpp
blob0c21f086022ec0d8106ef0b4177853c936d57268
1 /*
2 * Copyright (c) 2018 Jaroslav Jindrak
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 #ifndef LIBCPP_BITS_ADT_RBTREE
30 #define LIBCPP_BITS_ADT_RBTREE
32 #include <__bits/adt/key_extractors.hpp>
33 #include <__bits/adt/rbtree_iterators.hpp>
34 #include <__bits/adt/rbtree_node.hpp>
35 #include <__bits/adt/rbtree_policies.hpp>
37 namespace std::aux
39 template<
40 class Value, class Key, class KeyExtractor,
41 class KeyComp, class Alloc, class Size,
42 class Iterator, class ConstIterator,
43 class Policy, class Node
45 class rbtree
47 public:
48 using value_type = Value;
49 using key_type = Key;
50 using size_type = Size;
51 using allocator_type = Alloc;
52 using key_compare = KeyComp;
53 using key_extract = KeyExtractor;
55 using iterator = Iterator;
56 using const_iterator = ConstIterator;
58 using reverse_iterator = std::reverse_iterator<iterator>;
59 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
61 using node_type = Node;
63 rbtree(const key_compare& kcmp = key_compare{})
64 : root_{nullptr}, size_{}, key_compare_{},
65 key_extractor_{}
66 { /* DUMMY BODY */ }
68 rbtree(const rbtree& other)
69 : rbtree{other.key_compare_}
71 for (const auto& x: other)
72 insert(x);
75 rbtree(rbtree&& other)
76 : root_{other.root_}, size_{other.size_},
77 key_compare_{move(other.key_compare_)},
78 key_extractor_{move(other.key_extractor_)}
80 other.root_ = nullptr;
81 other.size_ = size_type{};
84 rbtree& operator=(const rbtree& other)
86 auto tmp{other};
87 tmp.swap(*this);
89 return *this;
92 rbtree& operator=(rbtree&& other)
94 rbtree tmp{move(other)};
95 tmp.swap(*this);
97 return *this;
100 bool empty() const noexcept
102 return size_ == 0U;
105 size_type size() const noexcept
107 return size_;
110 size_type max_size(allocator_type& alloc)
112 return allocator_traits<allocator_type>::max_size(alloc);
115 iterator begin()
117 return iterator{find_smallest_(), false};
120 const_iterator begin() const
122 return cbegin();
125 iterator end()
128 * In case we have lists of nodes
129 * we need to get the actual end
130 * from the largest node.
132 auto res = find_largest_();
133 if (res)
134 return iterator{res->get_end(), true};
135 else
136 return iterator{res, true};
139 const_iterator end() const
141 return cend();
144 reverse_iterator rbegin()
146 return make_reverse_iterator(end());
149 const_reverse_iterator rbegin() const
151 return make_reverse_iterator(cend());
154 reverse_iterator rend()
156 return make_reverse_iterator(begin());
159 const_reverse_iterator rend() const
161 return make_reverse_iterator(cbegin());
164 const_iterator cbegin() const
166 return const_iterator{find_smallest_(), false};
169 const_iterator cend() const
171 auto res = find_largest_();
172 if (res)
173 return const_iterator{res->get_end(), true};
174 else
175 return const_iterator{res, true};
178 const_reverse_iterator crbegin() const
180 return make_reverse_iterator(cend());
183 const_reverse_iterator crend() const
185 return make_reverse_iterator(cbegin());
188 template<class... Args>
189 auto emplace(Args&&... args)
191 return Policy::emplace(*this, forward<Args>(args)...);
194 auto insert(const value_type& val)
196 return Policy::insert(*this, val);
199 auto insert(value_type&& val)
201 return Policy::insert(*this, forward<value_type>(val));
204 size_type erase(const key_type& key)
206 return Policy::erase(*this, key);
209 iterator erase(const_iterator it)
211 if (it == cend())
212 return end();
214 auto node = const_cast<node_type*>(it.node());
216 node = delete_node(node);
217 if (!node)
218 return iterator{find_largest_(), true};
219 else
220 return iterator{const_cast<node_type*>(node), false};
223 void clear() noexcept
225 if (root_)
227 delete root_;
228 root_ = nullptr;
229 size_ = size_type{};
233 void swap(rbtree& other)
234 noexcept(allocator_traits<allocator_type>::is_always_equal::value &&
235 noexcept(std::swap(declval<KeyComp&>(), declval<KeyComp&>())))
237 std::swap(root_, other.root_);
238 std::swap(size_, other.size_);
239 std::swap(key_compare_, other.key_compare_);
240 std::swap(key_extractor_, other.key_extractor_);
243 key_compare key_comp() const
245 return key_compare_;
248 iterator find(const key_type& key)
250 auto node = find_(key);
251 if (node)
252 return iterator{node, false};
253 else
254 return end();
257 const_iterator find(const key_type& key) const
259 auto node = find_(key);
260 if (node)
261 return const_iterator{node, false};
262 else
263 return end();
266 size_type count(const key_type& key) const
268 return Policy::count(*this, key);
271 iterator upper_bound(const key_type& key)
273 return Policy::upper_bound(*this, key);
276 const_iterator upper_bound(const key_type& key) const
278 return Policy::upper_bound_const(*this, key);
281 iterator lower_bound(const key_type& key)
283 return Policy::lower_bound(*this, key);
286 const_iterator lower_bound(const key_type& key) const
288 return Policy::lower_bound_const(*this, key);
291 pair<iterator, iterator> equal_range(const key_type& key)
293 return Policy::equal_range(*this, key);
296 pair<const_iterator, const_iterator> equal_range(const key_type& key) const
298 return Policy::equal_range_const(*this, key);
301 bool is_eq_to(const rbtree& other) const
303 if (size_ != other.size())
304 return false;
306 auto it1 = begin();
307 auto it2 = other.begin();
309 // TODO: this doesn't compare values :/
310 while (keys_equal(*it1++, *it2++))
311 { /* DUMMY BODY */ }
313 return (it1 == end()) && (it2 == other.end());
316 const key_type& get_key(const value_type& val) const
318 return key_extractor_(val);
321 bool keys_comp(const key_type& key, const value_type& val) const
323 return key_compare_(key, key_extractor_(val));
326 bool keys_equal(const key_type& k1, const key_type& k2) const
328 return !key_compare_(k1, k2) && !key_compare_(k2, k1);
331 node_type* find_parent_for_insertion(const key_type& key) const
333 auto current = root_;
334 auto parent = current;
336 while (current)
338 parent = current;
339 if (key_compare_(key, key_extractor_(current->value)))
340 current = current->left();
341 else if (key_compare_(key_extractor_(current->value), key))
342 current = current->right();
343 else
344 return current;
347 return parent;
350 node_type* delete_node(const node_type* n)
352 auto node = const_cast<node_type*>(n);
353 if (!node)
354 return nullptr;
356 --size_;
358 auto succ = node->successor();
359 if (auto tmp = node->get_node_for_deletion(); tmp != nullptr)
362 * This will kick in multi containers,
363 * we popped one node from a list of nodes
364 * with equivalent keys and we can delete it
365 * and return the successor which was the next
366 * in the list.
368 delete tmp;
370 update_root_(succ); // Incase the first in list was root.
371 return succ;
373 else if (node == root_)
374 { // Only executed if root_ is unique.
375 root_ = nullptr;
376 delete node;
378 return nullptr;
381 if (node->left() && node->right())
383 node->swap(succ);
384 if (succ && !succ->parent())
385 root_ = succ;
387 // Node now has at most one child.
388 // Also: If succ was nullptr, the swap
389 // didn't do anything and we can
390 // safely delete node.
391 return delete_node(node);
394 auto child = node->right() ? node->right() : node->left();
395 if (!child)
397 // Simply remove the node.
398 // TODO: repair here too?
399 node->unlink();
400 delete node;
402 else
404 // Replace with the child.
405 child->parent(node->parent());
406 if (node->is_left_child())
407 child->parent()->left(child);
408 else if (node->is_right_child())
409 child->parent()->right(child);
410 node->parent(nullptr);
411 node->left(nullptr);
412 node->right(nullptr);
414 // Repair if needed.
415 repair_after_erase_(node, child);
416 update_root_(child);
418 delete node;
421 return succ;
424 void insert_node(node_type* node, node_type* parent)
426 Policy::insert(*this, node, parent);
429 private:
430 node_type* root_;
431 size_type size_;
432 key_compare key_compare_;
433 key_extract key_extractor_;
435 node_type* find_(const key_type& key) const
437 auto current = root_;
438 while (current != nullptr)
440 if (key_compare_(key, key_extractor_(current->value)))
441 current = current->left();
442 else if (key_compare_(key_extractor_(current->value), key))
443 current = current->right();
444 else
445 return current;
448 return nullptr;
451 node_type* find_smallest_() const
453 if (root_)
454 return root_->find_smallest();
455 else
456 return nullptr;
459 node_type* find_largest_() const
461 if (root_)
462 return root_->find_largest();
463 else
464 return nullptr;
467 void update_root_(const node_type* node)
469 if (!node)
470 return;
472 root_ = const_cast<node_type*>(node);
473 while (root_->parent())
474 root_ = root_->parent();
477 void repair_after_insert_(const node_type* node)
479 // TODO: implement
482 void repair_after_erase_(const node_type* node, const node_type* child)
484 // TODO: implement
487 friend Policy;
491 #endif