self_list.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**************************************************************************/
  2. /* self_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 SELF_LIST_H
  31. #define SELF_LIST_H
  32. #include "core/error/error_macros.h"
  33. #include "core/typedefs.h"
  34. template <typename T>
  35. class SelfList {
  36. public:
  37. class List {
  38. SelfList<T> *_first = nullptr;
  39. SelfList<T> *_last = nullptr;
  40. public:
  41. void add(SelfList<T> *p_elem) {
  42. ERR_FAIL_COND(p_elem->_root);
  43. p_elem->_root = this;
  44. p_elem->_next = _first;
  45. p_elem->_prev = nullptr;
  46. if (_first) {
  47. _first->_prev = p_elem;
  48. } else {
  49. _last = p_elem;
  50. }
  51. _first = p_elem;
  52. }
  53. void add_last(SelfList<T> *p_elem) {
  54. ERR_FAIL_COND(p_elem->_root);
  55. p_elem->_root = this;
  56. p_elem->_next = nullptr;
  57. p_elem->_prev = _last;
  58. if (_last) {
  59. _last->_next = p_elem;
  60. } else {
  61. _first = p_elem;
  62. }
  63. _last = p_elem;
  64. }
  65. void remove(SelfList<T> *p_elem) {
  66. ERR_FAIL_COND(p_elem->_root != this);
  67. if (p_elem->_next) {
  68. p_elem->_next->_prev = p_elem->_prev;
  69. }
  70. if (p_elem->_prev) {
  71. p_elem->_prev->_next = p_elem->_next;
  72. }
  73. if (_first == p_elem) {
  74. _first = p_elem->_next;
  75. }
  76. if (_last == p_elem) {
  77. _last = p_elem->_prev;
  78. }
  79. p_elem->_next = nullptr;
  80. p_elem->_prev = nullptr;
  81. p_elem->_root = nullptr;
  82. }
  83. void clear() {
  84. while (_first) {
  85. remove(_first);
  86. }
  87. }
  88. void sort() {
  89. sort_custom<Comparator<T>>();
  90. }
  91. template <typename C>
  92. void sort_custom() {
  93. if (_first == _last) {
  94. return;
  95. }
  96. SelfList<T> *from = _first;
  97. SelfList<T> *current = from;
  98. SelfList<T> *to = from;
  99. while (current) {
  100. SelfList<T> *next = current->_next;
  101. if (from != current) {
  102. current->_prev = nullptr;
  103. current->_next = from;
  104. SelfList<T> *find = from;
  105. C less;
  106. while (find && less(*find->_self, *current->_self)) {
  107. current->_prev = find;
  108. current->_next = find->_next;
  109. find = find->_next;
  110. }
  111. if (current->_prev) {
  112. current->_prev->_next = current;
  113. } else {
  114. from = current;
  115. }
  116. if (current->_next) {
  117. current->_next->_prev = current;
  118. } else {
  119. to = current;
  120. }
  121. } else {
  122. current->_prev = nullptr;
  123. current->_next = nullptr;
  124. }
  125. current = next;
  126. }
  127. _first = from;
  128. _last = to;
  129. }
  130. _FORCE_INLINE_ SelfList<T> *first() { return _first; }
  131. _FORCE_INLINE_ const SelfList<T> *first() const { return _first; }
  132. // Forbid copying, which has broken behavior.
  133. void operator=(const List &) = delete;
  134. _FORCE_INLINE_ List() {}
  135. _FORCE_INLINE_ ~List() {
  136. // A self list must be empty on destruction.
  137. DEV_ASSERT(_first == nullptr);
  138. }
  139. };
  140. private:
  141. List *_root = nullptr;
  142. T *_self = nullptr;
  143. SelfList<T> *_next = nullptr;
  144. SelfList<T> *_prev = nullptr;
  145. public:
  146. _FORCE_INLINE_ bool in_list() const { return _root; }
  147. _FORCE_INLINE_ void remove_from_list() {
  148. if (_root) {
  149. _root->remove(this);
  150. }
  151. }
  152. _FORCE_INLINE_ SelfList<T> *next() { return _next; }
  153. _FORCE_INLINE_ SelfList<T> *prev() { return _prev; }
  154. _FORCE_INLINE_ const SelfList<T> *next() const { return _next; }
  155. _FORCE_INLINE_ const SelfList<T> *prev() const { return _prev; }
  156. _FORCE_INLINE_ T *self() const { return _self; }
  157. // Forbid copying, which has broken behavior.
  158. void operator=(const SelfList<T> &) = delete;
  159. _FORCE_INLINE_ SelfList(T *p_self) {
  160. _self = p_self;
  161. }
  162. _FORCE_INLINE_ ~SelfList() {
  163. if (_root) {
  164. _root->remove(this);
  165. }
  166. }
  167. };
  168. #endif // SELF_LIST_H