10#ifdef JPH_USE_STD_VECTOR
18template <
class T,
class Allocator = STLAllocator<T>>
using Array = std::vector<T, Allocator>;
34template <
class T,
class Allocator = STLAllocator<T>>
35class [[nodiscard]]
Array :
private Allocator
54 if constexpr (std::is_trivially_copyable<T>())
55 memmove(inDestination, inSource, inCount *
sizeof(T));
58 if (inDestination < inSource)
60 for (T *destination_end = inDestination + inCount; inDestination < destination_end; ++inDestination, ++inSource)
62 ::new (inDestination) T(std::move(*inSource));
68 for (T *destination = inDestination + inCount - 1, *source = inSource + inCount - 1; destination >= inDestination; --destination, --source)
70 ::new (destination) T(std::move(*source));
78 inline void reallocate(size_type inNewCapacity)
80 JPH_ASSERT(inNewCapacity > 0 && inNewCapacity >= mSize);
86 pointer = get_allocator().reallocate(mElements, mCapacity, inNewCapacity);
91 pointer = get_allocator().allocate(inNewCapacity);
92 if (mElements !=
nullptr)
94 move(pointer, mElements, mSize);
95 get_allocator().deallocate(mElements, mCapacity);
99 mCapacity = inNewCapacity;
103 inline void destruct(size_type inStart, size_type inEnd)
105 if constexpr (!is_trivially_destructible<T>())
107 for (T *element = mElements + inStart, *element_end = mElements + inEnd; element < element_end; ++element)
115 if (mCapacity < inNewSize)
116 reallocate(inNewSize);
122 destruct(inNewSize, mSize);
125 if constexpr (!is_trivially_constructible<T>())
126 for (T *element = mElements + mSize, *element_end = mElements + inNewSize; element < element_end; ++element)
134 JPH_ASSERT(&inValue < mElements || &inValue >= mElements + mSize,
"Can't pass an element from the array to resize");
136 destruct(inNewSize, mSize);
139 for (T *element = mElements + mSize, *element_end = mElements + inNewSize; element < element_end; ++element)
140 ::new (element) T(inValue);
153 inline void grow(size_type inAmount = 1)
155 size_type min_size = mSize + inAmount;
156 if (min_size > mCapacity)
158 size_type new_capacity = max(min_size, mCapacity * 2);
159 reserve(new_capacity);
166 get_allocator().deallocate(mElements, mCapacity);
172 inline void destroy()
174 if (mElements !=
nullptr)
183 template <
class Iterator>
184 inline void assign(Iterator inBegin, Iterator inEnd)
187 reserve(
size_type(std::distance(inBegin, inEnd)));
189 for (Iterator element = inBegin; element != inEnd; ++element)
190 ::new (&mElements[mSize++]) T(*element);
194 inline void assign(std::initializer_list<T> inList)
199 for (
const T &v : inList)
200 ::new (&mElements[mSize++]) T(v);
207 explicit inline Array(
const Allocator &inAllocator) :
208 Allocator(inAllocator)
214 Allocator(inAllocator)
220 inline Array(
size_type inLength,
const T &inValue,
const Allocator &inAllocator = { }) :
221 Allocator(inAllocator)
223 resize(inLength, inValue);
227 inline Array(std::initializer_list<T> inList,
const Allocator &inAllocator = { }) :
228 Allocator(inAllocator)
235 Allocator(inAllocator)
237 assign(inBegin, inEnd);
242 Allocator(inRHS.get_allocator())
249 Allocator(std::move(inRHS.get_allocator())),
251 mCapacity(inRHS.mCapacity),
252 mElements(inRHS.mElements)
256 inRHS.mElements =
nullptr;
279 JPH_ASSERT(&inValue < mElements || &inValue >= mElements + mSize,
"Can't pass an element from the array to push_back");
283 T *element = mElements + mSize++;
284 ::new (element) T(inValue);
291 T *element = mElements + mSize++;
292 ::new (element) T(std::move(inValue));
296 template <
class... A>
301 T *element = mElements + mSize++;
302 ::new (element) T(std::forward<A>(inValue)...);
310 mElements[--mSize].~T();
334 if (mElements !=
nullptr)
338 else if (mCapacity > mSize)
346 std::swap(get_allocator(), inRHS.get_allocator());
347 std::swap(mSize, inRHS.mSize);
348 std::swap(mCapacity, inRHS.mCapacity);
349 std::swap(mElements, inRHS.mElements);
352 template <
class Iterator>
356 if (num_elements > 0)
359 size_type first_element = inPos - mElements;
363 T *element_begin = mElements + first_element;
364 T *element_end = element_begin + num_elements;
365 move(element_end, element_begin, mSize - first_element);
367 for (T *element = element_begin; element < element_end; ++element, ++inBegin)
368 ::new (element) T(*inBegin);
370 mSize += num_elements;
376 JPH_ASSERT(&inValue < mElements || &inValue >= mElements + mSize,
"Can't pass an element from the array to insert");
379 size_type first_element = inPos - mElements;
383 T *element = mElements + first_element;
384 move(element + 1, element, mSize - first_element);
386 ::new (element) T(inValue);
397 move(mElements + p, mElements + p + 1, mSize - p - 1);
409 move(mElements + p, mElements + p + n, mSize - p - n);
421 return mElements + mSize;
431 return mElements + mSize;
441 return mElements + mSize;
458 return mElements[inIdx];
464 return mElements[inIdx];
471 return mElements[inIdx];
477 return mElements[inIdx];
497 return mElements[mSize - 1];
503 return mElements[mSize - 1];
509 if (
static_cast<const void *
>(
this) !=
static_cast<const void *
>(&inRHS))
518 if (
static_cast<const void *
>(
this) !=
static_cast<const void *
>(&inRHS))
522 get_allocator() = std::move(inRHS.get_allocator());
525 mCapacity = inRHS.mCapacity;
526 mElements = inRHS.mElements;
530 inRHS.mElements =
nullptr;
547 if (mSize != inRHS.mSize)
550 if (!(mElements[i] == inRHS.mElements[i]))
557 if (mSize != inRHS.mSize)
560 if (mElements[i] != inRHS.mElements[i])
572 for (
const T *element = mElements, *element_end = mElements + mSize; element < element_end; ++element)
580 size_type mCapacity = 0;
581 T * mElements =
nullptr;
586JPH_SUPPRESS_WARNING_PUSH
592 template <
class T,
class Allocator>
593 struct hash<JPH::
Array<T, Allocator>>
595 size_t operator () (
const JPH::Array<T, Allocator> &inRHS)
const
597 return std::size_t(inRHS.GetHash());
602JPH_SUPPRESS_WARNING_POP
#define JPH_SUPPRESS_WARNINGS_STD_BEGIN
Definition Core.h:384
#define JPH_SUPPRESS_WARNINGS_STD_END
Definition Core.h:396
std::uint64_t uint64
Definition Core.h:457
#define JPH_NAMESPACE_END
Definition Core.h:379
#define JPH_CLANG_SUPPRESS_WARNING(w)
Definition Core.h:263
std::uint32_t uint32
Definition Core.h:456
#define JPH_NAMESPACE_BEGIN
Definition Core.h:373
void HashCombine(uint64 &ioSeed, const T &inValue)
Commonly used types.
Definition HashCombine.h:148
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
void resize(size_type inNewSize)
Resize array to new length.
Definition Array.h:120
Array()=default
Default constructor.
void push_back(T &&inValue)
Definition Array.h:287
void pop_back()
Remove element from the back of the array.
Definition Array.h:307
bool empty() const
Returns true if there are no elements in the array.
Definition Array.h:314
size_type capacity() const
Returns maximum amount of elements the array can hold.
Definition Array.h:326
Array(const Allocator &inAllocator)
Constructor with allocator.
Definition Array.h:207
size_t size_type
Definition Array.h:40
~Array()
Destruct all elements.
Definition Array.h:260
const T & back() const
Last element in the array.
Definition Array.h:494
const T * const_iterator
Definition Array.h:47
T * data()
Definition Array.h:449
const_iterator begin() const
Iterators.
Definition Array.h:414
const_iterator cbegin() const
Definition Array.h:424
void insert(const_iterator inPos, Iterator inBegin, Iterator inEnd)
Definition Array.h:353
iterator end()
Definition Array.h:439
void erase(const_iterator inBegin, const_iterator inEnd)
Remove multiple element from the array.
Definition Array.h:402
void resize(size_type inNewSize, const T &inValue)
Resize array to new length and initialize all elements with inValue.
Definition Array.h:132
iterator begin()
Definition Array.h:434
const T * const_pointer
Definition Array.h:43
void swap(Array< T, Allocator > &inRHS) noexcept
Swap the contents of two arrays.
Definition Array.h:344
Array(size_type inLength, const T &inValue, const Allocator &inAllocator={ })
Constructor with length and value.
Definition Array.h:220
const T * data() const
Definition Array.h:444
Array(std::initializer_list< T > inList, const Allocator &inAllocator={ })
Constructor from initializer list.
Definition Array.h:227
size_type size() const
Returns amount of elements in the array.
Definition Array.h:320
Array(Array< T, Allocator > &&inRHS) noexcept
Move constructor.
Definition Array.h:248
void shrink_to_fit()
Reduce the capacity of the array to match its size.
Definition Array.h:332
void clear()
Destruct all elements and set length to zero.
Definition Array.h:145
const_iterator cend() const
Definition Array.h:429
uint64 GetHash() const
Get hash for this array.
Definition Array.h:566
const Allocator & get_allocator() const
Definition Array.h:271
T & emplace_back(A &&... inValue)
Construct element at the back of the array.
Definition Array.h:297
T * pointer
Definition Array.h:42
T * iterator
Definition Array.h:48
Array(size_type inLength, const Allocator &inAllocator={ })
Constructor with length.
Definition Array.h:213
void push_back(const T &inValue)
Add element to the back of the array.
Definition Array.h:277
void insert(const_iterator inPos, const T &inValue)
Definition Array.h:374
T & back()
Definition Array.h:500
const T & const_reference
Definition Array.h:45
T & at(size_type inIdx)
Access element.
Definition Array.h:468
const T & front() const
First element in the array.
Definition Array.h:481
void reserve(size_type inNewSize)
Reserve array space.
Definition Array.h:113
Allocator allocator_type
Definition Array.h:39
void assign(Iterator inBegin, Iterator inEnd)
Replace the contents of this array with inBegin .. inEnd.
Definition Array.h:184
T value_type
Definition Array.h:38
Allocator & get_allocator()
Get the allocator.
Definition Array.h:266
void erase(const_iterator inIter)
Remove one element from the array.
Definition Array.h:391
const_iterator end() const
Definition Array.h:419
T & front()
Definition Array.h:487
T & reference
Definition Array.h:44
const T & at(size_type inIdx) const
Definition Array.h:474
Array(const_iterator inBegin, const_iterator inEnd, const Allocator &inAllocator={ })
Constructor from iterator.
Definition Array.h:234
typename Allocator::difference_type difference_type
Definition Array.h:41
void assign(std::initializer_list< T > inList)
Replace the contents of this array with inList.
Definition Array.h:194
Array(const Array< T, Allocator > &inRHS)
Copy constructor.
Definition Array.h:241
Default implementation of AllocatorHasReallocate which tells if an allocator has a reallocate functio...
Definition STLAllocator.h:10
Fallback hash function that calls T::GetHash()
Definition HashCombine.h:59