list.hpp 16 KB

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