rbtree 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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. template <typename U>
  380. constexpr node* _find(const U& key) const
  381. {
  382. for (node* cur = root; cur; ) {
  383. if (comp(key, cur->value))
  384. cur = cur->left;
  385. else if (comp(cur->value, key))
  386. cur = cur->right;
  387. else
  388. return cur;
  389. }
  390. return nullptr;
  391. }
  392. template <typename U>
  393. constexpr iterator find(const U& key) const
  394. { return iterator { _find(key) }; }
  395. // RBTREE RECURSIVE DELETE
  396. // THIS FUNCTION DOES NOT DELLOCATE THE NODE
  397. // CALLER IS RESPONSIBLE FOR FREEING THE MEMORY
  398. // @param: nd is guaranteed to be a leaf node
  399. constexpr void _erase(node* nd)
  400. {
  401. if (nd->is_root())
  402. return;
  403. if (node::is_black(nd)) {
  404. node* p = nd->parent;
  405. node* s = nullptr;
  406. if (nd->is_left_child())
  407. s = p->right;
  408. else
  409. s = p->left;
  410. if (node::is_red(s)) {
  411. p->tored();
  412. s->toblack();
  413. if (nd->is_right_child()) {
  414. this->rotateright(p);
  415. s = p->left;
  416. } else {
  417. this->rotateleft(p);
  418. s = p->right;
  419. }
  420. }
  421. node* r = nullptr;
  422. if (node::is_red(s->left)) {
  423. r = s->left;
  424. if (s->is_left_child()) {
  425. r->toblack();
  426. s->color = p->color;
  427. this->rotateright(p);
  428. p->toblack();
  429. } else {
  430. r->color = p->color;
  431. this->rotateright(s);
  432. this->rotateleft(p);
  433. p->toblack();
  434. }
  435. } else if (node::is_red(s->right)) {
  436. r = s->right;
  437. if (s->is_left_child()) {
  438. r->color = p->color;
  439. this->rotateleft(s);
  440. this->rotateright(p);
  441. p->toblack();
  442. } else {
  443. r->toblack();
  444. s->color = p->color;
  445. this->rotateleft(p);
  446. p->toblack();
  447. }
  448. } else {
  449. s->tored();
  450. if (node::is_black(p))
  451. this->_erase(p);
  452. else
  453. p->toblack();
  454. }
  455. }
  456. }
  457. // delete nd from the tree. make nd safe to deallocate
  458. // THIS FUNCTION DOES NOT DELLOCATE THE NODE
  459. // CALLER IS RESPONSIBLE FOR FREEING THE MEMORY
  460. constexpr node* erase(node* nd)
  461. {
  462. if (nd->is_root() && nd->is_leaf()) {
  463. root = nullptr;
  464. return nullptr;
  465. }
  466. node* next = nd->next();
  467. while (!nd->is_leaf()) {
  468. node* alt = nd->right ? nd->right->leftmost() : nd->left;
  469. if (nd->is_root())
  470. this->root = alt;
  471. node::swap(nd, alt);
  472. }
  473. this->_erase(nd);
  474. if (nd->is_left_child())
  475. nd->parent->left = nullptr;
  476. else
  477. nd->parent->right = nullptr;
  478. return next;
  479. }
  480. constexpr iterator erase(iterator pos) noexcept
  481. {
  482. node* nextpos = erase(pos.p);
  483. delnode(pos.p);
  484. --_size;
  485. return iterator { nextpos };
  486. }
  487. constexpr iterator erase(const_iterator pos) noexcept
  488. {
  489. node* nextpos = erase(pos.p);
  490. delnode(pos.p);
  491. --_size;
  492. return const_iterator { nextpos };
  493. }
  494. // value in nd MUST NOT exist in the rbtree,
  495. // that is, if a < b, then a > b
  496. constexpr void insert(node* nd)
  497. {
  498. node* cur = root;
  499. while (cur) [[likely]] {
  500. if (comp(nd->value, cur->value)) {
  501. if (!cur->left) {
  502. nd->parent = cur;
  503. cur->left = nd;
  504. this->balance(nd);
  505. ++_size;
  506. return;
  507. } else {
  508. cur = cur->left;
  509. }
  510. } else {
  511. if (!cur->right) {
  512. nd->parent = cur;
  513. cur->right = nd;
  514. this->balance(nd);
  515. ++_size;
  516. return;
  517. } else {
  518. cur = cur->right;
  519. }
  520. }
  521. }
  522. root = nd;
  523. root->toblack();
  524. ++_size;
  525. }
  526. template <typename U>
  527. constexpr std::pair<iterator, bool> insert(U&& value)
  528. {
  529. auto iter = find(value);
  530. if (iter)
  531. return { iter, false };
  532. node* ptr = newnode(std::forward<U>(value));
  533. insert(ptr);
  534. return { iterator { ptr }, true };
  535. }
  536. template <typename... Args>
  537. constexpr std::pair<iterator, bool> emplace(Args&&... args)
  538. {
  539. node* nd = newnode(std::forward<Args>(args)...);
  540. node* exist_nd = _find(nd->value);
  541. if (exist_nd) {
  542. delnode(nd);
  543. return { iterator { exist_nd }, false };
  544. }
  545. insert(nd);
  546. return { iterator { nd }, true };
  547. }
  548. constexpr bool empty() const noexcept { return !root; }
  549. constexpr std::size_t size() const noexcept { return _size; }
  550. constexpr void swap(rbtree& other)
  551. {
  552. std::swap(root, other.root);
  553. std::swap(_size, other._size);
  554. std::swap(comp, other.comp);
  555. if constexpr (node_alloc_traits::propagate_on_container_swap::value)
  556. std::swap(alloc, other.alloc);
  557. }
  558. };
  559. } // namespace std::impl
  560. #endif