map.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. #pragma once
  2. #include <utility>
  3. #include <type_traits>
  4. #include <types/allocator.hpp>
  5. #include <types/types.h>
  6. namespace types {
  7. template <typename Key, typename Value, template <typename _T> class _Allocator = kernel_allocator>
  8. class map {
  9. public:
  10. using key_type = std::add_const_t<Key>;
  11. using value_type = Value;
  12. using pair_type = std::pair<key_type, value_type>;
  13. struct node {
  14. node* parent = nullptr;
  15. node* left = nullptr;
  16. node* right = nullptr;
  17. enum class node_color {
  18. RED,
  19. BLACK,
  20. } color
  21. = node_color::RED;
  22. pair_type v;
  23. constexpr node(pair_type&& pair)
  24. : v(std::move(pair))
  25. {
  26. }
  27. constexpr node(const pair_type& pair)
  28. : v(pair)
  29. {
  30. }
  31. constexpr node* grandparent(void) const
  32. {
  33. return this->parent->parent;
  34. }
  35. constexpr node* uncle(void) const
  36. {
  37. node* pp = this->grandparent();
  38. return (this->parent == pp->left) ? pp->right : pp->left;
  39. }
  40. constexpr node* leftmost(void)
  41. {
  42. node* nd = this;
  43. while (nd->left)
  44. nd = nd->left;
  45. return nd;
  46. }
  47. constexpr const node* leftmost(void) const
  48. {
  49. const node* nd = this;
  50. while (nd->left)
  51. nd = nd->left;
  52. return nd;
  53. }
  54. constexpr node* rightmost(void)
  55. {
  56. node* nd = this;
  57. while (nd->right)
  58. nd = nd->right;
  59. return nd;
  60. }
  61. constexpr const node* rightmost(void) const
  62. {
  63. const node* nd = this;
  64. while (nd->right)
  65. nd = nd->right;
  66. return nd;
  67. }
  68. constexpr node* next(void)
  69. {
  70. if (this->right) {
  71. return this->right->leftmost();
  72. } else {
  73. if (this->is_root()) {
  74. return nullptr;
  75. } else if (this->is_left_child()) {
  76. return this->parent;
  77. } else {
  78. node* ret = this;
  79. do {
  80. ret = ret->parent;
  81. } while (!ret->is_root() && !ret->is_left_child());
  82. return ret->parent;
  83. }
  84. }
  85. }
  86. constexpr const node* next(void) const
  87. {
  88. if (this->right) {
  89. return this->right->leftmost();
  90. } else {
  91. if (this->is_root()) {
  92. return nullptr;
  93. } else if (this->is_left_child()) {
  94. return this->parent;
  95. } else {
  96. const node* ret = this;
  97. do {
  98. ret = ret->parent;
  99. } while (!ret->is_root() && !ret->is_left_child());
  100. return ret->parent;
  101. }
  102. }
  103. }
  104. constexpr node* prev(void)
  105. {
  106. if (this->left) {
  107. return this->left->rightmost();
  108. } else {
  109. if (this->is_root()) {
  110. return nullptr;
  111. } else if (this->is_right_child()) {
  112. return this->parent;
  113. } else {
  114. node* ret = this;
  115. do {
  116. ret = ret->parent;
  117. } while (!ret->is_root() && !ret->is_right_child());
  118. return ret->parent;
  119. }
  120. }
  121. }
  122. static constexpr bool is_red(node* nd)
  123. {
  124. return nd && nd->color == node_color::RED;
  125. }
  126. static constexpr bool is_black(node* nd)
  127. {
  128. return !node::is_red(nd);
  129. }
  130. constexpr const node* prev(void) const
  131. {
  132. if (this->left) {
  133. return this->left->rightmost();
  134. } else {
  135. if (this->is_root()) {
  136. return nullptr;
  137. } else if (this->is_right_child()) {
  138. return this->parent;
  139. } else {
  140. const node* ret = this;
  141. do {
  142. ret = ret->parent;
  143. } while (!ret->is_root() && !ret->is_right_child());
  144. return ret->parent;
  145. }
  146. }
  147. }
  148. constexpr bool is_root(void) const
  149. {
  150. return this->parent == nullptr;
  151. }
  152. constexpr bool is_full(void) const
  153. {
  154. return this->left && this->right;
  155. }
  156. constexpr bool has_child(void) const
  157. {
  158. return this->left || this->right;
  159. }
  160. constexpr bool is_leaf(void) const
  161. {
  162. return !this->has_child();
  163. }
  164. constexpr bool is_left_child(void) const
  165. {
  166. return this == this->parent->left;
  167. }
  168. constexpr bool is_right_child(void) const
  169. {
  170. return this == this->parent->right;
  171. }
  172. constexpr void tored(void)
  173. {
  174. this->color = node_color::RED;
  175. }
  176. constexpr void toblack(void)
  177. {
  178. this->color = node_color::BLACK;
  179. }
  180. static constexpr void swap(node* first, node* second)
  181. {
  182. if (node::is_red(first)) {
  183. first->color = second->color;
  184. second->color = node_color::RED;
  185. } else {
  186. first->color = second->color;
  187. second->color = node_color::BLACK;
  188. }
  189. if (first->parent == second) {
  190. node* tmp = first;
  191. first = second;
  192. second = tmp;
  193. }
  194. bool f_is_left_child = first->parent ? first->is_left_child() : false;
  195. bool s_is_left_child = second->parent ? second->is_left_child() : false;
  196. node* fp = first->parent;
  197. node* fl = first->left;
  198. node* fr = first->right;
  199. node* sp = second->parent;
  200. node* sl = second->left;
  201. node* sr = second->right;
  202. if (second->parent != first) {
  203. first->parent = sp;
  204. if (sp) {
  205. if (s_is_left_child)
  206. sp->left = first;
  207. else
  208. sp->right = first;
  209. }
  210. first->left = sl;
  211. if (sl)
  212. sl->parent = first;
  213. first->right = sr;
  214. if (sr)
  215. sr->parent = first;
  216. second->parent = fp;
  217. if (fp) {
  218. if (f_is_left_child)
  219. fp->left = second;
  220. else
  221. fp->right = second;
  222. }
  223. second->left = fl;
  224. if (fl)
  225. fl->parent = second;
  226. second->right = fr;
  227. if (fr)
  228. fr->parent = second;
  229. } else {
  230. first->left = sl;
  231. if (sl)
  232. sl->parent = first;
  233. first->right = sr;
  234. if (sr)
  235. sr->parent = first;
  236. second->parent = fp;
  237. if (fp) {
  238. if (f_is_left_child)
  239. fp->left = second;
  240. else
  241. fp->right = second;
  242. }
  243. first->parent = second;
  244. if (s_is_left_child) {
  245. second->left = first;
  246. second->right = fr;
  247. if (fr)
  248. fr->parent = second;
  249. } else {
  250. second->right = first;
  251. second->left = fl;
  252. if (fl)
  253. fl->parent = second;
  254. }
  255. }
  256. }
  257. };
  258. using allocator_type = _Allocator<node>;
  259. template <bool Const>
  260. class iterator {
  261. public:
  262. using node_pointer_type = std::conditional_t<Const, const node*, node*>;
  263. using value_type = std::conditional_t<Const, const pair_type, pair_type>;
  264. using pointer_type = std::add_pointer_t<value_type>;
  265. using reference_type = std::add_lvalue_reference_t<value_type>;
  266. friend class map;
  267. private:
  268. node_pointer_type p;
  269. public:
  270. explicit constexpr iterator(node_pointer_type ptr)
  271. : p { ptr }
  272. {
  273. }
  274. constexpr iterator(const iterator& iter)
  275. : p { iter.p }
  276. {
  277. }
  278. constexpr iterator(iterator&& iter)
  279. : p { iter.p }
  280. {
  281. iter.p = nullptr;
  282. }
  283. constexpr ~iterator()
  284. {
  285. #ifndef NDEBUG
  286. p = nullptr;
  287. #endif
  288. }
  289. constexpr iterator& operator=(const iterator& iter)
  290. {
  291. p = iter.p;
  292. return *this;
  293. }
  294. constexpr iterator& operator=(iterator&& iter)
  295. {
  296. p = iter.p;
  297. iter.p = nullptr;
  298. return *this;
  299. }
  300. constexpr bool operator==(const iterator& iter) const
  301. {
  302. return p == iter.p;
  303. }
  304. constexpr bool operator!=(const iterator& iter) const
  305. {
  306. return !this->operator==(iter);
  307. }
  308. constexpr reference_type operator*(void) const
  309. {
  310. return p->v;
  311. }
  312. constexpr pointer_type operator&(void) const
  313. {
  314. return &p->v;
  315. }
  316. constexpr pointer_type operator->(void) const
  317. {
  318. return this->operator&();
  319. }
  320. constexpr iterator& operator++(void)
  321. {
  322. p = p->next();
  323. return *this;
  324. }
  325. constexpr iterator operator++(int)
  326. {
  327. iterator ret(p);
  328. (void)this->operator++();
  329. return ret;
  330. }
  331. constexpr iterator& operator--(void)
  332. {
  333. p = p->prev();
  334. return *this;
  335. }
  336. constexpr iterator operator--(int)
  337. {
  338. iterator ret(p);
  339. (void)this->operator--();
  340. return ret;
  341. }
  342. explicit constexpr operator bool(void)
  343. {
  344. return p;
  345. }
  346. };
  347. using iterator_type = iterator<false>;
  348. using const_iterator_type = iterator<true>;
  349. private:
  350. node* root = nullptr;
  351. private:
  352. static constexpr node* newnode(node* parent, const pair_type& val)
  353. {
  354. auto* ptr = allocator_traits<allocator_type>::allocate_and_construct(val);
  355. ptr->parent = parent;
  356. return ptr;
  357. }
  358. static constexpr node* newnode(node* parent, pair_type&& val)
  359. {
  360. auto* ptr = allocator_traits<allocator_type>::allocate_and_construct(std::move(val));
  361. ptr->parent = parent;
  362. return ptr;
  363. }
  364. static constexpr void delnode(node* nd)
  365. {
  366. allocator_traits<allocator_type>::deconstruct_and_deallocate(nd);
  367. }
  368. constexpr void rotateleft(node* rt)
  369. {
  370. node* nrt = rt->right;
  371. if (!rt->is_root()) {
  372. if (rt->is_left_child()) {
  373. rt->parent->left = nrt;
  374. } else {
  375. rt->parent->right = nrt;
  376. }
  377. } else {
  378. this->root = nrt;
  379. }
  380. nrt->parent = rt->parent;
  381. rt->parent = nrt;
  382. rt->right = nrt->left;
  383. nrt->left = rt;
  384. }
  385. constexpr void rotateright(node* rt)
  386. {
  387. node* nrt = rt->left;
  388. if (!rt->is_root()) {
  389. if (rt->is_left_child()) {
  390. rt->parent->left = nrt;
  391. } else {
  392. rt->parent->right = nrt;
  393. }
  394. } else {
  395. this->root = nrt;
  396. }
  397. nrt->parent = rt->parent;
  398. rt->parent = nrt;
  399. rt->left = nrt->right;
  400. nrt->right = rt;
  401. }
  402. constexpr void balance(node* nd)
  403. {
  404. if (nd->is_root()) {
  405. nd->toblack();
  406. return;
  407. }
  408. if (node::is_black(nd->parent))
  409. return;
  410. node* p = nd->parent;
  411. node* pp = nd->grandparent();
  412. node* uncle = nd->uncle();
  413. if (node::is_red(uncle)) {
  414. p->toblack();
  415. uncle->toblack();
  416. pp->tored();
  417. this->balance(pp);
  418. return;
  419. }
  420. if (p->is_left_child()) {
  421. if (nd->is_left_child()) {
  422. p->toblack();
  423. pp->tored();
  424. this->rotateright(pp);
  425. } else {
  426. this->rotateleft(p);
  427. this->balance(p);
  428. }
  429. } else {
  430. if (nd->is_right_child()) {
  431. p->toblack();
  432. pp->tored();
  433. this->rotateleft(pp);
  434. } else {
  435. this->rotateright(p);
  436. this->balance(p);
  437. }
  438. }
  439. }
  440. constexpr node* _find(const key_type& key) const
  441. {
  442. for (node* cur = root; cur;) {
  443. if (cur->v.first == key)
  444. return cur;
  445. if (key < cur->v.first)
  446. cur = cur->left;
  447. else
  448. cur = cur->right;
  449. }
  450. return nullptr;
  451. }
  452. // this function DOES NOT dellocate the node
  453. // caller is responsible for freeing the memory
  454. // @param: nd is guaranteed to be a leaf node
  455. constexpr void _erase(node* nd)
  456. {
  457. if (nd->is_root())
  458. return;
  459. if (node::is_black(nd)) {
  460. node* p = nd->parent;
  461. node* s = nullptr;
  462. if (nd->is_left_child())
  463. s = p->right;
  464. else
  465. s = p->left;
  466. if (node::is_red(s)) {
  467. p->tored();
  468. s->toblack();
  469. if (nd->is_right_child()) {
  470. this->rotateright(p);
  471. s = p->left;
  472. } else {
  473. this->rotateleft(p);
  474. s = p->right;
  475. }
  476. }
  477. node* r = nullptr;
  478. if (node::is_red(s->left)) {
  479. r = s->left;
  480. if (s->is_left_child()) {
  481. r->toblack();
  482. s->color = p->color;
  483. this->rotateright(p);
  484. p->toblack();
  485. } else {
  486. r->color = p->color;
  487. this->rotateright(s);
  488. this->rotateleft(p);
  489. p->toblack();
  490. }
  491. } else if (node::is_red(s->right)) {
  492. r = s->right;
  493. if (s->is_left_child()) {
  494. r->color = p->color;
  495. this->rotateleft(s);
  496. this->rotateright(p);
  497. p->toblack();
  498. } else {
  499. r->toblack();
  500. s->color = p->color;
  501. this->rotateleft(p);
  502. p->toblack();
  503. }
  504. } else {
  505. s->tored();
  506. if (node::is_black(p))
  507. this->_erase(p);
  508. else
  509. p->toblack();
  510. }
  511. }
  512. }
  513. public:
  514. constexpr iterator_type end(void)
  515. {
  516. return iterator_type(nullptr);
  517. }
  518. constexpr const_iterator_type end(void) const
  519. {
  520. return const_iterator_type(nullptr);
  521. }
  522. constexpr const_iterator_type cend(void) const
  523. {
  524. return const_iterator_type(nullptr);
  525. }
  526. constexpr iterator_type begin(void)
  527. {
  528. return root ? iterator_type(root->leftmost()) : end();
  529. }
  530. constexpr const_iterator_type begin(void) const
  531. {
  532. return root ? const_iterator_type(root->leftmost()) : end();
  533. }
  534. constexpr const_iterator_type cbegin(void) const
  535. {
  536. return root ? const_iterator_type(root->leftmost()) : end();
  537. }
  538. constexpr iterator_type find(const key_type& key)
  539. {
  540. return iterator_type(_find(key));
  541. }
  542. constexpr const_iterator_type find(const key_type& key) const
  543. {
  544. return const_iterator_type(_find(key));
  545. }
  546. constexpr iterator_type insert(pair_type val)
  547. {
  548. node* cur = root;
  549. while (likely(cur)) {
  550. if (val.first < cur->v.first) {
  551. if (!cur->left) {
  552. node* nd = newnode(cur, std::move(val));
  553. cur->left = nd;
  554. this->balance(nd);
  555. return iterator_type(nd);
  556. } else {
  557. cur = cur->left;
  558. }
  559. } else {
  560. if (!cur->right) {
  561. node* nd = newnode(cur, std::move(val));
  562. cur->right = nd;
  563. this->balance(nd);
  564. return iterator_type(nd);
  565. } else {
  566. cur = cur->right;
  567. }
  568. }
  569. }
  570. root = newnode(nullptr, std::move(val));
  571. root->toblack();
  572. return iterator_type(root);
  573. }
  574. constexpr iterator_type erase(const iterator_type& iter)
  575. {
  576. node* nd = iter.p;
  577. if (!nd)
  578. return end();
  579. if (nd->is_root() && nd->is_leaf()) {
  580. delnode(nd);
  581. root = nullptr;
  582. return end();
  583. }
  584. node* next = nd->next();
  585. while (!nd->is_leaf()) {
  586. node* alt = nd->right ? nd->right->leftmost() : nd->left;
  587. if (nd->is_root()) {
  588. this->root = alt;
  589. }
  590. node::swap(nd, alt);
  591. }
  592. this->_erase(nd);
  593. if (nd->is_left_child())
  594. nd->parent->left = nullptr;
  595. else
  596. nd->parent->right = nullptr;
  597. delnode(nd);
  598. return iterator_type(next);
  599. }
  600. constexpr void remove(const key_type& key)
  601. {
  602. auto iter = this->find(key);
  603. if (iter != this->end())
  604. this->erase(iter);
  605. }
  606. // destroy a subtree without adjusting nodes to maintain binary tree properties
  607. constexpr void destroy(node* nd)
  608. {
  609. if (nd) {
  610. this->destroy(nd->left);
  611. this->destroy(nd->right);
  612. delnode(nd);
  613. }
  614. }
  615. explicit constexpr map(void)
  616. {
  617. }
  618. constexpr map(const map& val)
  619. {
  620. for (const auto& item : val)
  621. this->insert(item);
  622. }
  623. constexpr map(map&& val)
  624. : root(val.root)
  625. {
  626. val.root = nullptr;
  627. }
  628. constexpr map& operator=(const map& val)
  629. {
  630. this->destroy(root);
  631. for (const auto& item : val)
  632. this->insert(item);
  633. }
  634. constexpr map& operator=(map&& val)
  635. {
  636. this->destroy(root);
  637. root = val.root;
  638. val.root = nullptr;
  639. }
  640. constexpr ~map()
  641. {
  642. this->destroy(root);
  643. }
  644. };
  645. } // namespace types