map.hpp 18 KB

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