set.h 14 KB

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