map.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. #pragma once
  2. #include <types/allocator.hpp>
  3. #include <types/cplusplus.hpp>
  4. #include <types/pair.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 = typename traits::add_const<Key>::type;
  11. using value_type = Value;
  12. using pair_type = 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(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. if (second->is_root()) {
  195. node* tmp = first;
  196. first = second;
  197. second = tmp;
  198. }
  199. if (first->is_root()) {
  200. node* sl = second->left;
  201. node* sr = second->right;
  202. if (sl)
  203. sl->parent = first;
  204. if (sr)
  205. sr->parent = first;
  206. if (second->is_right_child()) {
  207. second->left = first->left;
  208. second->right = first;
  209. if (second->left)
  210. second->left->parent = second;
  211. } else {
  212. second->right = first->right;
  213. second->left = first;
  214. if (second->right)
  215. second->right->parent = second;
  216. }
  217. first->left = sl;
  218. first->right = sr;
  219. first->parent = second;
  220. second->parent = nullptr;
  221. return;
  222. }
  223. node* p = first->parent;
  224. node* cl = first->left;
  225. node* cr = first->right;
  226. bool is_first_left = first->is_left_child();
  227. bool is_second_left = second->is_left_child();
  228. if (!first->is_root()) {
  229. if (is_first_left)
  230. p->left = second;
  231. else
  232. p->right = second;
  233. }
  234. first->left = second->left;
  235. first->right = second->right;
  236. if (first->left)
  237. first->left->parent = first;
  238. if (first->right)
  239. first->right->parent = first;
  240. if (second->parent == first) {
  241. first->parent = second;
  242. second->parent = p;
  243. if (is_second_left) {
  244. if (cr)
  245. cr->parent = second;
  246. second->left = first;
  247. second->right = cr;
  248. } else {
  249. if (cl)
  250. cl->parent = second;
  251. second->right = first;
  252. second->left = cl;
  253. }
  254. } else {
  255. first->parent = second->parent;
  256. if (cl)
  257. cl->parent = second;
  258. if (cr)
  259. cr->parent = second;
  260. second->left = cl;
  261. second->right = cr;
  262. if (!second->is_root()) {
  263. if (is_second_left)
  264. second->parent->left = first;
  265. else
  266. second->parent->right = first;
  267. }
  268. second->parent = p;
  269. }
  270. }
  271. };
  272. using allocator_type = _Allocator<node>;
  273. template <bool Const>
  274. class iterator {
  275. private:
  276. static constexpr bool _is_const_iterator = Const;
  277. public:
  278. using node_pointer_type = typename traits::condition<_is_const_iterator, const node*, node*>::type;
  279. using value_type = typename traits::condition<_is_const_iterator, const pair_type, pair_type>::type;
  280. using pointer_type = typename traits::add_pointer<value_type>::type;
  281. using reference_type = typename traits::add_reference<value_type>::type;
  282. friend class map;
  283. private:
  284. node_pointer_type p;
  285. public:
  286. explicit constexpr iterator(node_pointer_type ptr)
  287. : p { ptr }
  288. {
  289. }
  290. constexpr iterator(const iterator& iter)
  291. : p { iter.p }
  292. {
  293. }
  294. constexpr iterator(iterator&& iter)
  295. : p { iter.p }
  296. {
  297. iter.p = nullptr;
  298. }
  299. constexpr ~iterator()
  300. {
  301. #ifndef NDEBUG
  302. p = nullptr;
  303. #endif
  304. }
  305. constexpr iterator& operator=(const iterator& iter)
  306. {
  307. p = iter.p;
  308. return *this;
  309. }
  310. constexpr iterator& operator=(iterator&& iter)
  311. {
  312. p = iter.p;
  313. iter.p = nullptr;
  314. return *this;
  315. }
  316. constexpr bool operator==(const iterator& iter) const
  317. {
  318. return p == iter.p;
  319. }
  320. constexpr bool operator!=(const iterator& iter) const
  321. {
  322. return !this->operator==(iter);
  323. }
  324. constexpr reference_type operator*(void) const
  325. {
  326. return p->v;
  327. }
  328. constexpr pointer_type operator&(void) const
  329. {
  330. return &p->v;
  331. }
  332. constexpr pointer_type operator->(void) const
  333. {
  334. return this->operator&();
  335. }
  336. constexpr iterator& operator++(void)
  337. {
  338. p = p->next();
  339. return *this;
  340. }
  341. constexpr iterator operator++(int)
  342. {
  343. iterator ret(p);
  344. (void)this->operator++();
  345. return ret;
  346. }
  347. constexpr iterator& operator--(void)
  348. {
  349. p = p->prev();
  350. return *this;
  351. }
  352. constexpr iterator operator--(int)
  353. {
  354. iterator ret(p);
  355. (void)this->operator--();
  356. return ret;
  357. }
  358. explicit constexpr operator bool(void)
  359. {
  360. return p;
  361. }
  362. };
  363. using iterator_type = iterator<false>;
  364. using const_iterator_type = iterator<true>;
  365. private:
  366. node* root = nullptr;
  367. private:
  368. static constexpr node* newnode(node* parent, const pair_type& val)
  369. {
  370. auto* ptr = allocator_traits<allocator_type>::allocate_and_construct(val);
  371. ptr->parent = parent;
  372. return ptr;
  373. }
  374. static constexpr node* newnode(node* parent, pair_type&& val)
  375. {
  376. auto* ptr = allocator_traits<allocator_type>::allocate_and_construct(move(val));
  377. ptr->parent = parent;
  378. return ptr;
  379. }
  380. static constexpr void delnode(node* nd)
  381. {
  382. allocator_traits<allocator_type>::deconstruct_and_deallocate(nd);
  383. }
  384. constexpr void rotateleft(node* rt)
  385. {
  386. node* nrt = rt->right;
  387. if (!rt->is_root()) {
  388. if (rt->is_left_child()) {
  389. rt->parent->left = nrt;
  390. } else {
  391. rt->parent->right = nrt;
  392. }
  393. } else {
  394. this->root = nrt;
  395. }
  396. nrt->parent = rt->parent;
  397. rt->parent = nrt;
  398. rt->right = nrt->left;
  399. nrt->left = rt;
  400. }
  401. constexpr void rotateright(node* rt)
  402. {
  403. node* nrt = rt->left;
  404. if (!rt->is_root()) {
  405. if (rt->is_left_child()) {
  406. rt->parent->left = nrt;
  407. } else {
  408. rt->parent->right = nrt;
  409. }
  410. } else {
  411. this->root = nrt;
  412. }
  413. nrt->parent = rt->parent;
  414. rt->parent = nrt;
  415. rt->left = nrt->right;
  416. nrt->right = rt;
  417. }
  418. constexpr void balance(node* nd)
  419. {
  420. if (nd->is_root()) {
  421. nd->toblack();
  422. return;
  423. }
  424. if (node::is_black(nd->parent))
  425. return;
  426. node* p = nd->parent;
  427. node* pp = nd->grandparent();
  428. node* uncle = nd->uncle();
  429. if (node::is_red(uncle)) {
  430. p->toblack();
  431. uncle->toblack();
  432. pp->tored();
  433. this->balance(pp);
  434. return;
  435. }
  436. if (p->is_left_child()) {
  437. if (nd->is_left_child()) {
  438. p->toblack();
  439. pp->tored();
  440. this->rotateright(pp);
  441. } else {
  442. this->rotateleft(p);
  443. this->balance(p);
  444. }
  445. } else {
  446. if (nd->is_right_child()) {
  447. p->toblack();
  448. pp->tored();
  449. this->rotateleft(pp);
  450. } else {
  451. this->rotateright(p);
  452. this->balance(p);
  453. }
  454. }
  455. }
  456. constexpr node* _find(const key_type& key) const
  457. {
  458. node* cur = root;
  459. for (; cur;) {
  460. if (cur->v.key == key)
  461. return cur;
  462. if (key < cur->v.key)
  463. cur = cur->left;
  464. else
  465. cur = cur->right;
  466. }
  467. return nullptr;
  468. }
  469. // this function DOES NOT dellocate the node
  470. // caller is responsible for freeing the memory
  471. // @param: nd is guaranteed to be a leaf node
  472. constexpr void _erase(node* nd)
  473. {
  474. if (nd->is_root())
  475. return;
  476. if (node::is_black(nd)) {
  477. node* p = nd->parent;
  478. node* s = nullptr;
  479. if (nd->is_left_child())
  480. s = p->right;
  481. else
  482. s = p->left;
  483. if (node::is_red(s)) {
  484. p->tored();
  485. s->toblack();
  486. if (nd->is_right_child()) {
  487. this->rotateright(p);
  488. s = p->left;
  489. } else {
  490. this->rotateleft(p);
  491. s = p->right;
  492. }
  493. }
  494. node* r = nullptr;
  495. if (node::is_red(s->left)) {
  496. r = s->left;
  497. if (s->is_left_child()) {
  498. r->toblack();
  499. s->color = p->color;
  500. this->rotateright(p);
  501. p->toblack();
  502. } else {
  503. r->color = p->color;
  504. this->rotateright(s);
  505. this->rotateleft(p);
  506. p->toblack();
  507. }
  508. } else if (node::is_red(s->right)) {
  509. r = s->right;
  510. if (s->is_left_child()) {
  511. r->color = p->color;
  512. this->rotateleft(s);
  513. this->rotateright(p);
  514. p->toblack();
  515. } else {
  516. r->toblack();
  517. s->color = p->color;
  518. this->rotateleft(p);
  519. p->toblack();
  520. }
  521. } else {
  522. s->tored();
  523. if (node::is_black(p))
  524. this->_erase(p);
  525. else
  526. p->toblack();
  527. }
  528. }
  529. }
  530. public:
  531. constexpr iterator_type end(void)
  532. {
  533. return iterator_type(nullptr);
  534. }
  535. constexpr const_iterator_type end(void) const
  536. {
  537. return const_iterator_type(nullptr);
  538. }
  539. constexpr const_iterator_type cend(void) const
  540. {
  541. return const_iterator_type(nullptr);
  542. }
  543. constexpr iterator_type begin(void)
  544. {
  545. return root ? iterator_type(root->leftmost()) : end();
  546. }
  547. constexpr const_iterator_type begin(void) const
  548. {
  549. return root ? const_iterator_type(root->leftmost()) : end();
  550. }
  551. constexpr const_iterator_type cbegin(void) const
  552. {
  553. return root ? const_iterator_type(root->leftmost()) : end();
  554. }
  555. constexpr iterator_type find(const key_type& key)
  556. {
  557. return iterator_type(_find(key));
  558. }
  559. constexpr const_iterator_type find(const key_type& key) const
  560. {
  561. return const_iterator_type(_find(key));
  562. }
  563. constexpr iterator_type insert(pair_type&& val)
  564. {
  565. node* cur = root;
  566. while (likely(cur)) {
  567. if (val.key < cur->v.key) {
  568. if (!cur->left) {
  569. node* nd = newnode(cur, move(val));
  570. cur->left = nd;
  571. this->balance(nd);
  572. return iterator_type(nd);
  573. } else {
  574. cur = cur->left;
  575. }
  576. } else {
  577. if (!cur->right) {
  578. node* nd = newnode(cur, move(val));
  579. cur->right = nd;
  580. this->balance(nd);
  581. return iterator_type(nd);
  582. } else {
  583. cur = cur->right;
  584. }
  585. }
  586. }
  587. root = newnode(nullptr, move(val));
  588. root->toblack();
  589. return iterator_type(root);
  590. }
  591. constexpr iterator_type erase(const iterator_type& iter)
  592. {
  593. node* nd = iter.p;
  594. if (!nd)
  595. return end();
  596. if (nd->is_root() && nd->is_leaf()) {
  597. delnode(nd);
  598. root = nullptr;
  599. return end();
  600. }
  601. node* next = nd->next();
  602. while (!nd->is_leaf()) {
  603. node* alt = nd->right ? nd->right->leftmost() : nd->left;
  604. if (nd->is_root()) {
  605. this->root = alt;
  606. }
  607. node::swap(nd, alt);
  608. }
  609. this->_erase(nd);
  610. if (nd->is_left_child())
  611. nd->parent->left = nullptr;
  612. else
  613. nd->parent->right = nullptr;
  614. delnode(nd);
  615. return iterator_type(next);
  616. }
  617. constexpr void remove(const key_type& key)
  618. {
  619. auto iter = this->find(key);
  620. if (iter != this->end())
  621. this->erase(iter);
  622. }
  623. // destroy a subtree without adjusting nodes to maintain binary tree properties
  624. constexpr void destroy(node* nd)
  625. {
  626. if (nd) {
  627. this->destroy(nd->left);
  628. this->destroy(nd->right);
  629. delnode(nd);
  630. }
  631. }
  632. explicit constexpr map(void)
  633. {
  634. }
  635. constexpr map(const map& val)
  636. {
  637. for (const auto& item : val)
  638. this->insert(item);
  639. }
  640. constexpr map(map&& val)
  641. : root(val.root)
  642. {
  643. val.root = nullptr;
  644. }
  645. constexpr map& operator=(const map& val)
  646. {
  647. this->destroy(root);
  648. for (const auto& item : val)
  649. this->insert(item);
  650. }
  651. constexpr map& operator=(map&& val)
  652. {
  653. this->destroy(root);
  654. root = val.root;
  655. val.root = nullptr;
  656. }
  657. constexpr ~map()
  658. {
  659. this->destroy(root);
  660. }
  661. };
  662. } // namespace types