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
56 explicit rev_it(T *inValue) : mValue(inValue) { }
63 bool operator == (
const rev_it &inRHS)
const {
return mValue == inRHS.mValue; }
64 bool operator != (
const rev_it &inRHS)
const {
return mValue != inRHS.mValue; }
67 rev_it & operator ++ () { --mValue;
return *
this; }
69 rev_it & operator -- () { ++mValue;
return *
this; }
72 rev_it operator + (
int inValue) {
return rev_it(mValue - inValue); }
73 rev_it operator - (
int inValue) {
return rev_it(mValue + inValue); }
75 rev_it & operator += (
int inValue) { mValue -= inValue;
return *
this; }
76 rev_it & operator -= (
int inValue) { mValue += inValue;
return *
this; }
80 T & operator -> ()
const {
return *mValue; }
92 explicit crev_it(
const T *inValue) : mValue(inValue) { }
98 crev_it & operator = (
const rev_it &inRHS) { mValue = inRHS.mValue;
return *
this; }
101 bool operator == (
const crev_it &inRHS)
const {
return mValue == inRHS.mValue; }
102 bool operator != (
const crev_it &inRHS)
const {
return mValue != inRHS.mValue; }
105 crev_it & operator ++ () { --mValue;
return *
this; }
107 crev_it & operator -- () { ++mValue;
return *
this; }
113 crev_it & operator += (
int inValue) { mValue -= inValue;
return *
this; }
114 crev_it & operator -= (
int inValue) { mValue += inValue;
return *
this; }
118 const T & operator -> ()
const {
return *mValue; }
131 if constexpr (std::is_trivially_copyable<T>())
132 memmove(inDestination, inSource, inCount *
sizeof(T));
135 if (inDestination < inSource)
137 for (T *destination_end = inDestination + inCount; inDestination < destination_end; ++inDestination, ++inSource)
139 new (inDestination) T(std::move(*inSource));
145 for (T *destination = inDestination + inCount - 1, *source = inSource + inCount - 1; destination >= inDestination; --destination, --source)
147 new (destination) T(std::move(*source));
155 inline void reallocate(size_type inNewCapacity)
157 JPH_ASSERT(inNewCapacity > 0 && inNewCapacity >= mSize);
163 ptr = get_allocator().reallocate(mElements, mCapacity, inNewCapacity);
168 ptr = get_allocator().allocate(inNewCapacity);
169 if (mElements !=
nullptr)
171 move(ptr, mElements, mSize);
172 get_allocator().deallocate(mElements, mCapacity);
176 mCapacity = inNewCapacity;
180 inline void destruct(size_type inStart, size_type inEnd)
182 if constexpr (!std::is_trivially_destructible<T>())
184 for (T *element = mElements + inStart, *element_end = mElements + inEnd; element < element_end; ++element)
192 if (mCapacity < inNewSize)
193 reallocate(inNewSize);
199 destruct(inNewSize, mSize);
202 if constexpr (!std::is_trivially_constructible<T>())
203 for (T *element = mElements + mSize, *element_end = mElements + inNewSize; element < element_end; ++element)
211 JPH_ASSERT(&inValue < mElements || &inValue >= mElements + mSize,
"Can't pass an element from the array to resize");
213 destruct(inNewSize, mSize);
216 for (T *element = mElements + mSize, *element_end = mElements + inNewSize; element < element_end; ++element)
217 new (element) T(inValue);
230 inline void grow(size_type inAmount = 1)
232 size_type min_size = mSize + inAmount;
233 if (min_size > mCapacity)
235 size_type new_capacity = max(min_size, mCapacity * 2);
236 reserve(new_capacity);
243 get_allocator().deallocate(mElements, mCapacity);
249 inline void destroy()
251 if (mElements !=
nullptr)
260 template <
class Iterator>
261 inline void assign(Iterator inBegin, Iterator inEnd)
264 reserve(
size_type(std::distance(inBegin, inEnd)));
266 for (Iterator element = inBegin; element != inEnd; ++element)
267 new (&mElements[mSize++]) T(*element);
271 inline void assign(std::initializer_list<T> inList)
276 for (
const T &v : inList)
277 new (&mElements[mSize++]) T(v);
284 explicit inline Array(
const Allocator &inAllocator) :
285 Allocator(inAllocator)
291 Allocator(inAllocator)
297 inline Array(
size_type inLength,
const T &inValue,
const Allocator &inAllocator = { }) :
298 Allocator(inAllocator)
300 resize(inLength, inValue);
304 inline Array(std::initializer_list<T> inList,
const Allocator &inAllocator = { }) :
305 Allocator(inAllocator)
312 Allocator(inAllocator)
314 assign(inBegin, inEnd);
319 Allocator(inRHS.get_allocator())
326 Allocator(std::move(inRHS.get_allocator())),
328 mCapacity(inRHS.mCapacity),
329 mElements(inRHS.mElements)
333 inRHS.mElements =
nullptr;
356 JPH_ASSERT(&inValue < mElements || &inValue >= mElements + mSize,
"Can't pass an element from the array to push_back");
360 T *element = mElements + mSize++;
361 new (element) T(inValue);
368 T *element = mElements + mSize++;
369 new (element) T(std::move(inValue));
373 template <
class... A>
378 T *element = mElements + mSize++;
379 new (element) T(std::forward<A>(inValue)...);
387 mElements[--mSize].~T();
411 if (mElements !=
nullptr)
415 else if (mCapacity > mSize)
423 std::swap(get_allocator(), inRHS.get_allocator());
424 std::swap(mSize, inRHS.mSize);
425 std::swap(mCapacity, inRHS.mCapacity);
426 std::swap(mElements, inRHS.mElements);
429 template <
class Iterator>
433 if (num_elements > 0)
436 size_type first_element = inPos - mElements;
440 T *element_begin = mElements + first_element;
441 T *element_end = element_begin + num_elements;
442 move(element_end, element_begin, mSize - first_element);
444 for (T *element = element_begin; element < element_end; ++element, ++inBegin)
445 new (element) T(*inBegin);
447 mSize += num_elements;
453 JPH_ASSERT(&inValue < mElements || &inValue >= mElements + mSize,
"Can't pass an element from the array to insert");
456 size_type first_element = inPos - mElements;
460 T *element = mElements + first_element;
461 move(element + 1, element, mSize - first_element);
463 new (element) T(inValue);
474 move(mElements + p, mElements + p + 1, mSize - p - 1);
476 return const_cast<iterator>(inIter);
487 move(mElements + p, mElements + p + n, mSize - p - n);
489 return const_cast<iterator>(inBegin);
500 return mElements + mSize;
505 return crev_it(mElements + mSize - 1);
540 return mElements + mSize;
545 return rev_it(mElements + mSize - 1);
550 return rev_it(mElements - 1);
567 return mElements[inIdx];
573 return mElements[inIdx];
580 return mElements[inIdx];
586 return mElements[inIdx];
606 return mElements[mSize - 1];
612 return mElements[mSize - 1];
618 if (
static_cast<const void *
>(
this) !=
static_cast<const void *
>(&inRHS))
627 if (
static_cast<const void *
>(
this) !=
static_cast<const void *
>(&inRHS))
631 get_allocator() = std::move(inRHS.get_allocator());
634 mCapacity = inRHS.mCapacity;
635 mElements = inRHS.mElements;
639 inRHS.mElements =
nullptr;
656 if (mSize != inRHS.mSize)
659 if (!(mElements[i] == inRHS.mElements[i]))
666 if (mSize != inRHS.mSize)
669 if (mElements[i] != inRHS.mElements[i])
681 for (
const T *element = mElements, *element_end = mElements + mSize; element < element_end; ++element)
689 size_type mCapacity = 0;
690 T * mElements =
nullptr;
695JPH_SUPPRESS_WARNING_PUSH
701 template <
class T,
class Allocator>
702 struct hash<JPH::
Array<T, Allocator>>
704 size_t operator () (
const JPH::Array<T, Allocator> &inRHS)
const
706 return std::size_t(inRHS.GetHash());
711JPH_SUPPRESS_WARNING_POP
#define JPH_SUPPRESS_WARNINGS_STD_BEGIN
Definition Core.h:424
#define JPH_SUPPRESS_WARNINGS_STD_END
Definition Core.h:437
std::uint64_t uint64
Definition Core.h:491
#define JPH_NAMESPACE_END
Definition Core.h:419
#define JPH_CLANG_SUPPRESS_WARNING(w)
Definition Core.h:302
std::uint32_t uint32
Definition Core.h:490
#define JPH_NAMESPACE_BEGIN
Definition Core.h:413
DVec3 operator*(double inV1, DVec3Arg inV2)
Definition DVec3.inl:456
void HashCombine(uint64 &ioSeed, const T &inValue)
Commonly used types.
Definition HashCombine.h:148
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
A const iterator that traverses the array in reverse order.
Definition Array.h:88
crev_it()=default
Constructor.
crev_it(const T *inValue)
Definition Array.h:92
crev_it(const crev_it &)=default
Copying.
crev_it(const rev_it &inValue)
Definition Array.h:96
An iterator that traverses the array in reverse order.
Definition Array.h:52
rev_it(T *inValue)
Definition Array.h:56
rev_it()=default
Constructor.
rev_it(const rev_it &)=default
Copying.
void resize(size_type inNewSize)
Resize array to new length.
Definition Array.h:197
Array()=default
Default constructor.
crev_it crbegin() const
Definition Array.h:523
void push_back(T &&inValue)
Definition Array.h:364
void pop_back()
Remove element from the back of the array.
Definition Array.h:384
bool empty() const
Returns true if there are no elements in the array.
Definition Array.h:391
size_type capacity() const
Returns maximum amount of elements the array can hold.
Definition Array.h:403
Array(const Allocator &inAllocator)
Constructor with allocator.
Definition Array.h:284
size_t size_type
Definition Array.h:40
~Array()
Destruct all elements.
Definition Array.h:337
const T & back() const
Last element in the array.
Definition Array.h:603
const T * const_iterator
Definition Array.h:47
T * data()
Definition Array.h:558
const_iterator begin() const
Iterators.
Definition Array.h:493
const_iterator cbegin() const
Definition Array.h:513
void insert(const_iterator inPos, Iterator inBegin, Iterator inEnd)
Definition Array.h:430
iterator erase(const_iterator inIter)
Remove one element from the array.
Definition Array.h:468
iterator end()
Definition Array.h:538
crev_it rend() const
Definition Array.h:508
void resize(size_type inNewSize, const T &inValue)
Resize array to new length and initialize all elements with inValue.
Definition Array.h:209
iterator begin()
Definition Array.h:533
const T * const_pointer
Definition Array.h:43
rev_it rend()
Definition Array.h:548
void swap(Array< T, Allocator > &inRHS) noexcept
Swap the contents of two arrays.
Definition Array.h:421
iterator erase(const_iterator inBegin, const_iterator inEnd)
Remove multiple element from the array.
Definition Array.h:480
Array(size_type inLength, const T &inValue, const Allocator &inAllocator={ })
Constructor with length and value.
Definition Array.h:297
const T * data() const
Definition Array.h:553
Array(std::initializer_list< T > inList, const Allocator &inAllocator={ })
Constructor from initializer list.
Definition Array.h:304
rev_it rbegin()
Definition Array.h:543
size_type size() const
Returns amount of elements in the array.
Definition Array.h:397
Array(Array< T, Allocator > &&inRHS) noexcept
Move constructor.
Definition Array.h:325
void shrink_to_fit()
Reduce the capacity of the array to match its size.
Definition Array.h:409
void clear()
Destruct all elements and set length to zero.
Definition Array.h:222
const_iterator cend() const
Definition Array.h:518
uint64 GetHash() const
Get hash for this array.
Definition Array.h:675
const Allocator & get_allocator() const
Definition Array.h:348
T & emplace_back(A &&... inValue)
Construct element at the back of the array.
Definition Array.h:374
crev_it rbegin() const
Definition Array.h:503
T * pointer
Definition Array.h:42
T * iterator
Definition Array.h:48
crev_it crend() const
Definition Array.h:528
Array(size_type inLength, const Allocator &inAllocator={ })
Constructor with length.
Definition Array.h:290
void push_back(const T &inValue)
Add element to the back of the array.
Definition Array.h:354
void insert(const_iterator inPos, const T &inValue)
Definition Array.h:451
T & back()
Definition Array.h:609
const T & const_reference
Definition Array.h:45
T & at(size_type inIdx)
Access element.
Definition Array.h:577
const T & front() const
First element in the array.
Definition Array.h:590
void reserve(size_type inNewSize)
Reserve array space.
Definition Array.h:190
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:261
T value_type
Definition Array.h:38
Allocator & get_allocator()
Get the allocator.
Definition Array.h:343
const_iterator end() const
Definition Array.h:498
T & front()
Definition Array.h:596
T & reference
Definition Array.h:44
const T & at(size_type inIdx) const
Definition Array.h:583
Array(const_iterator inBegin, const_iterator inEnd, const Allocator &inAllocator={ })
Constructor from iterator.
Definition Array.h:311
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:271
Array(const Array< T, Allocator > &inRHS)
Copy constructor.
Definition Array.h:318
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