map.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*************************************************************************/
  2. /* map.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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_macros.h"
  33. #include "core/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;
  48. Element *right;
  49. Element *left;
  50. Element *parent;
  51. Element *_next;
  52. Element *_prev;
  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. color = RED;
  86. right = NULL;
  87. left = NULL;
  88. parent = NULL;
  89. _next = NULL;
  90. _prev = NULL;
  91. };
  92. };
  93. private:
  94. struct _Data {
  95. Element *_root;
  96. Element *_nil;
  97. int size_cache;
  98. _FORCE_INLINE_ _Data() {
  99. #ifdef GLOBALNIL_DISABLED
  100. _nil = memnew_allocator(Element, A);
  101. _nil->parent = _nil->left = _nil->right = _nil;
  102. _nil->color = BLACK;
  103. #else
  104. _nil = (Element *)&_GlobalNilClass::_nil;
  105. #endif
  106. _root = NULL;
  107. size_cache = 0;
  108. }
  109. void _create_root() {
  110. _root = memnew_allocator(Element, A);
  111. _root->parent = _root->left = _root->right = _nil;
  112. _root->color = BLACK;
  113. }
  114. void _free_root() {
  115. if (_root) {
  116. memdelete_allocator<Element, A>(_root);
  117. _root = NULL;
  118. }
  119. }
  120. ~_Data() {
  121. _free_root();
  122. #ifdef GLOBALNIL_DISABLED
  123. memdelete_allocator<Element, A>(_nil);
  124. #endif
  125. }
  126. };
  127. _Data _data;
  128. inline void _set_color(Element *p_node, int p_color) {
  129. ERR_FAIL_COND(p_node == _data._nil && p_color == RED);
  130. p_node->color = p_color;
  131. }
  132. inline void _rotate_left(Element *p_node) {
  133. Element *r = p_node->right;
  134. p_node->right = r->left;
  135. if (r->left != _data._nil)
  136. r->left->parent = p_node;
  137. r->parent = p_node->parent;
  138. if (p_node == p_node->parent->left)
  139. p_node->parent->left = r;
  140. else
  141. p_node->parent->right = r;
  142. r->left = p_node;
  143. p_node->parent = r;
  144. }
  145. inline void _rotate_right(Element *p_node) {
  146. Element *l = p_node->left;
  147. p_node->left = l->right;
  148. if (l->right != _data._nil)
  149. l->right->parent = p_node;
  150. l->parent = p_node->parent;
  151. if (p_node == p_node->parent->right)
  152. p_node->parent->right = l;
  153. else
  154. p_node->parent->left = l;
  155. l->right = p_node;
  156. p_node->parent = l;
  157. }
  158. inline Element *_successor(Element *p_node) const {
  159. Element *node = p_node;
  160. if (node->right != _data._nil) {
  161. node = node->right;
  162. while (node->left != _data._nil) { /* returns the minimum of the right subtree of node */
  163. node = node->left;
  164. }
  165. return node;
  166. } else {
  167. while (node == node->parent->right) {
  168. node = node->parent;
  169. }
  170. if (node->parent == _data._root)
  171. return NULL; // No successor, as p_node = last node
  172. return node->parent;
  173. }
  174. }
  175. inline Element *_predecessor(Element *p_node) const {
  176. Element *node = p_node;
  177. if (node->left != _data._nil) {
  178. node = node->left;
  179. while (node->right != _data._nil) { /* returns the minimum of the left subtree of node */
  180. node = node->right;
  181. }
  182. return node;
  183. } else {
  184. while (node == node->parent->left) {
  185. node = node->parent;
  186. }
  187. if (node == _data._root)
  188. return NULL; // No predecessor, as p_node = first node
  189. return node->parent;
  190. }
  191. }
  192. Element *_find(const K &p_key) const {
  193. Element *node = _data._root->left;
  194. C less;
  195. while (node != _data._nil) {
  196. if (less(p_key, node->_key))
  197. node = node->left;
  198. else if (less(node->_key, p_key))
  199. node = node->right;
  200. else
  201. return node; // found
  202. }
  203. return NULL;
  204. }
  205. Element *_find_closest(const K &p_key) const {
  206. Element *node = _data._root->left;
  207. Element *prev = NULL;
  208. C less;
  209. while (node != _data._nil) {
  210. prev = node;
  211. if (less(p_key, node->_key))
  212. node = node->left;
  213. else if (less(node->_key, p_key))
  214. node = node->right;
  215. else
  216. return node; // found
  217. }
  218. if (prev == NULL)
  219. return NULL; // tree empty
  220. if (less(p_key, prev->_key))
  221. prev = prev->_prev;
  222. return prev;
  223. }
  224. void _insert_rb_fix(Element *p_new_node) {
  225. Element *node = p_new_node;
  226. Element *nparent = node->parent;
  227. Element *ngrand_parent;
  228. while (nparent->color == RED) {
  229. ngrand_parent = nparent->parent;
  230. if (nparent == ngrand_parent->left) {
  231. if (ngrand_parent->right->color == RED) {
  232. _set_color(nparent, BLACK);
  233. _set_color(ngrand_parent->right, BLACK);
  234. _set_color(ngrand_parent, RED);
  235. node = ngrand_parent;
  236. nparent = node->parent;
  237. } else {
  238. if (node == nparent->right) {
  239. _rotate_left(nparent);
  240. node = nparent;
  241. nparent = node->parent;
  242. }
  243. _set_color(nparent, BLACK);
  244. _set_color(ngrand_parent, RED);
  245. _rotate_right(ngrand_parent);
  246. }
  247. } else {
  248. if (ngrand_parent->left->color == RED) {
  249. _set_color(nparent, BLACK);
  250. _set_color(ngrand_parent->left, BLACK);
  251. _set_color(ngrand_parent, RED);
  252. node = ngrand_parent;
  253. nparent = node->parent;
  254. } else {
  255. if (node == nparent->left) {
  256. _rotate_right(nparent);
  257. node = nparent;
  258. nparent = node->parent;
  259. }
  260. _set_color(nparent, BLACK);
  261. _set_color(ngrand_parent, RED);
  262. _rotate_left(ngrand_parent);
  263. }
  264. }
  265. }
  266. _set_color(_data._root->left, BLACK);
  267. }
  268. Element *_insert(const K &p_key, const V &p_value) {
  269. Element *new_parent = _data._root;
  270. Element *node = _data._root->left;
  271. C less;
  272. while (node != _data._nil) {
  273. new_parent = node;
  274. if (less(p_key, node->_key))
  275. node = node->left;
  276. else if (less(node->_key, p_key))
  277. node = node->right;
  278. else {
  279. node->_value = p_value;
  280. return node; // Return existing node with new value
  281. }
  282. }
  283. Element *new_node = memnew_allocator(Element, A);
  284. new_node->parent = new_parent;
  285. new_node->right = _data._nil;
  286. new_node->left = _data._nil;
  287. new_node->_key = p_key;
  288. new_node->_value = p_value;
  289. //new_node->data=_data;
  290. if (new_parent == _data._root || less(p_key, new_parent->_key)) {
  291. new_parent->left = new_node;
  292. } else {
  293. new_parent->right = new_node;
  294. }
  295. new_node->_next = _successor(new_node);
  296. new_node->_prev = _predecessor(new_node);
  297. if (new_node->_next)
  298. new_node->_next->_prev = new_node;
  299. if (new_node->_prev)
  300. new_node->_prev->_next = new_node;
  301. _data.size_cache++;
  302. _insert_rb_fix(new_node);
  303. return new_node;
  304. }
  305. void _erase_fix_rb(Element *p_node) {
  306. Element *root = _data._root->left;
  307. Element *node = _data._nil;
  308. Element *sibling = p_node;
  309. Element *parent = sibling->parent;
  310. while (node != root) { // If red node found, will exit at a break
  311. if (sibling->color == RED) {
  312. _set_color(sibling, BLACK);
  313. _set_color(parent, RED);
  314. if (sibling == parent->right) {
  315. sibling = sibling->left;
  316. _rotate_left(parent);
  317. } else {
  318. sibling = sibling->right;
  319. _rotate_right(parent);
  320. }
  321. }
  322. if ((sibling->left->color == BLACK) && (sibling->right->color == BLACK)) {
  323. _set_color(sibling, RED);
  324. if (parent->color == RED) {
  325. _set_color(parent, BLACK);
  326. break;
  327. } else { // loop: haven't found any red nodes yet
  328. node = parent;
  329. parent = node->parent;
  330. sibling = (node == parent->left) ? parent->right : parent->left;
  331. }
  332. } else {
  333. if (sibling == parent->right) {
  334. if (sibling->right->color == BLACK) {
  335. _set_color(sibling->left, BLACK);
  336. _set_color(sibling, RED);
  337. _rotate_right(sibling);
  338. sibling = sibling->parent;
  339. }
  340. _set_color(sibling, parent->color);
  341. _set_color(parent, BLACK);
  342. _set_color(sibling->right, BLACK);
  343. _rotate_left(parent);
  344. break;
  345. } else {
  346. if (sibling->left->color == BLACK) {
  347. _set_color(sibling->right, BLACK);
  348. _set_color(sibling, RED);
  349. _rotate_left(sibling);
  350. sibling = sibling->parent;
  351. }
  352. _set_color(sibling, parent->color);
  353. _set_color(parent, BLACK);
  354. _set_color(sibling->left, BLACK);
  355. _rotate_right(parent);
  356. break;
  357. }
  358. }
  359. }
  360. ERR_FAIL_COND(_data._nil->color != BLACK);
  361. }
  362. void _erase(Element *p_node) {
  363. Element *rp = ((p_node->left == _data._nil) || (p_node->right == _data._nil)) ? p_node : p_node->_next;
  364. Element *node = (rp->left == _data._nil) ? rp->right : rp->left;
  365. Element *sibling;
  366. if (rp == rp->parent->left) {
  367. rp->parent->left = node;
  368. sibling = rp->parent->right;
  369. } else {
  370. rp->parent->right = node;
  371. sibling = rp->parent->left;
  372. }
  373. if (node->color == RED) {
  374. node->parent = rp->parent;
  375. _set_color(node, BLACK);
  376. } else if (rp->color == BLACK && rp->parent != _data._root) {
  377. _erase_fix_rb(sibling);
  378. }
  379. if (rp != p_node) {
  380. ERR_FAIL_COND(rp == _data._nil);
  381. rp->left = p_node->left;
  382. rp->right = p_node->right;
  383. rp->parent = p_node->parent;
  384. rp->color = p_node->color;
  385. if (p_node->left != _data._nil)
  386. p_node->left->parent = rp;
  387. if (p_node->right != _data._nil)
  388. p_node->right->parent = rp;
  389. if (p_node == p_node->parent->left) {
  390. p_node->parent->left = rp;
  391. } else {
  392. p_node->parent->right = rp;
  393. }
  394. }
  395. if (p_node->_next)
  396. p_node->_next->_prev = p_node->_prev;
  397. if (p_node->_prev)
  398. p_node->_prev->_next = p_node->_next;
  399. memdelete_allocator<Element, A>(p_node);
  400. _data.size_cache--;
  401. ERR_FAIL_COND(_data._nil->color == RED);
  402. }
  403. void _calculate_depth(Element *p_element, int &max_d, int d) const {
  404. if (p_element == _data._nil)
  405. return;
  406. _calculate_depth(p_element->left, max_d, d + 1);
  407. _calculate_depth(p_element->right, max_d, d + 1);
  408. if (d > max_d)
  409. max_d = d;
  410. }
  411. void _cleanup_tree(Element *p_element) {
  412. if (p_element == _data._nil)
  413. return;
  414. _cleanup_tree(p_element->left);
  415. _cleanup_tree(p_element->right);
  416. memdelete_allocator<Element, A>(p_element);
  417. }
  418. void _copy_from(const Map &p_map) {
  419. clear();
  420. // not the fastest way, but safeset to write.
  421. for (Element *I = p_map.front(); I; I = I->next()) {
  422. insert(I->key(), I->value());
  423. }
  424. }
  425. public:
  426. const Element *find(const K &p_key) const {
  427. if (!_data._root)
  428. return NULL;
  429. const Element *res = _find(p_key);
  430. return res;
  431. }
  432. Element *find(const K &p_key) {
  433. if (!_data._root)
  434. return NULL;
  435. Element *res = _find(p_key);
  436. return res;
  437. }
  438. const Element *find_closest(const K &p_key) const {
  439. if (!_data._root)
  440. return NULL;
  441. const Element *res = _find_closest(p_key);
  442. return res;
  443. }
  444. Element *find_closest(const K &p_key) {
  445. if (!_data._root)
  446. return NULL;
  447. Element *res = _find_closest(p_key);
  448. return res;
  449. }
  450. bool has(const K &p_key) const {
  451. return find(p_key) != NULL;
  452. }
  453. Element *insert(const K &p_key, const V &p_value) {
  454. if (!_data._root)
  455. _data._create_root();
  456. return _insert(p_key, p_value);
  457. }
  458. void erase(Element *p_element) {
  459. if (!_data._root || !p_element)
  460. return;
  461. _erase(p_element);
  462. if (_data.size_cache == 0 && _data._root)
  463. _data._free_root();
  464. }
  465. bool erase(const K &p_key) {
  466. if (!_data._root)
  467. return false;
  468. Element *e = find(p_key);
  469. if (!e)
  470. return false;
  471. _erase(e);
  472. if (_data.size_cache == 0 && _data._root)
  473. _data._free_root();
  474. return true;
  475. }
  476. const V &operator[](const K &p_key) const {
  477. CRASH_COND(!_data._root);
  478. const Element *e = find(p_key);
  479. CRASH_COND(!e);
  480. return e->_value;
  481. }
  482. V &operator[](const K &p_key) {
  483. if (!_data._root)
  484. _data._create_root();
  485. Element *e = find(p_key);
  486. if (!e)
  487. e = insert(p_key, V());
  488. return e->_value;
  489. }
  490. Element *front() const {
  491. if (!_data._root)
  492. return NULL;
  493. Element *e = _data._root->left;
  494. if (e == _data._nil)
  495. return NULL;
  496. while (e->left != _data._nil)
  497. e = e->left;
  498. return e;
  499. }
  500. Element *back() const {
  501. if (!_data._root)
  502. return NULL;
  503. Element *e = _data._root->left;
  504. if (e == _data._nil)
  505. return NULL;
  506. while (e->right != _data._nil)
  507. e = e->right;
  508. return e;
  509. }
  510. inline bool empty() const { return _data.size_cache == 0; }
  511. inline int size() const { return _data.size_cache; }
  512. int calculate_depth() const {
  513. // used for debug mostly
  514. if (!_data._root)
  515. return 0;
  516. int max_d = 0;
  517. _calculate_depth(_data._root->left, max_d, 0);
  518. return max_d;
  519. }
  520. void clear() {
  521. if (!_data._root)
  522. return;
  523. _cleanup_tree(_data._root->left);
  524. _data._root->left = _data._nil;
  525. _data.size_cache = 0;
  526. _data._free_root();
  527. }
  528. void operator=(const Map &p_map) {
  529. _copy_from(p_map);
  530. }
  531. Map(const Map &p_map) {
  532. _copy_from(p_map);
  533. }
  534. _FORCE_INLINE_ Map() {
  535. }
  536. ~Map() {
  537. clear();
  538. }
  539. };
  540. #endif