List.cppm 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. module;
  2. #include <cassert>
  3. #include <cstdio>
  4. #include <new>
  5. export module Core.List;
  6. import Core.AlignedData;
  7. import Core.Math;
  8. import Core.Meta;
  9. import Core.Types;
  10. import Core.Utility;
  11. import Core.ToString;
  12. export namespace Core {
  13. template<typename T>
  14. class List final {
  15. size_t length;
  16. size_t capacity;
  17. T* data;
  18. public:
  19. List() : length(0), capacity(0), data(nullptr) {
  20. }
  21. List(const List& other) :
  22. length(0), capacity(other.capacity),
  23. data(allocate(other.capacity)) {
  24. for(const T& t : other) {
  25. unsafeAdd(t);
  26. }
  27. }
  28. List(List&& other) noexcept : List() {
  29. swap(other);
  30. }
  31. ~List() {
  32. clear();
  33. deleteWithSourceN<AlignedType<T>>(
  34. reinterpret_cast<AlignedType<T>*>(data));
  35. }
  36. List& operator=(List other) noexcept {
  37. swap(other);
  38. return *this;
  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. void reserve(size_t n) {
  53. if(n > capacity) {
  54. setSize(n);
  55. }
  56. }
  57. void shrink() {
  58. if(length != capacity) {
  59. setSize(length);
  60. }
  61. }
  62. void resize(size_t n, const T& t) {
  63. if(length < n) {
  64. reserve(n);
  65. for(size_t i = n - length; i != 0; i--) {
  66. unsafeAdd(t);
  67. }
  68. } else if(length > n) {
  69. for(size_t i = n; i < length; i++) {
  70. data[i].~T();
  71. }
  72. length = n;
  73. }
  74. }
  75. void resize(size_t n) {
  76. if(length < n) {
  77. reserve(n);
  78. for(size_t i = n - length; i != 0; i--) {
  79. unsafeAdd(T());
  80. }
  81. } else if(length > n) {
  82. for(size_t i = n; i < length; i++) {
  83. data[i].~T();
  84. }
  85. length = n;
  86. }
  87. }
  88. template<typename... Args>
  89. T& put(Args&&... args) {
  90. ensureCapacity();
  91. return *unsafeAdd(Core::forward<Args>(args)...);
  92. }
  93. template<typename... Args>
  94. List& add(Args&&... args) {
  95. put(Core::forward<Args>(args)...);
  96. return *this;
  97. }
  98. template<typename... Args>
  99. T& putAt(size_t t, Args&&... args) {
  100. if(t >= length) {
  101. return put(Core::forward<Args>(args)...);
  102. }
  103. // put must not reallocate, to keep the moved element alive
  104. ensureCapacity();
  105. put(Core::move(data[length - 1]));
  106. for(size_t i = length - 2; i > t; i--) {
  107. data[i] = Core::move(data[i - 1]);
  108. }
  109. data[t] = Core::move(T(Core::forward<Args>(args)...));
  110. return data[t];
  111. }
  112. template<typename... Args>
  113. List& addAt(size_t index, Args&&... args) {
  114. putAt(index, Core::forward<Args>(args)...);
  115. return *this;
  116. }
  117. T& operator[](size_t index) {
  118. assert(index < length);
  119. return data[index];
  120. }
  121. const T& operator[](size_t index) const {
  122. assert(index < length);
  123. return data[index];
  124. }
  125. T& getLast() {
  126. assert(length > 0);
  127. return data[length - 1];
  128. }
  129. const T& getLast() const {
  130. assert(length > 0);
  131. return data[length - 1];
  132. }
  133. size_t getLength() const {
  134. return length;
  135. }
  136. size_t getCapacity() const {
  137. return capacity;
  138. }
  139. void clear() {
  140. for(T& t : *this) {
  141. t.~T();
  142. }
  143. length = 0;
  144. }
  145. void removeBySwap(size_t index) {
  146. assert(index < length);
  147. length--;
  148. if(index != length) {
  149. data[index] = Core::move(data[length]);
  150. }
  151. data[length].~T();
  152. }
  153. void remove(size_t index) {
  154. assert(index < length);
  155. length--;
  156. T* currentT = begin() + index;
  157. T* endT = end();
  158. while(currentT != endT) {
  159. T* nextT = currentT + 1;
  160. *currentT = Core::move(*nextT);
  161. currentT = nextT;
  162. }
  163. endT->~T();
  164. }
  165. void removeLast() {
  166. removeBySwap(length - 1);
  167. }
  168. void swap(List& other) noexcept {
  169. Core::swap(length, other.length);
  170. Core::swap(capacity, other.capacity);
  171. Core::swap(data, other.data);
  172. }
  173. private:
  174. static T* allocate(size_t n) {
  175. if(n <= 0) {
  176. return nullptr;
  177. }
  178. return reinterpret_cast<T*>(newWithSourceN<AlignedType<T>>(n));
  179. }
  180. void ensureCapacity() {
  181. if(length >= capacity) {
  182. reserve(capacity + Core::max(4lu, capacity / 4));
  183. }
  184. }
  185. // does not check for capacity
  186. template<typename... Args>
  187. T* unsafeAdd(Args&&... args) {
  188. return new(data + length++) T(Core::forward<Args>(args)...);
  189. }
  190. void setSize(size_t n) {
  191. List copy;
  192. copy.data = allocate(n);
  193. copy.capacity = n;
  194. for(size_t i = 0; i < length; i++) {
  195. copy.unsafeAdd(Core::move(data[i]));
  196. }
  197. swap(copy);
  198. }
  199. };
  200. }