List.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. int length;
  10. int 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(int 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(int n) {
  51. if(n <= capacity) {
  52. return Error::NONE;
  53. }
  54. List copy;
  55. CORE_RETURN_ERROR(allocate(copy.data, n));
  56. copy.capacity = n;
  57. for(T& t : *this) {
  58. copy.unsafeAdd(Core::move(t));
  59. }
  60. swap(copy);
  61. return Error::NONE;
  62. }
  63. check_return Error shrink() {
  64. if(length == capacity) {
  65. return Error::NONE;
  66. }
  67. List copy;
  68. CORE_RETURN_ERROR(allocate(copy.data, length));
  69. copy.capacity = length;
  70. for(int i = 0; i < length; i++) {
  71. copy.unsafeAdd(Core::move(data[i]));
  72. }
  73. swap(copy);
  74. return Error::NONE;
  75. }
  76. check_return Error resize(int n, const T& t) {
  77. if(length < n) {
  78. CORE_RETURN_ERROR(reserve(n));
  79. for(int i = n - length; i != 0; i--) {
  80. unsafeAdd(t);
  81. }
  82. } else if(length > n) {
  83. for(int i = n; i < length; i++) {
  84. data[i].~T();
  85. }
  86. length = n;
  87. }
  88. return Error::NONE;
  89. }
  90. check_return Error resize(int n) {
  91. if(length < n) {
  92. CORE_RETURN_ERROR(reserve(n));
  93. for(int i = n - length; i != 0; i--) {
  94. unsafeAdd(T());
  95. }
  96. } else if(length > n) {
  97. for(int i = n; i < length; i++) {
  98. data[i].~T();
  99. }
  100. length = n;
  101. }
  102. return Error::NONE;
  103. }
  104. template<typename... Args>
  105. check_return Error put(T*& t, Args&&... args) {
  106. CORE_RETURN_ERROR(ensureCapacity());
  107. t = unsafeAdd(Core::forward<Args>(args)...);
  108. return Error::NONE;
  109. }
  110. template<typename... Args>
  111. check_return Error add(Args&&... args) {
  112. T* t = nullptr;
  113. return put(t, Core::forward<Args>(args)...);
  114. }
  115. T& operator[](int index) {
  116. return data[index];
  117. }
  118. const T& operator[](int index) const {
  119. return data[index];
  120. }
  121. int getLength() const {
  122. return length;
  123. }
  124. int getCapacity() const {
  125. return capacity;
  126. }
  127. void clear() {
  128. for(int i = 0; i < length; i++) {
  129. data[i].~T();
  130. }
  131. length = 0;
  132. }
  133. check_return Error removeBySwap(int index) {
  134. if(index < 0 || index >= length) {
  135. return Error::INVALID_INDEX;
  136. }
  137. length--;
  138. if(index != length) {
  139. data[index] = Core::move(data[length]);
  140. }
  141. data[length].~T();
  142. return Error::NONE;
  143. }
  144. check_return Error remove(int index) {
  145. if(index < 0 || index >= length) {
  146. return Error::INVALID_INDEX;
  147. }
  148. length--;
  149. T* currentT = begin() + index;
  150. T* endT = end();
  151. while(currentT != endT) {
  152. T* nextT = currentT + 1;
  153. *currentT = Core::move(*nextT);
  154. currentT = nextT;
  155. }
  156. endT->~T();
  157. return Error::NONE;
  158. }
  159. check_return Error removeLast() {
  160. return removeBySwap(length - 1);
  161. }
  162. template<typename String>
  163. check_return Error toString(String& s) const {
  164. return Core::toString(s, *this);
  165. }
  166. void swap(List& other) {
  167. Core::swap(length, other.length);
  168. Core::swap(capacity, other.capacity);
  169. Core::swap(data, other.data);
  170. }
  171. private:
  172. static Error allocate(T*& t, int n) {
  173. if(n <= 0) {
  174. return Error::NONE;
  175. }
  176. t = reinterpret_cast<T*>(
  177. new AlignedType<T>[static_cast<size_t>(n)]);
  178. return t == nullptr ? Error::OUT_OF_MEMORY : Error::NONE;
  179. }
  180. check_return Error ensureCapacity() {
  181. return length >= capacity
  182. ? reserve(capacity + Core::Math::max(4, capacity / 4))
  183. : Error::NONE;
  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. };
  191. }
  192. #endif