StaticArray.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. JPH_NAMESPACE_BEGIN
  5. /// Simple variable length array backed by a fixed size buffer
  6. template <class T, uint N>
  7. class [[nodiscard]] StaticArray
  8. {
  9. public:
  10. using value_type = T;
  11. using size_type = uint;
  12. /// Default constructor
  13. StaticArray() = default;
  14. /// Constructor from initializer list
  15. explicit StaticArray(std::initializer_list<T> inList)
  16. {
  17. JPH_ASSERT(inList.size() <= N);
  18. for (typename std::initializer_list<T>::iterator i = inList.begin(); i != inList.end(); ++i)
  19. ::new (reinterpret_cast<T *>(&mElements[mSize++])) T(*i);
  20. }
  21. /// Copy constructor
  22. StaticArray(const StaticArray<T, N> &inRHS)
  23. {
  24. while (mSize < inRHS.mSize)
  25. {
  26. ::new (&mElements[mSize]) T(inRHS[mSize]);
  27. ++mSize;
  28. }
  29. }
  30. /// Destruct all elements
  31. ~StaticArray()
  32. {
  33. if constexpr (!is_trivially_destructible<T>())
  34. for (T *e = reinterpret_cast<T *>(mElements), *end = e + mSize; e < end; ++e)
  35. e->~T();
  36. }
  37. /// Destruct all elements and set length to zero
  38. void clear()
  39. {
  40. if constexpr (!is_trivially_destructible<T>())
  41. for (T *e = reinterpret_cast<T *>(mElements), *end = e + mSize; e < end; ++e)
  42. e->~T();
  43. mSize = 0;
  44. }
  45. /// Add element to the back of the array
  46. void push_back(const T &inElement)
  47. {
  48. JPH_ASSERT(mSize < N);
  49. ::new (&mElements[mSize++]) T(inElement);
  50. }
  51. /// Construct element at the back of the array
  52. template <class... A>
  53. void emplace_back(A &&... inElement)
  54. {
  55. JPH_ASSERT(mSize < N);
  56. ::new (&mElements[mSize++]) T(std::forward<A>(inElement)...);
  57. }
  58. /// Remove element from the back of the array
  59. void pop_back()
  60. {
  61. JPH_ASSERT(mSize > 0);
  62. reinterpret_cast<T &>(mElements[--mSize]).~T();
  63. }
  64. /// Returns true if there are no elements in the array
  65. bool empty() const
  66. {
  67. return mSize == 0;
  68. }
  69. /// Returns amount of elements in the array
  70. size_type size() const
  71. {
  72. return mSize;
  73. }
  74. /// Returns maximum amount of elements the array can hold
  75. size_type capacity() const
  76. {
  77. return N;
  78. }
  79. /// Resize array to new length
  80. void resize(size_type inNewSize)
  81. {
  82. JPH_ASSERT(inNewSize <= N);
  83. if constexpr (!is_trivially_constructible<T>())
  84. for (T *element = reinterpret_cast<T *>(mElements) + mSize, *element_end = reinterpret_cast<T *>(mElements) + inNewSize; element < element_end; ++element)
  85. ::new (element) T;
  86. if constexpr (!is_trivially_destructible<T>())
  87. for (T *element = reinterpret_cast<T *>(mElements) + inNewSize, *element_end = reinterpret_cast<T *>(mElements) + mSize; element < element_end; ++element)
  88. element->~T();
  89. mSize = inNewSize;
  90. }
  91. using const_iterator = const T *;
  92. /// Iterators
  93. const_iterator begin() const
  94. {
  95. return reinterpret_cast<const T *>(mElements);
  96. }
  97. const_iterator end() const
  98. {
  99. return reinterpret_cast<const T *>(mElements + mSize);
  100. }
  101. using iterator = T *;
  102. iterator begin()
  103. {
  104. return reinterpret_cast<T *>(mElements);
  105. }
  106. iterator end()
  107. {
  108. return reinterpret_cast<T *>(mElements + mSize);
  109. }
  110. const T * data() const
  111. {
  112. return reinterpret_cast<const T *>(mElements);
  113. }
  114. T * data()
  115. {
  116. return reinterpret_cast<T *>(mElements);
  117. }
  118. /// Access element
  119. T & operator [] (size_type inIdx)
  120. {
  121. JPH_ASSERT(inIdx < mSize);
  122. return reinterpret_cast<T &>(mElements[inIdx]);
  123. }
  124. const T & operator [] (size_type inIdx) const
  125. {
  126. JPH_ASSERT(inIdx < mSize);
  127. return reinterpret_cast<const T &>(mElements[inIdx]);
  128. }
  129. /// First element in the array
  130. const T & front() const
  131. {
  132. JPH_ASSERT(mSize > 0);
  133. return reinterpret_cast<const T &>(mElements[0]);
  134. }
  135. T & front()
  136. {
  137. JPH_ASSERT(mSize > 0);
  138. return reinterpret_cast<T &>(mElements[0]);
  139. }
  140. /// Last element in the array
  141. const T & back() const
  142. {
  143. JPH_ASSERT(mSize > 0);
  144. return reinterpret_cast<const T &>(mElements[mSize - 1]);
  145. }
  146. T & back()
  147. {
  148. JPH_ASSERT(mSize > 0);
  149. return reinterpret_cast<T &>(mElements[mSize - 1]);
  150. }
  151. /// Remove one element from the array
  152. void erase(const_iterator inIter)
  153. {
  154. size_type p = size_type(inIter - begin());
  155. JPH_ASSERT(p < mSize);
  156. reinterpret_cast<T &>(mElements[p]).~T();
  157. if (p + 1 < mSize)
  158. memmove(mElements + p, mElements + p + 1, (mSize - p - 1) * sizeof(T));
  159. --mSize;
  160. }
  161. /// Remove multiple element from the array
  162. void erase(const_iterator inBegin, const_iterator inEnd)
  163. {
  164. size_type p = size_type(inBegin - begin());
  165. size_type n = size_type(inEnd - inBegin);
  166. JPH_ASSERT(inEnd <= end());
  167. for (size_type i = 0; i < n; ++i)
  168. reinterpret_cast<T &>(mElements[p + i]).~T();
  169. if (p + n < mSize)
  170. memmove(mElements + p, mElements + p + n, (mSize - p - n) * sizeof(T));
  171. mSize -= n;
  172. }
  173. /// Assignment operator
  174. StaticArray<T, N> & operator = (const StaticArray<T, N> &inRHS)
  175. {
  176. size_type rhs_size = inRHS.size();
  177. if ((void *)this != (void *)&inRHS)
  178. {
  179. clear();
  180. while (mSize < rhs_size)
  181. {
  182. ::new (&mElements[mSize]) T(inRHS[mSize]);
  183. ++mSize;
  184. }
  185. }
  186. return *this;
  187. }
  188. /// Assignment operator with static array of different max length
  189. template <uint M>
  190. StaticArray<T, N> & operator = (const StaticArray<T, M> &inRHS)
  191. {
  192. size_type rhs_size = inRHS.size();
  193. JPH_ASSERT(rhs_size <= N);
  194. if ((void *)this != (void *)&inRHS)
  195. {
  196. clear();
  197. while (mSize < rhs_size)
  198. {
  199. ::new (&mElements[mSize]) T(inRHS[mSize]);
  200. ++mSize;
  201. }
  202. }
  203. return *this;
  204. }
  205. /// Comparing arrays
  206. bool operator == (const StaticArray<T, N> &inRHS) const
  207. {
  208. if (mSize != inRHS.mSize)
  209. return false;
  210. for (size_type i = 0; i < mSize; ++i)
  211. if (!(reinterpret_cast<const T &>(mElements[i]) == reinterpret_cast<const T &>(inRHS.mElements[i])))
  212. return false;
  213. return true;
  214. }
  215. bool operator != (const StaticArray<T, N> &inRHS) const
  216. {
  217. if (mSize != inRHS.mSize)
  218. return true;
  219. for (size_type i = 0; i < mSize; ++i)
  220. if (reinterpret_cast<const T &>(mElements[i]) != reinterpret_cast<const T &>(inRHS.mElements[i]))
  221. return true;
  222. return false;
  223. }
  224. protected:
  225. struct alignas(T) Storage
  226. {
  227. uint8 mData[sizeof(T)];
  228. };
  229. static_assert(sizeof(T) == sizeof(Storage), "Mismatch in size");
  230. static_assert(alignof(T) == alignof(Storage), "Mismatch in alignment");
  231. size_type mSize = 0;
  232. Storage mElements[N];
  233. };
  234. JPH_NAMESPACE_END
  235. JPH_SUPPRESS_WARNING_PUSH
  236. JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat")
  237. namespace std
  238. {
  239. /// Declare std::hash for StaticArray
  240. template <class T, JPH::uint N>
  241. struct hash<JPH::StaticArray<T, N>>
  242. {
  243. size_t operator () (const JPH::StaticArray<T, N> &inRHS) const
  244. {
  245. std::size_t ret = 0;
  246. // Hash length first
  247. JPH::HashCombine(ret, inRHS.size());
  248. // Then hash elements
  249. for (const T &t : inRHS)
  250. JPH::HashCombine(ret, t);
  251. return ret;
  252. }
  253. };
  254. }
  255. JPH_SUPPRESS_WARNING_POP