Blender V5.0
generic_vector_array.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11namespace blender {
12
14 : type_(type), element_size_(type.size), items_(array_size)
15{
16}
17
19{
20 if (type_.is_trivially_destructible) {
21 return;
22 }
23 for (Item &item : items_) {
24 type_.destruct_n(item.start, item.length);
25 }
26}
27
28void GVectorArray::append(const int64_t index, const void *value)
29{
30 Item &item = items_[index];
31 if (item.length == item.capacity) {
32 this->realloc_to_at_least(item, item.capacity + 1);
33 }
34
35 void *dst = POINTER_OFFSET(item.start, element_size_ * item.length);
36 type_.copy_construct(value, dst);
37 item.length++;
38}
39
40void GVectorArray::extend(const int64_t index, const GVArray &values)
41{
42 BLI_assert(values.type() == type_);
43 for (const int i : IndexRange(values.size())) {
44 BUFFER_FOR_CPP_TYPE_VALUE(type_, buffer);
45 values.get(i, buffer);
46 this->append(index, buffer);
47 type_.destruct(buffer);
48 }
49}
50
51void GVectorArray::extend(const int64_t index, const GSpan values)
52{
53 this->extend(index, GVArray::from_span(values));
54}
55
57{
58 mask.foreach_index([&](const int64_t i) {
60 this->extend(i, GVArray(&array));
61 });
62}
63
65{
66 GVVectorArray_For_GVectorArray virtual_values{values};
67 this->extend(mask, virtual_values);
68}
69
71{
72 mask.foreach_index([&](const int64_t i) {
73 Item &item = items_[i];
74 type_.destruct_n(item.start, item.length);
75 item.length = 0;
76 });
77}
78
80{
81 Item &item = items_[index];
82 return GMutableSpan{type_, item.start, item.length};
83}
84
86{
87 const Item &item = items_[index];
88 return GSpan{type_, item.start, item.length};
89}
90
91void GVectorArray::realloc_to_at_least(Item &item, int64_t min_capacity)
92{
93 const int64_t new_capacity = std::max(min_capacity, item.length * 2);
94
95 void *new_buffer = allocator_.allocate(element_size_ * new_capacity, type_.alignment);
96 type_.relocate_assign_n(item.start, new_buffer, item.length);
97
98 item.start = new_buffer;
99 item.capacity = new_capacity;
100}
101
102} // namespace blender
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BUFFER_FOR_CPP_TYPE_VALUE(type, variable_name)
#define POINTER_OFFSET(v, ofs)
long long int int64_t
void relocate_assign_n(void *src, void *dst, int64_t n) const
void get(int64_t index, void *r_value) const
static GVArray from_span(GSpan span)
GMutableSpan operator[](int64_t index)
void append(int64_t index, const void *value)
const CPPType & type() const
void extend(int64_t index, const GVArray &values)
void clear(const IndexMask &mask)
void * allocate(const int64_t size, const int64_t alignment)
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
i
Definition text_draw.cc:230