map.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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. 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 = typename traits::condition<Const, const node*, node*>::type;
  263. using value_type = typename traits::condition<Const, const pair_type, pair_type>::type;
  264. using pointer_type = typename traits::add_pointer<value_type>::type;
  265. using reference_type = typename traits::add_reference<value_type>::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(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. node* cur = root;
  443. for (; cur;) {
  444. if (cur->v.key == key)
  445. return cur;
  446. if (key < cur->v.key)
  447. cur = cur->left;
  448. else
  449. cur = cur->right;
  450. }
  451. return nullptr;
  452. }
  453. // this function DOES NOT dellocate the node
  454. // caller is responsible for freeing the memory
  455. // @param: nd is guaranteed to be a leaf node
  456. constexpr void _erase(node* nd)
  457. {
  458. if (nd->is_root())
  459. return;
  460. if (node::is_black(nd)) {
  461. node* p = nd->parent;
  462. node* s = nullptr;
  463. if (nd->is_left_child())
  464. s = p->right;
  465. else
  466. s = p->left;
  467. if (node::is_red(s)) {
  468. p->tored();
  469. s->toblack();
  470. if (nd->is_right_child()) {
  471. this->rotateright(p);
  472. s = p->left;
  473. } else {
  474. this->rotateleft(p);
  475. s = p->right;
  476. }
  477. }
  478. node* r = nullptr;
  479. if (node::is_red(s->left)) {
  480. r = s->left;
  481. if (s->is_left_child()) {
  482. r->toblack();
  483. s->color = p->color;
  484. this->rotateright(p);
  485. p->toblack();
  486. } else {
  487. r->color = p->color;
  488. this->rotateright(s);
  489. this->rotateleft(p);
  490. p->toblack();
  491. }
  492. } else if (node::is_red(s->right)) {
  493. r = s->right;
  494. if (s->is_left_child()) {
  495. r->color = p->color;
  496. this->rotateleft(s);
  497. this->rotateright(p);
  498. p->toblack();
  499. } else {
  500. r->toblack();
  501. s->color = p->color;
  502. this->rotateleft(p);
  503. p->toblack();
  504. }
  505. } else {
  506. s->tored();
  507. if (node::is_black(p))
  508. this->_erase(p);
  509. else
  510. p->toblack();
  511. }
  512. }
  513. }
  514. public:
  515. constexpr iterator_type end(void)
  516. {
  517. return iterator_type(nullptr);
  518. }
  519. constexpr const_iterator_type end(void) const
  520. {
  521. return const_iterator_type(nullptr);
  522. }
  523. constexpr const_iterator_type cend(void) const
  524. {
  525. return const_iterator_type(nullptr);
  526. }
  527. constexpr iterator_type begin(void)
  528. {
  529. return root ? iterator_type(root->leftmost()) : end();
  530. }
  531. constexpr const_iterator_type begin(void) const
  532. {
  533. return root ? const_iterator_type(root->leftmost()) : end();
  534. }
  535. constexpr const_iterator_type cbegin(void) const
  536. {
  537. return root ? const_iterator_type(root->leftmost()) : end();
  538. }
  539. constexpr iterator_type find(const key_type& key)
  540. {
  541. return iterator_type(_find(key));
  542. }
  543. constexpr const_iterator_type find(const key_type& key) const
  544. {
  545. return const_iterator_type(_find(key));
  546. }
  547. constexpr iterator_type insert(pair_type&& val)
  548. {
  549. node* cur = root;
  550. while (likely(cur)) {
  551. if (val.key < cur->v.key) {
  552. if (!cur->left) {
  553. node* nd = newnode(cur, move(val));
  554. cur->left = nd;
  555. this->balance(nd);
  556. return iterator_type(nd);
  557. } else {
  558. cur = cur->left;
  559. }
  560. } else {
  561. if (!cur->right) {
  562. node* nd = newnode(cur, move(val));
  563. cur->right = nd;
  564. this->balance(nd);
  565. return iterator_type(nd);
  566. } else {
  567. cur = cur->right;
  568. }
  569. }
  570. }
  571. root = newnode(nullptr, move(val));
  572. root->toblack();
  573. return iterator_type(root);
  574. }
  575. constexpr iterator_type erase(const iterator_type& iter)
  576. {
  577. node* nd = iter.p;
  578. if (!nd)
  579. return end();
  580. if (nd->is_root() && nd->is_leaf()) {
  581. delnode(nd);
  582. root = nullptr;
  583. return end();
  584. }
  585. node* next = nd->next();
  586. while (!nd->is_leaf()) {
  587. node* alt = nd->right ? nd->right->leftmost() : nd->left;
  588. if (nd->is_root()) {
  589. this->root = alt;
  590. }
  591. node::swap(nd, alt);
  592. }
  593. this->_erase(nd);
  594. if (nd->is_left_child())
  595. nd->parent->left = nullptr;
  596. else
  597. nd->parent->right = nullptr;
  598. delnode(nd);
  599. return iterator_type(next);
  600. }
  601. constexpr void remove(const key_type& key)
  602. {
  603. auto iter = this->find(key);
  604. if (iter != this->end())
  605. this->erase(iter);
  606. }
  607. // destroy a subtree without adjusting nodes to maintain binary tree properties
  608. constexpr void destroy(node* nd)
  609. {
  610. if (nd) {
  611. this->destroy(nd->left);
  612. this->destroy(nd->right);
  613. delnode(nd);
  614. }
  615. }
  616. explicit constexpr map(void)
  617. {
  618. }
  619. constexpr map(const map& val)
  620. {
  621. for (const auto& item : val)
  622. this->insert(item);
  623. }
  624. constexpr map(map&& val)
  625. : root(val.root)
  626. {
  627. val.root = nullptr;
  628. }
  629. constexpr map& operator=(const map& val)
  630. {
  631. this->destroy(root);
  632. for (const auto& item : val)
  633. this->insert(item);
  634. }
  635. constexpr map& operator=(map&& val)
  636. {
  637. this->destroy(root);
  638. root = val.root;
  639. val.root = nullptr;
  640. }
  641. constexpr ~map()
  642. {
  643. this->destroy(root);
  644. }
  645. };
  646. } // namespace types