1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef __FN_SPANS_HH__
#define __FN_SPANS_HH__
/** \file
* \ingroup fn
*
* This file implements multiple variants of a span for different use cases. There are two
* requirements of the function system that require span implementations other from
* blender::Span<T>.
* 1. The function system works with a run-time type system (see `CPPType`). Therefore, it has to
* deal with types in a generic way. The type of a Span<T> has to be known at compile time.
* 2. Span<T> expects an underlying memory buffer that is as large as the span. However, sometimes
* we can save some memory and processing when we know that all elements are the same.
*
* The first requirement is solved with generic spans, which use the "G" prefix. Those
* store a CPPType instance to keep track of the type that is currently stored.
*
* The second requirement is solved with virtual spans. A virtual span behaves like a normal span,
* but it might not be backed up by an actual array. Elements in a virtual span are always
* immutable.
*
* Different use cases require different combinations of these properties and therefore use
* different data structures.
*/
#include "BLI_span.hh"
#include "FN_cpp_type.hh"
namespace blender {
namespace fn {
/**
* A generic span. It behaves just like a blender::Span<T>, but the type is only known at run-time.
*/
class GSpan {
private:
const CPPType *m_type;
const void *m_buffer;
uint m_size;
public:
GSpan(const CPPType &type, const void *buffer, uint size)
: m_type(&type), m_buffer(buffer), m_size(size)
{
BLI_assert(buffer != nullptr || size == 0);
BLI_assert(type.pointer_has_valid_alignment(buffer));
}
GSpan(const CPPType &type) : GSpan(type, nullptr, 0)
{
}
template<typename T>
GSpan(Span<T> array) : GSpan(CPPType::get<T>(), (const void *)array.begin(), array.size())
{
}
const CPPType &type() const
{
return *m_type;
}
bool is_empty() const
{
return m_size == 0;
}
uint size() const
{
return m_size;
}
const void *buffer() const
{
return m_buffer;
}
const void *operator[](uint index) const
{
BLI_assert(index < m_size);
return POINTER_OFFSET(m_buffer, m_type->size() * index);
}
template<typename T> Span<T> typed() const
{
BLI_assert(CPPType::get<T>() == *m_type);
return Span<T>((const T *)m_buffer, m_size);
}
};
/**
* A generic mutable span. It behaves just like a blender::MutableSpan<T>, but the type is only
* known at run-time.
*/
class GMutableSpan {
private:
const CPPType *m_type;
void *m_buffer;
uint m_size;
public:
GMutableSpan(const CPPType &type, void *buffer, uint size)
: m_type(&type), m_buffer(buffer), m_size(size)
{
BLI_assert(buffer != nullptr || size == 0);
BLI_assert(type.pointer_has_valid_alignment(buffer));
}
GMutableSpan(const CPPType &type) : GMutableSpan(type, nullptr, 0)
{
}
template<typename T>
GMutableSpan(MutableSpan<T> array)
: GMutableSpan(CPPType::get<T>(), (void *)array.begin(), array.size())
{
}
operator GSpan() const
{
return GSpan(*m_type, m_buffer, m_size);
}
const CPPType &type() const
{
return *m_type;
}
bool is_empty() const
{
return m_size == 0;
}
uint size() const
{
return m_size;
}
void *buffer()
{
return m_buffer;
}
void *operator[](uint index)
{
BLI_assert(index < m_size);
return POINTER_OFFSET(m_buffer, m_type->size() * index);
}
template<typename T> MutableSpan<T> typed()
{
BLI_assert(CPPType::get<T>() == *m_type);
return MutableSpan<T>((T *)m_buffer, m_size);
}
};
/**
* A virtual span. It behaves like a blender::Span<T>, but might not be backed up by an actual
* array.
*/
template<typename T> class VSpan {
private:
enum Category {
Single,
FullArray,
FullPointerArray,
};
uint m_virtual_size;
Category m_category;
union {
struct {
const T *data;
} single;
struct {
const T *data;
} full_array;
struct {
const T *const *data;
} full_pointer_array;
} m_data;
public:
VSpan()
{
m_virtual_size = 0;
m_category = FullArray;
m_data.full_array.data = nullptr;
}
VSpan(Span<T> values)
{
m_virtual_size = values.size();
m_category = FullArray;
m_data.full_array.data = values.begin();
}
VSpan(MutableSpan<T> values) : VSpan(Span<T>(values))
{
}
VSpan(Span<const T *> values)
{
m_virtual_size = values.size();
m_category = FullPointerArray;
m_data.full_pointer_array.data = values.begin();
}
static VSpan FromSingle(const T *value, uint virtual_size)
{
VSpan ref;
ref.m_virtual_size = virtual_size;
ref.m_category = Single;
ref.m_data.single.data = value;
return ref;
}
const T &operator[](uint index) const
{
BLI_assert(index < m_virtual_size);
switch (m_category) {
case Single:
return *m_data.single.data;
case FullArray:
return m_data.full_array.data[index];
case FullPointerArray:
return *m_data.full_pointer_array.data[index];
}
BLI_assert(false);
return *m_data.single.data;
}
bool is_empty() const
{
return m_virtual_size == 0;
}
uint size() const
{
return m_virtual_size;
}
};
/**
* A generic virtual span. It behaves like a blender::Span<T>, but the type is only known at
* run-time and it might not be backed up by an actual array.
*/
class GVSpan {
private:
enum Category {
Single,
FullArray,
FullPointerArray,
};
const CPPType *m_type;
uint m_virtual_size;
Category m_category;
union {
struct {
const void *data;
} single;
struct {
const void *data;
} full_array;
struct {
const void *const *data;
} full_pointer_array;
} m_data;
GVSpan() = default;
public:
GVSpan(const CPPType &type)
{
m_type = &type;
m_virtual_size = 0;
m_category = FullArray;
m_data.full_array.data = nullptr;
}
GVSpan(GSpan values)
{
m_type = &values.type();
m_virtual_size = values.size();
m_category = FullArray;
m_data.full_array.data = values.buffer();
}
GVSpan(GMutableSpan values) : GVSpan(GSpan(values))
{
}
template<typename T> GVSpan(Span<T> values) : GVSpan(GSpan(values))
{
}
template<typename T> GVSpan(MutableSpan<T> values) : GVSpan(GSpan(values))
{
}
static GVSpan FromSingle(const CPPType &type, const void *value, uint virtual_size)
{
GVSpan ref;
ref.m_type = &type;
ref.m_virtual_size = virtual_size;
ref.m_category = Single;
ref.m_data.single.data = value;
return ref;
}
static GVSpan FromFullPointerArray(const CPPType &type, const void *const *values, uint size)
{
GVSpan ref;
ref.m_type = &type;
ref.m_virtual_size = size;
ref.m_category = FullPointerArray;
ref.m_data.full_pointer_array.data = values;
return ref;
}
bool is_empty() const
{
return m_virtual_size == 0;
}
uint size() const
{
return m_virtual_size;
}
const CPPType &type() const
{
return *m_type;
}
const void *operator[](uint index) const
{
BLI_assert(index < m_virtual_size);
switch (m_category) {
case Single:
return m_data.single.data;
case FullArray:
return POINTER_OFFSET(m_data.full_array.data, index * m_type->size());
case FullPointerArray:
return m_data.full_pointer_array.data[index];
}
BLI_assert(false);
return m_data.single.data;
}
template<typename T> VSpan<T> typed() const
{
BLI_assert(CPPType::get<T>() == *m_type);
switch (m_category) {
case Single:
return VSpan<T>::FromSingle((const T *)m_data.single.data, m_virtual_size);
case FullArray:
return VSpan<T>(Span<T>((const T *)m_data.full_array.data, m_virtual_size));
case FullPointerArray:
return VSpan<T>(
Span<const T *>((const T *const *)m_data.full_pointer_array.data, m_virtual_size));
}
BLI_assert(false);
return {};
}
};
} // namespace fn
} // namespace blender
#endif /* __FN_SPANS_HH__ */
|