StaticArray.h 6.8 KB

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