StaticArray.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. /// Access element
  132. T & at(size_type inIdx)
  133. {
  134. JPH_ASSERT(inIdx < mSize);
  135. return reinterpret_cast<T &>(mElements[inIdx]);
  136. }
  137. const T & at(size_type inIdx) const
  138. {
  139. JPH_ASSERT(inIdx < mSize);
  140. return reinterpret_cast<const T &>(mElements[inIdx]);
  141. }
  142. /// First element in the array
  143. const T & front() const
  144. {
  145. JPH_ASSERT(mSize > 0);
  146. return reinterpret_cast<const T &>(mElements[0]);
  147. }
  148. T & front()
  149. {
  150. JPH_ASSERT(mSize > 0);
  151. return reinterpret_cast<T &>(mElements[0]);
  152. }
  153. /// Last element in the array
  154. const T & back() const
  155. {
  156. JPH_ASSERT(mSize > 0);
  157. return reinterpret_cast<const T &>(mElements[mSize - 1]);
  158. }
  159. T & back()
  160. {
  161. JPH_ASSERT(mSize > 0);
  162. return reinterpret_cast<T &>(mElements[mSize - 1]);
  163. }
  164. /// Remove one element from the array
  165. void erase(const_iterator inIter)
  166. {
  167. size_type p = size_type(inIter - begin());
  168. JPH_ASSERT(p < mSize);
  169. reinterpret_cast<T &>(mElements[p]).~T();
  170. if (p + 1 < mSize)
  171. memmove(mElements + p, mElements + p + 1, (mSize - p - 1) * sizeof(T));
  172. --mSize;
  173. }
  174. /// Remove multiple element from the array
  175. void erase(const_iterator inBegin, const_iterator inEnd)
  176. {
  177. size_type p = size_type(inBegin - begin());
  178. size_type n = size_type(inEnd - inBegin);
  179. JPH_ASSERT(inEnd <= end());
  180. for (size_type i = 0; i < n; ++i)
  181. reinterpret_cast<T &>(mElements[p + i]).~T();
  182. if (p + n < mSize)
  183. memmove(mElements + p, mElements + p + n, (mSize - p - n) * sizeof(T));
  184. mSize -= n;
  185. }
  186. /// Assignment operator
  187. StaticArray<T, N> & operator = (const StaticArray<T, N> &inRHS)
  188. {
  189. size_type rhs_size = inRHS.size();
  190. if (static_cast<const void *>(this) != static_cast<const void *>(&inRHS))
  191. {
  192. clear();
  193. while (mSize < rhs_size)
  194. {
  195. ::new (&mElements[mSize]) T(inRHS[mSize]);
  196. ++mSize;
  197. }
  198. }
  199. return *this;
  200. }
  201. /// Assignment operator with static array of different max length
  202. template <uint M>
  203. StaticArray<T, N> & operator = (const StaticArray<T, M> &inRHS)
  204. {
  205. size_type rhs_size = inRHS.size();
  206. JPH_ASSERT(rhs_size <= N);
  207. if (static_cast<const void *>(this) != static_cast<const void *>(&inRHS))
  208. {
  209. clear();
  210. while (mSize < rhs_size)
  211. {
  212. ::new (&mElements[mSize]) T(inRHS[mSize]);
  213. ++mSize;
  214. }
  215. }
  216. return *this;
  217. }
  218. /// Comparing arrays
  219. bool operator == (const StaticArray<T, N> &inRHS) const
  220. {
  221. if (mSize != inRHS.mSize)
  222. return false;
  223. for (size_type i = 0; i < mSize; ++i)
  224. if (!(reinterpret_cast<const T &>(mElements[i]) == reinterpret_cast<const T &>(inRHS.mElements[i])))
  225. return false;
  226. return true;
  227. }
  228. bool operator != (const StaticArray<T, N> &inRHS) const
  229. {
  230. if (mSize != inRHS.mSize)
  231. return true;
  232. for (size_type i = 0; i < mSize; ++i)
  233. if (reinterpret_cast<const T &>(mElements[i]) != reinterpret_cast<const T &>(inRHS.mElements[i]))
  234. return true;
  235. return false;
  236. }
  237. protected:
  238. struct alignas(T) Storage
  239. {
  240. uint8 mData[sizeof(T)];
  241. };
  242. static_assert(sizeof(T) == sizeof(Storage), "Mismatch in size");
  243. static_assert(alignof(T) == alignof(Storage), "Mismatch in alignment");
  244. size_type mSize = 0;
  245. Storage mElements[N];
  246. };
  247. JPH_NAMESPACE_END
  248. JPH_SUPPRESS_WARNING_PUSH
  249. JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat")
  250. namespace std
  251. {
  252. /// Declare std::hash for StaticArray
  253. template <class T, JPH::uint N>
  254. struct hash<JPH::StaticArray<T, N>>
  255. {
  256. size_t operator () (const JPH::StaticArray<T, N> &inRHS) const
  257. {
  258. std::size_t ret = 0;
  259. // Hash length first
  260. JPH::HashCombine(ret, inRHS.size());
  261. // Then hash elements
  262. for (const T &t : inRHS)
  263. JPH::HashCombine(ret, t);
  264. return ret;
  265. }
  266. };
  267. }
  268. JPH_SUPPRESS_WARNING_POP