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