list.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /**************************************************************************/
  2. /* list.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 = nullptr;
  125. prev_ptr = nullptr;
  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. }
  143. if (p_I->prev_ptr) {
  144. p_I->prev_ptr->next_ptr = p_I->next_ptr;
  145. }
  146. if (p_I->next_ptr) {
  147. p_I->next_ptr->prev_ptr = p_I->prev_ptr;
  148. }
  149. memdelete_allocator<Element, A>(const_cast<Element *>(p_I));
  150. size_cache--;
  151. return true;
  152. }
  153. };
  154. _Data *_data;
  155. public:
  156. /**
  157. * return a const iterator to the beginning of the list.
  158. */
  159. _FORCE_INLINE_ const Element *front() const {
  160. return _data ? _data->first : nullptr;
  161. };
  162. /**
  163. * return an iterator to the beginning of the list.
  164. */
  165. _FORCE_INLINE_ Element *front() {
  166. return _data ? _data->first : nullptr;
  167. };
  168. /**
  169. * return a const iterator to the last member of the list.
  170. */
  171. _FORCE_INLINE_ const Element *back() const {
  172. return _data ? _data->last : nullptr;
  173. };
  174. /**
  175. * return an iterator to the last member of the list.
  176. */
  177. _FORCE_INLINE_ Element *back() {
  178. return _data ? _data->last : nullptr;
  179. };
  180. /**
  181. * store a new element at the end of the list
  182. */
  183. Element *push_back(const T &value) {
  184. if (!_data) {
  185. _data = memnew_allocator(_Data, A);
  186. _data->first = nullptr;
  187. _data->last = nullptr;
  188. _data->size_cache = 0;
  189. }
  190. Element *n = memnew_allocator(Element, A);
  191. n->value = (T &)value;
  192. n->prev_ptr = _data->last;
  193. n->next_ptr = nullptr;
  194. n->data = _data;
  195. if (_data->last) {
  196. _data->last->next_ptr = n;
  197. }
  198. _data->last = n;
  199. if (!_data->first) {
  200. _data->first = n;
  201. }
  202. _data->size_cache++;
  203. return n;
  204. };
  205. void pop_back() {
  206. if (_data && _data->last) {
  207. erase(_data->last);
  208. }
  209. }
  210. /**
  211. * store a new element at the beginning of the list
  212. */
  213. Element *push_front(const T &value) {
  214. if (!_data) {
  215. _data = memnew_allocator(_Data, A);
  216. _data->first = nullptr;
  217. _data->last = nullptr;
  218. _data->size_cache = 0;
  219. }
  220. Element *n = memnew_allocator(Element, A);
  221. n->value = (T &)value;
  222. n->prev_ptr = nullptr;
  223. n->next_ptr = _data->first;
  224. n->data = _data;
  225. if (_data->first) {
  226. _data->first->prev_ptr = n;
  227. }
  228. _data->first = n;
  229. if (!_data->last) {
  230. _data->last = n;
  231. }
  232. _data->size_cache++;
  233. return n;
  234. };
  235. void pop_front() {
  236. if (_data && _data->first) {
  237. erase(_data->first);
  238. }
  239. }
  240. Element *insert_after(Element *p_element, const T &p_value) {
  241. CRASH_COND(p_element && (!_data || p_element->data != _data));
  242. if (!p_element) {
  243. return push_back(p_value);
  244. }
  245. Element *n = memnew_allocator(Element, A);
  246. n->value = (T &)p_value;
  247. n->prev_ptr = p_element;
  248. n->next_ptr = p_element->next_ptr;
  249. n->data = _data;
  250. if (!p_element->next_ptr) {
  251. _data->last = n;
  252. } else {
  253. p_element->next_ptr->prev_ptr = n;
  254. }
  255. p_element->next_ptr = n;
  256. _data->size_cache++;
  257. return n;
  258. }
  259. Element *insert_before(Element *p_element, const T &p_value) {
  260. CRASH_COND(p_element && (!_data || p_element->data != _data));
  261. if (!p_element) {
  262. return push_back(p_value);
  263. }
  264. Element *n = memnew_allocator(Element, A);
  265. n->value = (T &)p_value;
  266. n->prev_ptr = p_element->prev_ptr;
  267. n->next_ptr = p_element;
  268. n->data = _data;
  269. if (!p_element->prev_ptr) {
  270. _data->first = n;
  271. } else {
  272. p_element->prev_ptr->next_ptr = n;
  273. }
  274. p_element->prev_ptr = n;
  275. _data->size_cache++;
  276. return n;
  277. }
  278. /**
  279. * find an element in the list,
  280. */
  281. template <class T_v>
  282. Element *find(const T_v &p_val) {
  283. Element *it = front();
  284. while (it) {
  285. if (it->value == p_val) {
  286. return it;
  287. }
  288. it = it->next();
  289. };
  290. return nullptr;
  291. };
  292. /**
  293. * erase an element in the list, by iterator pointing to it. Return true if it was found/erased.
  294. */
  295. bool erase(const Element *p_I) {
  296. if (_data) {
  297. bool ret = _data->erase(p_I);
  298. if (_data->size_cache == 0) {
  299. memdelete_allocator<_Data, A>(_data);
  300. _data = nullptr;
  301. }
  302. return ret;
  303. }
  304. return false;
  305. };
  306. /**
  307. * erase the first element in the list, that contains value
  308. */
  309. bool erase(const T &value) {
  310. Element *I = find(value);
  311. return erase(I);
  312. };
  313. /**
  314. * return whether the list is empty
  315. */
  316. _FORCE_INLINE_ bool empty() const {
  317. return (!_data || !_data->size_cache);
  318. }
  319. /**
  320. * clear the list
  321. */
  322. void clear() {
  323. while (front()) {
  324. erase(front());
  325. };
  326. };
  327. _FORCE_INLINE_ int size() const {
  328. return _data ? _data->size_cache : 0;
  329. }
  330. void swap(Element *p_A, Element *p_B) {
  331. ERR_FAIL_COND(!p_A || !p_B);
  332. ERR_FAIL_COND(p_A->data != _data);
  333. ERR_FAIL_COND(p_B->data != _data);
  334. if (p_A == p_B) {
  335. return;
  336. }
  337. Element *A_prev = p_A->prev_ptr;
  338. Element *A_next = p_A->next_ptr;
  339. Element *B_prev = p_B->prev_ptr;
  340. Element *B_next = p_B->next_ptr;
  341. if (A_prev) {
  342. A_prev->next_ptr = p_B;
  343. } else {
  344. _data->first = p_B;
  345. }
  346. if (B_prev) {
  347. B_prev->next_ptr = p_A;
  348. } else {
  349. _data->first = p_A;
  350. }
  351. if (A_next) {
  352. A_next->prev_ptr = p_B;
  353. } else {
  354. _data->last = p_B;
  355. }
  356. if (B_next) {
  357. B_next->prev_ptr = p_A;
  358. } else {
  359. _data->last = p_A;
  360. }
  361. p_A->prev_ptr = A_next == p_B ? p_B : B_prev;
  362. p_A->next_ptr = B_next == p_A ? p_B : B_next;
  363. p_B->prev_ptr = B_next == p_A ? p_A : A_prev;
  364. p_B->next_ptr = A_next == p_B ? p_A : A_next;
  365. }
  366. /**
  367. * copy the list
  368. */
  369. void operator=(const List &p_list) {
  370. clear();
  371. const Element *it = p_list.front();
  372. while (it) {
  373. push_back(it->get());
  374. it = it->next();
  375. }
  376. }
  377. T &operator[](int p_index) {
  378. CRASH_BAD_INDEX(p_index, size());
  379. Element *I = front();
  380. int c = 0;
  381. while (I) {
  382. if (c == p_index) {
  383. return I->get();
  384. }
  385. I = I->next();
  386. c++;
  387. }
  388. CRASH_NOW(); // bug!!
  389. }
  390. const T &operator[](int p_index) const {
  391. CRASH_BAD_INDEX(p_index, size());
  392. const Element *I = front();
  393. int c = 0;
  394. while (I) {
  395. if (c == p_index) {
  396. return I->get();
  397. }
  398. I = I->next();
  399. c++;
  400. }
  401. CRASH_NOW(); // bug!!
  402. }
  403. void move_to_back(Element *p_I) {
  404. ERR_FAIL_COND(p_I->data != _data);
  405. if (!p_I->next_ptr) {
  406. return;
  407. }
  408. if (_data->first == p_I) {
  409. _data->first = p_I->next_ptr;
  410. };
  411. if (_data->last == p_I) {
  412. _data->last = p_I->prev_ptr;
  413. }
  414. if (p_I->prev_ptr) {
  415. p_I->prev_ptr->next_ptr = p_I->next_ptr;
  416. }
  417. p_I->next_ptr->prev_ptr = p_I->prev_ptr;
  418. _data->last->next_ptr = p_I;
  419. p_I->prev_ptr = _data->last;
  420. p_I->next_ptr = nullptr;
  421. _data->last = p_I;
  422. }
  423. void invert() {
  424. int s = size() / 2;
  425. Element *F = front();
  426. Element *B = back();
  427. for (int i = 0; i < s; i++) {
  428. SWAP(F->value, B->value);
  429. F = F->next();
  430. B = B->prev();
  431. }
  432. }
  433. void move_to_front(Element *p_I) {
  434. ERR_FAIL_COND(p_I->data != _data);
  435. if (!p_I->prev_ptr) {
  436. return;
  437. }
  438. if (_data->first == p_I) {
  439. _data->first = p_I->next_ptr;
  440. };
  441. if (_data->last == p_I) {
  442. _data->last = p_I->prev_ptr;
  443. }
  444. p_I->prev_ptr->next_ptr = p_I->next_ptr;
  445. if (p_I->next_ptr) {
  446. p_I->next_ptr->prev_ptr = p_I->prev_ptr;
  447. }
  448. _data->first->prev_ptr = p_I;
  449. p_I->next_ptr = _data->first;
  450. p_I->prev_ptr = nullptr;
  451. _data->first = p_I;
  452. }
  453. void move_before(Element *value, Element *where) {
  454. if (value->prev_ptr) {
  455. value->prev_ptr->next_ptr = value->next_ptr;
  456. } else {
  457. _data->first = value->next_ptr;
  458. }
  459. if (value->next_ptr) {
  460. value->next_ptr->prev_ptr = value->prev_ptr;
  461. } else {
  462. _data->last = value->prev_ptr;
  463. }
  464. value->next_ptr = where;
  465. if (!where) {
  466. value->prev_ptr = _data->last;
  467. _data->last = value;
  468. return;
  469. };
  470. value->prev_ptr = where->prev_ptr;
  471. if (where->prev_ptr) {
  472. where->prev_ptr->next_ptr = value;
  473. } else {
  474. _data->first = value;
  475. };
  476. where->prev_ptr = value;
  477. };
  478. /**
  479. * simple insertion sort
  480. */
  481. void sort() {
  482. sort_custom<Comparator<T>>();
  483. }
  484. template <class C>
  485. void sort_custom_inplace() {
  486. if (size() < 2) {
  487. return;
  488. }
  489. Element *from = front();
  490. Element *current = from;
  491. Element *to = from;
  492. while (current) {
  493. Element *next = current->next_ptr;
  494. if (from != current) {
  495. current->prev_ptr = NULL;
  496. current->next_ptr = from;
  497. Element *find = from;
  498. C less;
  499. while (find && less(find->value, current->value)) {
  500. current->prev_ptr = find;
  501. current->next_ptr = find->next_ptr;
  502. find = find->next_ptr;
  503. }
  504. if (current->prev_ptr) {
  505. current->prev_ptr->next_ptr = current;
  506. } else {
  507. from = current;
  508. }
  509. if (current->next_ptr) {
  510. current->next_ptr->prev_ptr = current;
  511. } else {
  512. to = current;
  513. }
  514. } else {
  515. current->prev_ptr = NULL;
  516. current->next_ptr = NULL;
  517. }
  518. current = next;
  519. }
  520. _data->first = from;
  521. _data->last = to;
  522. }
  523. template <class C>
  524. struct AuxiliaryComparator {
  525. C compare;
  526. _FORCE_INLINE_ bool operator()(const Element *a, const Element *b) const {
  527. return compare(a->value, b->value);
  528. }
  529. };
  530. template <class C>
  531. void sort_custom() {
  532. //this version uses auxiliary memory for speed.
  533. //if you don't want to use auxiliary memory, use the in_place version
  534. int s = size();
  535. if (s < 2) {
  536. return;
  537. }
  538. Element **aux_buffer = memnew_arr(Element *, s);
  539. int idx = 0;
  540. for (Element *E = front(); E; E = E->next_ptr) {
  541. aux_buffer[idx] = E;
  542. idx++;
  543. }
  544. SortArray<Element *, AuxiliaryComparator<C>> sort;
  545. sort.sort(aux_buffer, s);
  546. _data->first = aux_buffer[0];
  547. aux_buffer[0]->prev_ptr = nullptr;
  548. aux_buffer[0]->next_ptr = aux_buffer[1];
  549. _data->last = aux_buffer[s - 1];
  550. aux_buffer[s - 1]->prev_ptr = aux_buffer[s - 2];
  551. aux_buffer[s - 1]->next_ptr = nullptr;
  552. for (int i = 1; i < s - 1; i++) {
  553. aux_buffer[i]->prev_ptr = aux_buffer[i - 1];
  554. aux_buffer[i]->next_ptr = aux_buffer[i + 1];
  555. }
  556. memdelete_arr(aux_buffer);
  557. }
  558. const void *id() const {
  559. return (void *)_data;
  560. }
  561. /**
  562. * copy constructor for the list
  563. */
  564. List(const List &p_list) {
  565. _data = nullptr;
  566. const Element *it = p_list.front();
  567. while (it) {
  568. push_back(it->get());
  569. it = it->next();
  570. }
  571. }
  572. List() {
  573. _data = nullptr;
  574. };
  575. ~List() {
  576. clear();
  577. if (_data) {
  578. ERR_FAIL_COND(_data->size_cache);
  579. memdelete_allocator<_Data, A>(_data);
  580. }
  581. };
  582. };
  583. #endif // LIST_H