rb_map.h 18 KB

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