Blender  V2.93
generic_vector_array.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
20 
21 namespace blender::fn {
22 
24  : type_(type), element_size_(type.size()), items_(array_size)
25 {
26 }
27 
29 {
30  if (type_.is_trivially_destructible()) {
31  return;
32  }
33  for (Item &item : items_) {
34  type_.destruct_n(item.start, item.length);
35  }
36 }
37 
38 void GVectorArray::append(const int64_t index, const void *value)
39 {
40  Item &item = items_[index];
41  if (item.length == item.capacity) {
42  this->realloc_to_at_least(item, item.capacity + 1);
43  }
44 
45  void *dst = POINTER_OFFSET(item.start, element_size_ * item.length);
46  type_.copy_to_uninitialized(value, dst);
47  item.length++;
48 }
49 
50 void GVectorArray::extend(const int64_t index, const GVArray &values)
51 {
52  BLI_assert(values.type() == type_);
53  for (const int i : IndexRange(values.size())) {
55  values.get(i, buffer);
56  this->append(index, buffer);
57  type_.destruct(buffer);
58  }
59 }
60 
61 void GVectorArray::extend(const int64_t index, const GSpan values)
62 {
63  GVArrayForGSpan varray{values};
64  this->extend(index, varray);
65 }
66 
68 {
69  for (const int i : mask) {
71  this->extend(i, array);
72  }
73 }
74 
76 {
77  GVVectorArrayForGVectorArray virtual_values{values};
78  this->extend(mask, virtual_values);
79 }
80 
82 {
83  Item &item = items_[index];
84  return GMutableSpan{type_, item.start, item.length};
85 }
86 
88 {
89  const Item &item = items_[index];
90  return GSpan{type_, item.start, item.length};
91 }
92 
93 void GVectorArray::realloc_to_at_least(Item &item, int64_t min_capacity)
94 {
95  const int64_t new_capacity = std::max(min_capacity, item.length * 2);
96 
97  void *new_buffer = allocator_.allocate(element_size_ * new_capacity, type_.alignment());
98  type_.relocate_to_initialized_n(item.start, new_buffer, item.length);
99 
100  item.start = new_buffer;
101  item.capacity = new_capacity;
102 }
103 
104 } // namespace blender::fn
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define POINTER_OFFSET(v, ofs)
#define BUFFER_FOR_CPP_TYPE_VALUE(type, variable_name)
Definition: FN_cpp_type.hh:676
_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 * allocate(const int64_t size, const int64_t alignment)
int64_t alignment() const
Definition: FN_cpp_type.hh:291
bool is_trivially_destructible() const
Definition: FN_cpp_type.hh:303
void destruct(void *ptr) const
Definition: FN_cpp_type.hh:358
void destruct_n(void *ptr, int64_t n) const
Definition: FN_cpp_type.hh:365
void copy_to_uninitialized(const void *src, void *dst) const
Definition: FN_cpp_type.hh:425
void relocate_to_initialized_n(void *src, void *dst, int64_t n) const
Definition: FN_cpp_type.hh:539
const CPPType & type() const
void get(const int64_t index, void *r_value) const
GMutableSpan operator[](int64_t index)
void extend(int64_t index, const GVArray &values)
void append(int64_t index, const void *value)
__kernel void ccl_constant KernelData ccl_global void ccl_global char ccl_global int ccl_global char ccl_global unsigned int ccl_global float * buffer
__int64 int64_t
Definition: stdint.h:92
float max
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)