map.h 15 KB

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