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