[4d65515] | 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
|
---|
| 30 | #define LIBCPP_INTERNAL_RBTREE
|
---|
| 31 |
|
---|
| 32 | #include <internal/key_extractors.hpp>
|
---|
| 33 | #include <internal/rbtree_iterators.hpp>
|
---|
| 34 | #include <internal/rbtree_node.hpp>
|
---|
| 35 | #include <internal/rbtree_policies.hpp>
|
---|
| 36 |
|
---|
| 37 | namespace std::aux
|
---|
| 38 | {
|
---|
| 39 | template<
|
---|
| 40 | class Value, class Key, class KeyExtractor,
|
---|
| 41 | class KeyComp, class Alloc, class Size,
|
---|
| 42 | class Iterator, class ConstIterator,
|
---|
[73e3791] | 43 | class Policy, class Node
|
---|
[4d65515] | 44 | >
|
---|
| 45 | class rbtree
|
---|
| 46 | {
|
---|
| 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;
|
---|
| 54 |
|
---|
[73e3791] | 55 | using iterator = Iterator;
|
---|
| 56 | using const_iterator = ConstIterator;
|
---|
[4d65515] | 57 |
|
---|
[be9eb15] | 58 | using reverse_iterator = std::reverse_iterator<iterator>;
|
---|
| 59 | using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
---|
| 60 |
|
---|
[73e3791] | 61 | using node_type = Node;
|
---|
[4d65515] | 62 |
|
---|
| 63 | rbtree(const key_compare& kcmp = key_compare{})
|
---|
| 64 | : root_{nullptr}, size_{}, key_compare_{},
|
---|
| 65 | key_extractor_{}
|
---|
| 66 | { /* DUMMY BODY */ }
|
---|
| 67 |
|
---|
[009d78b] | 68 | rbtree(const rbtree& other)
|
---|
| 69 | : rbtree{other.key_compare_}
|
---|
| 70 | {
|
---|
| 71 | for (const auto& x: other)
|
---|
| 72 | insert(x);
|
---|
| 73 | }
|
---|
[be9eb15] | 74 |
|
---|
| 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_)}
|
---|
| 79 | {
|
---|
| 80 | other.root_ = nullptr;
|
---|
| 81 | other.size_ = size_type{};
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | rbtree& operator=(const rbtree& other)
|
---|
| 85 | {
|
---|
| 86 | auto tmp{other};
|
---|
| 87 | tmp.swap(*this);
|
---|
[4d65515] | 88 |
|
---|
[be9eb15] | 89 | return *this;
|
---|
| 90 | }
|
---|
[4d65515] | 91 |
|
---|
[be9eb15] | 92 | rbtree& operator=(rbtree&& other)
|
---|
| 93 | {
|
---|
| 94 | rbtree tmp{move(other)};
|
---|
| 95 | tmp.swap(*this);
|
---|
[4d65515] | 96 |
|
---|
[be9eb15] | 97 | return *this;
|
---|
| 98 | }
|
---|
[4d65515] | 99 |
|
---|
| 100 | bool empty() const noexcept
|
---|
| 101 | {
|
---|
[73e3791] | 102 | return size_ == 0U;
|
---|
[4d65515] | 103 | }
|
---|
| 104 |
|
---|
| 105 | size_type size() const noexcept
|
---|
| 106 | {
|
---|
| 107 | return size_;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | size_type max_size(allocator_type& alloc)
|
---|
| 111 | {
|
---|
| 112 | return allocator_traits<allocator_type>::max_size(alloc);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | iterator begin()
|
---|
| 116 | {
|
---|
| 117 | return iterator{find_smallest_(), false};
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | const_iterator begin() const
|
---|
| 121 | {
|
---|
| 122 | return cbegin();
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | iterator end()
|
---|
| 126 | {
|
---|
[4f22d0c3] | 127 | /**
|
---|
| 128 | * In case we have lists of nodes
|
---|
| 129 | * we need to get the actual end
|
---|
| 130 | * from the largest node.
|
---|
| 131 | */
|
---|
| 132 | auto res = find_largest_();
|
---|
| 133 | if (res)
|
---|
| 134 | return iterator{res->get_end(), true};
|
---|
| 135 | else
|
---|
| 136 | return iterator{res, true};
|
---|
[4d65515] | 137 | }
|
---|
| 138 |
|
---|
| 139 | const_iterator end() const
|
---|
| 140 | {
|
---|
| 141 | return cend();
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[be9eb15] | 144 | reverse_iterator rbegin()
|
---|
| 145 | {
|
---|
| 146 | return make_reverse_iterator(end());
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | const_reverse_iterator rbegin() const
|
---|
| 150 | {
|
---|
| 151 | return make_reverse_iterator(cend());
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | reverse_iterator rend()
|
---|
| 155 | {
|
---|
| 156 | return make_reverse_iterator(begin());
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | const_reverse_iterator rend() const
|
---|
| 160 | {
|
---|
| 161 | return make_reverse_iterator(cbegin());
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[4d65515] | 164 | const_iterator cbegin() const
|
---|
| 165 | {
|
---|
| 166 | return const_iterator{find_smallest_(), false};
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | const_iterator cend() const
|
---|
| 170 | {
|
---|
[4f22d0c3] | 171 | auto res = find_largest_();
|
---|
| 172 | if (res)
|
---|
| 173 | return const_iterator{res->get_end(), true};
|
---|
| 174 | else
|
---|
| 175 | return const_iterator{res, true};
|
---|
[4d65515] | 176 | }
|
---|
| 177 |
|
---|
[be9eb15] | 178 | const_reverse_iterator crbegin() const
|
---|
| 179 | {
|
---|
| 180 | return make_reverse_iterator(cend());
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | const_reverse_iterator crend() const
|
---|
| 184 | {
|
---|
| 185 | return make_reverse_iterator(cbegin());
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[4d65515] | 188 | template<class... Args>
|
---|
[369f5df] | 189 | auto emplace(Args&&... args)
|
---|
[4d65515] | 190 | {
|
---|
[369f5df] | 191 | return Policy::emplace(*this, forward<Args>(args)...);
|
---|
[4d65515] | 192 | }
|
---|
| 193 |
|
---|
[369f5df] | 194 | auto insert(const value_type& val)
|
---|
[4d65515] | 195 | {
|
---|
[369f5df] | 196 | return Policy::insert(*this, val);
|
---|
[4d65515] | 197 | }
|
---|
| 198 |
|
---|
[369f5df] | 199 | auto insert(value_type&& val)
|
---|
[4d65515] | 200 | {
|
---|
[369f5df] | 201 | return Policy::insert(*this, forward<value_type>(val));
|
---|
[4d65515] | 202 | }
|
---|
| 203 |
|
---|
| 204 | size_type erase(const key_type& key)
|
---|
| 205 | {
|
---|
[009d78b] | 206 | return Policy::erase(*this, key);
|
---|
[4d65515] | 207 | }
|
---|
| 208 |
|
---|
| 209 | iterator erase(const_iterator it)
|
---|
| 210 | {
|
---|
[009d78b] | 211 | if (it == cend())
|
---|
| 212 | return end();
|
---|
| 213 |
|
---|
| 214 | auto node = const_cast<node_type*>(it.node());
|
---|
| 215 |
|
---|
[369f5df] | 216 | node = delete_node(node);
|
---|
[73e3791] | 217 | if (!node)
|
---|
| 218 | return iterator{find_largest_(), true};
|
---|
| 219 | else
|
---|
| 220 | return iterator{const_cast<node_type*>(node), false};
|
---|
[4d65515] | 221 | }
|
---|
| 222 |
|
---|
| 223 | void clear() noexcept
|
---|
| 224 | {
|
---|
| 225 | if (root_)
|
---|
| 226 | {
|
---|
| 227 | delete root_;
|
---|
| 228 | root_ = nullptr;
|
---|
| 229 | size_ = size_type{};
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | void swap(rbtree& other)
|
---|
| 234 | noexcept(allocator_traits<allocator_type>::is_always_equal::value &&
|
---|
| 235 | noexcept(swap(declval<KeyComp&>(), declval<KeyComp&>())))
|
---|
| 236 | {
|
---|
| 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_);
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | key_compare key_comp() const
|
---|
| 244 | {
|
---|
| 245 | return key_compare_;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | iterator find(const key_type& key)
|
---|
| 249 | {
|
---|
[be9eb15] | 250 | auto node = find_(key);
|
---|
| 251 | if (node)
|
---|
| 252 | return iterator{node, false};
|
---|
| 253 | else
|
---|
| 254 | return end();
|
---|
[4d65515] | 255 | }
|
---|
| 256 |
|
---|
[be9eb15] | 257 | const_iterator find(const key_type& key) const
|
---|
[4d65515] | 258 | {
|
---|
[be9eb15] | 259 | auto node = find_(key);
|
---|
| 260 | if (node)
|
---|
| 261 | return const_iterator{node, false};
|
---|
| 262 | else
|
---|
| 263 | return end();
|
---|
[4d65515] | 264 | }
|
---|
| 265 |
|
---|
| 266 | size_type count(const key_type& key) const
|
---|
| 267 | {
|
---|
| 268 | return Policy::count(*this, key);
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | iterator upper_bound(const key_type& key)
|
---|
| 272 | {
|
---|
| 273 | return Policy::upper_bound(*this, key);
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | const_iterator upper_bound(const key_type& key) const
|
---|
| 277 | {
|
---|
| 278 | return Policy::upper_bound_const(*this, key);
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | iterator lower_bound(const key_type& key)
|
---|
| 282 | {
|
---|
| 283 | return Policy::lower_bound(*this, key);
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | const_iterator lower_bound(const key_type& key) const
|
---|
| 287 | {
|
---|
| 288 | return Policy::lower_bound_const(*this, key);
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | pair<iterator, iterator> equal_range(const key_type& key)
|
---|
| 292 | {
|
---|
| 293 | return Policy::equal_range(*this, key);
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | pair<const_iterator, const_iterator> equal_range(const key_type& key) const
|
---|
| 297 | {
|
---|
| 298 | return Policy::equal_range_const(*this, key);
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | bool is_eq_to(const rbtree& other) const
|
---|
| 302 | {
|
---|
[009d78b] | 303 | if (size_ != other.size())
|
---|
| 304 | return false;
|
---|
| 305 |
|
---|
| 306 | auto it1 = begin();
|
---|
| 307 | auto it2 = other.begin();
|
---|
| 308 |
|
---|
[369f5df] | 309 | // TODO: this doesn't compare values :/
|
---|
[009d78b] | 310 | while (keys_equal(*it1++, *it2++))
|
---|
| 311 | { /* DUMMY BODY */ }
|
---|
| 312 |
|
---|
| 313 | return (it1 == end()) && (it2 == other.end());
|
---|
[4d65515] | 314 | }
|
---|
| 315 |
|
---|
| 316 | const key_type& get_key(const value_type& val) const
|
---|
| 317 | {
|
---|
| 318 | return key_extractor_(val);
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | bool keys_comp(const key_type& key, const value_type& val) const
|
---|
| 322 | {
|
---|
| 323 | return key_compare_(key, key_extractor_(val));
|
---|
| 324 | }
|
---|
| 325 |
|
---|
[009d78b] | 326 | bool keys_equal(const key_type& k1, const key_type& k2) const
|
---|
| 327 | {
|
---|
| 328 | return !key_compare_(k1, k2) && !key_compare_(k2, k1);
|
---|
| 329 | }
|
---|
| 330 |
|
---|
[48f09f2f] | 331 | node_type* find_parent_for_insertion(const key_type& key) const
|
---|
[4d65515] | 332 | {
|
---|
| 333 | auto current = root_;
|
---|
| 334 | auto parent = current;
|
---|
| 335 |
|
---|
| 336 | while (current)
|
---|
| 337 | {
|
---|
| 338 | parent = current;
|
---|
[48f09f2f] | 339 | if (key_compare_(key, key_extractor_(current->value)))
|
---|
[73e3791] | 340 | current = current->left();
|
---|
[71cde76] | 341 | else if (key_compare_(key_extractor_(current->value), key))
|
---|
[73e3791] | 342 | current = current->right();
|
---|
[71cde76] | 343 | else
|
---|
| 344 | return current;
|
---|
[4d65515] | 345 | }
|
---|
| 346 |
|
---|
| 347 | return parent;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
[369f5df] | 350 | node_type* delete_node(const node_type* n)
|
---|
[009d78b] | 351 | {
|
---|
[369f5df] | 352 | auto node = const_cast<node_type*>(n);
|
---|
[009d78b] | 353 | if (!node)
|
---|
[369f5df] | 354 | return nullptr;
|
---|
[73e3791] | 355 | if (auto tmp = node->get_node_for_deletion(); tmp != nullptr)
|
---|
| 356 | {
|
---|
| 357 | /**
|
---|
| 358 | * This will kick in multi containers,
|
---|
| 359 | * we popped one node from a list of nodes
|
---|
| 360 | * with equivalent keys and we can delete it
|
---|
| 361 | * and return the original as it is still
|
---|
| 362 | * in place.
|
---|
| 363 | */
|
---|
| 364 | delete tmp;
|
---|
| 365 |
|
---|
| 366 | return node;
|
---|
| 367 | }
|
---|
[009d78b] | 368 |
|
---|
| 369 | --size_;
|
---|
| 370 |
|
---|
[73e3791] | 371 | if (node == root_)
|
---|
[009d78b] | 372 | {
|
---|
[73e3791] | 373 | delete node;
|
---|
| 374 | root_ = nullptr;
|
---|
[009d78b] | 375 |
|
---|
[73e3791] | 376 | return nullptr;
|
---|
| 377 | }
|
---|
[009d78b] | 378 |
|
---|
[73e3791] | 379 | auto succ = node->successor();
|
---|
| 380 | if (node->left() && node->right())
|
---|
| 381 | {
|
---|
| 382 | node->swap(succ);
|
---|
| 383 | if (succ && !succ->parent())
|
---|
| 384 | root_ = succ;
|
---|
| 385 |
|
---|
| 386 | // Node now has at most one child.
|
---|
| 387 | // Also: If succ was nullptr, the swap
|
---|
| 388 | // didn't do anything and we can
|
---|
| 389 | // safely delete node.
|
---|
| 390 | return delete_node(node);
|
---|
[009d78b] | 391 | }
|
---|
| 392 |
|
---|
[73e3791] | 393 | auto child = node->right() ? node->right() : node->left();
|
---|
[009d78b] | 394 | if (!child)
|
---|
| 395 | {
|
---|
| 396 | // Simply remove the node.
|
---|
[369f5df] | 397 | // TODO: repair here too?
|
---|
[009d78b] | 398 | node->unlink();
|
---|
| 399 | delete node;
|
---|
| 400 | }
|
---|
| 401 | else
|
---|
| 402 | {
|
---|
| 403 | // Replace with the child.
|
---|
[73e3791] | 404 | child->parent(node->parent());
|
---|
[009d78b] | 405 | if (node->is_left_child())
|
---|
[73e3791] | 406 | child->parent()->left(child);
|
---|
[369f5df] | 407 | else if (node->is_right_child())
|
---|
[73e3791] | 408 | child->parent()->right(child);
|
---|
[009d78b] | 409 |
|
---|
| 410 | // Repair if needed.
|
---|
| 411 | repair_after_erase_(node, child);
|
---|
| 412 | update_root_(child);
|
---|
[369f5df] | 413 |
|
---|
| 414 | delete node;
|
---|
[009d78b] | 415 | }
|
---|
[369f5df] | 416 |
|
---|
| 417 | return succ;
|
---|
[009d78b] | 418 | }
|
---|
| 419 |
|
---|
[7644d6e] | 420 | void insert_node(node_type* node, node_type* parent)
|
---|
| 421 | {
|
---|
[73e3791] | 422 | Policy::insert(*this, node, parent);
|
---|
[7644d6e] | 423 | }
|
---|
| 424 |
|
---|
[4d65515] | 425 | private:
|
---|
| 426 | node_type* root_;
|
---|
| 427 | size_type size_;
|
---|
| 428 | key_compare key_compare_;
|
---|
| 429 | key_extract key_extractor_;
|
---|
| 430 |
|
---|
[be9eb15] | 431 | node_type* find_(const key_type& key) const
|
---|
| 432 | {
|
---|
| 433 | auto current = root_;
|
---|
| 434 | while (current != nullptr)
|
---|
| 435 | {
|
---|
| 436 | if (key_compare_(key, key_extractor_(current->value)))
|
---|
[73e3791] | 437 | current = current->left();
|
---|
[009d78b] | 438 | else if (key_compare_(key_extractor_(current->value), key))
|
---|
[73e3791] | 439 | current = current->right();
|
---|
[009d78b] | 440 | else
|
---|
| 441 | return current;
|
---|
[be9eb15] | 442 | }
|
---|
| 443 |
|
---|
| 444 | return nullptr;
|
---|
| 445 | }
|
---|
| 446 |
|
---|
[4d65515] | 447 | node_type* find_smallest_() const
|
---|
| 448 | {
|
---|
| 449 | if (root_)
|
---|
| 450 | return root_->find_smallest();
|
---|
| 451 | else
|
---|
| 452 | return nullptr;
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | node_type* find_largest_() const
|
---|
| 456 | {
|
---|
| 457 | if (root_)
|
---|
| 458 | return root_->find_largest();
|
---|
| 459 | else
|
---|
| 460 | return nullptr;
|
---|
| 461 | }
|
---|
| 462 |
|
---|
[4f080f2a] | 463 | void update_root_(const node_type* node)
|
---|
[4d65515] | 464 | {
|
---|
| 465 | if (!node)
|
---|
| 466 | return;
|
---|
| 467 |
|
---|
[4f080f2a] | 468 | root_ = const_cast<node_type*>(node);
|
---|
[73e3791] | 469 | while (root_->parent())
|
---|
| 470 | root_ = root_->parent();
|
---|
[4d65515] | 471 | }
|
---|
| 472 |
|
---|
[4f080f2a] | 473 | void repair_after_insert_(const node_type* node)
|
---|
[4d65515] | 474 | {
|
---|
| 475 | // TODO: implement
|
---|
| 476 | }
|
---|
| 477 |
|
---|
[4f080f2a] | 478 | void repair_after_erase_(const node_type* node, const node_type* child)
|
---|
[be9eb15] | 479 | {
|
---|
| 480 | // TODO: implement
|
---|
| 481 | }
|
---|
| 482 |
|
---|
[4d65515] | 483 | friend Policy;
|
---|
| 484 | };
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | #endif
|
---|