map.h 15 KB

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