List.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #ifndef CORE_LIST_H
  2. #define CORE_LIST_H
  3. #include "utils/ArrayString.h"
  4. namespace Core {
  5. template<typename T>
  6. class List final {
  7. int length;
  8. int capacity;
  9. T* data;
  10. public:
  11. List() : length(0), capacity(0), data(nullptr) {
  12. }
  13. List(const List& other) = delete;
  14. List(List&& other) : List() {
  15. swap(other);
  16. }
  17. ~List() {
  18. clear();
  19. delete[] reinterpret_cast<char*>(data);
  20. }
  21. List& operator=(const List& other) = delete;
  22. List& operator=(List&& other) {
  23. swap(other);
  24. return *this;
  25. }
  26. // returns true on error
  27. check_return bool copyFrom(const List& other) {
  28. List copy;
  29. copy.data = allocate(other.capacity);
  30. if(copy.data == nullptr) {
  31. return true;
  32. }
  33. copy.capacity = other.capacity;
  34. for(int i = 0; i < other.length; i++) {
  35. copy.unsafeAdd(other[i]);
  36. }
  37. swap(copy);
  38. return false;
  39. }
  40. T* begin() {
  41. return data;
  42. }
  43. T* end() {
  44. return data + length;
  45. }
  46. const T* begin() const {
  47. return data;
  48. }
  49. const T* end() const {
  50. return data + length;
  51. }
  52. // returns true on error
  53. check_return bool reserve(int n) {
  54. if(n <= capacity) {
  55. return false;
  56. }
  57. List copy;
  58. copy.data = allocate(n);
  59. if(copy.data == nullptr) {
  60. return true;
  61. }
  62. copy.capacity = n;
  63. for(int i = 0; i < length; i++) {
  64. copy.unsafeAdd(Core::move(data[i]));
  65. }
  66. swap(copy);
  67. return false;
  68. }
  69. // returns true on error
  70. check_return bool shrink() {
  71. if(length == capacity) {
  72. return false;
  73. }
  74. List copy;
  75. copy.data = allocate(length);
  76. if(copy.data == nullptr) {
  77. return true;
  78. }
  79. copy.capacity = length;
  80. for(int i = 0; i < length; i++) {
  81. copy.unsafeAdd(Core::move(data[i]));
  82. }
  83. swap(copy);
  84. return false;
  85. }
  86. // returns true on error
  87. check_return bool resize(int n, const T& t) {
  88. if(length < n) {
  89. if(reserve(n)) {
  90. return true;
  91. }
  92. for(int i = length; i < n; i++) {
  93. unsafeAdd(t);
  94. }
  95. } else if(length > n) {
  96. for(int i = n; i < length; i++) {
  97. data[i].~T();
  98. }
  99. length = n;
  100. }
  101. return false;
  102. }
  103. // returns true on error
  104. check_return bool resize(int n) {
  105. if(length < n) {
  106. if(reserve(n)) {
  107. return true;
  108. }
  109. for(int i = length; i < n; i++) {
  110. unsafeAdd(T());
  111. }
  112. } else if(length > n) {
  113. for(int i = n; i < length; i++) {
  114. data[i].~T();
  115. }
  116. length = n;
  117. }
  118. return false;
  119. }
  120. // returns true on error
  121. template<typename... Args>
  122. check_return bool add(Args&&... args) {
  123. if(ensureCapacity()) {
  124. return true;
  125. }
  126. unsafeAdd(Core::forward<Args>(args)...);
  127. return false;
  128. }
  129. T& operator[](int index) {
  130. return data[index];
  131. }
  132. const T& operator[](int index) const {
  133. return data[index];
  134. }
  135. int getLength() const {
  136. return length;
  137. }
  138. int getCapacity() const {
  139. return capacity;
  140. }
  141. void clear() {
  142. for(int i = 0; i < length; i++) {
  143. data[i].~T();
  144. }
  145. length = 0;
  146. }
  147. // returns true on error
  148. check_return bool removeBySwap(int index) {
  149. if(index < 0 || index >= length) {
  150. return true;
  151. }
  152. length--;
  153. if(index != length) {
  154. data[index] = Core::move(data[length]);
  155. }
  156. data[length].~T();
  157. return false;
  158. }
  159. // returns true on error
  160. check_return bool remove(int index) {
  161. if(index < 0 || index >= length) {
  162. return true;
  163. }
  164. length--;
  165. for(int i = index; i < length; i++) {
  166. data[i] = Core::move(data[i + 1]);
  167. }
  168. data[length].~T();
  169. return false;
  170. }
  171. // returns true on error
  172. template<int L>
  173. check_return bool toString(ArrayString<L>& s) const {
  174. if(s.append("[")) {
  175. return true;
  176. }
  177. for(int i = 0; i < length - 1; i++) {
  178. if(s.append(data[i]) || s.append(", ")) {
  179. return true;
  180. }
  181. }
  182. if(length > 0 && s.append(data[length - 1])) {
  183. return true;
  184. }
  185. return s.append("]");
  186. }
  187. private:
  188. struct alignas(T) Aligned {
  189. char data[sizeof(T)];
  190. };
  191. static T* allocate(int n) {
  192. if(n <= 0) {
  193. return nullptr;
  194. }
  195. return reinterpret_cast<T*>(new Aligned[static_cast<size_t>(n)]);
  196. }
  197. void swap(List& other) {
  198. Core::swap(length, other.length);
  199. Core::swap(capacity, other.capacity);
  200. Core::swap(data, other.data);
  201. }
  202. // returns true on error
  203. check_return bool ensureCapacity() {
  204. if(length >= capacity) {
  205. return reserve(capacity + Core::Math::max(4, capacity / 4));
  206. }
  207. return false;
  208. }
  209. // does not check for capacity
  210. template<typename... Args>
  211. void unsafeAdd(Args&&... args) {
  212. new(data + length++) T(Core::forward<Args>(args)...);
  213. }
  214. };
  215. }
  216. #endif