Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
Array.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Containers/Algorithms/AlgorithmFind.h" // contains
5#include "../Containers/Vector.h" // ObjectVTable<T>
6#include "ContainersExport.h"
7namespace SC
8{
9namespace detail
10{
11template <typename T, int N>
12struct SC_CONTAINERS_EXPORT ArrayVTable : public ObjectVTable<T>
13{
14 static constexpr bool IsArray = true;
15
16 T* data() { return items; }
17 const T* data() const { return items; }
18 void setData(T*) {}
19 T* getInlineData() { return items; }
20 uint32_t getInlineCapacity() { return header.capacityBytes; }
21
22 static constexpr bool isInline() { return true; }
23
24 ArrayVTable(uint32_t capacity = 0, SegmentAllocator allocator = SegmentAllocator::Global)
25 : header(capacity, allocator)
26 {}
27 ~ArrayVTable() {}
28
29 SegmentHeader header;
30 union
31 {
32 T items[N];
33 };
34};
35
36} // namespace detail
39
50template <typename T, int N>
51struct SC_CONTAINERS_EXPORT Array : public Segment<detail::ArrayVTable<T, N>>
52{
54 Array() : Parent(sizeof(T) * N) {};
55 // clang-format off
56 Array(std::initializer_list<T> list) : Array() { SC_ASSERT_RELEASE(Parent::template assign<T>({list.begin(), list.size()})); }
57 Array(const Array& other) : Array() { SC_ASSERT_RELEASE(Parent::append(other.toSpanConst())); }
58 Array(Array&& other) : Array() { SC_ASSERT_RELEASE(Parent::appendMove(move(other))); }
59 Array& operator=(const Array& other) { SC_ASSERT_RELEASE(Parent::assign(other.toSpanConst())); return *this; }
60 Array& operator=(Array&& other) { SC_ASSERT_RELEASE(Parent::assignMove(move(other))); return *this; }
61 template <int M>
62 Array(const Array<T, M>& other) : Array() { SC_ASSERT_RELEASE(Parent::assign(other.toSpanConst()));}
63 template <int M>
64 Array(Array<T, M>&& other) : Array() { SC_ASSERT_RELEASE(Parent::assignMove(move(other))); }
65 template <int M>
66 Array& operator=(const Array<T, M>& other) { SC_ASSERT_RELEASE(Parent::assign(other.toSpanConst())); return *this; }
67 template <int M>
68 Array& operator=(Array<T, M>&& other) { SC_ASSERT_RELEASE(Parent::assignMove(move(other))); return *this; }
69 Array(Span<const T> span) : Array() { SC_ASSERT_RELEASE(Parent::assign(span)); }
70 template <typename U>
71 Array(Span<const U> span) : Array() { SC_ASSERT_RELEASE(Parent::assign(span)); }
72 // clang-format on
73
76 template <typename U>
77 [[nodiscard]] bool contains(const U& value, size_t* index = nullptr) const
78 {
79 return Algorithms::contains(*this, value, index);
80 }
81
84 template <typename Lambda>
85 [[nodiscard]] bool find(Lambda&& lambda, size_t* index = nullptr) const
86 {
87 return Algorithms::findIf(Parent::begin(), Parent::end(), move(lambda), index) != Parent::end();
88 }
89
92 template <typename Lambda>
93 [[nodiscard]] bool removeAll(Lambda&& criteria)
94 {
95 T* itBeg = Parent::begin();
96 T* itEnd = Parent::end();
97 T* it = Algorithms::removeIf(itBeg, itEnd, forward<Lambda>(criteria));
98
99 const size_t numBytes = static_cast<size_t>(itEnd - it) * sizeof(T);
100 const size_t offBytes = static_cast<size_t>(it - itBeg) * sizeof(T);
101 detail::VectorVTable<T>::destruct(Parent::getData(), offBytes, numBytes);
102 Parent::header.sizeBytes -= static_cast<decltype(Parent::header.sizeBytes)>(numBytes);
103 return it != itEnd;
104 }
105
108 template <typename U>
109 [[nodiscard]] bool remove(const U& value)
110 {
111 return removeAll([&](auto& item) { return item == value; });
112 }
113};
114
116
117} // namespace SC
#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
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition PrimitiveTypes.h:29
A contiguous sequence of elements kept inside its inline storage.
Definition Array.h:52
bool removeAll(Lambda &&criteria)
Removes all items matching criteria given by Lambda.
Definition Array.h:93
bool remove(const U &value)
Removes all values equal to value
Definition Array.h:109
bool find(Lambda &&lambda, size_t *index=nullptr) const
Finds the first item in array matching criteria given by the lambda.
Definition Array.h:85
bool contains(const U &value, size_t *index=nullptr) const
Check if the current array contains a given value.
Definition Array.h:77
A slice of contiguous memory, prefixed by and header containing size and capacity.
Definition Segment.h:114
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:29