rb_set.h 16 KB

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