Blender  V2.93
FN_generic_vector_array.hh
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 
17 #pragma once
18 
27 #include "BLI_array.hh"
28 #include "BLI_linear_allocator.hh"
29 
31 
32 namespace blender::fn {
33 
34 /* An array of vectors containing elements of a generic type. */
36  private:
37  struct Item {
38  void *start = nullptr;
39  int64_t length = 0;
40  int64_t capacity = 0;
41  };
42 
43  /* Use a linear allocator to pack many small vectors together. Currently, memory from reallocated
44  * vectors is not reused. This can be improved in the future. */
45  LinearAllocator<> allocator_;
46  /* The data type of individual elements. */
47  const CPPType &type_;
48  /* The size of an individual element. This is inlined from `type_.size()` for easier access. */
49  const int64_t element_size_;
50  /* The individual vectors. */
51  Array<Item> items_;
52 
53  public:
54  GVectorArray() = delete;
55 
56  GVectorArray(const CPPType &type, int64_t array_size);
57 
58  ~GVectorArray();
59 
60  int64_t size() const
61  {
62  return items_.size();
63  }
64 
65  bool is_empty() const
66  {
67  return items_.is_empty();
68  }
69 
70  const CPPType &type() const
71  {
72  return type_;
73  }
74 
75  void append(int64_t index, const void *value);
76 
77  /* Add multiple elements to a single vector. */
78  void extend(int64_t index, const GVArray &values);
79  void extend(int64_t index, GSpan values);
80 
81  /* Add multiple elements to multiple vectors. */
82  void extend(IndexMask mask, const GVVectorArray &values);
83  void extend(IndexMask mask, const GVectorArray &values);
84 
86  GSpan operator[](int64_t index) const;
87 
88  private:
89  void realloc_to_at_least(Item &item, int64_t min_capacity);
90 };
91 
92 /* A non-owning typed mutable reference to an `GVectorArray`. It simplifies access when the type of
93  * the data is known at compile time. */
94 template<typename T> class GVectorArray_TypedMutableRef {
95  private:
96  GVectorArray *vector_array_;
97 
98  public:
99  GVectorArray_TypedMutableRef(GVectorArray &vector_array) : vector_array_(&vector_array)
100  {
101  BLI_assert(vector_array_->type().is<T>());
102  }
103 
104  int64_t size() const
105  {
106  return vector_array_->size();
107  }
108 
109  bool is_empty() const
110  {
111  return vector_array_->is_empty();
112  }
113 
114  void append(const int64_t index, const T &value)
115  {
116  vector_array_->append(index, &value);
117  }
118 
119  void extend(const int64_t index, const Span<T> values)
120  {
121  vector_array_->extend(index, values);
122  }
123 
124  void extend(const int64_t index, const VArray<T> &values)
125  {
126  GVArrayForVArray<T> array{values};
127  this->extend(index, array);
128  }
129 
131  {
132  return (*vector_array_)[index].typed<T>();
133  }
134 };
135 
136 /* A generic virtual vector array implementation for a `GVectorArray`. */
138  private:
139  const GVectorArray &vector_array_;
140 
141  public:
143  : GVVectorArray(vector_array.type(), vector_array.size()), vector_array_(vector_array)
144  {
145  }
146 
147  protected:
148  int64_t get_vector_size_impl(const int64_t index) const override
149  {
150  return vector_array_[index].size();
151  }
152 
154  const int64_t index_in_vector,
155  void *r_value) const override
156  {
157  type_->copy_to_initialized(vector_array_[index][index_in_vector], r_value);
158  }
159 };
160 
161 } // namespace blender::fn
#define BLI_assert(a)
Definition: BLI_assert.h:58
SIMD_FORCE_INLINE btScalar length(const btQuaternion &q)
Return the length of a quaternion.
Definition: btQuaternion.h:895
int64_t size() const
Definition: BLI_array.hh:258
bool is_empty() const
Definition: BLI_array.hh:266
void copy_to_initialized(const void *src, void *dst) const
Definition: FN_cpp_type.hh:390
void get_vector_element_impl(const int64_t index, const int64_t index_in_vector, void *r_value) const override
GVVectorArrayForGVectorArray(const GVectorArray &vector_array)
int64_t get_vector_size_impl(const int64_t index) const override
void extend(const int64_t index, const Span< T > values)
MutableSpan< T > operator[](const int64_t index)
void append(const int64_t index, const T &value)
void extend(const int64_t index, const VArray< T > &values)
GVectorArray_TypedMutableRef(GVectorArray &vector_array)
GMutableSpan operator[](int64_t index)
const CPPType & type() const
void extend(int64_t index, const GVArray &values)
void append(int64_t index, const void *value)
#define T
__int64 int64_t
Definition: stdint.h:92
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)