List.h 3.9 KB

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