4#include "../Containers/Algorithms/AlgorithmRemove.h"
5#include "../Foundation/TypeTraits.h"
6#include "../Memory/Internal/Segment.inl"
7#include "../Memory/Internal/SegmentTrivial.inl"
8#include "ContainersExport.h"
15template <typename T, bool isTrivial = TypeTraits::IsTriviallyCopyable<T>::value>
16struct SegmentVTable :
public SegmentTrivial<T>
21struct SegmentVTable<T, false>
23 static void destruct(Span<T> data)
noexcept
25 forEach(data, [](
auto, T& item) { item.~T(); });
29 static void copyConstructAs(Span<T> data, Span<const U> value)
noexcept
31 const U& single = value[0];
32 forEach(data, [&single](
auto, T& item) { placementNew(item, single); });
36 static void copyConstruct(Span<T> data,
const U* src)
noexcept
38 forEach(data, [src](
auto idx, T& item) { placementNew(item, src[idx]); });
42 static void copyAssign(Span<T> data,
const U* src)
noexcept
44 forEach(data, [src](
auto idx, T& item) { item = src[idx]; });
48 static void moveConstruct(Span<T> data, U* src)
noexcept
50 forEach(data, [src](
auto idx, T& item) { placementNew(item,
move(src[idx])); });
54 static void moveAssign(Span<T> data, U* src)
noexcept
56 forEach(data, [src](
auto idx, T& item) { item =
move(src[idx]); });
60 static void copyInsert(Span<T> headerData, Span<const U> values)
noexcept
63 T* data = headerData.data();
64 const U* src = values.data();
66 const size_t numElements = headerData.sizeInElements();
67 const size_t numToInsert = values.sizeInElements();
72 for (
size_t idx = 0; idx < numToInsert; ++idx)
74 placementNew(data[idx], src[idx]);
80 for (
size_t idx = numElements; idx < numElements + numToInsert; ++idx)
85 if (idx >= numToInsert)
87 placementNew(data[idx],
move(data[idx - numToInsert]));
92 for (
size_t idx = numElements - 1; idx >= numToInsert; --idx)
94 if (idx >= numToInsert)
96 data[idx] =
move(data[idx - numToInsert]);
101 for (
size_t idx = 0; idx < numToInsert; ++idx)
104 if (idx < numElements)
106 data[idx] = src[idx];
110 placementNew(data[idx], src[idx]);
116 static void remove(Span<T> headerData,
size_t numToRemove)
noexcept
118 T* data = headerData.data();
120 const size_t numElements = headerData.sizeInElements();
122 for (
size_t idx = 0; idx < numElements - numToRemove; ++idx)
124 data[idx] =
move(data[idx + numToRemove]);
126 for (
size_t idx = numElements - numToRemove; idx < numElements; ++idx)
133 template <
typename Lambda>
134 static void forEach(Span<T> data, Lambda&& lambda)
noexcept
136 const size_t numElements = data.sizeInElements();
137 T* elements = data.data();
138 for (
size_t idx = 0; idx < numElements; ++idx)
140 lambda(idx, elements[idx]);
153 static void destruct(Span<T> data)
noexcept { SegmentVTable<T>::destruct(data); }
155 template <
typename U>
static void copyConstructAs(Span<T> data, Span<const U> value)
noexcept { SegmentVTable<T>::template copyConstructAs<U>(data, value);}
156 template <
typename U>
static void copyConstruct(Span<T> data,
const U* src)
noexcept { SegmentVTable<T>::template copyConstruct<U>(data, src);}
157 template <
typename U>
static void copyAssign(Span<T> data,
const U* src)
noexcept { SegmentVTable<T>::template copyAssign<U>(data, src);}
158 template <
typename U>
static void moveConstruct(Span<T> data, U* src)
noexcept { SegmentVTable<T>::template moveConstruct<U>(data, src);}
159 template <
typename U>
static void moveAssign(Span<T> data, U* src)
noexcept { SegmentVTable<T>::template moveAssign<U>(data, src);}
160 template <
typename U>
static void copyInsert(Span<T> data, Span<const U> values)
noexcept { SegmentVTable<T>::template copyInsert<U>(data, values);}
162 static void remove(Span<T> data,
size_t numElements)
noexcept { SegmentVTable<T>::remove(data, numElements); }
166struct VectorVTable :
public ObjectVTable<T>,
public SegmentSelfRelativePointer<T>
168 static constexpr bool IsArray =
false;
194 using Parent::Parent;
198 template <
typename U>
199 [[nodiscard]]
bool contains(
const U& value,
size_t* index =
nullptr) const noexcept
201 return Algorithms::contains(*
this, value, index);
206 template <
typename Lambda>
207 [[nodiscard]]
bool find(Lambda&& lambda,
size_t* index =
nullptr) const noexcept
209 return Algorithms::findIf(Parent::begin(), Parent::end(),
move(lambda), index) != Parent::end();
214 template <
typename Lambda>
215 [[nodiscard]]
bool removeAll(Lambda&& criteria)
noexcept
217 T* itBeg = Parent::begin();
218 T* itEnd = Parent::end();
221 const size_t numElements =
static_cast<size_t>(itEnd - it);
222 const size_t offset =
static_cast<size_t>(it - itBeg);
223 detail::VectorVTable<T>::destruct({Parent::data() + offset, numElements});
224 Parent::header.sizeBytes -=
static_cast<decltype(Parent::header.sizeBytes)
>(numElements *
sizeof(T));
232 template <
typename U>
233 [[nodiscard]]
bool remove(
const U& value)
noexcept
235 return removeAll([&](
auto& item) {
return item == value; });
250template <
typename T,
int N>
254 SmallVector(SegmentAllocator allocator = SegmentAllocator::Global) noexcept :
Vector<T>( N *
sizeof(T), allocator) {}
269 uint64_t inlineCapacity = N *
sizeof(T);
277using VectorTL = detail::SegmentCustom<Vector<T>,
Vector<T>, 0, SegmentAllocator::ThreadLocal>;
278template <
typename T,
int N>
279using SmallVectorTL = detail::SegmentCustom<SmallVector<T, N>,
Vector<T>, N, SegmentAllocator::ThreadLocal>;
#define SC_ASSERT_RELEASE(e)
Assert expression e to be true.
Definition Assert.h:48
constexpr T && move(T &value)
Converts an lvalue to an rvalue reference.
Definition Compiler.h:273
constexpr T && forward(typename TypeTraits::RemoveReference< T >::type &value)
Forwards an lvalue or an rvalue as an rvalue reference.
Definition Compiler.h:276
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition PrimitiveTypes.h:33
A slice of contiguous memory, prefixed by and header containing size and capacity.
Definition Segment.h:114
A Vector that can hold up to N elements inline and > N on heap.
Definition Vector.h:252
A contiguous sequence of heap allocated elements.
Definition Vector.h:190
bool remove(const U &value) noexcept
Removes all values equal to value
Definition Vector.h:233
bool find(Lambda &&lambda, size_t *index=nullptr) const noexcept
Finds the first item in array matching criteria given by the lambda.
Definition Vector.h:207
bool contains(const U &value, size_t *index=nullptr) const noexcept
Check if the current array contains a given value.
Definition Vector.h:199
bool removeAll(Lambda &&criteria) noexcept
Removes all items matching criteria given by Lambda.
Definition Vector.h:215