Blender V5.3
BLI_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
27
28#include "BLI_allocator.hh"
29#include "BLI_index_range.hh"
30#include "BLI_memory_utils.hh"
31#include "BLI_span.hh"
32#include "BLI_utildefines.hh"
33
34namespace blender {
35
36template<
40 typename T,
44 int64_t InlineBufferCapacity = default_inline_buffer_capacity(sizeof(T)),
49 typename Allocator = GuardedAllocator>
50class Array {
51 public:
52 using value_type = T;
53 using pointer = T *;
54 using const_pointer = const T *;
55 using reference = T &;
56 using const_reference = const T &;
57 using iterator = T *;
58 using const_iterator = const T *;
60
61 private:
63 T *data_;
64
66 int64_t size_;
67
69 BLI_NO_UNIQUE_ADDRESS Allocator allocator_;
70
73
74 public:
78 Array(Allocator allocator = {}) noexcept : allocator_(allocator)
79 {
80 data_ = inline_buffer_;
81 size_ = 0;
82 }
83
84 Array(NoExceptConstructor, Allocator allocator = {}) noexcept : Array(allocator) {}
85
89 template<typename U>
90 Array(Span<U> values, Allocator allocator = {})
91 requires(std::is_convertible_v<U, T>)
93 {
94 const int64_t size = values.size();
95 data_ = this->get_buffer_for_size(size);
96 uninitialized_convert_n<U, T>(values.data(), size, data_);
97 size_ = size;
98 }
99
103 template<typename U>
104 Array(const std::initializer_list<U> &values, Allocator allocator = {})
105 requires(std::is_convertible_v<U, T>)
106 : Array(Span<U>(values), allocator)
107 {
108 }
109
110 Array(const std::initializer_list<T> &values, Allocator allocator = {})
111 : Array(Span<T>(values), allocator)
112 {
113 }
114
124 {
125 BLI_assert(size >= 0);
126 data_ = this->get_buffer_for_size(size);
128 size_ = size;
129 }
130
135 Array(int64_t size, const T &value, Allocator allocator = {})
137 {
138 BLI_assert(size >= 0);
139 if (std::is_trivially_copyable_v<T> && value_is_zero_memory(value)) {
140 data_ = this->get_buffer_for_size(size, true);
141 }
142 else {
143 data_ = this->get_buffer_for_size(size);
144#if defined(__GNUC__) && !defined(__clang__)
145# pragma GCC diagnostic push
146# pragma GCC diagnostic ignored "-Warray-bounds"
147#endif
148 uninitialized_fill_n(data_, size, value);
149#if defined(__GNUC__) && !defined(__clang__)
150# pragma GCC diagnostic pop
151#endif
152 }
153 size_ = size;
154 }
155
170 {
171 BLI_assert(size >= 0);
172 data_ = this->get_buffer_for_size(size);
173 size_ = size;
174 }
175
176 Array(const Array &other) : Array(other.as_span(), other.allocator_) {}
177
178 Array(Array &&other) noexcept(std::is_nothrow_move_constructible_v<T>)
179 : Array(NoExceptConstructor(), other.allocator_)
180 {
181 if (other.data_ == other.inline_buffer_) {
182#if defined(__GNUC__) && !defined(__clang__)
183# pragma GCC diagnostic push
184# pragma GCC diagnostic ignored "-Warray-bounds"
185#endif
186 uninitialized_relocate_n(other.data_, other.size_, data_);
187#if defined(__GNUC__) && !defined(__clang__)
188# pragma GCC diagnostic pop
189#endif
190 }
191 else {
192 data_ = other.data_;
193 }
194 size_ = other.size_;
195
196 other.data_ = other.inline_buffer_;
197 other.size_ = 0;
198 }
199
201 {
202 destruct_n(data_, size_);
203 this->deallocate_if_not_inline(data_);
204 }
205
206 Array &operator=(const Array &other)
207 {
208 return copy_assign_container(*this, other);
209 }
210
211 Array &operator=(Array &&other) noexcept(std::is_nothrow_move_constructible_v<T>)
212 {
213 return move_assign_container(*this, std::move(other));
214 }
215
217 {
218 BLI_assert(index >= 0);
219 BLI_assert(index < size_);
220 return data_[index];
221 }
222
223 const T &operator[](int64_t index) const
224 {
225 BLI_assert(index >= 0);
226 BLI_assert(index < size_);
227 return data_[index];
228 }
229
230 operator Span<T>() const
231 {
232 return Span<T>(data_, size_);
233 }
234
235 operator MutableSpan<T>()
236 {
237 return MutableSpan<T>(data_, size_);
238 }
239
240 template<typename U>
241 operator Span<U>() const
243 {
244 return Span<U>(data_, size_);
245 }
246
247 template<typename U>
248 operator MutableSpan<U>()
250 {
251 return MutableSpan<U>(data_, size_);
252 }
253
255 {
256 return *this;
257 }
258
260 {
261 return *this;
262 }
263
267 int64_t size() const
268 {
269 return size_;
270 }
271
275 bool is_empty() const
276 {
277 return size_ == 0;
278 }
279
283 void fill(const T &value) const
284 {
285 initialized_fill_n(data_, size_, value);
286 }
287
292 const T &first() const
293 {
294 BLI_assert(size_ > 0);
295 return *data_;
296 }
298 {
299 BLI_assert(size_ > 0);
300 return *data_;
301 }
302
307 const T &last(const int64_t n = 0) const
308 {
309 BLI_assert(n >= 0);
310 BLI_assert(n < size_);
311 return *(data_ + size_ - 1 - n);
312 }
313 T &last(const int64_t n = 0)
314 {
315 BLI_assert(n >= 0);
316 BLI_assert(n < size_);
317 return *(data_ + size_ - 1 - n);
318 }
319
323 const T *data() const
324 {
325 return data_;
326 }
328 {
329 return data_;
330 }
331
332 const T *begin() const
333 {
334 return data_;
335 }
336 const T *end() const
337 {
338 return data_ + size_;
339 }
340
342 {
343 return data_;
344 }
346 {
347 return data_ + size_;
348 }
349
350 std::reverse_iterator<T *> rbegin()
351 {
352 return std::reverse_iterator<T *>(this->end());
353 }
354 std::reverse_iterator<T *> rend()
355 {
356 return std::reverse_iterator<T *>(this->begin());
357 }
358
359 std::reverse_iterator<const T *> rbegin() const
360 {
361 return std::reverse_iterator<T *>(this->end());
362 }
363 std::reverse_iterator<const T *> rend() const
364 {
365 return std::reverse_iterator<T *>(this->begin());
366 }
367
372 {
373 return IndexRange(size_);
374 }
375
377 {
378 return this->as_span().hash();
379 }
380
381 static uint64_t hash_as(const Span<T> values)
382 {
383 return values.hash();
384 }
385
386 friend bool operator==(const Array &a, const Array &b)
387 {
388 return a.as_span() == b.as_span();
389 }
390
391 friend bool operator!=(const Array &a, const Array &b)
392 {
393 return !(a == b);
394 }
395
401 {
402 size_ = 0;
403 }
404
408 Allocator &allocator()
409 {
410 return allocator_;
411 }
412 const Allocator &allocator() const
413 {
414 return allocator_;
415 }
416
422 {
423 return InlineBufferCapacity;
424 }
425
430 void reinitialize(const int64_t new_size)
431 {
432 BLI_assert(new_size >= 0);
433 int64_t old_size = size_;
434
435 destruct_n(data_, size_);
436 size_ = 0;
437
438 if (new_size <= old_size) {
439 default_construct_n(data_, new_size);
440 }
441 else {
442 T *new_data = this->get_buffer_for_size(new_size, false);
443 try {
444 default_construct_n(new_data, new_size);
445 }
446 catch (...) {
447 this->deallocate_if_not_inline(new_data);
448 throw;
449 }
450 this->deallocate_if_not_inline(data_);
451 data_ = new_data;
452 }
453
454 size_ = new_size;
455 }
456
457 private:
458 T *get_buffer_for_size(int64_t size, const bool zero = false)
459 {
460 if (size <= InlineBufferCapacity) {
461 if (zero) {
462 if constexpr (InlineBufferCapacity > 0) {
463 memset(static_cast<void *>(inline_buffer_), 0, size * sizeof(T));
464 }
465 }
466 return inline_buffer_;
467 }
468 return this->allocate(size, zero);
469 }
470
471 T *allocate(int64_t size, const bool zero)
472 {
473 if (zero) {
474 return static_cast<T *>(allocator_.allocate_zero(size_t(size) * sizeof(T), alignof(T), AT));
475 }
476 return static_cast<T *>(allocator_.allocate(size_t(size) * sizeof(T), alignof(T), AT));
477 }
478
479 void deallocate_if_not_inline(T *ptr)
480 {
481 if (ptr != inline_buffer_) {
482 allocator_.deallocate(ptr);
483 }
484 }
485};
486
487} // namespace blender
#define AT
#define BLI_NO_UNIQUE_ADDRESS
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
int64_t size() const
Definition BLI_array.hh:267
Array(Array &&other) noexcept(std::is_nothrow_move_constructible_v< T >)
Definition BLI_array.hh:178
Array(NoExceptConstructor, Allocator allocator={}) noexcept
Definition BLI_array.hh:84
std::reverse_iterator< T * > rbegin()
Definition BLI_array.hh:350
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
friend bool operator!=(const Array &a, const Array &b)
Definition BLI_array.hh:391
const T * end() const
Definition BLI_array.hh:336
const T & operator[](int64_t index) const
Definition BLI_array.hh:223
Array(const std::initializer_list< U > &values, Allocator allocator={})
Definition BLI_array.hh:104
const T & last(const int64_t n=0) const
Definition BLI_array.hh:307
Array(Allocator allocator={}) noexcept
Definition BLI_array.hh:78
static int64_t inline_buffer_capacity()
Definition BLI_array.hh:421
T & operator[](int64_t index)
Definition BLI_array.hh:216
const T * data() const
Definition BLI_array.hh:323
Array(int64_t size, const T &value, Allocator allocator={})
Definition BLI_array.hh:135
std::reverse_iterator< T * > rend()
Definition BLI_array.hh:354
static uint64_t hash_as(const Span< T > values)
Definition BLI_array.hh:381
IndexRange index_range() const
Definition BLI_array.hh:371
const Allocator & allocator() const
Definition BLI_array.hh:412
std::reverse_iterator< const T * > rend() const
Definition BLI_array.hh:363
T & last(const int64_t n=0)
Definition BLI_array.hh:313
int64_t size_type
Definition BLI_array.hh:59
std::reverse_iterator< const T * > rbegin() const
Definition BLI_array.hh:359
const T & first() const
Definition BLI_array.hh:292
Array(int64_t size, NoInitialization, Allocator allocator={})
Definition BLI_array.hh:168
void fill(const T &value) const
Definition BLI_array.hh:283
friend bool operator==(const Array &a, const Array &b)
Definition BLI_array.hh:386
const T & const_reference
Definition BLI_array.hh:56
Array(Span< U > values, Allocator allocator={})
Definition BLI_array.hh:90
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
const T * const_iterator
Definition BLI_array.hh:58
const T * begin() const
Definition BLI_array.hh:332
const T * const_pointer
Definition BLI_array.hh:54
Array & operator=(const Array &other)
Definition BLI_array.hh:206
void clear_without_destruct()
Definition BLI_array.hh:400
Array(int64_t size, Allocator allocator={})
Definition BLI_array.hh:123
Array & operator=(Array &&other) noexcept(std::is_nothrow_move_constructible_v< T >)
Definition BLI_array.hh:211
uint64_t hash() const
Definition BLI_array.hh:376
Array(const Array &other)
Definition BLI_array.hh:176
Array(const std::initializer_list< T > &values, Allocator allocator={})
Definition BLI_array.hh:110
bool is_empty() const
Definition BLI_array.hh:275
constexpr IndexRange()=default
constexpr MutableSpan()=default
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr Span()=default
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr uint64_t hash() const
Definition BLI_span.hh:410
#define T
BMesh Iterators.
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
void default_construct_n(T *ptr, int64_t n)
constexpr bool value_is_zero_memory(const VecBase< T, Size > &v)
void initialized_fill_n(T *dst, int64_t n, const T &value)
Container & copy_assign_container(Container &dst, const Container &src)
Container & move_assign_container(Container &dst, Container &&src) noexcept(std::is_nothrow_move_constructible_v< Container >)
constexpr int64_t default_inline_buffer_capacity(size_t element_size)
void uninitialized_fill_n(T *dst, int64_t n, const T &value)
void uninitialized_convert_n(const From *src, int64_t n, To *dst)
void destruct_n(T *ptr, int64_t n)
constexpr bool is_span_convertible_pointer_v