4#include "../Containers/Vector.h"
19template <
typename T,
int N>
26 header.capacityBytes = N *
sizeof(T);
28 header.isInlineBuffer =
true;
29 header.isFollowedByInlineBuffer =
false;
34 ~Array() { ArraySegment().unsafeSetHeader(&header); }
78 [[nodiscard]]
bool pop_back(T* removedValue =
nullptr)
85 [[nodiscard]]
bool pop_front(T* removedValue =
nullptr)
91 [[nodiscard]]
bool reserve(
size_t newCapacity)
93 return invoke([newCapacity](
ArraySegment& segment) {
return segment.
reserve(newCapacity); });
100 [[nodiscard]]
bool resize(
size_t newSize,
const T& value = T())
176 [[nodiscard]]
bool isEmpty()
const {
return header.sizeBytes == 0; }
179 [[nodiscard]]
size_t size()
const {
return header.sizeBytes /
sizeof(T); }
182 [[nodiscard]]
size_t capacity()
const {
return header.capacityBytes /
sizeof(T); }
185 [[nodiscard]]
const T*
data()
const {
return header.sizeBytes > 0 ? items :
nullptr; }
188 [[nodiscard]] T*
data() {
return header.sizeBytes > 0 ? items :
nullptr; }
191 [[nodiscard]] T* begin() SC_LANGUAGE_LIFETIME_BOUND {
return data(); }
192 [[nodiscard]]
const T* begin() const SC_LANGUAGE_LIFETIME_BOUND {
return data(); }
193 [[nodiscard]] T* end() SC_LANGUAGE_LIFETIME_BOUND {
return data() +
size(); }
194 [[nodiscard]]
const T* end() const SC_LANGUAGE_LIFETIME_BOUND {
return data() +
size(); }
198 [[nodiscard]] T& operator[](
size_t idx) SC_LANGUAGE_LIFETIME_BOUND {
SC_ASSERT_DEBUG(idx <
size());
return *(
data() + idx);}
201 [[nodiscard]]
const T& operator[](
size_t idx)
const SC_LANGUAGE_LIFETIME_BOUND {
SC_ASSERT_DEBUG(idx <
size());
return *(
data() + idx);}
206 template <
typename U>
207 [[nodiscard]]
bool contains(
const U& value,
size_t* index =
nullptr)
const
217 template <
typename Lambda>
218 [[nodiscard]]
bool find(Lambda&& lambda,
size_t* index =
nullptr)
const
230 template <
typename Lambda>
237 const size_t numBytes =
static_cast<size_t>(itEnd - it) *
sizeof(T);
238 const size_t offBytes =
static_cast<size_t>(it - itBeg) *
sizeof(T);
239 ArrayVTable::destruct(header, offBytes, numBytes);
240 header.sizeBytes -=
static_cast<decltype(header.sizeBytes)
>(numBytes);
245 template <
typename U>
246 [[nodiscard]]
bool remove(
const U& value)
248 return removeAll([&](
auto& v) {
return value == v; });
251 [[nodiscard]]
SegmentHeader& unsafeGetHeader() {
return header; }
254 static_assert(N > 0,
"Array must have N > 0");
256 template <
typename Lambda>
257 auto invoke(Lambda&& lambda)
259 ArraySegment segment;
260 segment.unsafeSetHeader(&header);
261 auto res = lambda(segment);
262 segment.unsafeSetHeader(
nullptr);
266 template <
typename Lambda>
267 auto call(Lambda&& lambda)
269 ArraySegment segment;
270 segment.unsafeSetHeader(&header);
272 segment.unsafeSetHeader(
nullptr);
275 struct ArrayVTable :
public Internal::ObjectVTable<T>
277 static SegmentHeader* allocateNewHeader(
size_t) {
return nullptr; }
278 static SegmentHeader* reallocateExistingHeader(SegmentHeader& src,
size_t newCapacityInBytes)
280 return newCapacityInBytes <
sizeof(items) ? &src :
nullptr;
282 static void destroyHeader(SegmentHeader&) {}
285 using ArraySegment = Segment<ArrayVTable>;
287 SegmentHeader header;
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_DEBUG(e)
Assert expression e to be true.
Definition: Assert.h:82
#define SC_COMPILER_EXPORT
Macro for symbol visibility in non-MSVC compilers.
Definition: Compiler.h:78
#define SC_COMPILER_EXTERN
Define compiler-specific export macros for DLL visibility.
Definition: Compiler.h:74
#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
char native_char_t
The native char for the platform (wchar_t (4 bytes) on Windows, char (1 byte) everywhere else )
Definition: PrimitiveTypes.h:34
A contiguous sequence of elements kept inside its inline storage.
Definition: Array.h:21
bool resize(size_t newSize, const T &value=T())
Resizes this array to newSize, preserving existing elements.
Definition: Array.h:100
bool push_back(const T &value)
Appends an element copying it at the end of the Array.
Definition: Array.h:65
bool removeAll(Lambda &&criteria)
Removes all items matching criteria by Lambda / Functor with a bool operator()(const T&)
Definition: Array.h:231
Span< T > toSpan()
Returns content of the array in a span.
Definition: Array.h:56
bool remove(const U &value)
Removes all values equal to value
Definition: Array.h:246
bool push_front(const T &value)
Copies an element in front of the Array, at position 0.
Definition: Array.h:59
bool reserve(size_t newCapacity)
Reserves memory for newCapacity elements.
Definition: Array.h:91
bool append(Span< const T > data)
Appends a range of items copying them at the end of array.
Definition: Array.h:129
bool assign(Array< T, M > &&other)
Replaces contents of the array moving all elements from the other array.
Definition: Array.h:159
bool resizeWithoutInitializing(size_t newSize)
Resizes to newSize, preserving existing elements without initializing newly added ones.
Definition: Array.h:106
size_t size() const
Returns the size of the array.
Definition: Array.h:179
bool insert(size_t idx, Span< const T > data)
Inserts a range of items copying them at given index.
Definition: Array.h:123
Span< const T > toSpanConst() const
Returns content of the array in a span.
Definition: Array.h:53
bool find(Lambda &&lambda, size_t *index=nullptr) const
Finds the first item in array matching criteria given by the lambda.
Definition: Array.h:218
bool isEmpty() const
Returns true if the array is empty.
Definition: Array.h:176
bool pop_back(T *removedValue=nullptr)
Removes the last element of the array.
Definition: Array.h:78
bool push_back(T &&value)
Appends an element moving it at the end of the Array.
Definition: Array.h:71
size_t capacity() const
Returns the capacity of the array.
Definition: Array.h:182
bool removeAt(size_t index)
Removes an item at a given index.
Definition: Array.h:224
const T * data() const
Gets pointer to first element of the array (or nullptr if empty)
Definition: Array.h:185
void clear()
Destroys all elements in the container, making the array empty.
Definition: Array.h:112
T * data()
Gets pointer to first element of the array (or nullptr if empty)
Definition: Array.h:188
bool assign(Span< const T > data)
Replaces contents of the array copying elements from the span.
Definition: Array.h:152
bool contains(const U &value, size_t *index=nullptr) const
Return true if array contains value, returning index of found item (if != nullptr)
Definition: Array.h:207
bool appendMove(Array &&other)
Appends another array moving its contents at the end of array.
Definition: Array.h:135
Array()
Constructs an empty Array.
Definition: Array.h:23
bool shrink_to_fit()
This operation is a no-op on Array.
Definition: Array.h:118
bool pop_front(T *removedValue=nullptr)
Removes the first element of the array.
Definition: Array.h:85
A slice of contiguous memory, prefixed by and header containing size and capacity.
Definition: Segment.h:35
bool pop_back(T *removedValue=nullptr)
Removes the last element of the segment.
bool appendMove(Segment &&other)
Moves contents of another segment to the end of this segment.
bool append(Span< const T > span)
Appends a Span to the end of the segment.
void clear()
Sets size to zero without freeing any memory (use shrink_to_fit() to free memory)
bool resize(size_t newSize, const T &value=T())
Re-allocates to the requested new size, preserving its contents and setting new items to value.
bool resizeWithoutInitializing(size_t newSize)
Re-allocates to the requested new size, preserving its contents.
bool push_front(const T &value)
Appends a single element to the start of the segment.
Definition: Segment.h:97
bool assign(Span< const T > span)
Replaces contents with contents of the span.
bool insert(size_t index, Span< const T > data)
Insert a span at the given index.
bool removeAt(size_t index)
Removes the element at index.
Definition: Segment.h:164
bool push_back(const T &value)
Appends a single element to the end of the segment.
Definition: Segment.h:91
bool reserve(size_t newCapacity)
Reserves capacity to avoid heap-allocation during a future append, assign or resize.
void unsafeSetHeader(SegmentHeader *newHeader)
Sets the internal header handled by this class.
Definition: Segment.h:173
bool pop_front(T *removedValue=nullptr)
Removes the first element of the segment.
bool assignMove(Segment &&other)
Replaces content moving (possibly "stealing") content of another segment.
View over a contiguous sequence of items (pointer + size in elements).
Definition: Span.h:24