Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/BLI_generic_array.hh
| Show First 20 Lines • Show All 167 Lines • ▼ Show 20 Lines | public: | ||||
| } | } | ||||
| void *operator[](int64_t index) | void *operator[](int64_t index) | ||||
| { | { | ||||
| BLI_assert(index < size_); | BLI_assert(index < size_); | ||||
| return POINTER_OFFSET(data_, type_->size() * index); | return POINTER_OFFSET(data_, type_->size() * index); | ||||
| } | } | ||||
| template<typename T> const T &first() const | |||||
| { | |||||
| BLI_assert(size_ > 0); | |||||
| return *(T *)data_; | |||||
| } | |||||
| template<typename T> T &last(const int64_t n = 0) const | |||||
| { | |||||
| BLI_assert(n >= 0); | |||||
| BLI_assert(n < size_); | |||||
| return *(T *)POINTER_OFFSET(data_, type_->size() * (size_ - 1 - n)); | |||||
| } | |||||
| void *last(const int64_t n = 0) const | |||||
| { | |||||
| BLI_assert(n >= 0); | |||||
| BLI_assert(n < size_); | |||||
| return POINTER_OFFSET(data_, type_->size() * size_ - 1 - n); | |||||
| } | |||||
| operator GSpan() const | operator GSpan() const | ||||
| { | { | ||||
| BLI_assert(type_ != nullptr); | BLI_assert(type_ != nullptr); | ||||
| return GSpan(*type_, data_, size_); | return GSpan(*type_, data_, size_); | ||||
| } | } | ||||
| operator GMutableSpan() | operator GMutableSpan() | ||||
| { | { | ||||
| BLI_assert(type_ != nullptr); | BLI_assert(type_ != nullptr); | ||||
| return GMutableSpan(*type_, data_, size_); | return GMutableSpan(*type_, data_, size_); | ||||
| } | } | ||||
| GSpan as_span() const | GSpan as_span() const | ||||
| { | { | ||||
| return *this; | return *this; | ||||
| } | } | ||||
| GMutableSpan as_mutable_span() | GMutableSpan as_mutable_span() | ||||
| { | { | ||||
| return *this; | return *this; | ||||
| } | } | ||||
| /** | /** | ||||
| * Create Array with same type. If type is different behavior is undefined. | |||||
| * Values are transferred without being copied. The original GArray is reset. | |||||
| */ | |||||
| template<typename T> Array<T> typed() | |||||
| { | |||||
| BLI_assert(type_->is<T>()); | |||||
| Array<T> typed_array(size_, (T *)data_, true); | |||||
| data_ = this->allocate(0); | |||||
| size_ = 0; | |||||
| return typed_array; | |||||
| } | |||||
| template<typename T> operator Array<T>() const | |||||
| { | |||||
| return this->typed<T>(); | |||||
| } | |||||
| /** | |||||
| * Access the allocator used by this array. | * Access the allocator used by this array. | ||||
| */ | */ | ||||
| Allocator &allocator() | Allocator &allocator() | ||||
| { | { | ||||
| return allocator_; | return allocator_; | ||||
| } | } | ||||
| const Allocator &allocator() const | const Allocator &allocator() const | ||||
| { | { | ||||
| return allocator_; | return allocator_; | ||||
| } | } | ||||
| IndexRange index_range() | |||||
| { | |||||
| return IndexRange(size_); | |||||
| } | |||||
| /** | |||||
| * Copies the given value to every element in the array. | |||||
| */ | |||||
| template<typename T> void fill(const T &value) const | |||||
| { | |||||
| BLI_assert(type_->is<T>()); | |||||
| initialized_fill_n((T *)data_, size_, value); | |||||
| } | |||||
| void fill(const void *value, const CPPType &type) const | |||||
| { | |||||
| BLI_assert(type_ == &type); | |||||
| const int64_t sizeof_ = type.size(); | |||||
| for (const int64_t index : IndexRange(size_)) { | |||||
| memcpy(POINTER_OFFSET(data_, sizeof_ * index), value, sizeof_); | |||||
| } | |||||
HooglyBoogly: This doesn't work when the type is nontrivial. The `CPPType` methods should be used instead. | |||||
| } | |||||
| /** | /** | ||||
| * Destruct values and create a new array of the given size. The values in the new array are | * Destruct values and create a new array of the given size. The values in the new array are | ||||
| * default constructed. | * default constructed. | ||||
| */ | */ | ||||
| void reinitialize(const int64_t new_size) | void reinitialize(const int64_t new_size) | ||||
| { | { | ||||
| BLI_assert(new_size >= 0); | BLI_assert(new_size >= 0); | ||||
| int64_t old_size = size_; | int64_t old_size = size_; | ||||
| Show All 38 Lines | |||||
This doesn't work when the type is nontrivial. The CPPType methods should be used instead.
Passing the type to this method is redundant, just the assertion is fine.