map.hpp 17 KB

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