Debian Codegraph @

src/base/SkFixedArray.h

raw
1 /*
2 * Copyright 2024 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkTFixedArray_DEFINED
9 #define SkTFixedArray_DEFINED
10
11 #include "include/private/base/SkAssert.h"
12 #include "include/private/base/SkSpan_impl.h"
13
14 #include <cstdint>
15 #include <cstring>
16 #include <initializer_list>
17 #include <type_traits> // IWYU pragma: keep for std::is_trivial_v
18
19 namespace skia_private {
20
21 /**
22 * Represents an array of `T` (must be a trivial type) that cannot grow past a fixed size `N`.
23 * The fixed-size restriction allows for tighter codegen and a smaller memory footprint.
24 * Missing methods from TArray (e.g. `fromBack`) can be added on demand.
25 *
26 * The trivial-type restriction is only to simplify implementation; if there is a need, we can
27 * adopt proper move/copy semantics in this class as well.
28 */
29 template <int N, typename T>
30 class FixedArray {
31 public:
32 using value_type = T;
33
34 FixedArray() = default;
35
36 FixedArray(std::initializer_list<T> values) {
37 SkASSERT(values.size() <= N);
38 for (T value : values) {
39 fData[fSize++] = value;
40 }
41 }
42
43 FixedArray(int reserveCount) {
44 // This is here to satisfy the TArray interface. Setting a reserve count on a fixed array
45 // isn't useful.
46 SkASSERT(reserveCount >= 0);
47 SkASSERT(reserveCount <= N);
48 }
49
50 FixedArray(SkSpan<const T> array) {
51 this->reset(array);
52 }
53
54 FixedArray(const FixedArray<N, T>& that) {
55 this->reset({that.data(), (size_t)that.size()});
56 }
57
58 FixedArray<N, T>& operator=(const FixedArray<N, T>& that) {
59 if (this != &that) {
60 this->reset({that.data(), (size_t)that.size()});
61 }
62 return *this;
63 }
64
65 T& operator[](size_t index) {
66 SkASSERT(index < fSize);
67 return fData[index];
68 }
69
70 const T& operator[](size_t index) const {
71 SkASSERT(index < fSize);
72 return fData[index];
73 }
74
75 bool operator==(const FixedArray<N, T>& that) const {
76 return fSize == that.fSize && (0 == memcmp(fData, that.fData, fSize * sizeof(T)));
77 }
78
79 bool operator!=(const FixedArray<N, T>& that) const {
80 return !this->operator==(that);
81 }
82
83 int size() const {
84 return fSize;
85 }
86
87 bool empty() const {
88 return fSize == 0;
89 }
90
91 void clear() {
92 fSize = 0;
93 }
94
95 void reset(SkSpan<const T> array) {
96 SkASSERT(array.size() <= N);
97 fSize = array.size();
98 std::memcpy(fData, array.data(), array.size_bytes());
99 }
100
101 void resize(int newSize) {
102 SkASSERT(newSize >= 0);
103 SkASSERT(newSize <= N);
104
105 if (fSize > newSize) {
106 fSize = newSize;
107 } else {
108 while (fSize < newSize) {
109 fData[fSize++] = T();
110 }
111 }
112 }
113
114 T& push_back() {
115 SkASSERT(fSize < N);
116 T& ref = fData[fSize++];
117 ref = T();
118 return ref;
119 }
120
121 void push_back(T x) {
122 SkASSERT(fSize < N);
123 fData[fSize++] = x;
124 }
125
126 T* push_back_n(int n) {
127 SkASSERT(n >= 0);
128 SkASSERT(fSize + n <= N);
129 T* ptr = fData + fSize;
130 for (int index = 0; index < n; ++index) {
131 ptr[index] = T();
132 }
133 fSize += n;
134 return ptr;
135 }
136
137 T* push_back_n(int n, const T& t) {
138 SkASSERT(n >= 0);
139 SkASSERT(fSize + n <= N);
140 T* ptr = fData + fSize;
141 for (int index = 0; index < n; ++index) {
142 ptr[index] = t;
143 }
144 fSize += n;
145 return ptr;
146 }
147
148 void pop_back() {
149 SkASSERT(fSize >= 1);
150 --fSize;
151 }
152
153 void pop_back_n(int n) {
154 SkASSERT(fSize >= n);
155 fSize -= n;
156 }
157
158 void removeShuffle(int n) {
159 SkASSERT(n < fSize);
160 int last = fSize - 1;
161 if (n != last) {
162 fData[n] = fData[last];
163 }
164 fSize = last;
165 }
166
167 T* data() {
168 return fData;
169 }
170
171 const T* data() const {
172 return fData;
173 }
174
175 T* begin() {
176 return fData;
177 }
178
179 const T* begin() const {
180 return fData;
181 }
182
183 T* end() {
184 return fData + fSize;
185 }
186
187 const T* end() const {
188 return fData + fSize;
189 }
190
191 T& front() {
192 SkASSERT(fSize > 0);
193 return fData[0];
194 }
195
196 const T& front() const {
197 SkASSERT(fSize > 0);
198 return fData[0];
199 }
200
201 T& back() {
202 SkASSERT(fSize > 0);
203 return fData[fSize - 1];
204 }
205
206 const T& back() const {
207 SkASSERT(fSize > 0);
208 return fData[fSize - 1];
209 }
210
211 void reserve(int size) {
212 // This is here to satisfy the TArray interface.
213 SkASSERT(size >= 0);
214 SkASSERT(size <= N);
215 }
216
217 constexpr int capacity() const {
218 return N;
219 }
220
221 private:
222 static_assert(std::is_trivial_v<T>);
223 static_assert(N > 0);
224 static_assert(N < 256); // limited by `uint8_t fSize`
225
226 T fData[N];
227 uint8_t fSize = 0;
228 };
229
230 } // namespace skia_private
231
232 #endif