4#include "../Algorithms/AlgorithmRemove.h"
5#include "../Foundation/Internal/Segment.inl"
6#include "../Foundation/Internal/SegmentTrivial.inl"
7#include "../Foundation/TypeTraits.h"
14template <typename T, bool isTrivial = TypeTraits::IsTriviallyCopyable<T>::value>
15struct SegmentVTable :
public SegmentTrivial<T>
20struct SegmentVTable<T, false>
22 static void destruct(Span<T> data)
noexcept
24 forEach(data, [](
auto, T& item) { item.~T(); });
28 static void copyConstructAs(Span<T> data, Span<const U> value)
noexcept
30 const U& single = value[0];
31 forEach(data, [&single](
auto, T& item) { placementNew(item, single); });
35 static void copyConstruct(Span<T> data,
const U* src)
noexcept
37 forEach(data, [src](
auto idx, T& item) { placementNew(item, src[idx]); });
41 static void copyAssign(Span<T> data,
const U* src)
noexcept
43 forEach(data, [src](
auto idx, T& item) { item = src[idx]; });
47 static void moveConstruct(Span<T> data, U* src)
noexcept
49 forEach(data, [src](
auto idx, T& item) { placementNew(item,
move(src[idx])); });
53 static void moveAssign(Span<T> data, U* src)
noexcept
55 forEach(data, [src](
auto idx, T& item) { item =
move(src[idx]); });
59 static void copyInsert(Span<T> headerData, Span<const U> values)
noexcept
62 T* data = headerData.data();
63 const U* src = values.data();
65 const size_t numElements = headerData.sizeInElements();
66 const size_t numToInsert = values.sizeInElements();
71 for (
size_t idx = 0; idx < numToInsert; ++idx)
73 placementNew(data[idx], src[idx]);
79 for (
size_t idx = numElements; idx < numElements + numToInsert; ++idx)
84 if (idx >= numToInsert)
86 placementNew(data[idx],
move(data[idx - numToInsert]));
91 for (
size_t idx = numElements - 1; idx >= numToInsert; --idx)
93 if (idx >= numToInsert)
95 data[idx] =
move(data[idx - numToInsert]);
100 for (
size_t idx = 0; idx < numToInsert; ++idx)
103 if (idx < numElements)
105 data[idx] = src[idx];
109 placementNew(data[idx], src[idx]);
115 static void remove(Span<T> headerData,
size_t numToRemove)
noexcept
117 T* data = headerData.data();
119 const size_t numElements = headerData.sizeInElements();
121 for (
size_t idx = 0; idx < numElements - numToRemove; ++idx)
123 data[idx] =
move(data[idx + numToRemove]);
125 for (
size_t idx = numElements - numToRemove; idx < numElements; ++idx)
132 template <
typename Lambda>
133 static void forEach(Span<T> data, Lambda&& lambda)
noexcept
135 const size_t numElements = data.sizeInElements();
136 T* elements = data.data();
137 for (
size_t idx = 0; idx < numElements; ++idx)
139 lambda(idx, elements[idx]);
152 static void destruct(Span<T> data)
noexcept { SegmentVTable<T>::destruct(data); }
154 template <
typename U>
static void copyConstructAs(Span<T> data, Span<const U> value)
noexcept { SegmentVTable<T>::template copyConstructAs<U>(data, value);}
155 template <
typename U>
static void copyConstruct(Span<T> data,
const U* src)
noexcept { SegmentVTable<T>::template copyConstruct<U>(data, src);}
156 template <
typename U>
static void copyAssign(Span<T> data,
const U* src)
noexcept { SegmentVTable<T>::template copyAssign<U>(data, src);}
157 template <
typename U>
static void moveConstruct(Span<T> data, U* src)
noexcept { SegmentVTable<T>::template moveConstruct<U>(data, src);}
158 template <
typename U>
static void moveAssign(Span<T> data, U* src)
noexcept { SegmentVTable<T>::template moveAssign<U>(data, src);}
159 template <
typename U>
static void copyInsert(Span<T> data, Span<const U> values)
noexcept { SegmentVTable<T>::template copyInsert<U>(data, values);}
161 static void remove(Span<T> data,
size_t numElements)
noexcept { SegmentVTable<T>::remove(data, numElements); }
165struct VectorVTable :
public ObjectVTable<T>,
public SegmentSelfRelativePointer<T>
167 static constexpr bool IsArray =
false;
193 using Parent::Parent;
201 template <
typename U>
202 [[nodiscard]]
bool contains(
const U& value,
size_t* index =
nullptr) const noexcept
212 template <
typename Lambda>
213 [[nodiscard]]
bool find(Lambda&& lambda,
size_t* index =
nullptr) const noexcept
222 template <
typename Lambda>
223 [[nodiscard]]
bool removeAll(Lambda&& criteria)
noexcept
225 T* itBeg = Parent::begin();
226 T* itEnd = Parent::end();
229 const size_t numElements =
static_cast<size_t>(itEnd - it);
230 const size_t offset =
static_cast<size_t>(it - itBeg);
231 detail::VectorVTable<T>::destruct({Parent::data() + offset, numElements});
232 Parent::header.sizeBytes -=
static_cast<decltype(Parent::header.sizeBytes)
>(numElements *
sizeof(T));
240 template <
typename U>
241 [[nodiscard]]
bool remove(
const U& value)
noexcept
243 return removeAll([&](
auto& item) {
return item == value; });
258template <
typename T,
int N>
262 SmallVector(SegmentAllocator allocator = SegmentAllocator::Global) noexcept :
Vector<T>( N *
sizeof(T), allocator) {}
277 uint64_t inlineCapacity = N *
sizeof(T);
285using VectorTL = detail::SegmentCustom<Vector<T>,
Vector<T>, 0, SegmentAllocator::ThreadLocal>;
286template <
typename T,
int N>
287using SmallVectorTL = detail::SegmentCustom<SmallVector<T, N>,
Vector<T>, N, SegmentAllocator::ThreadLocal>;
ForwardIterator removeIf(ForwardIterator first, ForwardIterator last, UnaryPredicate &&predicate)
Removes all items in the given range, satisfying the given predicate.
Definition: AlgorithmRemove.h:22
#define SC_ASSERT_RELEASE(e)
Assert expression e to be true.
Definition: Assert.h:66
constexpr T && move(T &value)
Converts an lvalue to an rvalue reference.
Definition: Compiler.h:269
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition: PrimitiveTypes.h:42
A slice of contiguous memory, prefixed by and header containing size and capacity.
Definition: Segment.h:113
Span< const T > toSpanConst() const noexcept SC_LANGUAGE_LIFETIME_BOUND
Definition: Segment.h:206
A Vector that can hold up to N elements inline and > N on heap.
Definition: Vector.h:260
A contiguous sequence of heap allocated elements.
Definition: Vector.h:189
bool remove(const U &value) noexcept
Removes all values equal to value
Definition: Vector.h:241
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:213
bool contains(const U &value, size_t *index=nullptr) const noexcept
Check if the current array contains a given value.
Definition: Vector.h:202
bool removeAll(Lambda &&criteria) noexcept
Removes all items matching criteria given by Lambda.
Definition: Vector.h:223