map.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /*************************************************************************/
  2. /* map.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef MAP_H
  31. #define MAP_H
  32. #include "core/error/error_macros.h"
  33. #include "core/templates/set.h"
  34. // based on the very nice implementation of rb-trees by:
  35. // https://web.archive.org/web/20120507164830/http://web.mit.edu/~emin/www/source_code/red_black_tree/index.html
  36. template <class K, class V, class C = Comparator<K>, class A = DefaultAllocator>
  37. class Map {
  38. enum Color {
  39. RED,
  40. BLACK
  41. };
  42. struct _Data;
  43. public:
  44. class Element {
  45. private:
  46. friend class Map<K, V, C, A>;
  47. int color = RED;
  48. Element *right = nullptr;
  49. Element *left = nullptr;
  50. Element *parent = nullptr;
  51. Element *_next = nullptr;
  52. Element *_prev = nullptr;
  53. K _key;
  54. V _value;
  55. //_Data *data;
  56. public:
  57. const Element *next() const {
  58. return _next;
  59. }
  60. Element *next() {
  61. return _next;
  62. }
  63. const Element *prev() const {
  64. return _prev;
  65. }
  66. Element *prev() {
  67. return _prev;
  68. }
  69. const K &key() const {
  70. return _key;
  71. }
  72. V &value() {
  73. return _value;
  74. }
  75. const V &value() const {
  76. return _value;
  77. }
  78. V &get() {
  79. return _value;
  80. }
  81. const V &get() const {
  82. return _value;
  83. }
  84. Element() {}
  85. };
  86. private:
  87. struct _Data {
  88. Element *_root = nullptr;
  89. Element *_nil;
  90. int size_cache = 0;
  91. _FORCE_INLINE_ _Data() {
  92. #ifdef GLOBALNIL_DISABLED
  93. _nil = memnew_allocator(Element, A);
  94. _nil->parent = _nil->left = _nil->right = _nil;
  95. _nil->color = BLACK;
  96. #else
  97. _nil = (Element *)&_GlobalNilClass::_nil;
  98. #endif
  99. }
  100. void _create_root() {
  101. _root = memnew_allocator(Element, A);
  102. _root->parent = _root->left = _root->right = _nil;
  103. _root->color = BLACK;
  104. }
  105. void _free_root() {
  106. if (_root) {
  107. memdelete_allocator<Element, A>(_root);
  108. _root = nullptr;
  109. }
  110. }
  111. ~_Data() {
  112. _free_root();
  113. #ifdef GLOBALNIL_DISABLED
  114. memdelete_allocator<Element, A>(_nil);
  115. #endif
  116. }
  117. };
  118. _Data _data;
  119. inline void _set_color(Element *p_node, int p_color) {
  120. ERR_FAIL_COND(p_node == _data._nil && p_color == RED);
  121. p_node->color = p_color;
  122. }
  123. inline void _rotate_left(Element *p_node) {
  124. Element *r = p_node->right;
  125. p_node->right = r->left;
  126. if (r->left != _data._nil) {
  127. r->left->parent = p_node;
  128. }
  129. r->parent = p_node->parent;
  130. if (p_node == p_node->parent->left) {
  131. p_node->parent->left = r;
  132. } else {
  133. p_node->parent->right = r;
  134. }
  135. r->left = p_node;
  136. p_node->parent = r;
  137. }
  138. inline void _rotate_right(Element *p_node) {
  139. Element *l = p_node->left;
  140. p_node->left = l->right;
  141. if (l->right != _data._nil) {
  142. l->right->parent = p_node;
  143. }
  144. l->parent = p_node->parent;
  145. if (p_node == p_node->parent->right) {
  146. p_node->parent->right = l;
  147. } else {
  148. p_node->parent->left = l;
  149. }
  150. l->right = p_node;
  151. p_node->parent = l;
  152. }
  153. inline Element *_successor(Element *p_node) const {
  154. Element *node = p_node;
  155. if (node->right != _data._nil) {
  156. node = node->right;
  157. while (node->left != _data._nil) { /* returns the minimum of the right subtree of node */
  158. node = node->left;
  159. }
  160. return node;
  161. } else {
  162. while (node == node->parent->right) {
  163. node = node->parent;
  164. }
  165. if (node->parent == _data._root) {
  166. return nullptr; // No successor, as p_node = last node
  167. }
  168. return node->parent;
  169. }
  170. }
  171. inline Element *_predecessor(Element *p_node) const {
  172. Element *node = p_node;
  173. if (node->left != _data._nil) {
  174. node = node->left;
  175. while (node->right != _data._nil) { /* returns the minimum of the left subtree of node */
  176. node = node->right;
  177. }
  178. return node;
  179. } else {
  180. while (node == node->parent->left) {
  181. node = node->parent;
  182. }
  183. if (node == _data._root) {
  184. return nullptr; // No predecessor, as p_node = first node
  185. }
  186. return node->parent;
  187. }
  188. }
  189. Element *_find(const K &p_key) const {
  190. Element *node = _data._root->left;
  191. C less;
  192. while (node != _data._nil) {
  193. if (less(p_key, node->_key)) {
  194. node = node->left;
  195. } else if (less(node->_key, p_key)) {
  196. node = node->right;
  197. } else {
  198. return node; // found
  199. }
  200. }
  201. return nullptr;
  202. }
  203. Element *_find_closest(const K &p_key) const {
  204. Element *node = _data._root->left;
  205. Element *prev = nullptr;
  206. C less;
  207. while (node != _data._nil) {
  208. prev = node;
  209. if (less(p_key, node->_key)) {
  210. node = node->left;
  211. } else if (less(node->_key, p_key)) {
  212. node = node->right;
  213. } else {
  214. return node; // found
  215. }
  216. }
  217. if (prev == nullptr) {
  218. return nullptr; // tree empty
  219. }
  220. if (less(p_key, prev->_key)) {
  221. prev = prev->_prev;
  222. }
  223. return prev;
  224. }
  225. void _insert_rb_fix(Element *p_new_node) {
  226. Element *node = p_new_node;
  227. Element *nparent = node->parent;
  228. Element *ngrand_parent;
  229. while (nparent->color == RED) {
  230. ngrand_parent = nparent->parent;
  231. if (nparent == ngrand_parent->left) {
  232. if (ngrand_parent->right->color == RED) {
  233. _set_color(nparent, BLACK);
  234. _set_color(ngrand_parent->right, BLACK);
  235. _set_color(ngrand_parent, RED);
  236. node = ngrand_parent;
  237. nparent = node->parent;
  238. } else {
  239. if (node == nparent->right) {
  240. _rotate_left(nparent);
  241. node = nparent;
  242. nparent = node->parent;
  243. }
  244. _set_color(nparent, BLACK);
  245. _set_color(ngrand_parent, RED);
  246. _rotate_right(ngrand_parent);
  247. }
  248. } else {
  249. if (ngrand_parent->left->color == RED) {
  250. _set_color(nparent, BLACK);
  251. _set_color(ngrand_parent->left, BLACK);
  252. _set_color(ngrand_parent, RED);
  253. node = ngrand_parent;
  254. nparent = node->parent;
  255. } else {
  256. if (node == nparent->left) {
  257. _rotate_right(nparent);
  258. node = nparent;
  259. nparent = node->parent;
  260. }
  261. _set_color(nparent, BLACK);
  262. _set_color(ngrand_parent, RED);
  263. _rotate_left(ngrand_parent);
  264. }
  265. }
  266. }
  267. _set_color(_data._root->left, BLACK);
  268. }
  269. Element *_insert(const K &p_key, const V &p_value) {
  270. Element *new_parent = _data._root;
  271. Element *node = _data._root->left;
  272. C less;
  273. while (node != _data._nil) {
  274. new_parent = node;
  275. if (less(p_key, node->_key)) {
  276. node = node->left;
  277. } else if (less(node->_key, p_key)) {
  278. node = node->right;
  279. } else {
  280. node->_value = p_value;
  281. return node; // Return existing node with new value
  282. }
  283. }
  284. Element *new_node = memnew_allocator(Element, A);
  285. new_node->parent = new_parent;
  286. new_node->right = _data._nil;
  287. new_node->left = _data._nil;
  288. new_node->_key = p_key;
  289. new_node->_value = p_value;
  290. //new_node->data=_data;
  291. if (new_parent == _data._root || less(p_key, new_parent->_key)) {
  292. new_parent->left = new_node;
  293. } else {
  294. new_parent->right = new_node;
  295. }
  296. new_node->_next = _successor(new_node);
  297. new_node->_prev = _predecessor(new_node);
  298. if (new_node->_next) {
  299. new_node->_next->_prev = new_node;
  300. }
  301. if (new_node->_prev) {
  302. new_node->_prev->_next = new_node;
  303. }
  304. _data.size_cache++;
  305. _insert_rb_fix(new_node);
  306. return new_node;
  307. }
  308. void _erase_fix_rb(Element *p_node) {
  309. Element *root = _data._root->left;
  310. Element *node = _data._nil;
  311. Element *sibling = p_node;
  312. Element *parent = sibling->parent;
  313. while (node != root) { // If red node found, will exit at a break
  314. if (sibling->color == RED) {
  315. _set_color(sibling, BLACK);
  316. _set_color(parent, RED);
  317. if (sibling == parent->right) {
  318. sibling = sibling->left;
  319. _rotate_left(parent);
  320. } else {
  321. sibling = sibling->right;
  322. _rotate_right(parent);
  323. }
  324. }
  325. if ((sibling->left->color == BLACK) && (sibling->right->color == BLACK)) {
  326. _set_color(sibling, RED);
  327. if (parent->color == RED) {
  328. _set_color(parent, BLACK);
  329. break;
  330. } else { // loop: haven't found any red nodes yet
  331. node = parent;
  332. parent = node->parent;
  333. sibling = (node == parent->left) ? parent->right : parent->left;
  334. }
  335. } else {
  336. if (sibling == parent->right) {
  337. if (sibling->right->color == BLACK) {
  338. _set_color(sibling->left, BLACK);
  339. _set_color(sibling, RED);
  340. _rotate_right(sibling);
  341. sibling = sibling->parent;
  342. }
  343. _set_color(sibling, parent->color);
  344. _set_color(parent, BLACK);
  345. _set_color(sibling->right, BLACK);
  346. _rotate_left(parent);
  347. break;
  348. } else {
  349. if (sibling->left->color == BLACK) {
  350. _set_color(sibling->right, BLACK);
  351. _set_color(sibling, RED);
  352. _rotate_left(sibling);
  353. sibling = sibling->parent;
  354. }
  355. _set_color(sibling, parent->color);
  356. _set_color(parent, BLACK);
  357. _set_color(sibling->left, BLACK);
  358. _rotate_right(parent);
  359. break;
  360. }
  361. }
  362. }
  363. ERR_FAIL_COND(_data._nil->color != BLACK);
  364. }
  365. void _erase(Element *p_node) {
  366. Element *rp = ((p_node->left == _data._nil) || (p_node->right == _data._nil)) ? p_node : p_node->_next;
  367. Element *node = (rp->left == _data._nil) ? rp->right : rp->left;
  368. Element *sibling;
  369. if (rp == rp->parent->left) {
  370. rp->parent->left = node;
  371. sibling = rp->parent->right;
  372. } else {
  373. rp->parent->right = node;
  374. sibling = rp->parent->left;
  375. }
  376. if (node->color == RED) {
  377. node->parent = rp->parent;
  378. _set_color(node, BLACK);
  379. } else if (rp->color == BLACK && rp->parent != _data._root) {
  380. _erase_fix_rb(sibling);
  381. }
  382. if (rp != p_node) {
  383. ERR_FAIL_COND(rp == _data._nil);
  384. rp->left = p_node->left;
  385. rp->right = p_node->right;
  386. rp->parent = p_node->parent;
  387. rp->color = p_node->color;
  388. if (p_node->left != _data._nil) {
  389. p_node->left->parent = rp;
  390. }
  391. if (p_node->right != _data._nil) {
  392. p_node->right->parent = rp;
  393. }
  394. if (p_node == p_node->parent->left) {
  395. p_node->parent->left = rp;
  396. } else {
  397. p_node->parent->right = rp;
  398. }
  399. }
  400. if (p_node->_next) {
  401. p_node->_next->_prev = p_node->_prev;
  402. }
  403. if (p_node->_prev) {
  404. p_node->_prev->_next = p_node->_next;
  405. }
  406. memdelete_allocator<Element, A>(p_node);
  407. _data.size_cache--;
  408. ERR_FAIL_COND(_data._nil->color == RED);
  409. }
  410. void _calculate_depth(Element *p_element, int &max_d, int d) const {
  411. if (p_element == _data._nil) {
  412. return;
  413. }
  414. _calculate_depth(p_element->left, max_d, d + 1);
  415. _calculate_depth(p_element->right, max_d, d + 1);
  416. if (d > max_d) {
  417. max_d = d;
  418. }
  419. }
  420. void _cleanup_tree(Element *p_element) {
  421. if (p_element == _data._nil) {
  422. return;
  423. }
  424. _cleanup_tree(p_element->left);
  425. _cleanup_tree(p_element->right);
  426. memdelete_allocator<Element, A>(p_element);
  427. }
  428. void _copy_from(const Map &p_map) {
  429. clear();
  430. // not the fastest way, but safeset to write.
  431. for (Element *I = p_map.front(); I; I = I->next()) {
  432. insert(I->key(), I->value());
  433. }
  434. }
  435. public:
  436. const Element *find(const K &p_key) const {
  437. if (!_data._root) {
  438. return nullptr;
  439. }
  440. const Element *res = _find(p_key);
  441. return res;
  442. }
  443. Element *find(const K &p_key) {
  444. if (!_data._root) {
  445. return nullptr;
  446. }
  447. Element *res = _find(p_key);
  448. return res;
  449. }
  450. const Element *find_closest(const K &p_key) const {
  451. if (!_data._root) {
  452. return nullptr;
  453. }
  454. const Element *res = _find_closest(p_key);
  455. return res;
  456. }
  457. Element *find_closest(const K &p_key) {
  458. if (!_data._root) {
  459. return nullptr;
  460. }
  461. Element *res = _find_closest(p_key);
  462. return res;
  463. }
  464. bool has(const K &p_key) const {
  465. return find(p_key) != nullptr;
  466. }
  467. Element *insert(const K &p_key, const V &p_value) {
  468. if (!_data._root) {
  469. _data._create_root();
  470. }
  471. return _insert(p_key, p_value);
  472. }
  473. void erase(Element *p_element) {
  474. if (!_data._root || !p_element) {
  475. return;
  476. }
  477. _erase(p_element);
  478. if (_data.size_cache == 0 && _data._root) {
  479. _data._free_root();
  480. }
  481. }
  482. bool erase(const K &p_key) {
  483. if (!_data._root) {
  484. return false;
  485. }
  486. Element *e = find(p_key);
  487. if (!e) {
  488. return false;
  489. }
  490. _erase(e);
  491. if (_data.size_cache == 0 && _data._root) {
  492. _data._free_root();
  493. }
  494. return true;
  495. }
  496. const V &operator[](const K &p_key) const {
  497. CRASH_COND(!_data._root);
  498. const Element *e = find(p_key);
  499. CRASH_COND(!e);
  500. return e->_value;
  501. }
  502. V &operator[](const K &p_key) {
  503. if (!_data._root) {
  504. _data._create_root();
  505. }
  506. Element *e = find(p_key);
  507. if (!e) {
  508. e = insert(p_key, V());
  509. }
  510. return e->_value;
  511. }
  512. Element *front() const {
  513. if (!_data._root) {
  514. return nullptr;
  515. }
  516. Element *e = _data._root->left;
  517. if (e == _data._nil) {
  518. return nullptr;
  519. }
  520. while (e->left != _data._nil) {
  521. e = e->left;
  522. }
  523. return e;
  524. }
  525. Element *back() const {
  526. if (!_data._root) {
  527. return nullptr;
  528. }
  529. Element *e = _data._root->left;
  530. if (e == _data._nil) {
  531. return nullptr;
  532. }
  533. while (e->right != _data._nil) {
  534. e = e->right;
  535. }
  536. return e;
  537. }
  538. inline bool is_empty() const { return _data.size_cache == 0; }
  539. inline int size() const { return _data.size_cache; }
  540. int calculate_depth() const {
  541. // used for debug mostly
  542. if (!_data._root) {
  543. return 0;
  544. }
  545. int max_d = 0;
  546. _calculate_depth(_data._root->left, max_d, 0);
  547. return max_d;
  548. }
  549. void clear() {
  550. if (!_data._root) {
  551. return;
  552. }
  553. _cleanup_tree(_data._root->left);
  554. _data._root->left = _data._nil;
  555. _data.size_cache = 0;
  556. _data._free_root();
  557. }
  558. void operator=(const Map &p_map) {
  559. _copy_from(p_map);
  560. }
  561. Map(const Map &p_map) {
  562. _copy_from(p_map);
  563. }
  564. _FORCE_INLINE_ Map() {}
  565. ~Map() {
  566. clear();
  567. }
  568. };
  569. #endif // MAP_H