rbtree 19 KB

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