set.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /*************************************************************************/
  2. /* set.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef SET_H
  30. #define SET_H
  31. #include "typedefs.h"
  32. #include "os/memory.h"
  33. /**
  34. @author Juan Linietsky <reduzio@gmail.com>
  35. */
  36. // based on the very nice implementation of rb-trees by:
  37. // http://web.mit.edu/~emin/www/source_code/red_black_tree/index.html
  38. template <class T,class C=Comparator<T>,class A=DefaultAllocator >
  39. class Set {
  40. enum Color {
  41. RED,
  42. BLACK
  43. };
  44. struct _Data;
  45. public:
  46. class Element {
  47. private:
  48. friend class Set<T,C,A>;
  49. int color;
  50. Element* right;
  51. Element* left;
  52. Element* parent;
  53. Element* _next;
  54. Element* _prev;
  55. T value;
  56. //_Data *data;
  57. public:
  58. const Element *next() const {
  59. return _next;
  60. }
  61. Element *next() {
  62. return _next;
  63. }
  64. const Element *prev() const {
  65. return _prev;
  66. }
  67. Element *prev() {
  68. return _prev;
  69. }
  70. const T& get() const {
  71. return value;
  72. };
  73. Element() {
  74. color=RED;
  75. right=NULL;
  76. left=NULL;
  77. parent=NULL;
  78. _next=NULL;
  79. _prev=NULL;
  80. };
  81. };
  82. private:
  83. struct _Data {
  84. Element* _root;
  85. Element* _nil;
  86. int size_cache;
  87. _Data() {
  88. #ifdef GLOBALNIL_DISABLED
  89. _nil = memnew_allocator( Element,A );
  90. _nil->parent=_nil->left=_nil->right=_nil;
  91. _nil->color=BLACK;
  92. #else
  93. _nil=(Element*)&_GlobalNilClass::_nil;
  94. #endif
  95. _root = NULL;
  96. size_cache=0;
  97. }
  98. void _create_root() {
  99. _root = memnew_allocator( Element,A );
  100. _root->parent=_root->left=_root->right=_nil;
  101. _root->color=BLACK;
  102. }
  103. void _free_root() {
  104. if (_root) {
  105. memdelete_allocator<Element,A>(_root);
  106. _root=NULL;
  107. }
  108. }
  109. ~_Data() {
  110. _free_root();
  111. #ifdef GLOBALNIL_DISABLED
  112. memdelete_allocator<Element,A>(_nil);
  113. #endif
  114. // memdelete_allocator<Element,A>(_root);
  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;
  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. if (node->parent == _data._root)
  176. return NULL;
  177. node=node->parent;
  178. }
  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. break; // found
  192. }
  193. return (node!=_data._nil)?node: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. break; // found
  207. }
  208. if (node==_data._nil) {
  209. if (prev==NULL)
  210. return NULL;
  211. if (less(prev->value,p_value)) {
  212. prev=prev->_next;
  213. }
  214. return prev;
  215. } else
  216. return node;
  217. }
  218. Element *_insert(const T& p_value, bool& r_exists) {
  219. Element *new_parent=_data._root;
  220. Element *node = _data._root->left;
  221. C less;
  222. while (node!=_data._nil) {
  223. new_parent=node;
  224. if (less(p_value,node->value))
  225. node=node->left;
  226. else if (less(node->value,p_value))
  227. node=node->right;
  228. else {
  229. r_exists=true;
  230. return node;
  231. }
  232. }
  233. Element *new_node = memnew_allocator( Element,A );
  234. new_node->parent=new_parent;
  235. new_node->right=_data._nil;
  236. new_node->left=_data._nil;
  237. new_node->value=p_value;
  238. // new_node->data=_data;
  239. if (new_parent==_data._root || less(p_value,new_parent->value)) {
  240. new_parent->left=new_node;
  241. } else {
  242. new_parent->right=new_node;
  243. }
  244. r_exists=false;
  245. new_node->_next=_successor(new_node);
  246. new_node->_prev=_predecessor(new_node);
  247. if (new_node->_next)
  248. new_node->_next->_prev=new_node;
  249. if (new_node->_prev)
  250. new_node->_prev->_next=new_node;
  251. return new_node;
  252. }
  253. Element * _insert_rb(const T& p_value) {
  254. bool exists=false;
  255. Element *new_node = _insert(p_value,exists);
  256. if (exists)
  257. return new_node;
  258. Element *node=new_node;
  259. _data.size_cache++;
  260. while(node->parent->color==RED) {
  261. if (node->parent == node->parent->parent->left) {
  262. Element *aux=node->parent->parent->right;
  263. if (aux->color==RED) {
  264. _set_color(node->parent,BLACK);
  265. _set_color(aux,BLACK);
  266. _set_color(node->parent->parent,RED);
  267. node=node->parent->parent;
  268. } else {
  269. if (node == node->parent->right) {
  270. node=node->parent;
  271. _rotate_left(node);
  272. }
  273. _set_color(node->parent,BLACK);
  274. _set_color(node->parent->parent,RED);
  275. _rotate_right(node->parent->parent);
  276. }
  277. } else {
  278. Element *aux=node->parent->parent->left;
  279. if (aux->color==RED) {
  280. _set_color(node->parent,BLACK);
  281. _set_color(aux,BLACK);
  282. _set_color(node->parent->parent,RED);
  283. node=node->parent->parent;
  284. } else {
  285. if (node == node->parent->left) {
  286. node=node->parent;
  287. _rotate_right(node);
  288. }
  289. _set_color(node->parent,BLACK);
  290. _set_color(node->parent->parent,RED);
  291. _rotate_left(node->parent->parent);
  292. }
  293. }
  294. }
  295. _set_color(_data._root->left,BLACK);
  296. return new_node;
  297. }
  298. void _erase_fix(Element *p_node) {
  299. Element *root = _data._root->left;
  300. Element *node=p_node;
  301. while( (node->color==BLACK) && (root != node)) {
  302. if (node == node->parent->left) {
  303. Element *aux=node->parent->right;
  304. if (aux->color==RED) {
  305. _set_color(aux,BLACK);
  306. _set_color(node->parent,RED);
  307. _rotate_left(node->parent);
  308. aux=node->parent->right;
  309. }
  310. if ( (aux->right->color==BLACK) && (aux->left->color==BLACK) ) {
  311. _set_color(aux,RED);
  312. node=node->parent;
  313. } else {
  314. if (aux->right->color==BLACK) {
  315. _set_color(aux->left,BLACK);
  316. _set_color(aux,RED);
  317. _rotate_right(aux);
  318. aux=node->parent->right;
  319. }
  320. _set_color(aux,node->parent->color);
  321. _set_color(node->parent,BLACK);
  322. _set_color(aux->right,BLACK);
  323. _rotate_left(node->parent);
  324. node=root; /* this is to exit while loop */
  325. }
  326. } else { /* the code below is has left and right switched from above */
  327. Element *aux=node->parent->left;
  328. if (aux->color==RED) {
  329. _set_color(aux,BLACK);
  330. _set_color(node->parent,RED);;
  331. _rotate_right(node->parent);
  332. aux=node->parent->left;
  333. }
  334. if ( (aux->right->color==BLACK) && (aux->left->color==BLACK) ) {
  335. _set_color(aux,RED);
  336. node=node->parent;
  337. } else {
  338. if (aux->left->color==BLACK) {
  339. _set_color(aux->right,BLACK);
  340. _set_color(aux,RED);
  341. _rotate_left(aux);
  342. aux=node->parent->left;
  343. }
  344. _set_color(aux,node->parent->color);
  345. _set_color(node->parent,BLACK);
  346. _set_color(aux->left,BLACK);
  347. _rotate_right(node->parent);
  348. node=root;
  349. }
  350. }
  351. }
  352. _set_color(node,BLACK);
  353. ERR_FAIL_COND(_data._nil->color!=BLACK);
  354. }
  355. void _erase(Element *p_node) {
  356. Element *rp= ((p_node->left == _data._nil) || (p_node->right == _data._nil)) ? p_node : _successor(p_node);
  357. if (!rp)
  358. rp=_data._nil;
  359. Element *node= (rp->left == _data._nil) ? rp->right : rp->left;
  360. if (_data._root == (node->parent=rp->parent) ) {
  361. _data._root->left=node;
  362. } else {
  363. if (rp == rp->parent->left) {
  364. rp->parent->left=node;
  365. } else {
  366. rp->parent->right=node;
  367. }
  368. }
  369. if (rp != p_node) {
  370. ERR_FAIL_COND( rp == _data._nil );
  371. if (rp->color==BLACK)
  372. _erase_fix(node);
  373. rp->left=p_node->left;
  374. rp->right=p_node->right;
  375. rp->parent=p_node->parent;
  376. rp->color=p_node->color;
  377. p_node->left->parent=rp;
  378. p_node->right->parent=rp;
  379. if (p_node == p_node->parent->left) {
  380. p_node->parent->left=rp;
  381. } else {
  382. p_node->parent->right=rp;
  383. }
  384. } else {
  385. if (p_node->color==BLACK)
  386. _erase_fix(node);
  387. }
  388. if (p_node->_next)
  389. p_node->_next->_prev=p_node->_prev;
  390. if (p_node->_prev)
  391. p_node->_prev->_next=p_node->_next;
  392. memdelete_allocator<Element,A>(p_node);
  393. _data.size_cache--;
  394. ERR_FAIL_COND( _data._nil->color==RED );
  395. }
  396. void _calculate_depth(Element *p_element,int &max_d,int d) const {
  397. if (p_element==_data._nil) {
  398. return;
  399. }
  400. _calculate_depth(p_element->left,max_d,d+1);
  401. _calculate_depth(p_element->right,max_d,d+1);
  402. if (d>max_d)
  403. max_d=d;
  404. }
  405. void _cleanup_tree(Element *p_element) {
  406. if (p_element==_data._nil)
  407. return;
  408. _cleanup_tree(p_element->left);
  409. _cleanup_tree(p_element->right);
  410. memdelete_allocator<Element,A>( p_element );
  411. }
  412. void _copy_from( const Set& p_set) {
  413. clear();
  414. // not the fastest way, but safeset to write.
  415. for(Element *I=p_set.front();I;I=I->next()) {
  416. insert(I->get());
  417. }
  418. }
  419. public:
  420. const Element *find(const T& p_value) const {
  421. if (!_data._root)
  422. return NULL;
  423. const Element *res=_find(p_value);
  424. return res;
  425. }
  426. Element *find(const T& p_value) {
  427. if (!_data._root)
  428. return NULL;
  429. Element *res=_find(p_value);
  430. return res;
  431. }
  432. bool has(const T& p_value) const {
  433. if (!_data._root)
  434. return false;
  435. return find(p_value)!=NULL;
  436. }
  437. Element *insert(const T& p_value) {
  438. if (!_data._root)
  439. _data._create_root();
  440. return _insert_rb(p_value);
  441. }
  442. void erase(Element* p_element) {
  443. if (!_data._root)
  444. return;
  445. _erase(p_element);
  446. if (_data.size_cache==0 && _data._root)
  447. _data._free_root();
  448. }
  449. bool erase(const T& p_value) {
  450. if (!_data._root)
  451. return false;
  452. Element *e=find(p_value);
  453. if (!e)
  454. return false;
  455. _erase(e);
  456. if (_data.size_cache==0 && _data._root)
  457. _data._free_root();
  458. return true;
  459. }
  460. Element *front() const {
  461. if (!_data._root)
  462. return NULL;
  463. Element *e=_data._root->left;
  464. if (e==_data._nil)
  465. return NULL;
  466. while(e->left!=_data._nil)
  467. e=e->left;
  468. return e;
  469. }
  470. Element *back() const {
  471. if (!_data._root)
  472. return NULL;
  473. Element *e=_data._root->left;
  474. if (e==_data._nil)
  475. return NULL;
  476. while(e->right!=_data._nil)
  477. e=e->right;
  478. return e;
  479. }
  480. Element *lower_bound(const T& p_value) const {
  481. return _lower_bound(p_value);
  482. }
  483. inline int size() const { return _data.size_cache; }
  484. int calculate_depth() const {
  485. // used for debug mostly
  486. if (!_data._root)
  487. return 0;
  488. int max_d=0;
  489. _calculate_depth(_data._root->left,max_d,0);
  490. return max_d;
  491. }
  492. void clear() {
  493. if (!_data._root)
  494. return;
  495. _cleanup_tree(_data._root->left);
  496. _data._root->left=_data._nil;
  497. _data.size_cache=0;
  498. _data._nil->parent=_data._nil;
  499. _data._free_root();
  500. }
  501. void operator=(const Set& p_set) {
  502. _copy_from( p_set );
  503. }
  504. Set(const Set& p_set) {
  505. _copy_from( p_set );
  506. }
  507. _FORCE_INLINE_ Set() {
  508. }
  509. ~Set() {
  510. clear();
  511. }
  512. };
  513. #endif