List.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. List& add(const T& t) {
  56. ensureCapacity();
  57. new(data + length++) T(t);
  58. return *this;
  59. }
  60. List& add(T&& t) {
  61. ensureCapacity();
  62. new(data + length++) T(std::move(t));
  63. return *this;
  64. }
  65. template<typename... Args>
  66. List& add(Args&&... args) {
  67. ensureCapacity();
  68. new(data + length++) T(std::forward<Args>(args)...);
  69. return *this;
  70. }
  71. T& operator[](int index) {
  72. return data[index];
  73. }
  74. const T& operator[](int index) const {
  75. return data[index];
  76. }
  77. int getLength() const {
  78. return length;
  79. }
  80. void clear() {
  81. for(int i = 0; i < length; i++) {
  82. data[i].~T();
  83. }
  84. length = 0;
  85. }
  86. void remove(int index) {
  87. length--;
  88. if(index != length) {
  89. data[index] = std::move(data[length]);
  90. }
  91. data[length].~T();
  92. }
  93. template<int L>
  94. void toString(StringBuffer<L>& s) const {
  95. s.append("[");
  96. for(int i = 0; i < length - 1; i++) {
  97. s.append(data[i]);
  98. s.append(", ");
  99. }
  100. if(length > 0) {
  101. s.append(data[length - 1]);
  102. }
  103. s.append("]");
  104. }
  105. private:
  106. static T* allocate(int n) {
  107. return reinterpret_cast<T*>(new char[sizeof(T) * n]);
  108. }
  109. void swap(List& other) {
  110. std::swap(length, other.length);
  111. std::swap(capacity, other.capacity);
  112. std::swap(data, other.data);
  113. }
  114. void ensureCapacity() {
  115. if(length >= capacity) {
  116. reserve(capacity == 0 ? 8 : capacity * 2);
  117. }
  118. }
  119. };
  120. #endif