rbtree 19 KB

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