list.h 16 KB

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