ArrayList.h 3.2 KB

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