Blender V4.3
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
6
7namespace blender {
8
9GVectorArray::GVectorArray(const CPPType &type, const int64_t array_size)
10 : type_(type), element_size_(type.size()), items_(array_size)
11{
12}
13
15{
16 if (type_.is_trivially_destructible()) {
17 return;
18 }
19 for (Item &item : items_) {
20 type_.destruct_n(item.start, item.length);
21 }
22}
23
24void GVectorArray::append(const int64_t index, const void *value)
25{
26 Item &item = items_[index];
27 if (item.length == item.capacity) {
28 this->realloc_to_at_least(item, item.capacity + 1);
29 }
30
31 void *dst = POINTER_OFFSET(item.start, element_size_ * item.length);
32 type_.copy_construct(value, dst);
33 item.length++;
34}
35
36void GVectorArray::extend(const int64_t index, const GVArray &values)
37{
38 BLI_assert(values.type() == type_);
39 for (const int i : IndexRange(values.size())) {
40 BUFFER_FOR_CPP_TYPE_VALUE(type_, buffer);
41 values.get(i, buffer);
42 this->append(index, buffer);
43 type_.destruct(buffer);
44 }
45}
46
47void GVectorArray::extend(const int64_t index, const GSpan values)
48{
49 this->extend(index, GVArray::ForSpan(values));
50}
51
52void GVectorArray::extend(const IndexMask &mask, const GVVectorArray &values)
53{
54 mask.foreach_index([&](const int64_t i) {
56 this->extend(i, GVArray(&array));
57 });
58}
59
60void GVectorArray::extend(const IndexMask &mask, const GVectorArray &values)
61{
62 GVVectorArray_For_GVectorArray virtual_values{values};
63 this->extend(mask, virtual_values);
64}
65
67{
68 mask.foreach_index([&](const int64_t i) {
69 Item &item = items_[i];
70 type_.destruct_n(item.start, item.length);
71 item.length = 0;
72 });
73}
74
76{
77 Item &item = items_[index];
78 return GMutableSpan{type_, item.start, item.length};
79}
80
82{
83 const Item &item = items_[index];
84 return GSpan{type_, item.start, item.length};
85}
86
87void GVectorArray::realloc_to_at_least(Item &item, int64_t min_capacity)
88{
89 const int64_t new_capacity = std::max(min_capacity, item.length * 2);
90
91 void *new_buffer = allocator_.allocate(element_size_ * new_capacity, type_.alignment());
92 type_.relocate_assign_n(item.start, new_buffer, item.length);
93
94 item.start = new_buffer;
95 item.capacity = new_capacity;
96}
97
98} // namespace blender
#define BLI_assert(a)
Definition BLI_assert.h:50
#define BUFFER_FOR_CPP_TYPE_VALUE(type, variable_name)
#define POINTER_OFFSET(v, ofs)
void destruct_n(void *ptr, int64_t n) const
void relocate_assign_n(void *src, void *dst, int64_t n) const
void copy_construct(const void *src, void *dst) const
void destruct(void *ptr) const
int64_t alignment() const
bool is_trivially_destructible() const
static GVArray ForSpan(GSpan span)
GMutableSpan operator[](int64_t index)
void append(int64_t index, const void *value)
void extend(int64_t index, const GVArray &values)
void clear(const IndexMask &mask)
void * allocate(const int64_t size, const int64_t alignment)
append
__int64 int64_t
Definition stdint.h:89