ArrayList.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #ifndef CORE_ARRAYLIST_HPP
  2. #define CORE_ARRAYLIST_HPP
  3. #include "core/utils/AlignedData.hpp"
  4. #include "core/utils/ArrayString.hpp"
  5. namespace Core {
  6. template<typename T, size_t N>
  7. class ArrayList final {
  8. AlignedType<T> data[N];
  9. size_t 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(Core::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(Core::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. template<typename... Args>
  51. check_return Error put(T*& t, Args&&... args) {
  52. if(length >= N) {
  53. return ErrorCode::CAPACITY_REACHED;
  54. }
  55. t = new(begin() + length++) T(Core::forward<Args>(args)...);
  56. return ErrorCode::NONE;
  57. }
  58. template<typename... Args>
  59. check_return Error add(Args&&... args) {
  60. T* t = nullptr;
  61. return put(t, Core::forward<Args>(args)...);
  62. }
  63. T& operator[](size_t index) {
  64. return begin()[index];
  65. }
  66. const T& operator[](size_t index) const {
  67. return begin()[index];
  68. }
  69. size_t getLength() const {
  70. return length;
  71. }
  72. void clear() {
  73. for(size_t i = 0; i < length; i++) {
  74. begin()[i].~T();
  75. }
  76. length = 0;
  77. }
  78. check_return Error removeBySwap(size_t index) {
  79. if(index >= length) {
  80. return ErrorCode::INVALID_INDEX;
  81. }
  82. length--;
  83. if(index != length) {
  84. begin()[index] = Core::move(begin()[length]);
  85. }
  86. begin()[length].~T();
  87. return ErrorCode::NONE;
  88. }
  89. check_return Error removeLast() {
  90. return removeBySwap(length - 1);
  91. }
  92. template<typename String>
  93. check_return Error toString(String& s) const {
  94. return Core::toString(s, *this);
  95. }
  96. private:
  97. void copy(const ArrayList& other) {
  98. for(size_t i = 0; i < other.length; i++) {
  99. (void)add(other[i]);
  100. }
  101. }
  102. void move(ArrayList&& other) {
  103. for(size_t i = 0; i < other.length; i++) {
  104. (void)add(Core::move(other[i]));
  105. }
  106. }
  107. };
  108. }
  109. #endif