ArrayList.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 a nullptr on error
  54. template<typename... Args>
  55. check_return T* add(Args&&... args) {
  56. if(length >= N) {
  57. return nullptr;
  58. }
  59. return new(begin() + length++) T(Core::forward<Args>(args)...);
  60. }
  61. T& operator[](int index) {
  62. return begin()[index];
  63. }
  64. const T& operator[](int index) const {
  65. return begin()[index];
  66. }
  67. int getLength() const {
  68. return length;
  69. }
  70. void clear() {
  71. for(int i = 0; i < length; i++) {
  72. begin()[i].~T();
  73. }
  74. length = 0;
  75. }
  76. // returns true on error
  77. check_return bool removeBySwap(int index) {
  78. if(index < 0 || index >= length) {
  79. return true;
  80. }
  81. length--;
  82. if(index != length) {
  83. begin()[index] = Core::move(begin()[length]);
  84. }
  85. begin()[length].~T();
  86. return false;
  87. }
  88. // returns true on error
  89. template<int L>
  90. check_return bool toString(ArrayString<L>& s) const {
  91. if(s.append("[")) {
  92. return true;
  93. }
  94. for(int i = 0; i < length - 1; i++) {
  95. if(s.append(begin()[i]) || s.append(", ")) {
  96. return true;
  97. }
  98. }
  99. if(length > 0 && s.append(begin()[length - 1])) {
  100. return true;
  101. }
  102. return s.append("]");
  103. }
  104. private:
  105. void copy(const ArrayList& other) {
  106. for(int i = 0; i < other.length; i++) {
  107. (void)add(other[i]);
  108. }
  109. }
  110. void move(ArrayList&& other) {
  111. for(int i = 0; i < other.length; i++) {
  112. (void)add(Core::move(other[i]));
  113. }
  114. }
  115. };
  116. }
  117. #endif