List.h 3.8 KB

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