map.hpp 17 KB

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