map.hpp 19 KB

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