list.h 14 KB

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