ArrayList.hpp 3.2 KB

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