List.hpp 5.7 KB

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