ArrayList.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #ifndef ARRAYLIST_H
  2. #define ARRAYLIST_H
  3. #include <new>
  4. #include <utility>
  5. #include "utils/StringBuffer.h"
  6. template<typename T, int N>
  7. class ArrayList final {
  8. alignas(T) char data[sizeof(T) * N];
  9. int length;
  10. public:
  11. ArrayList() : length(0) {
  12. }
  13. ArrayList(const ArrayList& other) : ArrayList() {
  14. copy(other);
  15. }
  16. ArrayList(ArrayList&& other) : ArrayList() {
  17. move(std::move(other));
  18. other.clear();
  19. }
  20. ~ArrayList() {
  21. clear();
  22. }
  23. ArrayList& operator=(const ArrayList& other) {
  24. if(&other != this) {
  25. clear();
  26. copy(other);
  27. }
  28. return *this;
  29. }
  30. ArrayList& operator=(ArrayList&& other) {
  31. if(&other != this) {
  32. clear();
  33. move(std::move(other));
  34. other.clear();
  35. }
  36. return *this;
  37. }
  38. T* begin() {
  39. return reinterpret_cast<T*>(data);
  40. }
  41. T* end() {
  42. return begin() + length;
  43. }
  44. const T* begin() const {
  45. return reinterpret_cast<const T*>(data);
  46. }
  47. const T* end() const {
  48. return begin() + length;
  49. }
  50. bool add(const T& t) {
  51. if(length >= N) {
  52. return true;
  53. }
  54. new(begin() + length++) T(t);
  55. return false;
  56. }
  57. bool add(T&& t) {
  58. if(length >= N) {
  59. return true;
  60. }
  61. new(begin() + length++) T(std::move(t));
  62. return false;
  63. }
  64. template<typename... Args>
  65. bool add(Args&&... args) {
  66. if(length >= N) {
  67. return true;
  68. }
  69. new(begin() + length++) T(std::forward<Args>(args)...);
  70. return false;
  71. }
  72. T& operator[](int index) {
  73. return begin()[index];
  74. }
  75. const T& operator[](int index) const {
  76. return begin()[index];
  77. }
  78. int getLength() const {
  79. return length;
  80. }
  81. void clear() {
  82. for(int i = 0; i < length; i++) {
  83. begin()[i].~T();
  84. }
  85. length = 0;
  86. }
  87. void remove(int index) {
  88. length--;
  89. if(index != length) {
  90. begin()[index] = std::move(begin()[length]);
  91. }
  92. begin()[length].~T();
  93. }
  94. template<int L>
  95. void toString(StringBuffer<L>& s) const {
  96. s.append("[");
  97. for(int i = 0; i < length - 1; i++) {
  98. s.append(begin()[i]);
  99. s.append(", ");
  100. }
  101. if(length > 0) {
  102. s.append(begin()[length - 1]);
  103. }
  104. s.append("]");
  105. }
  106. private:
  107. void copy(const ArrayList& other) {
  108. for(int i = 0; i < other.length; i++) {
  109. add(other[i]);
  110. }
  111. }
  112. void move(ArrayList&& other) {
  113. for(int i = 0; i < other.length; i++) {
  114. add(std::move(other[i]));
  115. }
  116. }
  117. };
  118. #endif