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