map.h 15 KB

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