List.h 2.9 KB

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