rb_map.h 17 KB

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