List.cppm 5.7 KB

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