Blender  V3.3
generic_vector_array.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
4 
5 namespace blender {
6 
7 GVectorArray::GVectorArray(const CPPType &type, const int64_t array_size)
8  : type_(type), element_size_(type.size()), items_(array_size)
9 {
10 }
11 
13 {
14  if (type_.is_trivially_destructible()) {
15  return;
16  }
17  for (Item &item : items_) {
18  type_.destruct_n(item.start, item.length);
19  }
20 }
21 
22 void GVectorArray::append(const int64_t index, const void *value)
23 {
24  Item &item = items_[index];
25  if (item.length == item.capacity) {
26  this->realloc_to_at_least(item, item.capacity + 1);
27  }
28 
29  void *dst = POINTER_OFFSET(item.start, element_size_ * item.length);
30  type_.copy_construct(value, dst);
31  item.length++;
32 }
33 
34 void GVectorArray::extend(const int64_t index, const GVArray &values)
35 {
36  BLI_assert(values.type() == type_);
37  for (const int i : IndexRange(values.size())) {
39  values.get(i, buffer);
40  this->append(index, buffer);
41  type_.destruct(buffer);
42  }
43 }
44 
45 void GVectorArray::extend(const int64_t index, const GSpan values)
46 {
47  this->extend(index, GVArray::ForSpan(values));
48 }
49 
51 {
52  for (const int i : mask) {
54  this->extend(i, GVArray(&array));
55  }
56 }
57 
59 {
60  GVVectorArray_For_GVectorArray virtual_values{values};
61  this->extend(mask, virtual_values);
62 }
63 
65 {
66  for (const int64_t i : mask) {
67  Item &item = items_[i];
68  type_.destruct_n(item.start, item.length);
69  item.length = 0;
70  }
71 }
72 
74 {
75  Item &item = items_[index];
76  return GMutableSpan{type_, item.start, item.length};
77 }
78 
80 {
81  const Item &item = items_[index];
82  return GSpan{type_, item.start, item.length};
83 }
84 
85 void GVectorArray::realloc_to_at_least(Item &item, int64_t min_capacity)
86 {
87  const int64_t new_capacity = std::max(min_capacity, item.length * 2);
88 
89  void *new_buffer = allocator_.allocate(element_size_ * new_capacity, type_.alignment());
90  type_.relocate_assign_n(item.start, new_buffer, item.length);
91 
92  item.start = new_buffer;
93  item.capacity = new_capacity;
94 }
95 
96 } // 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)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
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
const CPPType & type() const
void get(int64_t index, void *r_value) const
static GVArray ForSpan(GSpan span)
GMutableSpan operator[](int64_t index)
void append(int64_t index, const void *value)
void clear(IndexMask mask)
void extend(int64_t index, const GVArray &values)
void * allocate(const int64_t size, const int64_t alignment)
ccl_global float * buffer
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
Definition: math_float4.h:513
__int64 int64_t
Definition: stdint.h:89
float max