Blender  V3.3
BLI_generic_vector_array.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
13 #include "BLI_array.hh"
15 #include "BLI_linear_allocator.hh"
16 
17 namespace blender {
18 
19 /* An array of vectors containing elements of a generic type. */
21  private:
22  struct Item {
23  void *start = nullptr;
24  int64_t length = 0;
25  int64_t capacity = 0;
26  };
27 
28  /* Use a linear allocator to pack many small vectors together. Currently, memory from reallocated
29  * vectors is not reused. This can be improved in the future. */
30  LinearAllocator<> allocator_;
31  /* The data type of individual elements. */
32  const CPPType &type_;
33  /* The size of an individual element. This is inlined from `type_.size()` for easier access. */
34  const int64_t element_size_;
35  /* The individual vectors. */
36  Array<Item> items_;
37 
38  public:
39  GVectorArray() = delete;
40 
41  GVectorArray(const CPPType &type, int64_t array_size);
42 
43  ~GVectorArray();
44 
45  int64_t size() const
46  {
47  return items_.size();
48  }
49 
50  bool is_empty() const
51  {
52  return items_.is_empty();
53  }
54 
55  const CPPType &type() const
56  {
57  return type_;
58  }
59 
60  void append(int64_t index, const void *value);
61 
62  /* Add multiple elements to a single vector. */
63  void extend(int64_t index, const GVArray &values);
64  void extend(int64_t index, GSpan values);
65 
66  /* Add multiple elements to multiple vectors. */
67  void extend(IndexMask mask, const GVVectorArray &values);
68  void extend(IndexMask mask, const GVectorArray &values);
69 
70  void clear(IndexMask mask);
71 
73  GSpan operator[](int64_t index) const;
74 
75  private:
76  void realloc_to_at_least(Item &item, int64_t min_capacity);
77 };
78 
79 /* A non-owning typed mutable reference to an `GVectorArray`. It simplifies access when the type of
80  * the data is known at compile time. */
81 template<typename T> class GVectorArray_TypedMutableRef {
82  private:
83  GVectorArray *vector_array_;
84 
85  public:
86  GVectorArray_TypedMutableRef(GVectorArray &vector_array) : vector_array_(&vector_array)
87  {
88  BLI_assert(vector_array_->type().is<T>());
89  }
90 
91  int64_t size() const
92  {
93  return vector_array_->size();
94  }
95 
96  bool is_empty() const
97  {
98  return vector_array_->is_empty();
99  }
100 
101  void append(const int64_t index, const T &value)
102  {
103  vector_array_->append(index, &value);
104  }
105 
106  void extend(const int64_t index, const Span<T> values)
107  {
108  vector_array_->extend(index, values);
109  }
110 
111  void extend(const int64_t index, const VArray<T> &values)
112  {
113  vector_array_->extend(index, values);
114  }
115 
117  {
118  return (*vector_array_)[index].typed<T>();
119  }
120 };
121 
122 /* A generic virtual vector array implementation for a `GVectorArray`. */
124  private:
125  const GVectorArray &vector_array_;
126 
127  public:
129  : GVVectorArray(vector_array.type(), vector_array.size()), vector_array_(vector_array)
130  {
131  }
132 
133  protected:
134  int64_t get_vector_size_impl(const int64_t index) const override
135  {
136  return vector_array_[index].size();
137  }
138 
140  const int64_t index_in_vector,
141  void *r_value) const override
142  {
143  type_->copy_assign(vector_array_[index][index_in_vector], r_value);
144  }
145 };
146 
147 } // namespace blender
#define BLI_assert(a)
Definition: BLI_assert.h:46
int64_t size() const
Definition: BLI_array.hh:244
bool is_empty() const
Definition: BLI_array.hh:252
bool is() const
void copy_assign(const void *src, void *dst) const
void get_vector_element_impl(const int64_t index, const int64_t index_in_vector, void *r_value) const override
GVVectorArray_For_GVectorArray(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)
void append(const int64_t index, const T &value)
void extend(const int64_t index, const VArray< T > &values)
MutableSpan< T > operator[](const int64_t index)
GVectorArray_TypedMutableRef(GVectorArray &vector_array)
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)
const CPPType & type() const
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
Definition: math_float4.h:513
#define T
T length(const vec_base< T, Size > &a)
__int64 int64_t
Definition: stdint.h:89