List.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #ifndef CORE_LIST_HPP
  2. #define CORE_LIST_HPP
  3. #include "utils/AlignedData.hpp"
  4. #include "utils/ArrayString.hpp"
  5. #include "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(int i = 0; i < length; i++) {
  58. copy.unsafeAdd(Core::move(data[i]));
  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 = length; i < n; 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 = length; i < n; 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. for(int i = index; i < length; i++) {
  150. data[i] = Core::move(data[i + 1]);
  151. }
  152. data[length].~T();
  153. return Error::NONE;
  154. }
  155. check_return Error removeLast() {
  156. return removeBySwap(length - 1);
  157. }
  158. template<typename String>
  159. check_return Error toString(String& s) const {
  160. return Core::toString(s, *this);
  161. }
  162. private:
  163. static Error allocate(T*& t, int n) {
  164. if(n <= 0) {
  165. return Error::NEGATIVE_ARGUMENT;
  166. }
  167. t = reinterpret_cast<T*>(
  168. new AlignedType<T>[static_cast<size_t>(n)]);
  169. return t == nullptr ? Error::OUT_OF_MEMORY : Error::NONE;
  170. }
  171. void swap(List& other) {
  172. Core::swap(length, other.length);
  173. Core::swap(capacity, other.capacity);
  174. Core::swap(data, other.data);
  175. }
  176. check_return Error ensureCapacity() {
  177. return length >= capacity
  178. ? reserve(capacity + Core::Math::max(4, capacity / 4))
  179. : Error::NONE;
  180. }
  181. // does not check for capacity
  182. template<typename... Args>
  183. T* unsafeAdd(Args&&... args) {
  184. return new(data + length++) T(Core::forward<Args>(args)...);
  185. }
  186. };
  187. }
  188. #endif