map.h 15 KB

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