2
0

map.h 17 KB

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