set.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*************************************************************************/
  2. /* set.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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 <[email protected]>
  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 *_insert(const T& p_value, bool& r_exists) {
  196. Element *new_parent=_data._root;
  197. Element *node = _data._root->left;
  198. C less;
  199. while (node!=_data._nil) {
  200. new_parent=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. r_exists=true;
  207. return node;
  208. }
  209. }
  210. Element *new_node = memnew_allocator( Element,A );
  211. new_node->parent=new_parent;
  212. new_node->right=_data._nil;
  213. new_node->left=_data._nil;
  214. new_node->value=p_value;
  215. // new_node->data=_data;
  216. if (new_parent==_data._root || less(p_value,new_parent->value)) {
  217. new_parent->left=new_node;
  218. } else {
  219. new_parent->right=new_node;
  220. }
  221. r_exists=false;
  222. new_node->_next=_successor(new_node);
  223. new_node->_prev=_predecessor(new_node);
  224. if (new_node->_next)
  225. new_node->_next->_prev=new_node;
  226. if (new_node->_prev)
  227. new_node->_prev->_next=new_node;
  228. return new_node;
  229. }
  230. Element * _insert_rb(const T& p_value) {
  231. bool exists=false;
  232. Element *new_node = _insert(p_value,exists);
  233. if (exists)
  234. return new_node;
  235. Element *node=new_node;
  236. _data.size_cache++;
  237. while(node->parent->color==RED) {
  238. if (node->parent == node->parent->parent->left) {
  239. Element *aux=node->parent->parent->right;
  240. if (aux->color==RED) {
  241. _set_color(node->parent,BLACK);
  242. _set_color(aux,BLACK);
  243. _set_color(node->parent->parent,RED);
  244. node=node->parent->parent;
  245. } else {
  246. if (node == node->parent->right) {
  247. node=node->parent;
  248. _rotate_left(node);
  249. }
  250. _set_color(node->parent,BLACK);
  251. _set_color(node->parent->parent,RED);
  252. _rotate_right(node->parent->parent);
  253. }
  254. } else {
  255. Element *aux=node->parent->parent->left;
  256. if (aux->color==RED) {
  257. _set_color(node->parent,BLACK);
  258. _set_color(aux,BLACK);
  259. _set_color(node->parent->parent,RED);
  260. node=node->parent->parent;
  261. } else {
  262. if (node == node->parent->left) {
  263. node=node->parent;
  264. _rotate_right(node);
  265. }
  266. _set_color(node->parent,BLACK);
  267. _set_color(node->parent->parent,RED);
  268. _rotate_left(node->parent->parent);
  269. }
  270. }
  271. }
  272. _set_color(_data._root->left,BLACK);
  273. return new_node;
  274. }
  275. void _erase_fix(Element *p_node) {
  276. Element *root = _data._root->left;
  277. Element *node=p_node;
  278. while( (node->color==BLACK) && (root != node)) {
  279. if (node == node->parent->left) {
  280. Element *aux=node->parent->right;
  281. if (aux->color==RED) {
  282. _set_color(aux,BLACK);
  283. _set_color(node->parent,RED);
  284. _rotate_left(node->parent);
  285. aux=node->parent->right;
  286. }
  287. if ( (aux->right->color==BLACK) && (aux->left->color==BLACK) ) {
  288. _set_color(aux,RED);
  289. node=node->parent;
  290. } else {
  291. if (aux->right->color==BLACK) {
  292. _set_color(aux->left,BLACK);
  293. _set_color(aux,RED);
  294. _rotate_right(aux);
  295. aux=node->parent->right;
  296. }
  297. _set_color(aux,node->parent->color);
  298. _set_color(node->parent,BLACK);
  299. _set_color(aux->right,BLACK);
  300. _rotate_left(node->parent);
  301. node=root; /* this is to exit while loop */
  302. }
  303. } else { /* the code below is has left and right switched from above */
  304. Element *aux=node->parent->left;
  305. if (aux->color==RED) {
  306. _set_color(aux,BLACK);
  307. _set_color(node->parent,RED);;
  308. _rotate_right(node->parent);
  309. aux=node->parent->left;
  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->left->color==BLACK) {
  316. _set_color(aux->right,BLACK);
  317. _set_color(aux,RED);
  318. _rotate_left(aux);
  319. aux=node->parent->left;
  320. }
  321. _set_color(aux,node->parent->color);
  322. _set_color(node->parent,BLACK);
  323. _set_color(aux->left,BLACK);
  324. _rotate_right(node->parent);
  325. node=root;
  326. }
  327. }
  328. }
  329. _set_color(node,BLACK);
  330. ERR_FAIL_COND(_data._nil->color!=BLACK);
  331. }
  332. void _erase(Element *p_node) {
  333. Element *rp= ((p_node->left == _data._nil) || (p_node->right == _data._nil)) ? p_node : _successor(p_node);
  334. if (!rp)
  335. rp=_data._nil;
  336. Element *node= (rp->left == _data._nil) ? rp->right : rp->left;
  337. if (_data._root == (node->parent=rp->parent) ) {
  338. _data._root->left=node;
  339. } else {
  340. if (rp == rp->parent->left) {
  341. rp->parent->left=node;
  342. } else {
  343. rp->parent->right=node;
  344. }
  345. }
  346. if (rp != p_node) {
  347. ERR_FAIL_COND( rp == _data._nil );
  348. if (rp->color==BLACK)
  349. _erase_fix(node);
  350. rp->left=p_node->left;
  351. rp->right=p_node->right;
  352. rp->parent=p_node->parent;
  353. rp->color=p_node->color;
  354. p_node->left->parent=rp;
  355. p_node->right->parent=rp;
  356. if (p_node == p_node->parent->left) {
  357. p_node->parent->left=rp;
  358. } else {
  359. p_node->parent->right=rp;
  360. }
  361. } else {
  362. if (p_node->color==BLACK)
  363. _erase_fix(node);
  364. }
  365. if (p_node->_next)
  366. p_node->_next->_prev=p_node->_prev;
  367. if (p_node->_prev)
  368. p_node->_prev->_next=p_node->_next;
  369. memdelete_allocator<Element,A>(p_node);
  370. _data.size_cache--;
  371. ERR_FAIL_COND( _data._nil->color==RED );
  372. }
  373. void _calculate_depth(Element *p_element,int &max_d,int d) const {
  374. if (p_element==_data._nil) {
  375. return;
  376. }
  377. _calculate_depth(p_element->left,max_d,d+1);
  378. _calculate_depth(p_element->right,max_d,d+1);
  379. if (d>max_d)
  380. max_d=d;
  381. }
  382. void _cleanup_tree(Element *p_element) {
  383. if (p_element==_data._nil)
  384. return;
  385. _cleanup_tree(p_element->left);
  386. _cleanup_tree(p_element->right);
  387. memdelete_allocator<Element,A>( p_element );
  388. }
  389. void _copy_from( const Set& p_set) {
  390. clear();
  391. // not the fastest way, but safeset to write.
  392. for(Element *I=p_set.front();I;I=I->next()) {
  393. insert(I->get());
  394. }
  395. }
  396. public:
  397. const Element *find(const T& p_value) const {
  398. if (!_data._root)
  399. return NULL;
  400. const Element *res=_find(p_value);
  401. return res;
  402. }
  403. Element *find(const T& p_value) {
  404. if (!_data._root)
  405. return NULL;
  406. Element *res=_find(p_value);
  407. return res;
  408. }
  409. bool has(const T& p_value) const {
  410. if (!_data._root)
  411. return false;
  412. return find(p_value)!=NULL;
  413. }
  414. Element *insert(const T& p_value) {
  415. if (!_data._root)
  416. _data._create_root();
  417. return _insert_rb(p_value);
  418. }
  419. void erase(Element* p_element) {
  420. if (!_data._root)
  421. return;
  422. _erase(p_element);
  423. if (_data.size_cache==0 && _data._root)
  424. _data._free_root();
  425. }
  426. bool erase(const T& p_value) {
  427. if (!_data._root)
  428. return false;
  429. Element *e=find(p_value);
  430. if (!e)
  431. return false;
  432. _erase(e);
  433. if (_data.size_cache==0 && _data._root)
  434. _data._free_root();
  435. return true;
  436. }
  437. Element *front() const {
  438. if (!_data._root)
  439. return NULL;
  440. Element *e=_data._root->left;
  441. if (e==_data._nil)
  442. return NULL;
  443. while(e->left!=_data._nil)
  444. e=e->left;
  445. return e;
  446. }
  447. Element *back() const {
  448. if (!_data._root)
  449. return NULL;
  450. Element *e=_data._root->left;
  451. if (e==_data._nil)
  452. return NULL;
  453. while(e->right!=_data._nil)
  454. e=e->right;
  455. return e;
  456. }
  457. inline int size() const { return _data.size_cache; }
  458. int calculate_depth() const {
  459. // used for debug mostly
  460. if (!_data._root)
  461. return 0;
  462. int max_d=0;
  463. _calculate_depth(_data._root->left,max_d,0);
  464. return max_d;
  465. }
  466. void clear() {
  467. if (!_data._root)
  468. return;
  469. _cleanup_tree(_data._root->left);
  470. _data._root->left=_data._nil;
  471. _data.size_cache=0;
  472. _data._nil->parent=_data._nil;
  473. _data._free_root();
  474. }
  475. void operator=(const Set& p_set) {
  476. _copy_from( p_set );
  477. }
  478. Set(const Set& p_set) {
  479. _copy_from( p_set );
  480. }
  481. _FORCE_INLINE_ Set() {
  482. }
  483. ~Set() {
  484. clear();
  485. }
  486. };
  487. #endif