rbtree 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. #ifndef __GBLIBCPP_BITS_RBTREE__
  2. #define __GBLIBCPP_BITS_RBTREE__
  3. #include <cstddef>
  4. #include <functional>
  5. #include <utility>
  6. #include <memory>
  7. namespace std::impl {
  8. template <typename T, typename Compare, typename Allocator>
  9. struct rbtree {
  10. struct node {
  11. node* parent;
  12. node* left;
  13. node* right;
  14. T value;
  15. enum class node_color : unsigned char { RED, BLACK, } color;
  16. constexpr node(const T& val)
  17. : parent {}, left {}, right {}
  18. , value { val } , color {node_color::RED} {}
  19. constexpr node(T&& val)
  20. : parent {}, left {}, right {}
  21. , value { std::move(val) } , color {node_color::RED} {}
  22. template <typename... Args, std::enable_if_t<
  23. (sizeof...(Args) > 1)
  24. || !(... && std::is_same_v<T, std::remove_cvref_t<Args>>)
  25. , bool> = true>
  26. constexpr node(Args&&... args)
  27. : parent {}, left {}, right {}
  28. , value { std::forward<Args>(args)... }, color { node_color::RED } {}
  29. constexpr node* grandparent(void) const
  30. { return this->parent->parent; }
  31. constexpr node* uncle(void) const
  32. {
  33. node* pp = this->grandparent();
  34. if (this->parent == pp->left)
  35. return pp->right;
  36. return pp->left;
  37. }
  38. constexpr node* leftmost(void)
  39. {
  40. node* nd = this;
  41. while (nd->left)
  42. nd = nd->left;
  43. return nd;
  44. }
  45. constexpr node* rightmost(void)
  46. {
  47. node* nd = this;
  48. while (nd->right)
  49. nd = nd->right;
  50. return nd;
  51. }
  52. constexpr node* next(void)
  53. {
  54. if (this->right)
  55. return this->right->leftmost();
  56. if (this->is_root())
  57. return nullptr;
  58. if (this->is_left_child())
  59. return this->parent;
  60. node* ret = this;
  61. do {
  62. ret = ret->parent;
  63. } while (!ret->is_root() && !ret->is_left_child());
  64. return ret->parent;
  65. }
  66. constexpr node* prev(void)
  67. {
  68. if (this->left)
  69. return this->left->rightmost();
  70. if (this->is_root())
  71. return nullptr;
  72. if (this->is_right_child())
  73. return this->parent;
  74. node* ret = this;
  75. do {
  76. ret = ret->parent;
  77. } while (!ret->is_root() && !ret->is_right_child());
  78. return ret->parent;
  79. }
  80. static constexpr bool is_red(node* nd)
  81. { return nd && nd->color == node_color::RED; }
  82. static constexpr bool is_black(node* nd)
  83. { return !node::is_red(nd); }
  84. constexpr bool is_root(void) const
  85. { return this->parent == nullptr; }
  86. constexpr bool is_full(void) const
  87. { return this->left && this->right; }
  88. constexpr bool has_child(void) const
  89. { return this->left || this->right; }
  90. constexpr bool is_leaf(void) const
  91. { return !this->has_child(); }
  92. constexpr bool is_left_child(void) const
  93. { return this == this->parent->left; }
  94. constexpr bool is_right_child(void) const
  95. { return this == this->parent->right; }
  96. constexpr void tored(void)
  97. { this->color = node_color::RED; }
  98. constexpr void toblack(void)
  99. { this->color = node_color::BLACK; }
  100. static constexpr void swap(node* first, node* second)
  101. {
  102. if (node::is_red(first)) {
  103. first->color = second->color;
  104. second->color = node_color::RED;
  105. } else {
  106. first->color = second->color;
  107. second->color = node_color::BLACK;
  108. }
  109. if (first->parent == second) {
  110. node* tmp = first;
  111. first = second;
  112. second = tmp;
  113. }
  114. bool f_is_left_child = first->parent ? first->is_left_child() : false;
  115. bool s_is_left_child = second->parent ? second->is_left_child() : false;
  116. node* fp = first->parent;
  117. node* fl = first->left;
  118. node* fr = first->right;
  119. node* sp = second->parent;
  120. node* sl = second->left;
  121. node* sr = second->right;
  122. if (second->parent != first) {
  123. first->parent = sp;
  124. if (sp) {
  125. if (s_is_left_child)
  126. sp->left = first;
  127. else
  128. sp->right = first;
  129. }
  130. first->left = sl;
  131. if (sl)
  132. sl->parent = first;
  133. first->right = sr;
  134. if (sr)
  135. sr->parent = first;
  136. second->parent = fp;
  137. if (fp) {
  138. if (f_is_left_child)
  139. fp->left = second;
  140. else
  141. fp->right = second;
  142. }
  143. second->left = fl;
  144. if (fl)
  145. fl->parent = second;
  146. second->right = fr;
  147. if (fr)
  148. fr->parent = second;
  149. } else {
  150. first->left = sl;
  151. if (sl)
  152. sl->parent = first;
  153. first->right = sr;
  154. if (sr)
  155. sr->parent = first;
  156. second->parent = fp;
  157. if (fp) {
  158. if (f_is_left_child)
  159. fp->left = second;
  160. else
  161. fp->right = second;
  162. }
  163. first->parent = second;
  164. if (s_is_left_child) {
  165. second->left = first;
  166. second->right = fr;
  167. if (fr)
  168. fr->parent = second;
  169. } else {
  170. second->right = first;
  171. second->left = fl;
  172. if (fl)
  173. fl->parent = second;
  174. }
  175. }
  176. }
  177. };
  178. template <bool Const>
  179. class _iterator {
  180. public:
  181. using node_pointer = node*;
  182. using value_type = std::conditional_t<Const, const T, T>;
  183. using pointer = std::add_pointer_t<value_type>;
  184. using reference = std::add_lvalue_reference_t<value_type>;
  185. friend rbtree;
  186. private:
  187. node_pointer p;
  188. public:
  189. constexpr _iterator() = default;
  190. explicit constexpr _iterator(node_pointer ptr)
  191. : p { ptr } {}
  192. constexpr _iterator(const _iterator& iter) = default;
  193. constexpr _iterator(_iterator&& iter) = default;
  194. constexpr ~_iterator() = default;
  195. constexpr _iterator& operator=(const _iterator& iter) = default;
  196. constexpr _iterator& operator=(_iterator&& iter) = default;
  197. constexpr bool operator==(const _iterator& iter) const = default;
  198. constexpr reference operator*(void) const { return p->value; }
  199. constexpr pointer operator&(void) const { return std::addressof(p->value); }
  200. constexpr pointer operator->(void) const { return this->operator&(); }
  201. constexpr _iterator& operator++(void)
  202. { p = p->next(); return *this; }
  203. constexpr _iterator operator++(int)
  204. { _iterator ret(p); (void)this->operator++(); return ret; }
  205. constexpr _iterator& operator--(void)
  206. { p = p->prev(); return *this; }
  207. constexpr _iterator operator--(int)
  208. { _iterator ret(p); (void)this->operator--(); return ret; }
  209. constexpr operator bool(void)
  210. { return p; }
  211. constexpr operator _iterator<true>()
  212. { return _iterator<true> { p }; }
  213. };
  214. using iterator = _iterator<false>;
  215. using const_iterator = _iterator<true>;
  216. using node_allocator = typename
  217. std::allocator_traits<Allocator>::template rebind_alloc<node>;
  218. using node_alloc_traits = std::allocator_traits<node_allocator>;
  219. node* root;
  220. node_allocator alloc;
  221. std::size_t _size;
  222. Compare comp;
  223. private:
  224. template <typename... Args>
  225. constexpr node* newnode(Args&&... key)
  226. {
  227. node* ptr = node_alloc_traits::allocate(alloc, 1);
  228. node_alloc_traits::construct(alloc, ptr, std::forward<Args>(key)...);
  229. return ptr;
  230. }
  231. constexpr void delnode(node* nd)
  232. {
  233. node_alloc_traits::destroy(alloc, nd);
  234. node_alloc_traits::deallocate(alloc, nd, 1);
  235. }
  236. constexpr void do_insertion(node* parent,
  237. node*& child_inserted, node* nd)
  238. {
  239. nd->parent = parent;
  240. child_inserted = nd;
  241. this->balance(nd);
  242. ++_size;
  243. }
  244. public:
  245. constexpr iterator end(void) noexcept
  246. { return iterator(nullptr); }
  247. constexpr const_iterator end(void) const noexcept
  248. { return const_iterator(nullptr); }
  249. constexpr const_iterator cend(void) const noexcept
  250. { return const_iterator(nullptr); }
  251. constexpr iterator begin(void) noexcept
  252. { return root ? iterator(root->leftmost()) : end(); }
  253. constexpr const_iterator begin(void) const noexcept
  254. { return root ? const_iterator(root->leftmost()) : end(); }
  255. constexpr const_iterator cbegin(void) const noexcept
  256. { return root ? const_iterator(root->leftmost()) : end(); }
  257. constexpr void destroy(node* nd)
  258. {
  259. if (!nd)
  260. return;
  261. destroy(nd->left);
  262. destroy(nd->right);
  263. delnode(nd);
  264. }
  265. constexpr void destroy() { destroy(root); root = nullptr; _size = 0; }
  266. constexpr node* copy(node* nd)
  267. {
  268. if (!nd)
  269. return nullptr;
  270. node* newnd = newnode(nd->value);
  271. newnd->color = nd->color;
  272. ++_size;
  273. newnd->left = copy(nd->left);
  274. if (newnd->left)
  275. newnd->left->parent = newnd->left;
  276. newnd->right = copy(nd->right);
  277. if (newnd->right)
  278. newnd->right->parent = newnd->right;
  279. return newnd;
  280. }
  281. explicit constexpr rbtree(const Compare& comp, const node_allocator& alloc)
  282. : root(), alloc(alloc), _size(), comp(comp) {}
  283. constexpr rbtree(const rbtree& other)
  284. : rbtree(other.comp, other.alloc)
  285. {
  286. root = copy(other.root);
  287. if (root)
  288. root->parent = nullptr;
  289. }
  290. constexpr rbtree(const rbtree& other, const node_allocator& alloc)
  291. : rbtree(other.comp, alloc)
  292. {
  293. root = copy(other.root);
  294. if (root)
  295. root->parent = nullptr;
  296. }
  297. constexpr rbtree(rbtree&& other) noexcept
  298. : root(std::exchange(other.root, nullptr))
  299. , alloc(std::move(other.alloc))
  300. , _size(std::exchange(other._size, 0))
  301. , comp(std::move(other.comp)) {}
  302. constexpr rbtree(rbtree&& other, const node_allocator& alloc) noexcept
  303. : root(std::exchange(other.root, nullptr))
  304. , alloc(alloc) , _size(std::exchange(other._size, 0))
  305. , comp(std::move(other.comp)) {}
  306. constexpr ~rbtree() { destroy(); }
  307. constexpr rbtree& operator=(const rbtree& other)
  308. {
  309. destroy(root);
  310. comp = other.comp;
  311. if constexpr (node_alloc_traits::
  312. propagate_on_container_copy_assignment::value)
  313. alloc = other.alloc;
  314. root = copy(other.root);
  315. if (root)
  316. root->parent = nullptr;
  317. }
  318. constexpr rbtree& operator=(rbtree&& other) noexcept
  319. {
  320. destroy(root);
  321. root = std::exchange(other.root, nullptr);
  322. _size = std::exchange(other._size, 0);
  323. comp = std::move(other.comp);
  324. if constexpr (node_alloc_traits::
  325. propagate_on_container_move_assignment::value)
  326. alloc = std::move(other.alloc);
  327. }
  328. constexpr void rotateleft(node* rt)
  329. {
  330. node* nrt = rt->right;
  331. if (!rt->is_root()) {
  332. if (rt->is_left_child())
  333. rt->parent->left = nrt;
  334. else
  335. rt->parent->right = nrt;
  336. } else {
  337. this->root = nrt;
  338. }
  339. nrt->parent = rt->parent;
  340. rt->parent = nrt;
  341. rt->right = nrt->left;
  342. if (rt->right)
  343. rt->right->parent = rt;
  344. nrt->left = rt;
  345. }
  346. constexpr void rotateright(node* rt)
  347. {
  348. node* nrt = rt->left;
  349. if (!rt->is_root()) {
  350. if (rt->is_left_child())
  351. rt->parent->left = nrt;
  352. else
  353. rt->parent->right = nrt;
  354. } else {
  355. this->root = nrt;
  356. }
  357. nrt->parent = rt->parent;
  358. rt->parent = nrt;
  359. rt->left = nrt->right;
  360. if (rt->left)
  361. rt->left->parent = rt;
  362. nrt->right = rt;
  363. }
  364. constexpr void balance(node* nd)
  365. {
  366. if (nd->is_root()) {
  367. nd->toblack();
  368. return;
  369. }
  370. if (node::is_black(nd->parent))
  371. return;
  372. node* p = nd->parent;
  373. node* pp = nd->grandparent();
  374. node* uncle = nd->uncle();
  375. if (node::is_red(uncle)) {
  376. p->toblack();
  377. uncle->toblack();
  378. pp->tored();
  379. this->balance(pp);
  380. return;
  381. }
  382. if (p->is_left_child()) {
  383. if (nd->is_left_child()) {
  384. p->toblack();
  385. pp->tored();
  386. this->rotateright(pp);
  387. } else {
  388. this->rotateleft(p);
  389. this->balance(p);
  390. }
  391. } else {
  392. if (nd->is_right_child()) {
  393. p->toblack();
  394. pp->tored();
  395. this->rotateleft(pp);
  396. } else {
  397. this->rotateright(p);
  398. this->balance(p);
  399. }
  400. }
  401. }
  402. template <typename U>
  403. constexpr node* _find(const U& key) const
  404. {
  405. for (node* cur = root; cur; ) {
  406. if (comp(key, cur->value))
  407. cur = cur->left;
  408. else if (comp(cur->value, key))
  409. cur = cur->right;
  410. else
  411. return cur;
  412. }
  413. return nullptr;
  414. }
  415. template <typename U>
  416. constexpr iterator find(const U& key) const
  417. { return iterator { _find(key) }; }
  418. // RBTREE RECURSIVE DELETE
  419. // THIS FUNCTION DOES NOT DELLOCATE THE NODE
  420. // CALLER IS RESPONSIBLE FOR FREEING THE MEMORY
  421. // @param: nd is guaranteed to be a leaf node
  422. constexpr void _erase(node* nd)
  423. {
  424. if (nd->is_root())
  425. return;
  426. if (node::is_black(nd)) {
  427. node* p = nd->parent;
  428. node* s = nullptr;
  429. if (nd->is_left_child())
  430. s = p->right;
  431. else
  432. s = p->left;
  433. if (node::is_red(s)) {
  434. p->tored();
  435. s->toblack();
  436. if (nd->is_right_child()) {
  437. this->rotateright(p);
  438. s = p->left;
  439. } else {
  440. this->rotateleft(p);
  441. s = p->right;
  442. }
  443. }
  444. node* r = nullptr;
  445. if (node::is_red(s->left)) {
  446. r = s->left;
  447. if (s->is_left_child()) {
  448. r->toblack();
  449. s->color = p->color;
  450. this->rotateright(p);
  451. p->toblack();
  452. } else {
  453. r->color = p->color;
  454. this->rotateright(s);
  455. this->rotateleft(p);
  456. p->toblack();
  457. }
  458. } else if (node::is_red(s->right)) {
  459. r = s->right;
  460. if (s->is_left_child()) {
  461. r->color = p->color;
  462. this->rotateleft(s);
  463. this->rotateright(p);
  464. p->toblack();
  465. } else {
  466. r->toblack();
  467. s->color = p->color;
  468. this->rotateleft(p);
  469. p->toblack();
  470. }
  471. } else {
  472. s->tored();
  473. if (node::is_black(p))
  474. this->_erase(p);
  475. else
  476. p->toblack();
  477. }
  478. }
  479. }
  480. // delete nd from the tree. make nd safe to deallocate
  481. // THIS FUNCTION DOES NOT DELLOCATE THE NODE
  482. // CALLER IS RESPONSIBLE FOR FREEING THE MEMORY
  483. constexpr node* erase(node* nd)
  484. {
  485. if (nd->is_root() && nd->is_leaf()) {
  486. root = nullptr;
  487. return nullptr;
  488. }
  489. node* next = nd->next();
  490. while (!nd->is_leaf()) {
  491. node* alt = nd->right ? nd->right->leftmost() : nd->left;
  492. if (nd->is_root())
  493. this->root = alt;
  494. node::swap(nd, alt);
  495. }
  496. this->_erase(nd);
  497. if (nd->is_left_child())
  498. nd->parent->left = nullptr;
  499. else
  500. nd->parent->right = nullptr;
  501. return next;
  502. }
  503. constexpr iterator erase(iterator pos) noexcept
  504. {
  505. node* nextpos = erase(pos.p);
  506. delnode(pos.p);
  507. --_size;
  508. return iterator { nextpos };
  509. }
  510. constexpr iterator erase(const_iterator pos) noexcept
  511. {
  512. node* nextpos = erase(pos.p);
  513. delnode(pos.p);
  514. --_size;
  515. return const_iterator { nextpos };
  516. }
  517. template <typename U>
  518. constexpr iterator lower_bound(U&& val) const
  519. {
  520. node* cur = root;
  521. node* result = nullptr;
  522. while (cur) {
  523. if (!comp(cur->value, val)) {
  524. result = cur;
  525. cur = cur->left;
  526. }
  527. else {
  528. cur = cur->right;
  529. }
  530. }
  531. return iterator { result };
  532. }
  533. template <typename U>
  534. constexpr iterator upper_bound(U&& val) const
  535. {
  536. iterator iter = lower_bound(std::forward<U>(val));
  537. if (iter && !comp(*iter, val) && !comp(val, *iter))
  538. return ++iter;
  539. return iter;
  540. }
  541. // value in nd MUST NOT exist in the rbtree,
  542. // that is, if a < b, then a > b
  543. constexpr void insert(node* nd)
  544. {
  545. node* cur = root;
  546. while (cur) {
  547. if (comp(nd->value, cur->value)) {
  548. if (!cur->left) {
  549. do_insertion(cur, cur->left, nd);
  550. return;
  551. }
  552. cur = cur->left;
  553. } else {
  554. if (!cur->right) {
  555. do_insertion(cur, cur->right, nd);
  556. return;
  557. }
  558. cur = cur->right;
  559. }
  560. }
  561. do_insertion(cur, root, nd);
  562. }
  563. template <typename U>
  564. constexpr std::pair<iterator, bool> insert(U&& value)
  565. {
  566. auto iter = find(value);
  567. if (iter)
  568. return { iter, false };
  569. node* ptr = newnode(std::forward<U>(value));
  570. insert(ptr);
  571. return { iterator { ptr }, true };
  572. }
  573. template <typename... Args>
  574. constexpr std::pair<iterator, bool> emplace(Args&&... args)
  575. {
  576. node* nd = newnode(std::forward<Args>(args)...);
  577. node* exist_nd = _find(nd->value);
  578. if (exist_nd) {
  579. delnode(nd);
  580. return { iterator { exist_nd }, false };
  581. }
  582. insert(nd);
  583. return { iterator { nd }, true };
  584. }
  585. constexpr bool empty() const noexcept { return !root; }
  586. constexpr std::size_t size() const noexcept { return _size; }
  587. constexpr void swap(rbtree& other)
  588. {
  589. std::swap(root, other.root);
  590. std::swap(_size, other._size);
  591. std::swap(comp, other.comp);
  592. if constexpr (node_alloc_traits::propagate_on_container_swap::value)
  593. std::swap(alloc, other.alloc);
  594. }
  595. };
  596. } // namespace std::impl
  597. #endif