set.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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 "os/memory.h"
  33. #include "typedefs.h"
  34. /**
  35. @author Juan Linietsky <reduzio@gmail.com>
  36. */
  37. // based on the very nice implementation of rb-trees by:
  38. // http://web.mit.edu/~emin/www/source_code/red_black_tree/index.html
  39. template <class T, class C = Comparator<T>, class A = DefaultAllocator>
  40. class Set {
  41. enum Color {
  42. RED,
  43. BLACK
  44. };
  45. struct _Data;
  46. public:
  47. class Element {
  48. private:
  49. friend class Set<T, C, A>;
  50. int color;
  51. Element *right;
  52. Element *left;
  53. Element *parent;
  54. Element *_next;
  55. Element *_prev;
  56. T value;
  57. //_Data *data;
  58. public:
  59. const Element *next() const {
  60. return _next;
  61. }
  62. Element *next() {
  63. return _next;
  64. }
  65. const Element *prev() const {
  66. return _prev;
  67. }
  68. Element *prev() {
  69. return _prev;
  70. }
  71. const T &get() const {
  72. return value;
  73. };
  74. Element() {
  75. color = RED;
  76. right = NULL;
  77. left = NULL;
  78. parent = NULL;
  79. _next = NULL;
  80. _prev = NULL;
  81. };
  82. };
  83. private:
  84. struct _Data {
  85. Element *_root;
  86. Element *_nil;
  87. int size_cache;
  88. _Data() {
  89. #ifdef GLOBALNIL_DISABLED
  90. _nil = memnew_allocator(Element, A);
  91. _nil->parent = _nil->left = _nil->right = _nil;
  92. _nil->color = BLACK;
  93. #else
  94. _nil = (Element *)&_GlobalNilClass::_nil;
  95. #endif
  96. _root = NULL;
  97. size_cache = 0;
  98. }
  99. void _create_root() {
  100. _root = memnew_allocator(Element, A);
  101. _root->parent = _root->left = _root->right = _nil;
  102. _root->color = BLACK;
  103. }
  104. void _free_root() {
  105. if (_root) {
  106. memdelete_allocator<Element, A>(_root);
  107. _root = NULL;
  108. }
  109. }
  110. ~_Data() {
  111. _free_root();
  112. #ifdef GLOBALNIL_DISABLED
  113. memdelete_allocator<Element, A>(_nil);
  114. #endif
  115. // memdelete_allocator<Element,A>(_root);
  116. }
  117. };
  118. _Data _data;
  119. inline void _set_color(Element *p_node, int p_color) {
  120. ERR_FAIL_COND(p_node == _data._nil && p_color == RED);
  121. p_node->color = p_color;
  122. }
  123. inline void _rotate_left(Element *p_node) {
  124. Element *r = p_node->right;
  125. p_node->right = r->left;
  126. if (r->left != _data._nil)
  127. r->left->parent = p_node;
  128. r->parent = p_node->parent;
  129. if (p_node == p_node->parent->left)
  130. p_node->parent->left = r;
  131. else
  132. p_node->parent->right = r;
  133. r->left = p_node;
  134. p_node->parent = r;
  135. }
  136. inline void _rotate_right(Element *p_node) {
  137. Element *l = p_node->left;
  138. p_node->left = l->right;
  139. if (l->right != _data._nil)
  140. l->right->parent = p_node;
  141. l->parent = p_node->parent;
  142. if (p_node == p_node->parent->right)
  143. p_node->parent->right = l;
  144. else
  145. p_node->parent->left = l;
  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 minium 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 NULL;
  163. return node->parent;
  164. }
  165. }
  166. inline Element *_predecessor(Element *p_node) const {
  167. Element *node = p_node;
  168. if (node->left != _data._nil) {
  169. node = node->left;
  170. while (node->right != _data._nil) { /* returns the minium of the left subtree of node */
  171. node = node->right;
  172. }
  173. return node;
  174. } else {
  175. while (node == node->parent->left) {
  176. if (node->parent == _data._root)
  177. return NULL;
  178. node = node->parent;
  179. }
  180. return node->parent;
  181. }
  182. }
  183. Element *_find(const T &p_value) const {
  184. Element *node = _data._root->left;
  185. C less;
  186. while (node != _data._nil) {
  187. if (less(p_value, node->value))
  188. node = node->left;
  189. else if (less(node->value, p_value))
  190. node = node->right;
  191. else
  192. break; // found
  193. }
  194. return (node != _data._nil) ? node : NULL;
  195. }
  196. Element *_lower_bound(const T &p_value) const {
  197. Element *node = _data._root->left;
  198. Element *prev = NULL;
  199. C less;
  200. while (node != _data._nil) {
  201. prev = node;
  202. if (less(p_value, node->value))
  203. node = node->left;
  204. else if (less(node->value, p_value))
  205. node = node->right;
  206. else
  207. break; // found
  208. }
  209. if (node == _data._nil) {
  210. if (prev == NULL)
  211. return NULL;
  212. if (less(prev->value, p_value)) {
  213. prev = prev->_next;
  214. }
  215. return prev;
  216. } else
  217. return node;
  218. }
  219. Element *_insert(const T &p_value, bool &r_exists) {
  220. Element *new_parent = _data._root;
  221. Element *node = _data._root->left;
  222. C less;
  223. while (node != _data._nil) {
  224. new_parent = node;
  225. if (less(p_value, node->value))
  226. node = node->left;
  227. else if (less(node->value, p_value))
  228. node = node->right;
  229. else {
  230. r_exists = true;
  231. return node;
  232. }
  233. }
  234. Element *new_node = memnew_allocator(Element, A);
  235. new_node->parent = new_parent;
  236. new_node->right = _data._nil;
  237. new_node->left = _data._nil;
  238. new_node->value = p_value;
  239. // new_node->data=_data;
  240. if (new_parent == _data._root || less(p_value, new_parent->value)) {
  241. new_parent->left = new_node;
  242. } else {
  243. new_parent->right = new_node;
  244. }
  245. r_exists = false;
  246. new_node->_next = _successor(new_node);
  247. new_node->_prev = _predecessor(new_node);
  248. if (new_node->_next)
  249. new_node->_next->_prev = new_node;
  250. if (new_node->_prev)
  251. new_node->_prev->_next = new_node;
  252. return new_node;
  253. }
  254. Element *_insert_rb(const T &p_value) {
  255. bool exists = false;
  256. Element *new_node = _insert(p_value, exists);
  257. if (exists)
  258. return new_node;
  259. Element *node = new_node;
  260. _data.size_cache++;
  261. while (node->parent->color == RED) {
  262. if (node->parent == node->parent->parent->left) {
  263. Element *aux = node->parent->parent->right;
  264. if (aux->color == RED) {
  265. _set_color(node->parent, BLACK);
  266. _set_color(aux, BLACK);
  267. _set_color(node->parent->parent, RED);
  268. node = node->parent->parent;
  269. } else {
  270. if (node == node->parent->right) {
  271. node = node->parent;
  272. _rotate_left(node);
  273. }
  274. _set_color(node->parent, BLACK);
  275. _set_color(node->parent->parent, RED);
  276. _rotate_right(node->parent->parent);
  277. }
  278. } else {
  279. Element *aux = node->parent->parent->left;
  280. if (aux->color == RED) {
  281. _set_color(node->parent, BLACK);
  282. _set_color(aux, BLACK);
  283. _set_color(node->parent->parent, RED);
  284. node = node->parent->parent;
  285. } else {
  286. if (node == node->parent->left) {
  287. node = node->parent;
  288. _rotate_right(node);
  289. }
  290. _set_color(node->parent, BLACK);
  291. _set_color(node->parent->parent, RED);
  292. _rotate_left(node->parent->parent);
  293. }
  294. }
  295. }
  296. _set_color(_data._root->left, BLACK);
  297. return new_node;
  298. }
  299. void _erase_fix(Element *p_node) {
  300. Element *root = _data._root->left;
  301. Element *node = p_node;
  302. while ((node->color == BLACK) && (root != node)) {
  303. if (node == node->parent->left) {
  304. Element *aux = node->parent->right;
  305. if (aux->color == RED) {
  306. _set_color(aux, BLACK);
  307. _set_color(node->parent, RED);
  308. _rotate_left(node->parent);
  309. aux = node->parent->right;
  310. }
  311. if ((aux->right->color == BLACK) && (aux->left->color == BLACK)) {
  312. _set_color(aux, RED);
  313. node = node->parent;
  314. } else {
  315. if (aux->right->color == BLACK) {
  316. _set_color(aux->left, BLACK);
  317. _set_color(aux, RED);
  318. _rotate_right(aux);
  319. aux = node->parent->right;
  320. }
  321. _set_color(aux, node->parent->color);
  322. _set_color(node->parent, BLACK);
  323. _set_color(aux->right, BLACK);
  324. _rotate_left(node->parent);
  325. node = root; /* this is to exit while loop */
  326. }
  327. } else { /* the code below is has left and right switched from above */
  328. Element *aux = node->parent->left;
  329. if (aux->color == RED) {
  330. _set_color(aux, BLACK);
  331. _set_color(node->parent, RED);
  332. _rotate_right(node->parent);
  333. aux = node->parent->left;
  334. }
  335. if ((aux->right->color == BLACK) && (aux->left->color == BLACK)) {
  336. _set_color(aux, RED);
  337. node = node->parent;
  338. } else {
  339. if (aux->left->color == BLACK) {
  340. _set_color(aux->right, BLACK);
  341. _set_color(aux, RED);
  342. _rotate_left(aux);
  343. aux = node->parent->left;
  344. }
  345. _set_color(aux, node->parent->color);
  346. _set_color(node->parent, BLACK);
  347. _set_color(aux->left, BLACK);
  348. _rotate_right(node->parent);
  349. node = root;
  350. }
  351. }
  352. }
  353. _set_color(node, BLACK);
  354. ERR_FAIL_COND(_data._nil->color != BLACK);
  355. }
  356. void _erase(Element *p_node) {
  357. Element *rp = ((p_node->left == _data._nil) || (p_node->right == _data._nil)) ? p_node : _successor(p_node);
  358. if (!rp)
  359. rp = _data._nil;
  360. Element *node = (rp->left == _data._nil) ? rp->right : rp->left;
  361. if (_data._root == (node->parent = rp->parent)) {
  362. _data._root->left = node;
  363. } else {
  364. if (rp == rp->parent->left) {
  365. rp->parent->left = node;
  366. } else {
  367. rp->parent->right = node;
  368. }
  369. }
  370. if (rp != p_node) {
  371. ERR_FAIL_COND(rp == _data._nil);
  372. if (rp->color == BLACK)
  373. _erase_fix(node);
  374. rp->left = p_node->left;
  375. rp->right = p_node->right;
  376. rp->parent = p_node->parent;
  377. rp->color = p_node->color;
  378. p_node->left->parent = rp;
  379. p_node->right->parent = rp;
  380. if (p_node == p_node->parent->left) {
  381. p_node->parent->left = rp;
  382. } else {
  383. p_node->parent->right = rp;
  384. }
  385. } else {
  386. if (p_node->color == BLACK)
  387. _erase_fix(node);
  388. }
  389. if (p_node->_next)
  390. p_node->_next->_prev = p_node->_prev;
  391. if (p_node->_prev)
  392. p_node->_prev->_next = p_node->_next;
  393. memdelete_allocator<Element, A>(p_node);
  394. _data.size_cache--;
  395. ERR_FAIL_COND(_data._nil->color == RED);
  396. }
  397. void _calculate_depth(Element *p_element, int &max_d, int d) const {
  398. if (p_element == _data._nil) {
  399. return;
  400. }
  401. _calculate_depth(p_element->left, max_d, d + 1);
  402. _calculate_depth(p_element->right, max_d, d + 1);
  403. if (d > max_d)
  404. max_d = d;
  405. }
  406. void _cleanup_tree(Element *p_element) {
  407. if (p_element == _data._nil)
  408. return;
  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 NULL;
  424. const Element *res = _find(p_value);
  425. return res;
  426. }
  427. Element *find(const T &p_value) {
  428. if (!_data._root)
  429. return NULL;
  430. Element *res = _find(p_value);
  431. return res;
  432. }
  433. bool has(const T &p_value) const {
  434. if (!_data._root)
  435. return false;
  436. return find(p_value) != NULL;
  437. }
  438. Element *insert(const T &p_value) {
  439. if (!_data._root)
  440. _data._create_root();
  441. return _insert_rb(p_value);
  442. }
  443. void erase(Element *p_element) {
  444. if (!_data._root)
  445. return;
  446. _erase(p_element);
  447. if (_data.size_cache == 0 && _data._root)
  448. _data._free_root();
  449. }
  450. bool erase(const T &p_value) {
  451. if (!_data._root)
  452. return false;
  453. Element *e = find(p_value);
  454. if (!e)
  455. return false;
  456. _erase(e);
  457. if (_data.size_cache == 0 && _data._root)
  458. _data._free_root();
  459. return true;
  460. }
  461. Element *front() const {
  462. if (!_data._root)
  463. return NULL;
  464. Element *e = _data._root->left;
  465. if (e == _data._nil)
  466. return NULL;
  467. while (e->left != _data._nil)
  468. e = e->left;
  469. return e;
  470. }
  471. Element *back() const {
  472. if (!_data._root)
  473. return NULL;
  474. Element *e = _data._root->left;
  475. if (e == _data._nil)
  476. return NULL;
  477. while (e->right != _data._nil)
  478. e = e->right;
  479. return e;
  480. }
  481. Element *lower_bound(const T &p_value) const {
  482. return _lower_bound(p_value);
  483. }
  484. inline int size() const { return _data.size_cache; }
  485. int calculate_depth() const {
  486. // used for debug mostly
  487. if (!_data._root)
  488. return 0;
  489. int max_d = 0;
  490. _calculate_depth(_data._root->left, max_d, 0);
  491. return max_d;
  492. }
  493. void clear() {
  494. if (!_data._root)
  495. return;
  496. _cleanup_tree(_data._root->left);
  497. _data._root->left = _data._nil;
  498. _data.size_cache = 0;
  499. _data._nil->parent = _data._nil;
  500. _data._free_root();
  501. }
  502. void operator=(const Set &p_set) {
  503. _copy_from(p_set);
  504. }
  505. Set(const Set &p_set) {
  506. _copy_from(p_set);
  507. }
  508. _FORCE_INLINE_ Set() {
  509. }
  510. ~Set() {
  511. clear();
  512. }
  513. };
  514. #endif