map.hpp 18 KB

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