rbtree 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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::propagate_on_copy_assignment::value)
  312. alloc = other.alloc;
  313. root = copy(other.root);
  314. if (root)
  315. root->parent = nullptr;
  316. }
  317. constexpr rbtree& operator=(rbtree&& other) noexcept
  318. {
  319. destroy(root);
  320. root = std::exchange(other.root, nullptr);
  321. _size = std::exchange(other._size, 0);
  322. comp = std::move(other.comp);
  323. if constexpr (node_alloc_traits::propagate_on_move_assignment::value)
  324. alloc = std::move(other.alloc);
  325. }
  326. constexpr void rotateleft(node* rt)
  327. {
  328. node* nrt = rt->right;
  329. if (!rt->is_root()) {
  330. if (rt->is_left_child())
  331. rt->parent->left = nrt;
  332. else
  333. rt->parent->right = nrt;
  334. } else {
  335. this->root = nrt;
  336. }
  337. nrt->parent = rt->parent;
  338. rt->parent = nrt;
  339. rt->right = nrt->left;
  340. nrt->left = rt;
  341. }
  342. constexpr void rotateright(node* rt)
  343. {
  344. node* nrt = rt->left;
  345. if (!rt->is_root()) {
  346. if (rt->is_left_child())
  347. rt->parent->left = nrt;
  348. else
  349. rt->parent->right = nrt;
  350. } else {
  351. this->root = nrt;
  352. }
  353. nrt->parent = rt->parent;
  354. rt->parent = nrt;
  355. rt->left = nrt->right;
  356. nrt->right = rt;
  357. }
  358. constexpr void balance(node* nd)
  359. {
  360. if (nd->is_root()) {
  361. nd->toblack();
  362. return;
  363. }
  364. if (node::is_black(nd->parent))
  365. return;
  366. node* p = nd->parent;
  367. node* pp = nd->grandparent();
  368. node* uncle = nd->uncle();
  369. if (node::is_red(uncle)) {
  370. p->toblack();
  371. uncle->toblack();
  372. pp->tored();
  373. this->balance(pp);
  374. return;
  375. }
  376. if (p->is_left_child()) {
  377. if (nd->is_left_child()) {
  378. p->toblack();
  379. pp->tored();
  380. this->rotateright(pp);
  381. } else {
  382. this->rotateleft(p);
  383. this->balance(p);
  384. }
  385. } else {
  386. if (nd->is_right_child()) {
  387. p->toblack();
  388. pp->tored();
  389. this->rotateleft(pp);
  390. } else {
  391. this->rotateright(p);
  392. this->balance(p);
  393. }
  394. }
  395. }
  396. template <typename U>
  397. constexpr node* _find(const U& key) const
  398. {
  399. for (node* cur = root; cur; ) {
  400. if (comp(key, cur->value))
  401. cur = cur->left;
  402. else if (comp(cur->value, key))
  403. cur = cur->right;
  404. else
  405. return cur;
  406. }
  407. return nullptr;
  408. }
  409. template <typename U>
  410. constexpr iterator find(const U& key) const
  411. { return iterator { _find(key) }; }
  412. // RBTREE RECURSIVE DELETE
  413. // THIS FUNCTION DOES NOT DELLOCATE THE NODE
  414. // CALLER IS RESPONSIBLE FOR FREEING THE MEMORY
  415. // @param: nd is guaranteed to be a leaf node
  416. constexpr void _erase(node* nd)
  417. {
  418. if (nd->is_root())
  419. return;
  420. if (node::is_black(nd)) {
  421. node* p = nd->parent;
  422. node* s = nullptr;
  423. if (nd->is_left_child())
  424. s = p->right;
  425. else
  426. s = p->left;
  427. if (node::is_red(s)) {
  428. p->tored();
  429. s->toblack();
  430. if (nd->is_right_child()) {
  431. this->rotateright(p);
  432. s = p->left;
  433. } else {
  434. this->rotateleft(p);
  435. s = p->right;
  436. }
  437. }
  438. node* r = nullptr;
  439. if (node::is_red(s->left)) {
  440. r = s->left;
  441. if (s->is_left_child()) {
  442. r->toblack();
  443. s->color = p->color;
  444. this->rotateright(p);
  445. p->toblack();
  446. } else {
  447. r->color = p->color;
  448. this->rotateright(s);
  449. this->rotateleft(p);
  450. p->toblack();
  451. }
  452. } else if (node::is_red(s->right)) {
  453. r = s->right;
  454. if (s->is_left_child()) {
  455. r->color = p->color;
  456. this->rotateleft(s);
  457. this->rotateright(p);
  458. p->toblack();
  459. } else {
  460. r->toblack();
  461. s->color = p->color;
  462. this->rotateleft(p);
  463. p->toblack();
  464. }
  465. } else {
  466. s->tored();
  467. if (node::is_black(p))
  468. this->_erase(p);
  469. else
  470. p->toblack();
  471. }
  472. }
  473. }
  474. // delete nd from the tree. make nd safe to deallocate
  475. // THIS FUNCTION DOES NOT DELLOCATE THE NODE
  476. // CALLER IS RESPONSIBLE FOR FREEING THE MEMORY
  477. constexpr node* erase(node* nd)
  478. {
  479. if (nd->is_root() && nd->is_leaf()) {
  480. root = nullptr;
  481. return nullptr;
  482. }
  483. node* next = nd->next();
  484. while (!nd->is_leaf()) {
  485. node* alt = nd->right ? nd->right->leftmost() : nd->left;
  486. if (nd->is_root())
  487. this->root = alt;
  488. node::swap(nd, alt);
  489. }
  490. this->_erase(nd);
  491. if (nd->is_left_child())
  492. nd->parent->left = nullptr;
  493. else
  494. nd->parent->right = nullptr;
  495. return next;
  496. }
  497. constexpr iterator erase(iterator pos) noexcept
  498. {
  499. node* nextpos = erase(pos.p);
  500. delnode(pos.p);
  501. --_size;
  502. return iterator { nextpos };
  503. }
  504. constexpr iterator erase(const_iterator pos) noexcept
  505. {
  506. node* nextpos = erase(pos.p);
  507. delnode(pos.p);
  508. --_size;
  509. return const_iterator { nextpos };
  510. }
  511. template <typename U>
  512. constexpr iterator lower_bound(U&& val) const
  513. {
  514. node* cur = root;
  515. node* result = nullptr;
  516. while (cur) {
  517. if (!comp(cur->value, val)) {
  518. result = cur;
  519. cur = cur->left;
  520. }
  521. else {
  522. cur = cur->right;
  523. }
  524. }
  525. return iterator { result };
  526. }
  527. template <typename U>
  528. constexpr iterator upper_bound(U&& val) const
  529. {
  530. iterator iter = lower_bound(std::forward<U>(val));
  531. if (iter && !comp(*iter, val) && !comp(val, *iter))
  532. return ++iter;
  533. return iter;
  534. }
  535. // value in nd MUST NOT exist in the rbtree,
  536. // that is, if a < b, then a > b
  537. constexpr void insert(node* nd)
  538. {
  539. node* cur = root;
  540. while (cur) {
  541. if (comp(nd->value, cur->value)) {
  542. if (!cur->left) {
  543. do_insertion(cur, cur->left, nd);
  544. return;
  545. }
  546. cur = cur->left;
  547. } else {
  548. if (!cur->right) {
  549. do_insertion(cur, cur->right, nd);
  550. return;
  551. }
  552. cur = cur->right;
  553. }
  554. }
  555. do_insertion(cur, root, nd);
  556. }
  557. template <typename U>
  558. constexpr std::pair<iterator, bool> insert(U&& value)
  559. {
  560. auto iter = find(value);
  561. if (iter)
  562. return { iter, false };
  563. node* ptr = newnode(std::forward<U>(value));
  564. insert(ptr);
  565. return { iterator { ptr }, true };
  566. }
  567. template <typename... Args>
  568. constexpr std::pair<iterator, bool> emplace(Args&&... args)
  569. {
  570. node* nd = newnode(std::forward<Args>(args)...);
  571. node* exist_nd = _find(nd->value);
  572. if (exist_nd) {
  573. delnode(nd);
  574. return { iterator { exist_nd }, false };
  575. }
  576. insert(nd);
  577. return { iterator { nd }, true };
  578. }
  579. constexpr bool empty() const noexcept { return !root; }
  580. constexpr std::size_t size() const noexcept { return _size; }
  581. constexpr void swap(rbtree& other)
  582. {
  583. std::swap(root, other.root);
  584. std::swap(_size, other._size);
  585. std::swap(comp, other.comp);
  586. if constexpr (node_alloc_traits::propagate_on_container_swap::value)
  587. std::swap(alloc, other.alloc);
  588. }
  589. };
  590. } // namespace std::impl
  591. #endif