List.h 2.9 KB

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