123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
- // SPDX-License-Identifier: MIT
- #pragma once
- JPH_NAMESPACE_BEGIN
- /// Simple variable length array backed by a fixed size buffer
- template <class T, uint N>
- class [[nodiscard]] StaticArray
- {
- public:
- using value_type = T;
- using size_type = uint;
- /// Default constructor
- StaticArray() = default;
- /// Constructor from initializer list
- explicit StaticArray(std::initializer_list<T> inList)
- {
- JPH_ASSERT(inList.size() <= N);
- for (typename std::initializer_list<T>::iterator i = inList.begin(); i != inList.end(); ++i)
- ::new (reinterpret_cast<T *>(&mElements[mSize++])) T(*i);
- }
- /// Copy constructor
- StaticArray(const StaticArray<T, N> &inRHS)
- {
- while (mSize < inRHS.mSize)
- {
- ::new (&mElements[mSize]) T(inRHS[mSize]);
- ++mSize;
- }
- }
- /// Destruct all elements
- ~StaticArray()
- {
- if constexpr (!is_trivially_destructible<T>())
- for (T *e = reinterpret_cast<T *>(mElements), *end = e + mSize; e < end; ++e)
- e->~T();
- }
- /// Destruct all elements and set length to zero
- void clear()
- {
- if constexpr (!is_trivially_destructible<T>())
- for (T *e = reinterpret_cast<T *>(mElements), *end = e + mSize; e < end; ++e)
- e->~T();
- mSize = 0;
- }
- /// Add element to the back of the array
- void push_back(const T &inElement)
- {
- JPH_ASSERT(mSize < N);
- ::new (&mElements[mSize++]) T(inElement);
- }
- /// Construct element at the back of the array
- template <class... A>
- void emplace_back(A &&... inElement)
- {
- JPH_ASSERT(mSize < N);
- ::new (&mElements[mSize++]) T(std::forward<A>(inElement)...);
- }
- /// Remove element from the back of the array
- void pop_back()
- {
- JPH_ASSERT(mSize > 0);
- reinterpret_cast<T &>(mElements[--mSize]).~T();
- }
- /// Returns true if there are no elements in the array
- bool empty() const
- {
- return mSize == 0;
- }
- /// Returns amount of elements in the array
- size_type size() const
- {
- return mSize;
- }
- /// Returns maximum amount of elements the array can hold
- size_type capacity() const
- {
- return N;
- }
- /// Resize array to new length
- void resize(size_type inNewSize)
- {
- JPH_ASSERT(inNewSize <= N);
- if constexpr (!is_trivially_constructible<T>())
- for (T *element = reinterpret_cast<T *>(mElements) + mSize, *element_end = reinterpret_cast<T *>(mElements) + inNewSize; element < element_end; ++element)
- ::new (element) T;
- if constexpr (!is_trivially_destructible<T>())
- for (T *element = reinterpret_cast<T *>(mElements) + inNewSize, *element_end = reinterpret_cast<T *>(mElements) + mSize; element < element_end; ++element)
- element->~T();
- mSize = inNewSize;
- }
- using const_iterator = const T *;
- /// Iterators
- const_iterator begin() const
- {
- return reinterpret_cast<const T *>(mElements);
- }
- const_iterator end() const
- {
- return reinterpret_cast<const T *>(mElements + mSize);
- }
- using iterator = T *;
- iterator begin()
- {
- return reinterpret_cast<T *>(mElements);
- }
- iterator end()
- {
- return reinterpret_cast<T *>(mElements + mSize);
- }
- const T * data() const
- {
- return reinterpret_cast<const T *>(mElements);
- }
- T * data()
- {
- return reinterpret_cast<T *>(mElements);
- }
- /// Access element
- T & operator [] (size_type inIdx)
- {
- JPH_ASSERT(inIdx < mSize);
- return reinterpret_cast<T &>(mElements[inIdx]);
- }
- const T & operator [] (size_type inIdx) const
- {
- JPH_ASSERT(inIdx < mSize);
- return reinterpret_cast<const T &>(mElements[inIdx]);
- }
- /// First element in the array
- const T & front() const
- {
- JPH_ASSERT(mSize > 0);
- return reinterpret_cast<const T &>(mElements[0]);
- }
- T & front()
- {
- JPH_ASSERT(mSize > 0);
- return reinterpret_cast<T &>(mElements[0]);
- }
- /// Last element in the array
- const T & back() const
- {
- JPH_ASSERT(mSize > 0);
- return reinterpret_cast<const T &>(mElements[mSize - 1]);
- }
- T & back()
- {
- JPH_ASSERT(mSize > 0);
- return reinterpret_cast<T &>(mElements[mSize - 1]);
- }
- /// Remove one element from the array
- void erase(const_iterator inIter)
- {
- size_type p = size_type(inIter - begin());
- JPH_ASSERT(p < mSize);
- reinterpret_cast<T &>(mElements[p]).~T();
- if (p + 1 < mSize)
- memmove(mElements + p, mElements + p + 1, (mSize - p - 1) * sizeof(T));
- --mSize;
- }
- /// Remove multiple element from the array
- void erase(const_iterator inBegin, const_iterator inEnd)
- {
- size_type p = size_type(inBegin - begin());
- size_type n = size_type(inEnd - inBegin);
- JPH_ASSERT(inEnd <= end());
- for (size_type i = 0; i < n; ++i)
- reinterpret_cast<T &>(mElements[p + i]).~T();
- if (p + n < mSize)
- memmove(mElements + p, mElements + p + n, (mSize - p - n) * sizeof(T));
- mSize -= n;
- }
- /// Assignment operator
- StaticArray<T, N> & operator = (const StaticArray<T, N> &inRHS)
- {
- size_type rhs_size = inRHS.size();
- if ((void *)this != (void *)&inRHS)
- {
- clear();
- while (mSize < rhs_size)
- {
- ::new (&mElements[mSize]) T(inRHS[mSize]);
- ++mSize;
- }
- }
- return *this;
- }
- /// Assignment operator with static array of different max length
- template <uint M>
- StaticArray<T, N> & operator = (const StaticArray<T, M> &inRHS)
- {
- size_type rhs_size = inRHS.size();
- JPH_ASSERT(rhs_size <= N);
- if ((void *)this != (void *)&inRHS)
- {
- clear();
- while (mSize < rhs_size)
- {
- ::new (&mElements[mSize]) T(inRHS[mSize]);
- ++mSize;
- }
- }
- return *this;
- }
-
- /// Comparing arrays
- bool operator == (const StaticArray<T, N> &inRHS) const
- {
- if (mSize != inRHS.mSize)
- return false;
- for (size_type i = 0; i < mSize; ++i)
- if (!(reinterpret_cast<const T &>(mElements[i]) == reinterpret_cast<const T &>(inRHS.mElements[i])))
- return false;
- return true;
- }
- bool operator != (const StaticArray<T, N> &inRHS) const
- {
- if (mSize != inRHS.mSize)
- return true;
- for (size_type i = 0; i < mSize; ++i)
- if (reinterpret_cast<const T &>(mElements[i]) != reinterpret_cast<const T &>(inRHS.mElements[i]))
- return true;
- return false;
- }
-
- protected:
- struct alignas(T) Storage
- {
- uint8 mData[sizeof(T)];
- };
- static_assert(sizeof(T) == sizeof(Storage), "Mismatch in size");
- static_assert(alignof(T) == alignof(Storage), "Mismatch in alignment");
- size_type mSize = 0;
- Storage mElements[N];
- };
- JPH_NAMESPACE_END
- JPH_SUPPRESS_WARNING_PUSH
- JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat")
- namespace std
- {
- /// Declare std::hash for StaticArray
- template <class T, JPH::uint N>
- struct hash<JPH::StaticArray<T, N>>
- {
- size_t operator () (const JPH::StaticArray<T, N> &inRHS) const
- {
- std::size_t ret = 0;
- // Hash length first
- JPH::HashCombine(ret, inRHS.size());
- // Then hash elements
- for (const T &t : inRHS)
- JPH::HashCombine(ret, t);
- return ret;
- }
- };
- }
- JPH_SUPPRESS_WARNING_POP
|