20template <
class T,
class Allocator = STLAllocator<T>>
21class [[nodiscard]]
Array :
private Allocator
42 explicit rev_it(T *inValue) : mValue(inValue) { }
49 bool operator == (
const rev_it &inRHS)
const {
return mValue == inRHS.mValue; }
50 bool operator != (
const rev_it &inRHS)
const {
return mValue != inRHS.mValue; }
53 rev_it & operator ++ () { --mValue;
return *
this; }
55 rev_it & operator -- () { ++mValue;
return *
this; }
58 rev_it operator + (
int inValue)
const {
return rev_it(mValue - inValue); }
59 rev_it operator - (
int inValue)
const {
return rev_it(mValue + inValue); }
61 rev_it & operator += (
int inValue) { mValue -= inValue;
return *
this; }
62 rev_it & operator -= (
int inValue) { mValue += inValue;
return *
this; }
66 T & operator -> ()
const {
return *mValue; }
78 explicit crev_it(
const T *inValue) : mValue(inValue) { }
84 crev_it & operator = (
const rev_it &inRHS) { mValue = inRHS.mValue;
return *
this; }
87 bool operator == (
const crev_it &inRHS)
const {
return mValue == inRHS.mValue; }
88 bool operator != (
const crev_it &inRHS)
const {
return mValue != inRHS.mValue; }
91 crev_it & operator ++ () { --mValue;
return *
this; }
93 crev_it & operator -- () { ++mValue;
return *
this; }
99 crev_it & operator += (
int inValue) { mValue -= inValue;
return *
this; }
100 crev_it & operator -= (
int inValue) { mValue += inValue;
return *
this; }
104 const T & operator -> ()
const {
return *mValue; }
117 if constexpr (std::is_trivially_copyable<T>())
118 memmove(inDestination, inSource, inCount *
sizeof(T));
121 if (inDestination < inSource)
123 for (T *destination_end = inDestination + inCount; inDestination < destination_end; ++inDestination, ++inSource)
125 new (inDestination) T(std::move(*inSource));
131 for (T *destination = inDestination + inCount - 1, *source = inSource + inCount - 1; destination >= inDestination; --destination, --source)
133 new (destination) T(std::move(*source));
141 inline void reallocate(size_type inNewCapacity)
143 JPH_ASSERT(inNewCapacity > 0 && inNewCapacity >= mSize);
149 ptr = get_allocator().reallocate(mElements, mCapacity, inNewCapacity);
154 ptr = get_allocator().allocate(inNewCapacity);
155 if (mElements !=
nullptr)
157 move(ptr, mElements, mSize);
158 get_allocator().deallocate(mElements, mCapacity);
162 mCapacity = inNewCapacity;
166 inline void destruct(size_type inStart, size_type inEnd)
168 if constexpr (!std::is_trivially_destructible<T>())
170 for (T *element = mElements + inStart, *element_end = mElements + inEnd; element < element_end; ++element)
178 if (mCapacity < inNewSize)
179 reallocate(inNewSize);
185 destruct(inNewSize, mSize);
188 if constexpr (!std::is_trivially_constructible<T>())
189 for (T *element = mElements + mSize, *element_end = mElements + inNewSize; element < element_end; ++element)
197 JPH_ASSERT(&inValue < mElements || &inValue >= mElements + mSize,
"Can't pass an element from the array to resize");
199 destruct(inNewSize, mSize);
202 for (T *element = mElements + mSize, *element_end = mElements + inNewSize; element < element_end; ++element)
203 new (element) T(inValue);
216 inline void grow(size_type inAmount = 1)
218 size_type min_size = mSize + inAmount;
219 if (min_size > mCapacity)
221 size_type new_capacity = max(min_size, mCapacity * 2);
222 reserve(new_capacity);
227 inline void deallocate()
229 get_allocator().deallocate(mElements, mCapacity);
235 inline void destroy()
237 if (mElements !=
nullptr)
246 template <
class Iterator>
247 inline void assign(Iterator inBegin, Iterator inEnd)
250 reserve(
size_type(std::distance(inBegin, inEnd)));
252 for (Iterator element = inBegin; element != inEnd; ++element)
253 new (&mElements[mSize++]) T(*element);
257 inline void assign(std::initializer_list<T> inList)
262 for (
const T &v : inList)
263 new (&mElements[mSize++]) T(v);
270 explicit inline Array(
const Allocator &inAllocator) :
271 Allocator(inAllocator)
277 Allocator(inAllocator)
283 inline Array(
size_type inLength,
const T &inValue,
const Allocator &inAllocator = { }) :
284 Allocator(inAllocator)
286 resize(inLength, inValue);
290 inline Array(std::initializer_list<T> inList,
const Allocator &inAllocator = { }) :
291 Allocator(inAllocator)
298 Allocator(inAllocator)
300 assign(inBegin, inEnd);
305 Allocator(inRHS.get_allocator())
312 Allocator(std::move(inRHS.get_allocator())),
314 mCapacity(inRHS.mCapacity),
315 mElements(inRHS.mElements)
319 inRHS.mElements =
nullptr;
342 JPH_ASSERT(&inValue < mElements || &inValue >= mElements + mSize,
"Can't pass an element from the array to push_back");
346 T *element = mElements + mSize++;
347 new (element) T(inValue);
354 T *element = mElements + mSize++;
355 new (element) T(std::move(inValue));
359 template <
class... A>
364 T *element = mElements + mSize++;
365 new (element) T(std::forward<A>(inValue)...);
373 mElements[--mSize].~T();
397 if (mElements !=
nullptr)
401 else if (mCapacity > mSize)
409 std::swap(get_allocator(), inRHS.get_allocator());
410 std::swap(mSize, inRHS.mSize);
411 std::swap(mCapacity, inRHS.mCapacity);
412 std::swap(mElements, inRHS.mElements);
415 template <
class Iterator>
419 if (num_elements > 0)
422 size_type first_element = inPos - mElements;
426 T *element_begin = mElements + first_element;
427 T *element_end = element_begin + num_elements;
428 move(element_end, element_begin, mSize - first_element);
430 for (T *element = element_begin; element < element_end; ++element, ++inBegin)
431 new (element) T(*inBegin);
433 mSize += num_elements;
439 JPH_ASSERT(&inValue < mElements || &inValue >= mElements + mSize,
"Can't pass an element from the array to insert");
442 size_type first_element = inPos - mElements;
446 T *element = mElements + first_element;
447 move(element + 1, element, mSize - first_element);
449 new (element) T(inValue);
460 move(mElements + p, mElements + p + 1, mSize - p - 1);
462 return const_cast<iterator>(inIter);
473 move(mElements + p, mElements + p + n, mSize - p - n);
475 return const_cast<iterator>(inBegin);
486 return mElements + mSize;
491 return crev_it(mElements + mSize - 1);
526 return mElements + mSize;
531 return rev_it(mElements + mSize - 1);
536 return rev_it(mElements - 1);
553 return mElements[inIdx];
559 return mElements[inIdx];
566 return mElements[inIdx];
572 return mElements[inIdx];
592 return mElements[mSize - 1];
598 return mElements[mSize - 1];
604 if (
static_cast<const void *
>(
this) !=
static_cast<const void *
>(&inRHS))
613 if (
static_cast<const void *
>(
this) !=
static_cast<const void *
>(&inRHS))
617 get_allocator() = std::move(inRHS.get_allocator());
620 mCapacity = inRHS.mCapacity;
621 mElements = inRHS.mElements;
625 inRHS.mElements =
nullptr;
642 if (mSize != inRHS.mSize)
645 if (!(mElements[i] == inRHS.mElements[i]))
652 if (mSize != inRHS.mSize)
655 if (mElements[i] != inRHS.mElements[i])
667 for (
const T *element = mElements, *element_end = mElements + mSize; element < element_end; ++element)
675 size_type mCapacity = 0;
676 T * mElements =
nullptr;
687 template <
class T,
class Allocator>
688 struct hash<JPH::
Array<T, Allocator>>
690 size_t operator () (
const JPH::Array<T, Allocator> &inRHS)
const
692 return std::size_t(inRHS.GetHash());
std::uint64_t uint64
Definition Core.h:561
#define JPH_NAMESPACE_END
Definition Core.h:479
#define JPH_CLANG_SUPPRESS_WARNING(w)
Definition Core.h:334
std::uint32_t uint32
Definition Core.h:559
#define JPH_NAMESPACE_BEGIN
Definition Core.h:473
DVec3 operator*(double inV1, DVec3Arg inV2)
Definition DVec3.inl:623
void HashCombine(uint64 &ioSeed, const T &inValue)
Commonly used types.
Definition HashCombine.h:148
#define JPH_ASSERT(...)
Definition IssueReporting.h:28
#define JPH_SUPPRESS_WARNING_POP
Definition ShaderCore.h:38
#define JPH_SUPPRESS_WARNING_PUSH
Definition ShaderCore.h:37
A const iterator that traverses the array in reverse order.
Definition Array.h:74
crev_it()=default
Constructor.
crev_it(const T *inValue)
Definition Array.h:78
crev_it(const crev_it &)=default
Copying.
crev_it(const rev_it &inValue)
Definition Array.h:82
An iterator that traverses the array in reverse order.
Definition Array.h:38
rev_it(T *inValue)
Definition Array.h:42
rev_it()=default
Constructor.
rev_it(const rev_it &)=default
Copying.
void resize(size_type inNewSize)
Resize array to new length.
Definition Array.h:183
Array()=default
Default constructor.
crev_it crbegin() const
Definition Array.h:509
void push_back(T &&inValue)
Definition Array.h:350
void pop_back()
Remove element from the back of the array.
Definition Array.h:370
bool empty() const
Returns true if there are no elements in the array.
Definition Array.h:377
size_type capacity() const
Returns maximum amount of elements the array can hold.
Definition Array.h:389
Array(const Allocator &inAllocator)
Constructor with allocator.
Definition Array.h:270
size_t size_type
Definition Array.h:26
~Array()
Destruct all elements.
Definition Array.h:323
const T & back() const
Last element in the array.
Definition Array.h:589
const T * const_iterator
Definition Array.h:33
T * data()
Definition Array.h:544
const_iterator begin() const
Iterators.
Definition Array.h:479
const_iterator cbegin() const
Definition Array.h:499
void insert(const_iterator inPos, Iterator inBegin, Iterator inEnd)
Definition Array.h:416
iterator erase(const_iterator inIter)
Remove one element from the array.
Definition Array.h:454
iterator end()
Definition Array.h:524
crev_it rend() const
Definition Array.h:494
void resize(size_type inNewSize, const T &inValue)
Resize array to new length and initialize all elements with inValue.
Definition Array.h:195
iterator begin()
Definition Array.h:519
const T * const_pointer
Definition Array.h:29
rev_it rend()
Definition Array.h:534
void swap(Array< T, Allocator > &inRHS) noexcept
Swap the contents of two arrays.
Definition Array.h:407
iterator erase(const_iterator inBegin, const_iterator inEnd)
Remove multiple element from the array.
Definition Array.h:466
Array(size_type inLength, const T &inValue, const Allocator &inAllocator={ })
Constructor with length and value.
Definition Array.h:283
const T * data() const
Definition Array.h:539
Array(std::initializer_list< T > inList, const Allocator &inAllocator={ })
Constructor from initializer list.
Definition Array.h:290
rev_it rbegin()
Definition Array.h:529
size_type size() const
Returns amount of elements in the array.
Definition Array.h:383
Array(Array< T, Allocator > &&inRHS) noexcept
Move constructor.
Definition Array.h:311
void shrink_to_fit()
Reduce the capacity of the array to match its size.
Definition Array.h:395
void clear()
Destruct all elements and set length to zero.
Definition Array.h:208
const_iterator cend() const
Definition Array.h:504
uint64 GetHash() const
Get hash for this array.
Definition Array.h:661
const Allocator & get_allocator() const
Definition Array.h:334
T & emplace_back(A &&... inValue)
Construct element at the back of the array.
Definition Array.h:360
crev_it rbegin() const
Definition Array.h:489
T * pointer
Definition Array.h:28
T * iterator
Definition Array.h:34
crev_it crend() const
Definition Array.h:514
Array(size_type inLength, const Allocator &inAllocator={ })
Constructor with length.
Definition Array.h:276
void push_back(const T &inValue)
Add element to the back of the array.
Definition Array.h:340
void insert(const_iterator inPos, const T &inValue)
Definition Array.h:437
T & back()
Definition Array.h:595
const T & const_reference
Definition Array.h:31
T & at(size_type inIdx)
Access element.
Definition Array.h:563
const T & front() const
First element in the array.
Definition Array.h:576
void reserve(size_type inNewSize)
Reserve array space.
Definition Array.h:176
Allocator allocator_type
Definition Array.h:25
void assign(Iterator inBegin, Iterator inEnd)
Replace the contents of this array with inBegin .. inEnd.
Definition Array.h:247
T value_type
Definition Array.h:24
Allocator & get_allocator()
Get the allocator.
Definition Array.h:329
const_iterator end() const
Definition Array.h:484
T & front()
Definition Array.h:582
T & reference
Definition Array.h:30
const T & at(size_type inIdx) const
Definition Array.h:569
Array(const_iterator inBegin, const_iterator inEnd, const Allocator &inAllocator={ })
Constructor from iterator.
Definition Array.h:297
typename Allocator::difference_type difference_type
Definition Array.h:27
void assign(std::initializer_list< T > inList)
Replace the contents of this array with inList.
Definition Array.h:257
Array(const Array< T, Allocator > &inRHS)
Copy constructor.
Definition Array.h:304
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