rbtree 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. node* root;
  211. Compare comp;
  212. node_allocator alloc;
  213. private:
  214. template <typename UKey>
  215. constexpr node* newnode(UKey&& key)
  216. {
  217. node* ptr = std::allocator_traits<node_allocator>::allocate(alloc, 1);
  218. std::allocator_traits<node_allocator>::construct(
  219. alloc, ptr, std::forward<UKey>(key));
  220. return ptr;
  221. }
  222. constexpr void delnode(node* nd)
  223. {
  224. std::allocator_traits<node_allocator>::destroy(alloc, nd);
  225. std::allocator_traits<node_allocator>::deallocate(alloc, nd, 1);
  226. }
  227. public:
  228. constexpr iterator end(void) noexcept
  229. { return iterator(nullptr); }
  230. constexpr const_iterator end(void) const noexcept
  231. { return const_iterator(nullptr); }
  232. constexpr const_iterator cend(void) const noexcept
  233. { return const_iterator(nullptr); }
  234. constexpr iterator begin(void) noexcept
  235. { return root ? iterator(root->leftmost()) : end(); }
  236. constexpr const_iterator begin(void) const noexcept
  237. { return root ? const_iterator(root->leftmost()) : end(); }
  238. constexpr const_iterator cbegin(void) const noexcept
  239. { return root ? const_iterator(root->leftmost()) : end(); }
  240. constexpr void destroy(node* nd)
  241. {
  242. if (!nd)
  243. return;
  244. destroy(nd->left);
  245. destroy(nd->right);
  246. delnode(nd);
  247. }
  248. constexpr void destroy() { destroy(root); root = nullptr; }
  249. constexpr node* copy(node* nd)
  250. {
  251. if (!nd)
  252. return nullptr;
  253. node* newnd = newnode(nd->value);
  254. newnd->color = nd->color;
  255. newnd->left = copy(nd->left);
  256. if (newnd->left)
  257. newnd->left->parent = newnd->left;
  258. newnd->right = copy(nd->right);
  259. if (newnd->right)
  260. newnd->right->parent = newnd->right;
  261. return newnd;
  262. }
  263. explicit constexpr rbtree(const Compare& comp, const node_allocator& alloc)
  264. : root(), comp(comp), alloc(alloc) {}
  265. constexpr rbtree(const rbtree& other)
  266. : rbtree(other.comp, other.alloc)
  267. {
  268. root = copy(other.root);
  269. if (root)
  270. root->parent = nullptr;
  271. }
  272. constexpr rbtree(const rbtree& other, const node_allocator& alloc)
  273. : rbtree(other.comp, alloc)
  274. {
  275. root = copy(other.root);
  276. if (root)
  277. root->parent = nullptr;
  278. }
  279. constexpr rbtree(rbtree&& other) noexcept
  280. : root(std::exchange(other.root, nullptr))
  281. , comp(std::move(other.comp)), alloc(std::move(other.alloc)) {}
  282. constexpr rbtree(rbtree&& other, const node_allocator& alloc) noexcept
  283. : root(std::exchange(other.root, nullptr))
  284. , comp(std::move(other.comp)), alloc(alloc) {}
  285. constexpr ~rbtree() { destroy(); }
  286. constexpr rbtree& operator=(const rbtree& other)
  287. {
  288. destroy(root);
  289. comp = other.comp;
  290. alloc = other.alloc;
  291. root = copy(other.root);
  292. if (root)
  293. root->parent = nullptr;
  294. }
  295. constexpr rbtree& operator=(rbtree&& other) noexcept
  296. {
  297. destroy(root);
  298. root = std::exchange(other.root, nullptr);
  299. comp = std::move(other.comp);
  300. alloc = std::move(other.alloc);
  301. }
  302. constexpr void rotateleft(node* rt)
  303. {
  304. node* nrt = rt->right;
  305. if (!rt->is_root()) {
  306. if (rt->is_left_child())
  307. rt->parent->left = nrt;
  308. else
  309. rt->parent->right = nrt;
  310. } else {
  311. this->root = nrt;
  312. }
  313. nrt->parent = rt->parent;
  314. rt->parent = nrt;
  315. rt->right = nrt->left;
  316. nrt->left = rt;
  317. }
  318. constexpr void rotateright(node* rt)
  319. {
  320. node* nrt = rt->left;
  321. if (!rt->is_root()) {
  322. if (rt->is_left_child())
  323. rt->parent->left = nrt;
  324. else
  325. rt->parent->right = nrt;
  326. } else {
  327. this->root = nrt;
  328. }
  329. nrt->parent = rt->parent;
  330. rt->parent = nrt;
  331. rt->left = nrt->right;
  332. nrt->right = rt;
  333. }
  334. constexpr void balance(node* nd)
  335. {
  336. if (nd->is_root()) {
  337. nd->toblack();
  338. return;
  339. }
  340. if (node::is_black(nd->parent))
  341. return;
  342. node* p = nd->parent;
  343. node* pp = nd->grandparent();
  344. node* uncle = nd->uncle();
  345. if (node::is_red(uncle)) {
  346. p->toblack();
  347. uncle->toblack();
  348. pp->tored();
  349. this->balance(pp);
  350. return;
  351. }
  352. if (p->is_left_child()) {
  353. if (nd->is_left_child()) {
  354. p->toblack();
  355. pp->tored();
  356. this->rotateright(pp);
  357. } else {
  358. this->rotateleft(p);
  359. this->balance(p);
  360. }
  361. } else {
  362. if (nd->is_right_child()) {
  363. p->toblack();
  364. pp->tored();
  365. this->rotateleft(pp);
  366. } else {
  367. this->rotateright(p);
  368. this->balance(p);
  369. }
  370. }
  371. }
  372. constexpr node* _find(const T& key) const
  373. {
  374. for (node* cur = root; cur; ) {
  375. if (comp(key, cur->value))
  376. cur = cur->left;
  377. else if (comp(cur->value, key))
  378. cur = cur->right;
  379. else
  380. return cur;
  381. }
  382. return nullptr;
  383. }
  384. template <typename U, typename UCompare>
  385. constexpr node* _find(const U& key, const UCompare& ucomp) const
  386. {
  387. for (node* cur = root; cur; ) {
  388. if (ucomp(key, cur->value))
  389. cur = cur->left;
  390. else if (ucomp(cur->value, key))
  391. cur = cur->right;
  392. else
  393. return cur;
  394. }
  395. return nullptr;
  396. }
  397. constexpr iterator find(const T& key)
  398. { return iterator { _find(key) }; }
  399. constexpr const_iterator find(const T& key) const
  400. { return const_iterator { _find(key) }; }
  401. // RBTREE RECURSIVE DELETE
  402. // THIS FUNCTION DOES NOT DELLOCATE THE NODE
  403. // CALLER IS RESPONSIBLE FOR FREEING THE MEMORY
  404. // @param: nd is guaranteed to be a leaf node
  405. constexpr void _erase(node* nd)
  406. {
  407. if (nd->is_root())
  408. return;
  409. if (node::is_black(nd)) {
  410. node* p = nd->parent;
  411. node* s = nullptr;
  412. if (nd->is_left_child())
  413. s = p->right;
  414. else
  415. s = p->left;
  416. if (node::is_red(s)) {
  417. p->tored();
  418. s->toblack();
  419. if (nd->is_right_child()) {
  420. this->rotateright(p);
  421. s = p->left;
  422. } else {
  423. this->rotateleft(p);
  424. s = p->right;
  425. }
  426. }
  427. node* r = nullptr;
  428. if (node::is_red(s->left)) {
  429. r = s->left;
  430. if (s->is_left_child()) {
  431. r->toblack();
  432. s->color = p->color;
  433. this->rotateright(p);
  434. p->toblack();
  435. } else {
  436. r->color = p->color;
  437. this->rotateright(s);
  438. this->rotateleft(p);
  439. p->toblack();
  440. }
  441. } else if (node::is_red(s->right)) {
  442. r = s->right;
  443. if (s->is_left_child()) {
  444. r->color = p->color;
  445. this->rotateleft(s);
  446. this->rotateright(p);
  447. p->toblack();
  448. } else {
  449. r->toblack();
  450. s->color = p->color;
  451. this->rotateleft(p);
  452. p->toblack();
  453. }
  454. } else {
  455. s->tored();
  456. if (node::is_black(p))
  457. this->_erase(p);
  458. else
  459. p->toblack();
  460. }
  461. }
  462. }
  463. // delete nd from the tree. make nd safe to deallocate
  464. // THIS FUNCTION DOES NOT DELLOCATE THE NODE
  465. // CALLER IS RESPONSIBLE FOR FREEING THE MEMORY
  466. constexpr node* erase(node* nd)
  467. {
  468. if (nd->is_root() && nd->is_leaf()) {
  469. root = nullptr;
  470. return nullptr;
  471. }
  472. node* next = nd->next();
  473. while (!nd->is_leaf()) {
  474. node* alt = nd->right ? nd->right->leftmost() : nd->left;
  475. if (nd->is_root())
  476. this->root = alt;
  477. node::swap(nd, alt);
  478. }
  479. this->_erase(nd);
  480. if (nd->is_left_child())
  481. nd->parent->left = nullptr;
  482. else
  483. nd->parent->right = nullptr;
  484. return next;
  485. }
  486. constexpr iterator erase(iterator pos) noexcept
  487. {
  488. node* nextpos = erase(pos.p);
  489. delnode(pos.p);
  490. return iterator { nextpos };
  491. }
  492. constexpr iterator erase(const_iterator pos) noexcept
  493. {
  494. node* nextpos = erase(pos.p);
  495. delnode(pos.p);
  496. return const_iterator { nextpos };
  497. }
  498. // value in nd MUST NOT exist in the rbtree,
  499. // that is, if a < b, then a > b
  500. constexpr void insert(node* nd)
  501. {
  502. node* cur = root;
  503. while (cur) [[likely]] {
  504. if (comp(nd->value, cur->value)) {
  505. if (!cur->left) {
  506. nd->parent = cur;
  507. cur->left = nd;
  508. this->balance(nd);
  509. return;
  510. } else {
  511. cur = cur->left;
  512. }
  513. } else {
  514. if (!cur->right) {
  515. nd->parent = cur;
  516. cur->right = nd;
  517. this->balance(nd);
  518. return;
  519. } else {
  520. cur = cur->right;
  521. }
  522. }
  523. }
  524. root = nd;
  525. root->toblack();
  526. }
  527. template <typename U>
  528. constexpr std::pair<iterator, bool> insert(U&& value)
  529. {
  530. auto iter = find(value);
  531. if (iter)
  532. return { iter, false };
  533. node* ptr = newnode(std::forward<U>(value));
  534. insert(ptr);
  535. return { iterator { ptr }, true };
  536. }
  537. constexpr bool empty() const noexcept { return !root; }
  538. };
  539. } // namespace std::impl
  540. #endif