List.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 resize(int n, const T& t) {
  56. if(length < n) {
  57. reserve(n);
  58. for(int i = length; i < n; i++) {
  59. add(t);
  60. }
  61. } else if(length > n) {
  62. for(int i = n; i < length; i++) {
  63. data[i].~T();
  64. }
  65. length = n;
  66. }
  67. }
  68. template<typename... Args>
  69. void resize(int n, Args&&... args) {
  70. if(length < n) {
  71. reserve(n);
  72. for(int i = length; i < n; i++) {
  73. add(std::forward<Args>(args)...);
  74. }
  75. } else if(length > n) {
  76. for(int i = n; i < length; i++) {
  77. data[i].~T();
  78. }
  79. length = n;
  80. }
  81. }
  82. List& add(const T& t) {
  83. ensureCapacity();
  84. new(data + length++) T(t);
  85. return *this;
  86. }
  87. List& add(T&& t) {
  88. ensureCapacity();
  89. new(data + length++) T(std::move(t));
  90. return *this;
  91. }
  92. template<typename... Args>
  93. List& add(Args&&... args) {
  94. ensureCapacity();
  95. new(data + length++) T(std::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. void clear() {
  108. for(int i = 0; i < length; i++) {
  109. data[i].~T();
  110. }
  111. length = 0;
  112. }
  113. void remove(int index) {
  114. length--;
  115. if(index != length) {
  116. data[index] = std::move(data[length]);
  117. }
  118. data[length].~T();
  119. }
  120. template<int L>
  121. void toString(StringBuffer<L>& s) const {
  122. s.append("[");
  123. for(int i = 0; i < length - 1; i++) {
  124. s.append(data[i]);
  125. s.append(", ");
  126. }
  127. if(length > 0) {
  128. s.append(data[length - 1]);
  129. }
  130. s.append("]");
  131. }
  132. private:
  133. static T* allocate(int n) {
  134. return reinterpret_cast<T*>(new char[sizeof(T) * n]);
  135. }
  136. void swap(List& other) {
  137. std::swap(length, other.length);
  138. std::swap(capacity, other.capacity);
  139. std::swap(data, other.data);
  140. }
  141. void ensureCapacity() {
  142. if(length >= capacity) {
  143. reserve(capacity == 0 ? 8 : capacity * 2);
  144. }
  145. }
  146. };
  147. #endif