rbtree 19 KB

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