rb_set.h 16 KB

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