ArrayList.h 3.3 KB

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