list.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /*************************************************************************/
  2. /* list.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef GLOBALS_LIST_H
  31. #define GLOBALS_LIST_H
  32. #include "os/memory.h"
  33. #include "sort.h"
  34. /**
  35. * Generic Templatized Linked List Implementation.
  36. * The implementation differs from the STL one because
  37. * a compatible preallocated linked list can be written
  38. * using the same API, or features such as erasing an element
  39. * from the iterator.
  40. */
  41. template <class T, class A = DefaultAllocator>
  42. class List {
  43. struct _Data;
  44. public:
  45. class Element {
  46. private:
  47. friend class List<T, A>;
  48. T value;
  49. Element *next_ptr;
  50. Element *prev_ptr;
  51. _Data *data;
  52. public:
  53. /**
  54. * Get NEXT Element iterator, for constant lists.
  55. */
  56. _FORCE_INLINE_ const Element *next() const {
  57. return next_ptr;
  58. };
  59. /**
  60. * Get NEXT Element iterator,
  61. */
  62. _FORCE_INLINE_ Element *next() {
  63. return next_ptr;
  64. };
  65. /**
  66. * Get PREV Element iterator, for constant lists.
  67. */
  68. _FORCE_INLINE_ const Element *prev() const {
  69. return prev_ptr;
  70. };
  71. /**
  72. * Get PREV Element iterator,
  73. */
  74. _FORCE_INLINE_ Element *prev() {
  75. return prev_ptr;
  76. };
  77. /**
  78. * * operator, for using as *iterator, when iterators are defined on stack.
  79. */
  80. _FORCE_INLINE_ const T &operator*() const {
  81. return value;
  82. };
  83. /**
  84. * operator->, for using as iterator->, when iterators are defined on stack, for constant lists.
  85. */
  86. _FORCE_INLINE_ const T *operator->() const {
  87. return &value;
  88. };
  89. /**
  90. * * operator, for using as *iterator, when iterators are defined on stack,
  91. */
  92. _FORCE_INLINE_ T &operator*() {
  93. return value;
  94. };
  95. /**
  96. * operator->, for using as iterator->, when iterators are defined on stack, for constant lists.
  97. */
  98. _FORCE_INLINE_ T *operator->() {
  99. return &value;
  100. };
  101. /**
  102. * get the value stored in this element.
  103. */
  104. _FORCE_INLINE_ T &get() {
  105. return value;
  106. };
  107. /**
  108. * get the value stored in this element, for constant lists
  109. */
  110. _FORCE_INLINE_ const T &get() const {
  111. return value;
  112. };
  113. /**
  114. * set the value stored in this element.
  115. */
  116. _FORCE_INLINE_ void set(const T &p_value) {
  117. value = (T &)p_value;
  118. };
  119. void erase() {
  120. data->erase(this);
  121. }
  122. _FORCE_INLINE_ Element() {
  123. next_ptr = 0;
  124. prev_ptr = 0;
  125. data = NULL;
  126. };
  127. };
  128. private:
  129. struct _Data {
  130. Element *first;
  131. Element *last;
  132. int size_cache;
  133. bool erase(const Element *p_I) {
  134. ERR_FAIL_COND_V(!p_I, false);
  135. ERR_FAIL_COND_V(p_I->data != this, false);
  136. if (first == p_I) {
  137. first = p_I->next_ptr;
  138. };
  139. if (last == p_I)
  140. last = p_I->prev_ptr;
  141. if (p_I->prev_ptr)
  142. p_I->prev_ptr->next_ptr = p_I->next_ptr;
  143. if (p_I->next_ptr)
  144. p_I->next_ptr->prev_ptr = p_I->prev_ptr;
  145. memdelete_allocator<Element, A>(const_cast<Element *>(p_I));
  146. size_cache--;
  147. return true;
  148. }
  149. };
  150. _Data *_data;
  151. public:
  152. /**
  153. * return an const iterator to the begining of the list.
  154. */
  155. _FORCE_INLINE_ const Element *front() const {
  156. return _data ? _data->first : 0;
  157. };
  158. /**
  159. * return an iterator to the begining of the list.
  160. */
  161. _FORCE_INLINE_ Element *front() {
  162. return _data ? _data->first : 0;
  163. };
  164. /**
  165. * return an const iterator to the last member of the list.
  166. */
  167. _FORCE_INLINE_ const Element *back() const {
  168. return _data ? _data->last : 0;
  169. };
  170. /**
  171. * return an iterator to the last member of the list.
  172. */
  173. _FORCE_INLINE_ Element *back() {
  174. return _data ? _data->last : 0;
  175. };
  176. /**
  177. * store a new element at the end of the list
  178. */
  179. Element *push_back(const T &value) {
  180. if (!_data) {
  181. _data = memnew_allocator(_Data, A);
  182. _data->first = NULL;
  183. _data->last = NULL;
  184. _data->size_cache = 0;
  185. }
  186. Element *n = memnew_allocator(Element, A);
  187. n->value = (T &)value;
  188. n->prev_ptr = _data->last;
  189. n->next_ptr = 0;
  190. n->data = _data;
  191. if (_data->last) {
  192. _data->last->next_ptr = n;
  193. }
  194. _data->last = n;
  195. if (!_data->first)
  196. _data->first = n;
  197. _data->size_cache++;
  198. return n;
  199. };
  200. void pop_back() {
  201. if (_data && _data->last)
  202. erase(_data->last);
  203. }
  204. /**
  205. * store a new element at the begining of the list
  206. */
  207. Element *push_front(const T &value) {
  208. if (!_data) {
  209. _data = memnew_allocator(_Data, A);
  210. _data->first = NULL;
  211. _data->last = NULL;
  212. _data->size_cache = 0;
  213. }
  214. Element *n = memnew_allocator(Element, A);
  215. n->value = (T &)value;
  216. n->prev_ptr = 0;
  217. n->next_ptr = _data->first;
  218. n->data = _data;
  219. if (_data->first) {
  220. _data->first->prev_ptr = n;
  221. }
  222. _data->first = n;
  223. if (!_data->last)
  224. _data->last = n;
  225. _data->size_cache++;
  226. return n;
  227. };
  228. void pop_front() {
  229. if (_data && _data->first)
  230. erase(_data->first);
  231. }
  232. /**
  233. * find an element in the list,
  234. */
  235. template <class T_v>
  236. Element *find(const T_v &p_val) {
  237. Element *it = front();
  238. while (it) {
  239. if (it->value == p_val) return it;
  240. it = it->next();
  241. };
  242. return NULL;
  243. };
  244. /**
  245. * erase an element in the list, by iterator pointing to it. Return true if it was found/erased.
  246. */
  247. bool erase(const Element *p_I) {
  248. if (_data) {
  249. bool ret = _data->erase(p_I);
  250. if (_data->size_cache == 0) {
  251. memdelete_allocator<_Data, A>(_data);
  252. _data = NULL;
  253. }
  254. return ret;
  255. }
  256. return false;
  257. };
  258. /**
  259. * erase the first element in the list, that contains value
  260. */
  261. bool erase(const T &value) {
  262. Element *I = find(value);
  263. return erase(I);
  264. };
  265. /**
  266. * return wether the list is empty
  267. */
  268. _FORCE_INLINE_ bool empty() const {
  269. return (!_data || !_data->size_cache);
  270. }
  271. /**
  272. * clear the list
  273. */
  274. void clear() {
  275. while (front()) {
  276. erase(front());
  277. };
  278. };
  279. _FORCE_INLINE_ int size() const {
  280. return _data ? _data->size_cache : 0;
  281. }
  282. void swap(Element *p_A, Element *p_B) {
  283. ERR_FAIL_COND(!p_A || !p_B);
  284. ERR_FAIL_COND(p_A->data != _data);
  285. ERR_FAIL_COND(p_B->data != _data);
  286. Element *A_prev = p_A->prev_ptr;
  287. Element *A_next = p_A->next_ptr;
  288. p_A->next_ptr = p_B->next_ptr;
  289. p_A->prev_ptr = p_B->prev_ptr;
  290. p_B->next_ptr = A_next;
  291. p_B->prev_ptr = A_prev;
  292. if (p_A->prev_ptr)
  293. p_A->prev_ptr->next_ptr = p_A;
  294. if (p_A->next_ptr)
  295. p_A->next_ptr->prev_ptr = p_A;
  296. if (p_B->prev_ptr)
  297. p_B->prev_ptr->next_ptr = p_B;
  298. if (p_B->next_ptr)
  299. p_B->next_ptr->prev_ptr = p_B;
  300. }
  301. /**
  302. * copy the list
  303. */
  304. void operator=(const List &p_list) {
  305. clear();
  306. const Element *it = p_list.front();
  307. while (it) {
  308. push_back(it->get());
  309. it = it->next();
  310. }
  311. }
  312. T &operator[](int p_index) {
  313. PRAY_BAD_INDEX(p_index, size(), T);
  314. Element *I = front();
  315. int c = 0;
  316. while (I) {
  317. if (c == p_index) {
  318. return I->get();
  319. }
  320. I = I->next();
  321. c++;
  322. }
  323. PRAY(T); // bug!!
  324. }
  325. const T &operator[](int p_index) const {
  326. PRAY_BAD_INDEX(p_index, size(), T);
  327. const Element *I = front();
  328. int c = 0;
  329. while (I) {
  330. if (c == p_index) {
  331. return I->get();
  332. }
  333. I = I->next();
  334. c++;
  335. }
  336. PRAY(T); // bug!!
  337. }
  338. void move_to_back(Element *p_I) {
  339. ERR_FAIL_COND(p_I->data != _data);
  340. if (!p_I->next_ptr)
  341. return;
  342. if (_data->first == p_I) {
  343. _data->first = p_I->next_ptr;
  344. };
  345. if (_data->last == p_I)
  346. _data->last = p_I->prev_ptr;
  347. if (p_I->prev_ptr)
  348. p_I->prev_ptr->next_ptr = p_I->next_ptr;
  349. if (p_I->next_ptr)
  350. p_I->next_ptr->prev_ptr = p_I->prev_ptr;
  351. _data->last->next_ptr = p_I;
  352. p_I->prev_ptr = _data->last;
  353. p_I->next_ptr = NULL;
  354. _data->last = p_I;
  355. }
  356. void invert() {
  357. int s = size() / 2;
  358. Element *F = front();
  359. Element *B = back();
  360. for (int i = 0; i < s; i++) {
  361. SWAP(F->value, B->value);
  362. F = F->next();
  363. B = B->prev();
  364. }
  365. }
  366. void move_to_front(Element *p_I) {
  367. ERR_FAIL_COND(p_I->data != _data);
  368. if (!p_I->prev_ptr)
  369. return;
  370. if (_data->first == p_I) {
  371. _data->first = p_I->next_ptr;
  372. };
  373. if (_data->last == p_I)
  374. _data->last = p_I->prev_ptr;
  375. if (p_I->prev_ptr)
  376. p_I->prev_ptr->next_ptr = p_I->next_ptr;
  377. if (p_I->next_ptr)
  378. p_I->next_ptr->prev_ptr = p_I->prev_ptr;
  379. _data->first->prev_ptr = p_I;
  380. p_I->next_ptr = _data->first;
  381. p_I->prev_ptr = NULL;
  382. _data->first = p_I;
  383. }
  384. void move_before(Element *value, Element *where) {
  385. if (value->prev_ptr) {
  386. value->prev_ptr->next_ptr = value->next_ptr;
  387. } else {
  388. _data->first = value->next_ptr;
  389. }
  390. if (value->next_ptr) {
  391. value->next_ptr->prev_ptr = value->prev_ptr;
  392. } else {
  393. _data->last = 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_inplace() {
  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. template <class C>
  454. struct AuxiliaryComparator {
  455. C compare;
  456. _FORCE_INLINE_ bool operator()(const Element *a, const Element *b) const {
  457. return compare(a->value, b->value);
  458. }
  459. };
  460. template <class C>
  461. void sort_custom() {
  462. //this version uses auxiliary memory for speed.
  463. //if you don't want to use auxiliary memory, use the in_place version
  464. int s = size();
  465. if (s < 2)
  466. return;
  467. Element **aux_buffer = memnew_arr(Element *, s);
  468. int idx = 0;
  469. for (Element *E = front(); E; E = E->next_ptr) {
  470. aux_buffer[idx] = E;
  471. idx++;
  472. }
  473. SortArray<Element *, AuxiliaryComparator<C> > sort;
  474. sort.sort(aux_buffer, s);
  475. _data->first = aux_buffer[0];
  476. aux_buffer[0]->prev_ptr = NULL;
  477. aux_buffer[0]->next_ptr = aux_buffer[1];
  478. _data->last = aux_buffer[s - 1];
  479. aux_buffer[s - 1]->prev_ptr = aux_buffer[s - 2];
  480. aux_buffer[s - 1]->next_ptr = NULL;
  481. for (int i = 1; i < s - 1; i++) {
  482. aux_buffer[i]->prev_ptr = aux_buffer[i - 1];
  483. aux_buffer[i]->next_ptr = aux_buffer[i + 1];
  484. }
  485. memdelete_arr(aux_buffer);
  486. }
  487. /**
  488. * copy constructor for the list
  489. */
  490. List(const List &p_list) {
  491. _data = NULL;
  492. const Element *it = p_list.front();
  493. while (it) {
  494. push_back(it->get());
  495. it = it->next();
  496. }
  497. }
  498. List() {
  499. _data = NULL;
  500. };
  501. ~List() {
  502. clear();
  503. if (_data) {
  504. ERR_FAIL_COND(_data->size_cache);
  505. memdelete_allocator<_Data, A>(_data);
  506. }
  507. };
  508. };
  509. #endif