set.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*************************************************************************/
  2. /* set.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. _FORCE_INLINE_ _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. }
  116. };
  117. _Data _data;
  118. inline void _set_color(Element *p_node, int p_color) {
  119. ERR_FAIL_COND(p_node == _data._nil && p_color == RED);
  120. p_node->color = p_color;
  121. }
  122. inline void _rotate_left(Element *p_node) {
  123. Element *r = p_node->right;
  124. p_node->right = r->left;
  125. if (r->left != _data._nil)
  126. r->left->parent = p_node;
  127. r->parent = p_node->parent;
  128. if (p_node == p_node->parent->left)
  129. p_node->parent->left = r;
  130. else
  131. p_node->parent->right = r;
  132. r->left = p_node;
  133. p_node->parent = r;
  134. }
  135. inline void _rotate_right(Element *p_node) {
  136. Element *l = p_node->left;
  137. p_node->left = l->right;
  138. if (l->right != _data._nil)
  139. l->right->parent = p_node;
  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. l->right = p_node;
  146. p_node->parent = l;
  147. }
  148. inline Element *_successor(Element *p_node) const {
  149. Element *node = p_node;
  150. if (node->right != _data._nil) {
  151. node = node->right;
  152. while (node->left != _data._nil) { /* returns the minium of the right subtree of node */
  153. node = node->left;
  154. }
  155. return node;
  156. } else {
  157. while (node == node->parent->right) {
  158. node = node->parent;
  159. }
  160. if (node->parent == _data._root)
  161. return NULL; // No successor, as p_node = last node
  162. return node->parent;
  163. }
  164. }
  165. inline Element *_predecessor(Element *p_node) const {
  166. Element *node = p_node;
  167. if (node->left != _data._nil) {
  168. node = node->left;
  169. while (node->right != _data._nil) { /* returns the minium of the left subtree of node */
  170. node = node->right;
  171. }
  172. return node;
  173. } else {
  174. while (node == node->parent->left) {
  175. node = node->parent;
  176. }
  177. if (node == _data._root)
  178. return NULL; // No predecessor, as p_node = first node.
  179. return node->parent;
  180. }
  181. }
  182. Element *_find(const T &p_value) const {
  183. Element *node = _data._root->left;
  184. C less;
  185. while (node != _data._nil) {
  186. if (less(p_value, node->value))
  187. node = node->left;
  188. else if (less(node->value, p_value))
  189. node = node->right;
  190. else
  191. return node; // found
  192. }
  193. return NULL;
  194. }
  195. Element *_lower_bound(const T &p_value) const {
  196. Element *node = _data._root->left;
  197. Element *prev = NULL;
  198. C less;
  199. while (node != _data._nil) {
  200. prev = node;
  201. if (less(p_value, node->value))
  202. node = node->left;
  203. else if (less(node->value, p_value))
  204. node = node->right;
  205. else
  206. return node; // found
  207. }
  208. if (prev == NULL)
  209. return NULL; // tree empty
  210. if (less(prev->value, p_value))
  211. prev = prev->_next;
  212. return prev;
  213. }
  214. void _insert_rb_fix(Element *p_new_node) {
  215. Element *node = p_new_node;
  216. Element *nparent = node->parent;
  217. Element *ngrand_parent;
  218. while (nparent->color == RED) {
  219. ngrand_parent = nparent->parent;
  220. if (nparent == ngrand_parent->left) {
  221. if (ngrand_parent->right->color == RED) {
  222. _set_color(nparent, BLACK);
  223. _set_color(ngrand_parent->right, BLACK);
  224. _set_color(ngrand_parent, RED);
  225. node = ngrand_parent;
  226. nparent = node->parent;
  227. } else {
  228. if (node == nparent->right) {
  229. _rotate_left(nparent);
  230. node = nparent;
  231. nparent = node->parent;
  232. }
  233. _set_color(nparent, BLACK);
  234. _set_color(ngrand_parent, RED);
  235. _rotate_right(ngrand_parent);
  236. }
  237. } else {
  238. if (ngrand_parent->left->color == RED) {
  239. _set_color(nparent, BLACK);
  240. _set_color(ngrand_parent->left, BLACK);
  241. _set_color(ngrand_parent, RED);
  242. node = ngrand_parent;
  243. nparent = node->parent;
  244. } else {
  245. if (node == nparent->left) {
  246. _rotate_right(nparent);
  247. node = nparent;
  248. nparent = node->parent;
  249. }
  250. _set_color(nparent, BLACK);
  251. _set_color(ngrand_parent, RED);
  252. _rotate_left(ngrand_parent);
  253. }
  254. }
  255. }
  256. _set_color(_data._root->left, BLACK);
  257. }
  258. Element *_insert(const T &p_value) {
  259. Element *new_parent = _data._root;
  260. Element *node = _data._root->left;
  261. C less;
  262. while (node != _data._nil) {
  263. new_parent = node;
  264. if (less(p_value, node->value))
  265. node = node->left;
  266. else if (less(node->value, p_value))
  267. node = node->right;
  268. else {
  269. return node; // Return existing node
  270. }
  271. }
  272. Element *new_node = memnew_allocator(Element, A);
  273. new_node->parent = new_parent;
  274. new_node->right = _data._nil;
  275. new_node->left = _data._nil;
  276. new_node->value = p_value;
  277. //new_node->data=_data;
  278. if (new_parent == _data._root || less(p_value, new_parent->value)) {
  279. new_parent->left = new_node;
  280. } else {
  281. new_parent->right = new_node;
  282. }
  283. new_node->_next = _successor(new_node);
  284. new_node->_prev = _predecessor(new_node);
  285. if (new_node->_next)
  286. new_node->_next->_prev = new_node;
  287. if (new_node->_prev)
  288. new_node->_prev->_next = new_node;
  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. if (p_node->right != _data._nil)
  376. p_node->right->parent = rp;
  377. if (p_node == p_node->parent->left) {
  378. p_node->parent->left = rp;
  379. } else {
  380. p_node->parent->right = rp;
  381. }
  382. }
  383. if (p_node->_next)
  384. p_node->_next->_prev = p_node->_prev;
  385. if (p_node->_prev)
  386. p_node->_prev->_next = p_node->_next;
  387. memdelete_allocator<Element, A>(p_node);
  388. _data.size_cache--;
  389. ERR_FAIL_COND(_data._nil->color == RED);
  390. }
  391. void _calculate_depth(Element *p_element, int &max_d, int d) const {
  392. if (p_element == _data._nil)
  393. return;
  394. _calculate_depth(p_element->left, max_d, d + 1);
  395. _calculate_depth(p_element->right, max_d, d + 1);
  396. if (d > max_d)
  397. max_d = d;
  398. }
  399. void _cleanup_tree(Element *p_element) {
  400. if (p_element == _data._nil)
  401. return;
  402. _cleanup_tree(p_element->left);
  403. _cleanup_tree(p_element->right);
  404. memdelete_allocator<Element, A>(p_element);
  405. }
  406. void _copy_from(const Set &p_set) {
  407. clear();
  408. // not the fastest way, but safeset to write.
  409. for (Element *I = p_set.front(); I; I = I->next()) {
  410. insert(I->get());
  411. }
  412. }
  413. public:
  414. const Element *find(const T &p_value) const {
  415. if (!_data._root)
  416. return NULL;
  417. const Element *res = _find(p_value);
  418. return res;
  419. }
  420. Element *find(const T &p_value) {
  421. if (!_data._root)
  422. return NULL;
  423. Element *res = _find(p_value);
  424. return res;
  425. }
  426. Element *lower_bound(const T &p_value) const {
  427. return _lower_bound(p_value);
  428. }
  429. bool has(const T &p_value) const {
  430. return find(p_value) != NULL;
  431. }
  432. Element *insert(const T &p_value) {
  433. if (!_data._root)
  434. _data._create_root();
  435. return _insert(p_value);
  436. }
  437. void erase(Element *p_element) {
  438. if (!_data._root || !p_element)
  439. return;
  440. _erase(p_element);
  441. if (_data.size_cache == 0 && _data._root)
  442. _data._free_root();
  443. }
  444. bool erase(const T &p_value) {
  445. if (!_data._root)
  446. return false;
  447. Element *e = find(p_value);
  448. if (!e)
  449. return false;
  450. _erase(e);
  451. if (_data.size_cache == 0 && _data._root)
  452. _data._free_root();
  453. return true;
  454. }
  455. Element *front() const {
  456. if (!_data._root)
  457. return NULL;
  458. Element *e = _data._root->left;
  459. if (e == _data._nil)
  460. return NULL;
  461. while (e->left != _data._nil)
  462. e = e->left;
  463. return e;
  464. }
  465. Element *back() const {
  466. if (!_data._root)
  467. return NULL;
  468. Element *e = _data._root->left;
  469. if (e == _data._nil)
  470. return NULL;
  471. while (e->right != _data._nil)
  472. e = e->right;
  473. return e;
  474. }
  475. inline int size() const { return _data.size_cache; }
  476. int calculate_depth() const {
  477. // used for debug mostly
  478. if (!_data._root)
  479. return 0;
  480. int max_d = 0;
  481. _calculate_depth(_data._root->left, max_d, 0);
  482. return max_d;
  483. }
  484. void clear() {
  485. if (!_data._root)
  486. return;
  487. _cleanup_tree(_data._root->left);
  488. _data._root->left = _data._nil;
  489. _data.size_cache = 0;
  490. _data._free_root();
  491. }
  492. void operator=(const Set &p_set) {
  493. _copy_from(p_set);
  494. }
  495. Set(const Set &p_set) {
  496. _copy_from(p_set);
  497. }
  498. _FORCE_INLINE_ Set() {
  499. }
  500. ~Set() {
  501. clear();
  502. }
  503. };
  504. #endif