list.h 12 KB

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