set.h 14 KB

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