Sane C++ Libraries: Array.h Source File (original) (raw)
1
2
3#pragma once
4#include "../Containers/Algorithms/AlgorithmFind.h"
5#include "../Containers/Vector.h"
6namespace SC
7{
8namespace detail
9{
10template <typename T, int N>
11struct SC_COMPILER_EXPORT ArrayVTable : public ObjectVTable
12{
13 static constexpr bool IsArray = true;
14
15 T* data() { return items; }
16 const T* data() const { return items; }
17 void setData(T*) {}
18 T* getInlineData() { return items; }
19 uint32_t getInlineCapacity() { return header.capacityBytes; }
20
21 static constexpr bool isInline() { return true; }
22
23 ArrayVTable(uint32_t capacity = 0, SegmentAllocator allocator = SegmentAllocator::Global)
24 : header(capacity, allocator)
25 {}
26 ~ArrayVTable() {}
27
28 SegmentHeader header;
29 union
30 {
31 T items[N];
32 };
33};
34
35}
38
49template <typename T, int N>
50struct SC_COMPILER_EXPORT Array : public Segment<detail::ArrayVTable<T, N>>
51{
54
55 Array(std::initializer_list list) : Array() { SC_ASSERT_RELEASE(Parent::template assign({list.begin(), list.size()})); }
58 Array& operator=(const Array& other) { SC_ASSERT_RELEASE(Parent::assign(other.toSpanConst())); return *this; }
60 template
62 template
64 template
66 template
69 template
71
72
75 template
76 [[nodiscard]] bool contains(const U& value, size_t* index = nullptr) const
77 {
78 return Algorithms::contains(*this, value, index);
79 }
80
83 template
84 [[nodiscard]] bool find(Lambda&& lambda, size_t* index = nullptr) const
85 {
86 return Algorithms::findIf(Parent::begin(), Parent::end(), move(lambda), index) != Parent::end();
87 }
88
91 template
92 [[nodiscard]] bool removeAll(Lambda&& criteria)
93 {
94 T* itBeg = Parent::begin();
95 T* itEnd = Parent::end();
96 T* it = Algorithms::removeIf(itBeg, itEnd, forward(criteria));
97
98 const size_t numBytes = static_cast<size_t>(itEnd - it) * sizeof(T);
99 const size_t offBytes = static_cast<size_t>(it - itBeg) * sizeof(T);
100 detail::VectorVTable::destruct(Parent::getData(), offBytes, numBytes);
101 Parent::header.sizeBytes -= static_cast<decltype(Parent::header.sizeBytes)>(numBytes);
102 return it != itEnd;
103 }
104
107 template
108 [[nodiscard]] bool remove(const U& value)
109 {
110 return removeAll([&](auto& item) { return item == value; });
111 }
112};
113
115
116}
#define SC_ASSERT_RELEASE(e)
Assert expression e to be true.
Definition Assert.h:46
constexpr T && move(T &value)
Converts an lvalue to an rvalue reference.
Definition Compiler.h:264
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition PrimitiveTypes.h:38
A contiguous sequence of elements kept inside its inline storage.
Definition Array.h:51
bool removeAll(Lambda &&criteria)
Removes all items matching criteria given by Lambda.
Definition Array.h:92
bool remove(const U &value)
Removes all values equal to value
Definition Array.h:108
bool find(Lambda &&lambda, size_t *index=nullptr) const
Finds the first item in array matching criteria given by the lambda.
Definition Array.h:84
bool contains(const U &value, size_t *index=nullptr) const
Check if the current array contains a given value.
Definition Array.h:76
A slice of contiguous memory, prefixed by and header containing size and capacity.
Definition Segment.h:113
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:29