List.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. template<typename... Args>
  94. List& add(Args&&... args) {
  95. ensureCapacity();
  96. new(data + length++) T(std::forward<Args>(args)...);
  97. return *this;
  98. }
  99. T& operator[](int index) {
  100. return data[index];
  101. }
  102. const T& operator[](int index) const {
  103. return data[index];
  104. }
  105. int getLength() const {
  106. return length;
  107. }
  108. int getCapacity() const {
  109. return capacity;
  110. }
  111. void clear() {
  112. for(int i = 0; i < length; i++) {
  113. data[i].~T();
  114. }
  115. length = 0;
  116. }
  117. void removeBySwap(int index) {
  118. length--;
  119. if(index != length) {
  120. data[index] = std::move(data[length]);
  121. }
  122. data[length].~T();
  123. }
  124. void remove(int index) {
  125. length--;
  126. for(int i = index; i < length; i++) {
  127. data[i] = std::move(data[i + 1]);
  128. }
  129. data[length].~T();
  130. }
  131. template<int L>
  132. void toString(StringBuffer<L>& s) const {
  133. s.append("[");
  134. for(int i = 0; i < length - 1; i++) {
  135. s.append(data[i]);
  136. s.append(", ");
  137. }
  138. if(length > 0) {
  139. s.append(data[length - 1]);
  140. }
  141. s.append("]");
  142. }
  143. private:
  144. static T* allocate(int n) {
  145. return reinterpret_cast<T*>(new char[sizeof(T) * n]);
  146. }
  147. void swap(List& other) {
  148. std::swap(length, other.length);
  149. std::swap(capacity, other.capacity);
  150. std::swap(data, other.data);
  151. }
  152. void ensureCapacity() {
  153. if(length >= capacity) {
  154. reserve(capacity == 0 ? 8 : capacity * 2);
  155. }
  156. }
  157. };
  158. #endif