list.h 14 KB

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