list.hpp 17 KB

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