Debian Codegraph @

thirdparty/jolt_physics/Jolt/Core/StaticArray.h

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