set.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*************************************************************************/
  2. /* set.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 SET_H
  31. #define 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/http://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 Set {
  38. enum Color {
  39. RED,
  40. BLACK
  41. };
  42. struct _Data;
  43. public:
  44. class Element {
  45. private:
  46. friend class Set<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. const T &get() const {
  69. return value;
  70. };
  71. Element() {}
  72. };
  73. private:
  74. struct _Data {
  75. Element *_root = nullptr;
  76. Element *_nil = nullptr;
  77. int size_cache = 0;
  78. _FORCE_INLINE_ _Data() {
  79. #ifdef GLOBALNIL_DISABLED
  80. _nil = memnew_allocator(Element, A);
  81. _nil->parent = _nil->left = _nil->right = _nil;
  82. _nil->color = BLACK;
  83. #else
  84. _nil = (Element *)&_GlobalNilClass::_nil;
  85. #endif
  86. }
  87. void _create_root() {
  88. _root = memnew_allocator(Element, A);
  89. _root->parent = _root->left = _root->right = _nil;
  90. _root->color = BLACK;
  91. }
  92. void _free_root() {
  93. if (_root) {
  94. memdelete_allocator<Element, A>(_root);
  95. _root = nullptr;
  96. }
  97. }
  98. ~_Data() {
  99. _free_root();
  100. #ifdef GLOBALNIL_DISABLED
  101. memdelete_allocator<Element, A>(_nil);
  102. #endif
  103. }
  104. };
  105. _Data _data;
  106. inline void _set_color(Element *p_node, int p_color) {
  107. ERR_FAIL_COND(p_node == _data._nil && p_color == RED);
  108. p_node->color = p_color;
  109. }
  110. inline void _rotate_left(Element *p_node) {
  111. Element *r = p_node->right;
  112. p_node->right = r->left;
  113. if (r->left != _data._nil) {
  114. r->left->parent = p_node;
  115. }
  116. r->parent = p_node->parent;
  117. if (p_node == p_node->parent->left) {
  118. p_node->parent->left = r;
  119. } else {
  120. p_node->parent->right = r;
  121. }
  122. r->left = p_node;
  123. p_node->parent = r;
  124. }
  125. inline void _rotate_right(Element *p_node) {
  126. Element *l = p_node->left;
  127. p_node->left = l->right;
  128. if (l->right != _data._nil) {
  129. l->right->parent = p_node;
  130. }
  131. l->parent = p_node->parent;
  132. if (p_node == p_node->parent->right) {
  133. p_node->parent->right = l;
  134. } else {
  135. p_node->parent->left = l;
  136. }
  137. l->right = p_node;
  138. p_node->parent = l;
  139. }
  140. inline Element *_successor(Element *p_node) const {
  141. Element *node = p_node;
  142. if (node->right != _data._nil) {
  143. node = node->right;
  144. while (node->left != _data._nil) { /* returns the minimum of the right subtree of node */
  145. node = node->left;
  146. }
  147. return node;
  148. } else {
  149. while (node == node->parent->right) {
  150. node = node->parent;
  151. }
  152. if (node->parent == _data._root) {
  153. return nullptr; // No successor, as p_node = last node
  154. }
  155. return node->parent;
  156. }
  157. }
  158. inline Element *_predecessor(Element *p_node) const {
  159. Element *node = p_node;
  160. if (node->left != _data._nil) {
  161. node = node->left;
  162. while (node->right != _data._nil) { /* returns the minimum of the left subtree of node */
  163. node = node->right;
  164. }
  165. return node;
  166. } else {
  167. while (node == node->parent->left) {
  168. node = node->parent;
  169. }
  170. if (node == _data._root) {
  171. return nullptr; // No predecessor, as p_node = first node.
  172. }
  173. return node->parent;
  174. }
  175. }
  176. Element *_find(const T &p_value) const {
  177. Element *node = _data._root->left;
  178. C less;
  179. while (node != _data._nil) {
  180. if (less(p_value, node->value)) {
  181. node = node->left;
  182. } else if (less(node->value, p_value)) {
  183. node = node->right;
  184. } else {
  185. return node; // found
  186. }
  187. }
  188. return nullptr;
  189. }
  190. Element *_lower_bound(const T &p_value) const {
  191. Element *node = _data._root->left;
  192. Element *prev = nullptr;
  193. C less;
  194. while (node != _data._nil) {
  195. prev = node;
  196. if (less(p_value, node->value)) {
  197. node = node->left;
  198. } else if (less(node->value, p_value)) {
  199. node = node->right;
  200. } else {
  201. return node; // found
  202. }
  203. }
  204. if (prev == nullptr) {
  205. return nullptr; // tree empty
  206. }
  207. if (less(prev->value, p_value)) {
  208. prev = prev->_next;
  209. }
  210. return prev;
  211. }
  212. void _insert_rb_fix(Element *p_new_node) {
  213. Element *node = p_new_node;
  214. Element *nparent = node->parent;
  215. Element *ngrand_parent;
  216. while (nparent->color == RED) {
  217. ngrand_parent = nparent->parent;
  218. if (nparent == ngrand_parent->left) {
  219. if (ngrand_parent->right->color == RED) {
  220. _set_color(nparent, BLACK);
  221. _set_color(ngrand_parent->right, BLACK);
  222. _set_color(ngrand_parent, RED);
  223. node = ngrand_parent;
  224. nparent = node->parent;
  225. } else {
  226. if (node == nparent->right) {
  227. _rotate_left(nparent);
  228. node = nparent;
  229. nparent = node->parent;
  230. }
  231. _set_color(nparent, BLACK);
  232. _set_color(ngrand_parent, RED);
  233. _rotate_right(ngrand_parent);
  234. }
  235. } else {
  236. if (ngrand_parent->left->color == RED) {
  237. _set_color(nparent, BLACK);
  238. _set_color(ngrand_parent->left, BLACK);
  239. _set_color(ngrand_parent, RED);
  240. node = ngrand_parent;
  241. nparent = node->parent;
  242. } else {
  243. if (node == nparent->left) {
  244. _rotate_right(nparent);
  245. node = nparent;
  246. nparent = node->parent;
  247. }
  248. _set_color(nparent, BLACK);
  249. _set_color(ngrand_parent, RED);
  250. _rotate_left(ngrand_parent);
  251. }
  252. }
  253. }
  254. _set_color(_data._root->left, BLACK);
  255. }
  256. Element *_insert(const T &p_value) {
  257. Element *new_parent = _data._root;
  258. Element *node = _data._root->left;
  259. C less;
  260. while (node != _data._nil) {
  261. new_parent = node;
  262. if (less(p_value, node->value)) {
  263. node = node->left;
  264. } else if (less(node->value, p_value)) {
  265. node = node->right;
  266. } else {
  267. return node; // Return existing node
  268. }
  269. }
  270. Element *new_node = memnew_allocator(Element, A);
  271. new_node->parent = new_parent;
  272. new_node->right = _data._nil;
  273. new_node->left = _data._nil;
  274. new_node->value = p_value;
  275. //new_node->data=_data;
  276. if (new_parent == _data._root || less(p_value, new_parent->value)) {
  277. new_parent->left = new_node;
  278. } else {
  279. new_parent->right = new_node;
  280. }
  281. new_node->_next = _successor(new_node);
  282. new_node->_prev = _predecessor(new_node);
  283. if (new_node->_next) {
  284. new_node->_next->_prev = new_node;
  285. }
  286. if (new_node->_prev) {
  287. new_node->_prev->_next = new_node;
  288. }
  289. _data.size_cache++;
  290. _insert_rb_fix(new_node);
  291. return new_node;
  292. }
  293. void _erase_fix_rb(Element *p_node) {
  294. Element *root = _data._root->left;
  295. Element *node = _data._nil;
  296. Element *sibling = p_node;
  297. Element *parent = sibling->parent;
  298. while (node != root) { // If red node found, will exit at a break
  299. if (sibling->color == RED) {
  300. _set_color(sibling, BLACK);
  301. _set_color(parent, RED);
  302. if (sibling == parent->right) {
  303. sibling = sibling->left;
  304. _rotate_left(parent);
  305. } else {
  306. sibling = sibling->right;
  307. _rotate_right(parent);
  308. }
  309. }
  310. if ((sibling->left->color == BLACK) && (sibling->right->color == BLACK)) {
  311. _set_color(sibling, RED);
  312. if (parent->color == RED) {
  313. _set_color(parent, BLACK);
  314. break;
  315. } else { // loop: haven't found any red nodes yet
  316. node = parent;
  317. parent = node->parent;
  318. sibling = (node == parent->left) ? parent->right : parent->left;
  319. }
  320. } else {
  321. if (sibling == parent->right) {
  322. if (sibling->right->color == BLACK) {
  323. _set_color(sibling->left, BLACK);
  324. _set_color(sibling, RED);
  325. _rotate_right(sibling);
  326. sibling = sibling->parent;
  327. }
  328. _set_color(sibling, parent->color);
  329. _set_color(parent, BLACK);
  330. _set_color(sibling->right, BLACK);
  331. _rotate_left(parent);
  332. break;
  333. } else {
  334. if (sibling->left->color == BLACK) {
  335. _set_color(sibling->right, BLACK);
  336. _set_color(sibling, RED);
  337. _rotate_left(sibling);
  338. sibling = sibling->parent;
  339. }
  340. _set_color(sibling, parent->color);
  341. _set_color(parent, BLACK);
  342. _set_color(sibling->left, BLACK);
  343. _rotate_right(parent);
  344. break;
  345. }
  346. }
  347. }
  348. ERR_FAIL_COND(_data._nil->color != BLACK);
  349. }
  350. void _erase(Element *p_node) {
  351. Element *rp = ((p_node->left == _data._nil) || (p_node->right == _data._nil)) ? p_node : p_node->_next;
  352. Element *node = (rp->left == _data._nil) ? rp->right : rp->left;
  353. Element *sibling;
  354. if (rp == rp->parent->left) {
  355. rp->parent->left = node;
  356. sibling = rp->parent->right;
  357. } else {
  358. rp->parent->right = node;
  359. sibling = rp->parent->left;
  360. }
  361. if (node->color == RED) {
  362. node->parent = rp->parent;
  363. _set_color(node, BLACK);
  364. } else if (rp->color == BLACK && rp->parent != _data._root) {
  365. _erase_fix_rb(sibling);
  366. }
  367. if (rp != p_node) {
  368. ERR_FAIL_COND(rp == _data._nil);
  369. rp->left = p_node->left;
  370. rp->right = p_node->right;
  371. rp->parent = p_node->parent;
  372. rp->color = p_node->color;
  373. if (p_node->left != _data._nil) {
  374. p_node->left->parent = rp;
  375. }
  376. if (p_node->right != _data._nil) {
  377. p_node->right->parent = rp;
  378. }
  379. if (p_node == p_node->parent->left) {
  380. p_node->parent->left = rp;
  381. } else {
  382. p_node->parent->right = rp;
  383. }
  384. }
  385. if (p_node->_next) {
  386. p_node->_next->_prev = p_node->_prev;
  387. }
  388. if (p_node->_prev) {
  389. p_node->_prev->_next = p_node->_next;
  390. }
  391. memdelete_allocator<Element, A>(p_node);
  392. _data.size_cache--;
  393. ERR_FAIL_COND(_data._nil->color == RED);
  394. }
  395. void _calculate_depth(Element *p_element, int &max_d, int d) const {
  396. if (p_element == _data._nil) {
  397. return;
  398. }
  399. _calculate_depth(p_element->left, max_d, d + 1);
  400. _calculate_depth(p_element->right, max_d, d + 1);
  401. if (d > max_d) {
  402. max_d = d;
  403. }
  404. }
  405. void _cleanup_tree(Element *p_element) {
  406. if (p_element == _data._nil) {
  407. return;
  408. }
  409. _cleanup_tree(p_element->left);
  410. _cleanup_tree(p_element->right);
  411. memdelete_allocator<Element, A>(p_element);
  412. }
  413. void _copy_from(const Set &p_set) {
  414. clear();
  415. // not the fastest way, but safeset to write.
  416. for (Element *I = p_set.front(); I; I = I->next()) {
  417. insert(I->get());
  418. }
  419. }
  420. public:
  421. const Element *find(const T &p_value) const {
  422. if (!_data._root) {
  423. return nullptr;
  424. }
  425. const Element *res = _find(p_value);
  426. return res;
  427. }
  428. Element *find(const T &p_value) {
  429. if (!_data._root) {
  430. return nullptr;
  431. }
  432. Element *res = _find(p_value);
  433. return res;
  434. }
  435. Element *lower_bound(const T &p_value) const {
  436. return _lower_bound(p_value);
  437. }
  438. bool has(const T &p_value) const {
  439. return find(p_value) != nullptr;
  440. }
  441. Element *insert(const T &p_value) {
  442. if (!_data._root) {
  443. _data._create_root();
  444. }
  445. return _insert(p_value);
  446. }
  447. void erase(Element *p_element) {
  448. if (!_data._root || !p_element) {
  449. return;
  450. }
  451. _erase(p_element);
  452. if (_data.size_cache == 0 && _data._root) {
  453. _data._free_root();
  454. }
  455. }
  456. bool erase(const T &p_value) {
  457. if (!_data._root) {
  458. return false;
  459. }
  460. Element *e = find(p_value);
  461. if (!e) {
  462. return false;
  463. }
  464. _erase(e);
  465. if (_data.size_cache == 0 && _data._root) {
  466. _data._free_root();
  467. }
  468. return true;
  469. }
  470. Element *front() const {
  471. if (!_data._root) {
  472. return nullptr;
  473. }
  474. Element *e = _data._root->left;
  475. if (e == _data._nil) {
  476. return nullptr;
  477. }
  478. while (e->left != _data._nil) {
  479. e = e->left;
  480. }
  481. return e;
  482. }
  483. Element *back() const {
  484. if (!_data._root) {
  485. return nullptr;
  486. }
  487. Element *e = _data._root->left;
  488. if (e == _data._nil) {
  489. return nullptr;
  490. }
  491. while (e->right != _data._nil) {
  492. e = e->right;
  493. }
  494. return e;
  495. }
  496. inline bool is_empty() const { return _data.size_cache == 0; }
  497. inline int size() const { return _data.size_cache; }
  498. int calculate_depth() const {
  499. // used for debug mostly
  500. if (!_data._root) {
  501. return 0;
  502. }
  503. int max_d = 0;
  504. _calculate_depth(_data._root->left, max_d, 0);
  505. return max_d;
  506. }
  507. void clear() {
  508. if (!_data._root) {
  509. return;
  510. }
  511. _cleanup_tree(_data._root->left);
  512. _data._root->left = _data._nil;
  513. _data.size_cache = 0;
  514. _data._free_root();
  515. }
  516. void operator=(const Set &p_set) {
  517. _copy_from(p_set);
  518. }
  519. Set(const Set &p_set) {
  520. _copy_from(p_set);
  521. }
  522. _FORCE_INLINE_ Set() {}
  523. ~Set() {
  524. clear();
  525. }
  526. };
  527. #endif // SET_H