rb_map.h 18 KB

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