list.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /*************************************************************************/
  2. /* list.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 GLOBALS_LIST_H
  30. #define GLOBALS_LIST_H
  31. #include "os/memory.h"
  32. #include "sort.h"
  33. /**
  34. * Generic Templatized Linked List Implementation.
  35. * The implementation differs from the STL one because
  36. * a compatible preallocated linked list can be written
  37. * using the same API, or features such as erasing an element
  38. * from the iterator.
  39. */
  40. template <class T,class A=DefaultAllocator>
  41. class List {
  42. struct _Data;
  43. public:
  44. class Element {
  45. private:
  46. friend class List<T,A>;
  47. T value;
  48. Element* next_ptr;
  49. Element* prev_ptr;
  50. _Data *data;
  51. public:
  52. /**
  53. * Get NEXT Element iterator, for constant lists.
  54. */
  55. _FORCE_INLINE_ const Element* next() const {
  56. return next_ptr;
  57. };
  58. /**
  59. * Get NEXT Element iterator,
  60. */
  61. _FORCE_INLINE_ Element* next() {
  62. return next_ptr;
  63. };
  64. /**
  65. * Get PREV Element iterator, for constant lists.
  66. */
  67. _FORCE_INLINE_ const Element* prev() const {
  68. return prev_ptr;
  69. };
  70. /**
  71. * Get PREV Element iterator,
  72. */
  73. _FORCE_INLINE_ Element* prev() {
  74. return prev_ptr;
  75. };
  76. /**
  77. * * operator, for using as *iterator, when iterators are defined on stack.
  78. */
  79. _FORCE_INLINE_ const T& operator *() const {
  80. return value;
  81. };
  82. /**
  83. * operator->, for using as iterator->, when iterators are defined on stack, for constant lists.
  84. */
  85. _FORCE_INLINE_ const T* operator->() const {
  86. return &value;
  87. };
  88. /**
  89. * * operator, for using as *iterator, when iterators are defined on stack,
  90. */
  91. _FORCE_INLINE_ T& operator *() {
  92. return value;
  93. };
  94. /**
  95. * operator->, for using as iterator->, when iterators are defined on stack, for constant lists.
  96. */
  97. _FORCE_INLINE_ T* operator->() {
  98. return &value;
  99. };
  100. /**
  101. * get the value stored in this element.
  102. */
  103. _FORCE_INLINE_ T& get() {
  104. return value;
  105. };
  106. /**
  107. * get the value stored in this element, for constant lists
  108. */
  109. _FORCE_INLINE_ const T& get() const {
  110. return value;
  111. };
  112. /**
  113. * set the value stored in this element.
  114. */
  115. _FORCE_INLINE_ void set(const T& p_value) {
  116. value = (T&)p_value;
  117. };
  118. void erase() {
  119. data->erase(this);
  120. }
  121. _FORCE_INLINE_ Element() {
  122. next_ptr = 0;
  123. prev_ptr = 0;
  124. data=NULL;
  125. };
  126. };
  127. private:
  128. struct _Data {
  129. Element* first;
  130. Element* last;
  131. int size_cache;
  132. bool erase(const Element* p_I) {
  133. ERR_FAIL_COND_V(!p_I,false);
  134. ERR_FAIL_COND_V(p_I->data!=this,false);
  135. if (first==p_I) {
  136. first=p_I->next_ptr;
  137. };
  138. if (last==p_I)
  139. last=p_I->prev_ptr;
  140. if (p_I->prev_ptr)
  141. p_I->prev_ptr->next_ptr=p_I->next_ptr;
  142. if (p_I->next_ptr)
  143. p_I->next_ptr->prev_ptr=p_I->prev_ptr;
  144. memdelete_allocator<Element,A>( const_cast<Element*>(p_I) );
  145. size_cache--;
  146. return true;
  147. }
  148. };
  149. _Data *_data;
  150. public:
  151. /**
  152. * return an const iterator to the begining of the list.
  153. */
  154. _FORCE_INLINE_ const Element* front() const {
  155. return _data?_data->first:0;
  156. };
  157. /**
  158. * return an iterator to the begining of the list.
  159. */
  160. _FORCE_INLINE_ Element* front() {
  161. return _data?_data->first:0;
  162. };
  163. /**
  164. * return an const iterator to the last member of the list.
  165. */
  166. _FORCE_INLINE_ const Element* back() const {
  167. return _data?_data->last:0;
  168. };
  169. /**
  170. * return an iterator to the last member of the list.
  171. */
  172. _FORCE_INLINE_ Element* back() {
  173. return _data?_data->last:0;
  174. };
  175. /**
  176. * store a new element at the end of the list
  177. */
  178. Element* push_back(const T& value) {
  179. if (!_data) {
  180. _data=memnew_allocator(_Data,A);
  181. _data->first=NULL;
  182. _data->last=NULL;
  183. _data->size_cache=0;
  184. }
  185. Element* n = memnew_allocator(Element,A);
  186. n->value = (T&)value;
  187. n->prev_ptr=_data->last;
  188. n->next_ptr=0;
  189. n->data=_data;
  190. if (_data->last) {
  191. _data->last->next_ptr=n;
  192. }
  193. _data->last = n;
  194. if (!_data->first)
  195. _data->first=n;
  196. _data->size_cache++;
  197. return n;
  198. };
  199. void pop_back() {
  200. if (_data && _data->last)
  201. erase(_data->last);
  202. }
  203. /**
  204. * store a new element at the begining of the list
  205. */
  206. Element* push_front(const T& value) {
  207. if (!_data) {
  208. _data=memnew_allocator(_Data,A);
  209. _data->first=NULL;
  210. _data->last=NULL;
  211. _data->size_cache=0;
  212. }
  213. Element* n = memnew_allocator(Element,A);
  214. n->value = (T&)value;
  215. n->prev_ptr = 0;
  216. n->next_ptr = _data->first;
  217. n->data=_data;
  218. if (_data->first) {
  219. _data->first->prev_ptr=n;
  220. }
  221. _data->first = n;
  222. if (!_data->last)
  223. _data->last=n;
  224. _data->size_cache++;
  225. return n;
  226. };
  227. void pop_front() {
  228. if (_data && _data->first)
  229. erase(_data->first);
  230. }
  231. /**
  232. * find an element in the list,
  233. */
  234. template<class T_v>
  235. Element* find(const T_v& p_val) {
  236. Element* it = front();
  237. while (it) {
  238. if (it->value == p_val) return it;
  239. it = it->next();
  240. };
  241. return NULL;
  242. };
  243. /**
  244. * erase an element in the list, by iterator pointing to it. Return true if it was found/erased.
  245. */
  246. bool erase(const Element* p_I) {
  247. if (_data) {
  248. bool ret = _data->erase(p_I);
  249. if (_data->size_cache==0) {
  250. memdelete_allocator<_Data,A>(_data);
  251. _data=NULL;
  252. }
  253. return ret;
  254. }
  255. return false;
  256. };
  257. /**
  258. * erase the first element in the list, that contains value
  259. */
  260. bool erase(const T& value) {
  261. Element* I = find(value);
  262. return erase(I);
  263. };
  264. /**
  265. * return wether the list is empty
  266. */
  267. _FORCE_INLINE_ bool empty() const {
  268. return (!_data || !_data->size_cache);
  269. }
  270. /**
  271. * clear the list
  272. */
  273. void clear() {
  274. while (front()) {
  275. erase(front());
  276. };
  277. };
  278. _FORCE_INLINE_ int size() const {
  279. return _data?_data->size_cache:0;
  280. }
  281. void swap(Element* p_A, Element *p_B) {
  282. ERR_FAIL_COND(!p_A || !p_B);
  283. ERR_FAIL_COND(p_A->data!=_data);
  284. ERR_FAIL_COND(p_B->data!=_data);
  285. Element* A_prev=p_A->prev_ptr;
  286. Element* A_next=p_A->next_ptr;
  287. p_A->next_ptr=p_B->next_ptr;
  288. p_A->prev_ptr=p_B->prev_ptr;
  289. p_B->next_ptr=A_next;
  290. p_B->prev_ptr=A_prev;
  291. if (p_A->prev_ptr)
  292. p_A->prev_ptr->next_ptr=p_A;
  293. if (p_A->next_ptr)
  294. p_A->next_ptr->prev_ptr=p_A;
  295. if (p_B->prev_ptr)
  296. p_B->prev_ptr->next_ptr=p_B;
  297. if (p_B->next_ptr)
  298. p_B->next_ptr->prev_ptr=p_B;
  299. }
  300. /**
  301. * copy the list
  302. */
  303. void operator=(const List& p_list) {
  304. clear();
  305. const Element *it=p_list.front();
  306. while (it) {
  307. push_back( it->get() );
  308. it=it->next();
  309. }
  310. }
  311. T& operator[](int p_index) {
  312. if (p_index<0 || p_index>=size()) {
  313. T& aux=*((T*)0); //nullreturn
  314. ERR_FAIL_COND_V(p_index<0 || p_index>=size(),aux);
  315. }
  316. Element *I=front();
  317. int c=0;
  318. while(I) {
  319. if (c==p_index) {
  320. return I->get();
  321. }
  322. I=I->next();
  323. c++;
  324. }
  325. ERR_FAIL_V( *((T*)0) ); // bug!!
  326. }
  327. const T& operator[](int p_index) const {
  328. if (p_index<0 || p_index>=size()) {
  329. T& aux=*((T*)0); //nullreturn
  330. ERR_FAIL_COND_V(p_index<0 || p_index>=size(),aux);
  331. }
  332. const Element *I=front();
  333. int c=0;
  334. while(I) {
  335. if (c==p_index) {
  336. return I->get();
  337. }
  338. I=I->next();
  339. c++;
  340. }
  341. ERR_FAIL_V( *((T*)0) ); // bug!
  342. }
  343. void move_to_back(Element* p_I) {
  344. ERR_FAIL_COND(p_I->data!=_data);
  345. if (!p_I->next_ptr)
  346. return;
  347. if (_data->first==p_I) {
  348. _data->first=p_I->next_ptr;
  349. };
  350. if (_data->last==p_I)
  351. _data->last=p_I->prev_ptr;
  352. if (p_I->prev_ptr)
  353. p_I->prev_ptr->next_ptr=p_I->next_ptr;
  354. if (p_I->next_ptr)
  355. p_I->next_ptr->prev_ptr=p_I->prev_ptr;
  356. _data->last->next_ptr=p_I;
  357. p_I->prev_ptr=_data->last;
  358. p_I->next_ptr=NULL;
  359. _data->last=p_I;
  360. }
  361. void invert() {
  362. int s = size() / 2;
  363. Element *F = front();
  364. Element *B = back();
  365. for(int i=0;i<s;i++) {
  366. SWAP( F->value, B->value );
  367. F=F->next();
  368. B=B->prev();
  369. }
  370. }
  371. void move_to_front(Element* p_I) {
  372. ERR_FAIL_COND(p_I->data!=_data);
  373. if (!p_I->prev_ptr)
  374. return;
  375. if (_data->first==p_I) {
  376. _data->first=p_I->next_ptr;
  377. };
  378. if (_data->last==p_I)
  379. _data->last=p_I->prev_ptr;
  380. if (p_I->prev_ptr)
  381. p_I->prev_ptr->next_ptr=p_I->next_ptr;
  382. if (p_I->next_ptr)
  383. p_I->next_ptr->prev_ptr=p_I->prev_ptr;
  384. _data->first->prev_ptr=p_I;
  385. p_I->next_ptr=_data->first;
  386. p_I->prev_ptr=NULL;
  387. _data->first=p_I;
  388. }
  389. void move_before(Element* value, Element* where) {
  390. if (value->prev_ptr) {
  391. value->prev_ptr->next_ptr = value->next_ptr;
  392. };
  393. if (value->next_ptr) {
  394. value->next_ptr->prev_ptr = value->prev_ptr;
  395. };
  396. value->next_ptr = where;
  397. if (!where) {
  398. value->prev_ptr = _data->last;
  399. _data->last = value;
  400. return;
  401. };
  402. value->prev_ptr = where->prev_ptr;
  403. if (where->prev_ptr) {
  404. where->prev_ptr->next_ptr = value;
  405. } else {
  406. _data->first = value;
  407. };
  408. where->prev_ptr = value;
  409. };
  410. /**
  411. * simple insertion sort
  412. */
  413. void sort() {
  414. sort_custom< Comparator<T> >();
  415. }
  416. template<class C>
  417. void sort_custom_inplace() {
  418. if(size()<2)
  419. return;
  420. Element *from=front();
  421. Element *current=from;
  422. Element *to=from;
  423. while(current) {
  424. Element *next=current->next_ptr;
  425. //disconnect
  426. current->next_ptr=NULL;
  427. if (from!=current) {
  428. current->prev_ptr=NULL;
  429. current->next_ptr=from;
  430. Element *find=from;
  431. C less;
  432. while( find && less(find->value,current->value) ) {
  433. current->prev_ptr=find;
  434. current->next_ptr=find->next_ptr;
  435. find=find->next_ptr;
  436. }
  437. if (current->prev_ptr)
  438. current->prev_ptr->next_ptr=current;
  439. else
  440. from=current;
  441. if (current->next_ptr)
  442. current->next_ptr->prev_ptr=current;
  443. else
  444. to=current;
  445. } else {
  446. current->prev_ptr=NULL;
  447. current->next_ptr=NULL;
  448. }
  449. current=next;
  450. }
  451. _data->first=from;
  452. _data->last=to;
  453. }
  454. template<class C>
  455. struct AuxiliaryComparator {
  456. C compare;
  457. _FORCE_INLINE_ bool operator()(const Element *a,const Element* b) const {
  458. return compare(a->value,b->value);
  459. }
  460. };
  461. template<class C>
  462. void sort_custom() {
  463. //this version uses auxiliary memory for speed.
  464. //if you don't want to use auxiliary memory, use the in_place version
  465. int s = size();
  466. if(s<2)
  467. return;
  468. Element **aux_buffer = memnew_arr(Element*,s);
  469. int idx=0;
  470. for(Element *E=front();E;E=E->next_ptr) {
  471. aux_buffer[idx]=E;
  472. idx++;
  473. }
  474. SortArray<Element*,AuxiliaryComparator<C> > sort;
  475. sort.sort(aux_buffer,s);
  476. _data->first=aux_buffer[0];
  477. aux_buffer[0]->prev_ptr=NULL;
  478. aux_buffer[0]->next_ptr=aux_buffer[1];
  479. _data->last=aux_buffer[s-1];
  480. aux_buffer[s-1]->prev_ptr=aux_buffer[s-2];
  481. aux_buffer[s-1]->next_ptr=NULL;
  482. for(int i=1;i<s-1;i++) {
  483. aux_buffer[i]->prev_ptr=aux_buffer[i-1];
  484. aux_buffer[i]->next_ptr=aux_buffer[i+1];
  485. }
  486. memdelete_arr(aux_buffer);
  487. }
  488. /**
  489. * copy constructor for the list
  490. */
  491. List(const List& p_list) {
  492. _data=NULL;
  493. const Element *it=p_list.front();
  494. while (it) {
  495. push_back( it->get() );
  496. it=it->next();
  497. }
  498. }
  499. List() {
  500. _data=NULL;
  501. };
  502. ~List() {
  503. clear();
  504. if (_data) {
  505. ERR_FAIL_COND(_data->size_cache);
  506. memdelete_allocator<_Data,A>(_data);
  507. }
  508. };
  509. };
  510. #endif