rb_map.h 17 KB

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