List.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 a nullptr on error
  121. template<typename... Args>
  122. check_return T* add(Args&&... args) {
  123. if(ensureCapacity()) {
  124. return nullptr;
  125. }
  126. return unsafeAdd(Core::forward<Args>(args)...);
  127. }
  128. T& operator[](int index) {
  129. return data[index];
  130. }
  131. const T& operator[](int index) const {
  132. return data[index];
  133. }
  134. int getLength() const {
  135. return length;
  136. }
  137. int getCapacity() const {
  138. return capacity;
  139. }
  140. void clear() {
  141. for(int i = 0; i < length; i++) {
  142. data[i].~T();
  143. }
  144. length = 0;
  145. }
  146. // returns true on error
  147. check_return bool removeBySwap(int index) {
  148. if(index < 0 || index >= length) {
  149. return true;
  150. }
  151. length--;
  152. if(index != length) {
  153. data[index] = Core::move(data[length]);
  154. }
  155. data[length].~T();
  156. return false;
  157. }
  158. // returns true on error
  159. check_return bool remove(int index) {
  160. if(index < 0 || index >= length) {
  161. return true;
  162. }
  163. length--;
  164. for(int i = index; i < length; i++) {
  165. data[i] = Core::move(data[i + 1]);
  166. }
  167. data[length].~T();
  168. return false;
  169. }
  170. // returns true on error
  171. template<int L>
  172. check_return bool toString(ArrayString<L>& s) const {
  173. if(s.append("[")) {
  174. return true;
  175. }
  176. for(int i = 0; i < length - 1; i++) {
  177. if(s.append(data[i]) || s.append(", ")) {
  178. return true;
  179. }
  180. }
  181. if(length > 0 && s.append(data[length - 1])) {
  182. return true;
  183. }
  184. return s.append("]");
  185. }
  186. private:
  187. struct alignas(T) Aligned {
  188. char data[sizeof(T)];
  189. };
  190. static T* allocate(int n) {
  191. if(n <= 0) {
  192. return nullptr;
  193. }
  194. return reinterpret_cast<T*>(new Aligned[static_cast<size_t>(n)]);
  195. }
  196. void swap(List& other) {
  197. Core::swap(length, other.length);
  198. Core::swap(capacity, other.capacity);
  199. Core::swap(data, other.data);
  200. }
  201. // returns true on error
  202. check_return bool ensureCapacity() {
  203. if(length >= capacity) {
  204. return reserve(capacity + Core::Math::max(4, capacity / 4));
  205. }
  206. return false;
  207. }
  208. // does not check for capacity
  209. template<typename... Args>
  210. T* unsafeAdd(Args&&... args) {
  211. return new(data + length++) T(Core::forward<Args>(args)...);
  212. }
  213. };
  214. }
  215. #endif