2019-09-12 14:23:21 +02:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2020-04-21 17:31:56 +02:00
|
|
|
#ifndef __BLI_VECTOR_HH__
|
|
|
|
#define __BLI_VECTOR_HH__
|
2019-09-13 21:12:26 +10:00
|
|
|
|
2019-09-12 14:23:21 +02:00
|
|
|
/** \file
|
|
|
|
* \ingroup bli
|
|
|
|
*
|
2020-06-09 10:27:24 +02:00
|
|
|
* A `blender::Vector<T>` is a dynamically growing contiguous array for values of type T. It is
|
2020-06-09 10:10:56 +02:00
|
|
|
* designed to be a more convenient and efficient replacement for `std::vector`. Note that the term
|
|
|
|
* "vector" has nothing to do with a vector from computer graphics here.
|
|
|
|
*
|
|
|
|
* A vector supports efficient insertion and removal at the end (O(1) amortized). Removal in other
|
|
|
|
* places takes O(n) time, because all elements afterwards have to be moved. If the order of
|
|
|
|
* elements is not important, `remove_and_reorder` can be used instead of `remove` for better
|
|
|
|
* performance.
|
|
|
|
*
|
|
|
|
* The improved efficiency is mainly achieved by supporting small buffer optimization. As long as
|
|
|
|
* the number of elements in the vector does not become larger than InlineBufferCapacity, no memory
|
2020-06-09 10:27:24 +02:00
|
|
|
* allocation is done. As a consequence, iterators are invalidated when a blender::Vector is moved
|
2020-06-09 10:10:56 +02:00
|
|
|
* (iterators of std::vector remain valid when the vector is moved).
|
|
|
|
*
|
2020-06-09 10:27:24 +02:00
|
|
|
* `blender::Vector` should be your default choice for a vector data structure in Blender.
|
2019-09-12 14:23:21 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
|
|
|
|
2020-04-21 17:31:56 +02:00
|
|
|
#include "BLI_allocator.hh"
|
|
|
|
#include "BLI_index_range.hh"
|
|
|
|
#include "BLI_listbase_wrapper.hh"
|
2019-09-12 14:23:21 +02:00
|
|
|
#include "BLI_math_base.h"
|
2020-04-21 17:31:56 +02:00
|
|
|
#include "BLI_memory_utils.hh"
|
2020-06-09 11:58:47 +02:00
|
|
|
#include "BLI_span.hh"
|
2020-06-09 10:10:56 +02:00
|
|
|
#include "BLI_string.h"
|
|
|
|
#include "BLI_string_ref.hh"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2019-09-12 14:23:21 +02:00
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
2020-06-09 10:27:24 +02:00
|
|
|
namespace blender {
|
2019-09-12 14:23:21 +02:00
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
template<
|
|
|
|
/**
|
|
|
|
* Type of the values stored in this vector. It has to be movable.
|
|
|
|
*/
|
|
|
|
typename T,
|
|
|
|
/**
|
|
|
|
* The number of values that can be stored in this vector, without doing a heap allocation.
|
|
|
|
* Sometimes it makes sense to increase this value a lot. The memory in the inline buffer is
|
|
|
|
* not initialized when it is not needed.
|
|
|
|
*
|
|
|
|
* When T is large, the small buffer optimization is disabled by default to avoid large
|
2020-06-13 12:50:07 +10:00
|
|
|
* unexpected allocations on the stack. It can still be enabled explicitly though.
|
2020-06-09 10:10:56 +02:00
|
|
|
*/
|
|
|
|
uint InlineBufferCapacity = (sizeof(T) < 100) ? 4 : 0,
|
|
|
|
/**
|
|
|
|
* The allocator used by this vector. Should rarely be changed, except when you don't want that
|
|
|
|
* MEM_* is used internally.
|
|
|
|
*/
|
|
|
|
typename Allocator = GuardedAllocator>
|
2020-04-23 20:05:53 +02:00
|
|
|
class Vector {
|
2019-09-12 14:23:21 +02:00
|
|
|
private:
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
2020-06-13 12:50:07 +10:00
|
|
|
* Use pointers instead of storing the size explicitly. This reduces the number of instructions
|
2020-06-09 10:10:56 +02:00
|
|
|
* in `append`.
|
|
|
|
*
|
|
|
|
* The pointers might point to the memory in the inline buffer.
|
|
|
|
*/
|
2020-07-03 14:15:05 +02:00
|
|
|
T *begin_;
|
|
|
|
T *end_;
|
|
|
|
T *capacity_end_;
|
2020-06-09 10:10:56 +02:00
|
|
|
|
|
|
|
/** Used for allocations when the inline buffer is too small. */
|
2020-07-03 14:15:05 +02:00
|
|
|
Allocator allocator_;
|
2019-09-12 14:23:21 +02:00
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/** A placeholder buffer that will remain uninitialized until it is used. */
|
2020-07-05 15:08:26 +02:00
|
|
|
AlignedBuffer<(uint)sizeof(T) * InlineBufferCapacity, (uint)alignof(T)> inline_buffer_;
|
2020-06-09 10:10:56 +02:00
|
|
|
|
|
|
|
/**
|
2020-06-13 12:50:07 +10:00
|
|
|
* Store the size of the vector explicitly in debug builds. Otherwise you'd always have to call
|
2020-06-09 10:10:56 +02:00
|
|
|
* the `size` function or do the math to compute it from the pointers manually. This is rather
|
|
|
|
* annoying. Knowing the size of a vector is often quite essential when debugging some code.
|
|
|
|
*/
|
2019-09-20 23:39:13 +02:00
|
|
|
#ifndef NDEBUG
|
2020-07-03 14:15:05 +02:00
|
|
|
uint debug_size_;
|
|
|
|
# define UPDATE_VECTOR_SIZE(ptr) (ptr)->debug_size_ = (uint)((ptr)->end_ - (ptr)->begin_)
|
2019-09-12 14:23:21 +02:00
|
|
|
#else
|
|
|
|
# define UPDATE_VECTOR_SIZE(ptr) ((void)0)
|
|
|
|
#endif
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
2020-06-30 20:54:31 +10:00
|
|
|
* Be a friend with other vector instantiations. This is necessary to implement some memory
|
2020-06-09 10:10:56 +02:00
|
|
|
* management logic.
|
|
|
|
*/
|
|
|
|
template<typename OtherT, uint OtherInlineBufferCapacity, typename OtherAllocator>
|
|
|
|
friend class Vector;
|
2019-09-12 14:23:21 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Create an empty vector.
|
|
|
|
* This does not do any memory allocation.
|
|
|
|
*/
|
|
|
|
Vector()
|
|
|
|
{
|
2020-07-05 15:08:26 +02:00
|
|
|
begin_ = this->inline_buffer();
|
2020-07-03 14:15:05 +02:00
|
|
|
end_ = begin_;
|
|
|
|
capacity_end_ = begin_ + InlineBufferCapacity;
|
2019-09-12 14:23:21 +02:00
|
|
|
UPDATE_VECTOR_SIZE(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a vector with a specific size.
|
2020-06-09 10:10:56 +02:00
|
|
|
* The elements will be default constructed.
|
|
|
|
* If T is trivially constructible, the elements in the vector are not touched.
|
2019-09-12 14:23:21 +02:00
|
|
|
*/
|
|
|
|
explicit Vector(uint size) : Vector()
|
|
|
|
{
|
2020-06-09 10:10:56 +02:00
|
|
|
this->resize(size);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a vector filled with a specific value.
|
|
|
|
*/
|
|
|
|
Vector(uint size, const T &value) : Vector()
|
|
|
|
{
|
|
|
|
this->reserve(size);
|
2020-06-09 10:10:56 +02:00
|
|
|
this->increase_size_by_unchecked(size);
|
2020-07-03 14:15:05 +02:00
|
|
|
blender::uninitialized_fill_n(begin_, size, value);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-25 23:13:02 +10:00
|
|
|
* Create a vector that contains copies of the values in the initialized list.
|
2020-06-09 10:10:56 +02:00
|
|
|
*
|
|
|
|
* This allows you to write code like:
|
|
|
|
* Vector<int> vec = {3, 4, 5};
|
2019-09-12 14:23:21 +02:00
|
|
|
*/
|
2020-06-09 11:58:47 +02:00
|
|
|
Vector(const std::initializer_list<T> &values) : Vector(Span<T>(values))
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-09 10:10:56 +02:00
|
|
|
* Create a vector from an array ref. The values in the vector are copy constructed.
|
2019-09-12 14:23:21 +02:00
|
|
|
*/
|
2020-06-09 11:58:47 +02:00
|
|
|
Vector(Span<T> values) : Vector()
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
2020-07-03 14:52:51 +02:00
|
|
|
const uint size = values.size();
|
2020-06-09 10:10:56 +02:00
|
|
|
this->reserve(size);
|
|
|
|
this->increase_size_by_unchecked(size);
|
2020-07-03 14:15:05 +02:00
|
|
|
blender::uninitialized_copy_n(values.data(), size, begin_);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a vector from any container. It must be possible to use the container in a range-for
|
|
|
|
* loop.
|
|
|
|
*/
|
|
|
|
template<typename ContainerT> static Vector FromContainer(const ContainerT &container)
|
|
|
|
{
|
|
|
|
Vector vector;
|
|
|
|
for (const auto &value : container) {
|
|
|
|
vector.append(value);
|
|
|
|
}
|
|
|
|
return vector;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-09 10:10:56 +02:00
|
|
|
* Create a vector from a ListBase. The caller has to make sure that the values in the linked
|
|
|
|
* list have the correct type.
|
|
|
|
*
|
|
|
|
* Example Usage:
|
|
|
|
* Vector<ModifierData *> modifiers(ob->modifiers);
|
2019-09-12 14:23:21 +02:00
|
|
|
*/
|
2020-04-21 17:38:19 +02:00
|
|
|
Vector(ListBase &values) : Vector()
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
2020-06-09 10:10:56 +02:00
|
|
|
LISTBASE_FOREACH (T, value, &values) {
|
2020-04-21 17:38:19 +02:00
|
|
|
this->append(value);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-09 10:10:56 +02:00
|
|
|
* Create a copy of another vector. The other vector will not be changed. If the other vector has
|
|
|
|
* less than InlineBufferCapacity elements, no allocation will be made.
|
2019-09-12 14:23:21 +02:00
|
|
|
*/
|
2020-07-03 14:15:05 +02:00
|
|
|
Vector(const Vector &other) : allocator_(other.allocator_)
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
|
|
|
this->init_copy_from_other_vector(other);
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Create a copy of a vector with a different InlineBufferCapacity. This needs to be handled
|
|
|
|
* separately, so that the other one is a valid copy constructor.
|
|
|
|
*/
|
|
|
|
template<uint OtherInlineBufferCapacity>
|
|
|
|
Vector(const Vector<T, OtherInlineBufferCapacity, Allocator> &other)
|
2020-07-03 14:15:05 +02:00
|
|
|
: allocator_(other.allocator_)
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
|
|
|
this->init_copy_from_other_vector(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-09 10:10:56 +02:00
|
|
|
* Steal the elements from another vector. This does not do an allocation. The other vector will
|
|
|
|
* have zero elements afterwards.
|
2019-09-12 14:23:21 +02:00
|
|
|
*/
|
2020-06-09 10:10:56 +02:00
|
|
|
template<uint OtherInlineBufferCapacity>
|
|
|
|
Vector(Vector<T, OtherInlineBufferCapacity, Allocator> &&other) noexcept
|
2020-07-03 14:15:05 +02:00
|
|
|
: allocator_(other.allocator_)
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
2020-07-03 14:52:51 +02:00
|
|
|
const uint size = other.size();
|
2019-09-12 14:23:21 +02:00
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
if (other.is_inline()) {
|
2020-04-23 20:05:53 +02:00
|
|
|
if (size <= InlineBufferCapacity) {
|
2019-09-12 14:23:21 +02:00
|
|
|
/* Copy between inline buffers. */
|
2020-07-05 15:08:26 +02:00
|
|
|
begin_ = this->inline_buffer();
|
2020-07-03 14:15:05 +02:00
|
|
|
end_ = begin_ + size;
|
|
|
|
capacity_end_ = begin_ + InlineBufferCapacity;
|
|
|
|
uninitialized_relocate_n(other.begin_, size, begin_);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Copy from inline buffer to newly allocated buffer. */
|
2020-07-03 14:52:51 +02:00
|
|
|
const uint capacity = size;
|
2020-07-03 14:15:05 +02:00
|
|
|
begin_ = (T *)allocator_.allocate(sizeof(T) * capacity, alignof(T), AT);
|
|
|
|
end_ = begin_ + size;
|
|
|
|
capacity_end_ = begin_ + capacity;
|
|
|
|
uninitialized_relocate_n(other.begin_, size, begin_);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Steal the pointer. */
|
2020-07-03 14:15:05 +02:00
|
|
|
begin_ = other.begin_;
|
|
|
|
end_ = other.end_;
|
|
|
|
capacity_end_ = other.capacity_end_;
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
2020-07-05 15:08:26 +02:00
|
|
|
other.begin_ = other.inline_buffer();
|
2020-07-03 14:15:05 +02:00
|
|
|
other.end_ = other.begin_;
|
|
|
|
other.capacity_end_ = other.begin_ + OtherInlineBufferCapacity;
|
2019-09-12 14:23:21 +02:00
|
|
|
UPDATE_VECTOR_SIZE(this);
|
|
|
|
UPDATE_VECTOR_SIZE(&other);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Vector()
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
destruct_n(begin_, this->size());
|
2020-06-09 10:10:56 +02:00
|
|
|
if (!this->is_inline()) {
|
2020-07-03 14:15:05 +02:00
|
|
|
allocator_.deallocate(begin_);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector &operator=(const Vector &other)
|
|
|
|
{
|
|
|
|
if (this == &other) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->~Vector();
|
|
|
|
new (this) Vector(other);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector &operator=(Vector &&other)
|
|
|
|
{
|
|
|
|
if (this == &other) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/* This can be incorrect, when the vector is used to build a recursive data structure. However,
|
|
|
|
we don't take care of it at this low level. See https://youtu.be/7Qgd9B1KuMQ?t=840. */
|
2019-09-12 14:23:21 +02:00
|
|
|
this->~Vector();
|
|
|
|
new (this) Vector(std::move(other));
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-07-03 14:31:26 +02:00
|
|
|
/**
|
|
|
|
* Get the value at the given index. This invokes undefined behavior when the index is out of
|
|
|
|
* bounds.
|
|
|
|
*/
|
|
|
|
const T &operator[](uint index) const
|
|
|
|
{
|
|
|
|
BLI_assert(index < this->size());
|
|
|
|
return begin_[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
T &operator[](uint index)
|
|
|
|
{
|
|
|
|
BLI_assert(index < this->size());
|
|
|
|
return begin_[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
operator Span<T>() const
|
|
|
|
{
|
|
|
|
return Span<T>(begin_, this->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
operator MutableSpan<T>()
|
|
|
|
{
|
|
|
|
return MutableSpan<T>(begin_, this->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
Span<T> as_span() const
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
MutableSpan<T> as_mutable_span()
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-09-12 14:23:21 +02:00
|
|
|
/**
|
2020-06-09 10:10:56 +02:00
|
|
|
* Make sure that enough memory is allocated to hold min_capacity elements.
|
|
|
|
* This won't necessarily make an allocation when min_capacity is small.
|
2019-09-12 14:23:21 +02:00
|
|
|
* The actual size of the vector does not change.
|
|
|
|
*/
|
2020-07-03 14:52:51 +02:00
|
|
|
void reserve(const uint min_capacity)
|
2020-06-09 10:10:56 +02:00
|
|
|
{
|
|
|
|
if (min_capacity > this->capacity()) {
|
|
|
|
this->realloc_to_at_least(min_capacity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the size of the vector so that it contains new_size elements.
|
|
|
|
* If new_size is smaller than the old size, the elements at the end of the vector are
|
|
|
|
* destructed. If new_size is larger than the old size, the new elements at the end are default
|
|
|
|
* constructed. If T is trivially constructible, the memory is not touched by this function.
|
|
|
|
*/
|
2020-07-03 14:52:51 +02:00
|
|
|
void resize(const uint new_size)
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
2020-07-03 14:52:51 +02:00
|
|
|
const uint old_size = this->size();
|
2020-06-09 10:10:56 +02:00
|
|
|
if (new_size > old_size) {
|
|
|
|
this->reserve(new_size);
|
2020-07-03 14:15:05 +02:00
|
|
|
default_construct_n(begin_ + old_size, new_size - old_size);
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-07-03 14:15:05 +02:00
|
|
|
destruct_n(begin_ + new_size, old_size - new_size);
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
2020-07-03 14:15:05 +02:00
|
|
|
end_ = begin_ + new_size;
|
2020-06-09 10:10:56 +02:00
|
|
|
UPDATE_VECTOR_SIZE(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the size of the vector so that it contains new_size elements.
|
|
|
|
* If new_size is smaller than the old size, the elements at the end of the vector are
|
|
|
|
* destructed. If new_size is larger than the old size, the new elements will be copy constructed
|
|
|
|
* from the given value.
|
|
|
|
*/
|
2020-07-03 14:52:51 +02:00
|
|
|
void resize(const uint new_size, const T &value)
|
2020-06-09 10:10:56 +02:00
|
|
|
{
|
2020-07-03 14:52:51 +02:00
|
|
|
const uint old_size = this->size();
|
2020-06-09 10:10:56 +02:00
|
|
|
if (new_size > old_size) {
|
|
|
|
this->reserve(new_size);
|
2020-07-03 14:15:05 +02:00
|
|
|
uninitialized_fill_n(begin_ + old_size, new_size - old_size, value);
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-07-03 14:15:05 +02:00
|
|
|
destruct_n(begin_ + new_size, old_size - new_size);
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
2020-07-03 14:15:05 +02:00
|
|
|
end_ = begin_ + new_size;
|
2020-06-09 10:10:56 +02:00
|
|
|
UPDATE_VECTOR_SIZE(this);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Afterwards the vector has 0 elements, but will still have
|
|
|
|
* memory to be refilled again.
|
|
|
|
*/
|
|
|
|
void clear()
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
destruct_n(begin_, this->size());
|
|
|
|
end_ = begin_;
|
2019-09-12 14:23:21 +02:00
|
|
|
UPDATE_VECTOR_SIZE(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Afterwards the vector has 0 elements and any allocated memory
|
|
|
|
* will be freed.
|
|
|
|
*/
|
2020-06-09 10:10:56 +02:00
|
|
|
void clear_and_make_inline()
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
destruct_n(begin_, this->size());
|
2020-06-09 10:10:56 +02:00
|
|
|
if (!this->is_inline()) {
|
2020-07-03 14:15:05 +02:00
|
|
|
allocator_.deallocate(begin_);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
2020-07-05 15:08:26 +02:00
|
|
|
begin_ = this->inline_buffer();
|
2020-07-03 14:15:05 +02:00
|
|
|
end_ = begin_;
|
|
|
|
capacity_end_ = begin_ + InlineBufferCapacity;
|
2019-09-12 14:23:21 +02:00
|
|
|
UPDATE_VECTOR_SIZE(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a new element at the end of the vector.
|
|
|
|
* This might cause a reallocation with the capacity is exceeded.
|
2020-06-09 10:10:56 +02:00
|
|
|
*
|
|
|
|
* This is similar to std::vector::push_back.
|
2019-09-12 14:23:21 +02:00
|
|
|
*/
|
|
|
|
void append(const T &value)
|
|
|
|
{
|
|
|
|
this->ensure_space_for_one();
|
|
|
|
this->append_unchecked(value);
|
|
|
|
}
|
|
|
|
void append(T &&value)
|
|
|
|
{
|
|
|
|
this->ensure_space_for_one();
|
|
|
|
this->append_unchecked(std::move(value));
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Append the value to the vector and return the index that can be used to access the newly
|
|
|
|
* added value.
|
|
|
|
*/
|
2020-02-10 13:54:57 +01:00
|
|
|
uint append_and_get_index(const T &value)
|
|
|
|
{
|
2020-07-03 14:52:51 +02:00
|
|
|
const uint index = this->size();
|
2020-02-10 13:54:57 +01:00
|
|
|
this->append(value);
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Append the value if it is not yet in the vector. This has to do a linear search to check if
|
|
|
|
* the value is in the vector. Therefore, this should only be called when it is known that the
|
|
|
|
* vector is small.
|
|
|
|
*/
|
2020-02-10 13:54:57 +01:00
|
|
|
void append_non_duplicates(const T &value)
|
|
|
|
{
|
|
|
|
if (!this->contains(value)) {
|
|
|
|
this->append(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Append the value and assume that vector has enough memory reserved. This invokes undefined
|
|
|
|
* behavior when not enough capacity has been reserved beforehand. Only use this in performance
|
|
|
|
* critical code.
|
|
|
|
*/
|
2019-09-12 14:23:21 +02:00
|
|
|
void append_unchecked(const T &value)
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
BLI_assert(end_ < capacity_end_);
|
|
|
|
new (end_) T(value);
|
|
|
|
end_++;
|
2019-09-12 14:23:21 +02:00
|
|
|
UPDATE_VECTOR_SIZE(this);
|
|
|
|
}
|
|
|
|
void append_unchecked(T &&value)
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
BLI_assert(end_ < capacity_end_);
|
|
|
|
new (end_) T(std::move(value));
|
|
|
|
end_++;
|
2019-09-12 14:23:21 +02:00
|
|
|
UPDATE_VECTOR_SIZE(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert the same element n times at the end of the vector.
|
|
|
|
* This might result in a reallocation internally.
|
|
|
|
*/
|
2020-07-03 14:52:51 +02:00
|
|
|
void append_n_times(const T &value, const uint n)
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
|
|
|
this->reserve(this->size() + n);
|
2020-07-03 14:15:05 +02:00
|
|
|
blender::uninitialized_fill_n(end_, n, value);
|
2020-06-09 10:10:56 +02:00
|
|
|
this->increase_size_by_unchecked(n);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Enlarges the size of the internal buffer that is considered to be initialized. This invokes
|
|
|
|
* undefined behavior when when the new size is larger than the capacity. The method can be
|
|
|
|
* useful when you want to call constructors in the vector yourself. This should only be done in
|
|
|
|
* very rare cases and has to be justified every time.
|
|
|
|
*/
|
2020-07-03 14:52:51 +02:00
|
|
|
void increase_size_by_unchecked(const uint n)
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
BLI_assert(end_ + n <= capacity_end_);
|
|
|
|
end_ += n;
|
2019-09-12 14:23:21 +02:00
|
|
|
UPDATE_VECTOR_SIZE(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy the elements of another array to the end of this vector.
|
2020-06-09 10:10:56 +02:00
|
|
|
*
|
|
|
|
* This can be used to emulate parts of std::vector::insert.
|
2019-09-12 14:23:21 +02:00
|
|
|
*/
|
2020-06-09 11:58:47 +02:00
|
|
|
void extend(Span<T> array)
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
2020-06-09 10:10:56 +02:00
|
|
|
this->extend(array.data(), array.size());
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
void extend(const T *start, uint amount)
|
|
|
|
{
|
|
|
|
this->reserve(this->size() + amount);
|
|
|
|
this->extend_unchecked(start, amount);
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Adds all elements from the array that are not already in the vector. This is an expensive
|
|
|
|
* operation when the vector is large, but can be very cheap when it is known that the vector is
|
|
|
|
* small.
|
|
|
|
*/
|
2020-06-09 11:58:47 +02:00
|
|
|
void extend_non_duplicates(Span<T> array)
|
2020-02-10 13:54:57 +01:00
|
|
|
{
|
|
|
|
for (const T &value : array) {
|
|
|
|
this->append_non_duplicates(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Extend the vector without bounds checking. It is assumed that enough memory has been reserved
|
|
|
|
* beforehand. Only use this in performance critical code.
|
|
|
|
*/
|
2020-06-09 11:58:47 +02:00
|
|
|
void extend_unchecked(Span<T> array)
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
2020-06-09 10:10:56 +02:00
|
|
|
this->extend_unchecked(array.data(), array.size());
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
void extend_unchecked(const T *start, uint amount)
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
BLI_assert(begin_ + amount <= capacity_end_);
|
|
|
|
blender::uninitialized_copy_n(start, amount, end_);
|
|
|
|
end_ += amount;
|
2019-09-12 14:23:21 +02:00
|
|
|
UPDATE_VECTOR_SIZE(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a reference to the last element in the vector.
|
|
|
|
* This will assert when the vector is empty.
|
|
|
|
*/
|
|
|
|
const T &last() const
|
|
|
|
{
|
|
|
|
BLI_assert(this->size() > 0);
|
2020-07-03 14:15:05 +02:00
|
|
|
return *(end_ - 1);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
T &last()
|
|
|
|
{
|
|
|
|
BLI_assert(this->size() > 0);
|
2020-07-03 14:15:05 +02:00
|
|
|
return *(end_ - 1);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace every element with a new value.
|
|
|
|
*/
|
|
|
|
void fill(const T &value)
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
initialized_fill_n(begin_, this->size(), value);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Copy the value to all positions specified by the indices array.
|
|
|
|
*/
|
2020-06-09 11:58:47 +02:00
|
|
|
void fill_indices(Span<uint> indices, const T &value)
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
2020-06-09 11:58:47 +02:00
|
|
|
MutableSpan<T>(*this).fill_indices(indices, value);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return how many values are currently stored in the vector.
|
|
|
|
*/
|
|
|
|
uint size() const
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
BLI_assert(debug_size_ == (uint)(end_ - begin_));
|
|
|
|
return (uint)(end_ - begin_);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true when the vector contains no elements, otherwise false.
|
2020-06-09 10:10:56 +02:00
|
|
|
*
|
|
|
|
* This is the same as std::vector::empty.
|
2019-09-12 14:23:21 +02:00
|
|
|
*/
|
2020-04-28 17:04:07 +02:00
|
|
|
bool is_empty() const
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return begin_ == end_;
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-09 10:10:56 +02:00
|
|
|
* Destructs the last element and decreases the size by one. This invokes undefined behavior when
|
|
|
|
* the vector is empty.
|
2019-09-12 14:23:21 +02:00
|
|
|
*/
|
|
|
|
void remove_last()
|
|
|
|
{
|
2020-04-28 17:04:07 +02:00
|
|
|
BLI_assert(!this->is_empty());
|
2020-07-03 14:15:05 +02:00
|
|
|
end_--;
|
|
|
|
end_->~T();
|
2019-09-12 14:23:21 +02:00
|
|
|
UPDATE_VECTOR_SIZE(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-09 10:10:56 +02:00
|
|
|
* Remove the last element from the vector and return it. This invokes undefined behavior when
|
|
|
|
* the vector is empty.
|
|
|
|
*
|
|
|
|
* This is similar to std::vector::pop_back.
|
2019-09-12 14:23:21 +02:00
|
|
|
*/
|
|
|
|
T pop_last()
|
|
|
|
{
|
2020-04-28 17:04:07 +02:00
|
|
|
BLI_assert(!this->is_empty());
|
2020-07-03 14:15:05 +02:00
|
|
|
end_--;
|
|
|
|
T value = std::move(*end_);
|
|
|
|
end_->~T();
|
2019-09-12 14:23:21 +02:00
|
|
|
UPDATE_VECTOR_SIZE(this);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-09 10:10:56 +02:00
|
|
|
* Delete any element in the vector. The empty space will be filled by the previously last
|
|
|
|
* element. This takes O(1) time.
|
2019-09-12 14:23:21 +02:00
|
|
|
*/
|
2020-07-03 14:52:51 +02:00
|
|
|
void remove_and_reorder(const uint index)
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
|
|
|
BLI_assert(index < this->size());
|
2020-07-03 14:15:05 +02:00
|
|
|
T *element_to_remove = begin_ + index;
|
|
|
|
end_--;
|
|
|
|
if (element_to_remove < end_) {
|
|
|
|
*element_to_remove = std::move(*end_);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
2020-07-03 14:15:05 +02:00
|
|
|
end_->~T();
|
2019-09-12 14:23:21 +02:00
|
|
|
UPDATE_VECTOR_SIZE(this);
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
2020-06-25 23:13:02 +10:00
|
|
|
* Finds the first occurrence of the value, removes it and copies the last element to the hole in
|
2020-06-09 10:10:56 +02:00
|
|
|
* the vector. This takes O(n) time.
|
|
|
|
*/
|
2020-02-10 13:54:57 +01:00
|
|
|
void remove_first_occurrence_and_reorder(const T &value)
|
|
|
|
{
|
2020-07-03 14:52:51 +02:00
|
|
|
const uint index = this->first_index_of(value);
|
2020-02-10 13:54:57 +01:00
|
|
|
this->remove_and_reorder((uint)index);
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Remove the element at the given index and move all values coming after it one towards the
|
|
|
|
* front. This takes O(n) time. If the order is not important, remove_and_reorder should be used
|
|
|
|
* instead.
|
|
|
|
*
|
|
|
|
* This is similar to std::vector::erase.
|
|
|
|
*/
|
2020-07-03 14:52:51 +02:00
|
|
|
void remove(const uint index)
|
2020-06-09 10:10:56 +02:00
|
|
|
{
|
|
|
|
BLI_assert(index < this->size());
|
2020-07-03 14:52:51 +02:00
|
|
|
const uint last_index = this->size() - 1;
|
2020-06-09 10:10:56 +02:00
|
|
|
for (uint i = index; i < last_index; i++) {
|
2020-07-03 14:15:05 +02:00
|
|
|
begin_[i] = std::move(begin_[i + 1]);
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
2020-07-03 14:15:05 +02:00
|
|
|
begin_[last_index].~T();
|
|
|
|
end_--;
|
2020-06-09 10:10:56 +02:00
|
|
|
UPDATE_VECTOR_SIZE(this);
|
|
|
|
}
|
|
|
|
|
2019-09-12 14:23:21 +02:00
|
|
|
/**
|
|
|
|
* Do a linear search to find the value in the vector.
|
|
|
|
* When found, return the first index, otherwise return -1.
|
|
|
|
*/
|
2020-06-09 10:10:56 +02:00
|
|
|
int first_index_of_try(const T &value) const
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
2020-07-03 14:52:51 +02:00
|
|
|
for (const T *current = begin_; current != end_; current++) {
|
2019-09-12 14:23:21 +02:00
|
|
|
if (*current == value) {
|
2020-07-03 14:15:05 +02:00
|
|
|
return (int)(current - begin_);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-02-10 13:54:57 +01:00
|
|
|
/**
|
2020-06-09 10:10:56 +02:00
|
|
|
* Do a linear search to find the value in the vector and return the found index. This invokes
|
|
|
|
* undefined behavior when the value is not in the vector.
|
2020-02-10 13:54:57 +01:00
|
|
|
*/
|
2020-06-09 10:10:56 +02:00
|
|
|
uint first_index_of(const T &value) const
|
2020-02-10 13:54:57 +01:00
|
|
|
{
|
2020-07-03 14:52:51 +02:00
|
|
|
const int index = this->first_index_of_try(value);
|
2020-02-10 13:54:57 +01:00
|
|
|
BLI_assert(index >= 0);
|
|
|
|
return (uint)index;
|
|
|
|
}
|
|
|
|
|
2019-09-12 14:23:21 +02:00
|
|
|
/**
|
|
|
|
* Do a linear search to see of the value is in the vector.
|
|
|
|
* Return true when it exists, otherwise false.
|
|
|
|
*/
|
|
|
|
bool contains(const T &value) const
|
|
|
|
{
|
2020-06-09 10:10:56 +02:00
|
|
|
return this->first_index_of_try(value) != -1;
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Get access to the underlying array.
|
|
|
|
*/
|
|
|
|
T *data()
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return begin_;
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get access to the underlying array.
|
|
|
|
*/
|
|
|
|
const T *data() const
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return begin_;
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
2019-09-12 14:23:21 +02:00
|
|
|
T *begin()
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return begin_;
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
T *end()
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return end_;
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const T *begin() const
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return begin_;
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
const T *end() const
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return end_;
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
2019-09-14 14:41:19 +02:00
|
|
|
/**
|
2020-06-09 10:10:56 +02:00
|
|
|
* Get the current capacity of the vector, i.e. the maximum number of elements the vector can
|
|
|
|
* hold, before it has to reallocate.
|
2019-09-14 14:41:19 +02:00
|
|
|
*/
|
|
|
|
uint capacity() const
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return (uint)(capacity_end_ - begin_);
|
2019-09-14 14:41:19 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Get an index range that makes looping over all indices more convenient and less error prone.
|
|
|
|
* Obviously, this should only be used when you actually need the index in the loop.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* for (uint i : myvector.index_range()) {
|
|
|
|
* do_something(i, my_vector[i]);
|
|
|
|
* }
|
|
|
|
*/
|
2020-02-07 17:23:25 +01:00
|
|
|
IndexRange index_range() const
|
|
|
|
{
|
|
|
|
return IndexRange(this->size());
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/**
|
|
|
|
* Print some debug information about the vector.
|
|
|
|
*/
|
|
|
|
void print_stats(StringRef name = "") const
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
2020-06-09 10:10:56 +02:00
|
|
|
std::cout << "Vector Stats: " << name << "\n";
|
|
|
|
std::cout << " Address: " << this << "\n";
|
|
|
|
std::cout << " Elements: " << this->size() << "\n";
|
2020-07-03 14:15:05 +02:00
|
|
|
std::cout << " Capacity: " << (capacity_end_ - begin_) << "\n";
|
2020-06-09 10:10:56 +02:00
|
|
|
std::cout << " Inline Capacity: " << InlineBufferCapacity << "\n";
|
|
|
|
|
|
|
|
char memory_size_str[15];
|
|
|
|
BLI_str_format_byte_unit(memory_size_str, sizeof(*this), true);
|
|
|
|
std::cout << " Size on Stack: " << memory_size_str << "\n";
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-07-05 15:08:26 +02:00
|
|
|
T *inline_buffer() const
|
|
|
|
{
|
|
|
|
return (T *)inline_buffer_.ptr();
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
bool is_inline() const
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
2020-07-05 15:08:26 +02:00
|
|
|
return begin_ == this->inline_buffer();
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ensure_space_for_one()
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
if (UNLIKELY(end_ >= capacity_end_)) {
|
2020-06-09 10:10:56 +02:00
|
|
|
this->realloc_to_at_least(this->size() + 1);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 14:52:51 +02:00
|
|
|
BLI_NOINLINE void realloc_to_at_least(const uint min_capacity)
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
|
|
|
if (this->capacity() >= min_capacity) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
/* At least double the size of the previous allocation. Otherwise consecutive calls to grow can
|
|
|
|
* cause a reallocation every time even though min_capacity only increments. */
|
2020-07-03 14:52:51 +02:00
|
|
|
const uint min_new_capacity = this->capacity() * 2;
|
2020-02-10 13:54:57 +01:00
|
|
|
|
2020-07-03 14:52:51 +02:00
|
|
|
const uint new_capacity = std::max(min_capacity, min_new_capacity);
|
|
|
|
const uint size = this->size();
|
2019-09-12 14:23:21 +02:00
|
|
|
|
2020-07-03 14:15:05 +02:00
|
|
|
T *new_array = (T *)allocator_.allocate(new_capacity * (uint)sizeof(T), alignof(T), AT);
|
|
|
|
uninitialized_relocate_n(begin_, size, new_array);
|
2019-09-12 14:23:21 +02:00
|
|
|
|
2020-06-09 10:10:56 +02:00
|
|
|
if (!this->is_inline()) {
|
2020-07-03 14:15:05 +02:00
|
|
|
allocator_.deallocate(begin_);
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
2020-07-03 14:15:05 +02:00
|
|
|
begin_ = new_array;
|
|
|
|
end_ = begin_ + size;
|
|
|
|
capacity_end_ = begin_ + new_capacity;
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-07-03 14:15:05 +02:00
|
|
|
* Initialize all properties, except for allocator_, which has to be initialized beforehand.
|
2019-09-12 14:23:21 +02:00
|
|
|
*/
|
2020-06-09 10:10:56 +02:00
|
|
|
template<uint OtherInlineBufferCapacity>
|
|
|
|
void init_copy_from_other_vector(const Vector<T, OtherInlineBufferCapacity, Allocator> &other)
|
2019-09-12 14:23:21 +02:00
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
allocator_ = other.allocator_;
|
2019-09-12 14:23:21 +02:00
|
|
|
|
2020-07-03 14:52:51 +02:00
|
|
|
const uint size = other.size();
|
|
|
|
uint capacity;
|
2019-09-12 14:23:21 +02:00
|
|
|
|
2020-04-23 20:05:53 +02:00
|
|
|
if (size <= InlineBufferCapacity) {
|
2020-07-05 15:08:26 +02:00
|
|
|
begin_ = this->inline_buffer();
|
2020-04-23 20:05:53 +02:00
|
|
|
capacity = InlineBufferCapacity;
|
2019-09-12 14:23:21 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-07-03 14:15:05 +02:00
|
|
|
begin_ = (T *)allocator_.allocate(sizeof(T) * size, alignof(T), AT);
|
2019-09-12 14:23:21 +02:00
|
|
|
capacity = size;
|
|
|
|
}
|
|
|
|
|
2020-07-03 14:15:05 +02:00
|
|
|
end_ = begin_ + size;
|
|
|
|
capacity_end_ = begin_ + capacity;
|
2019-09-12 14:23:21 +02:00
|
|
|
|
2020-07-03 14:15:05 +02:00
|
|
|
uninitialized_copy_n(other.data(), size, begin_);
|
2019-09-12 14:23:21 +02:00
|
|
|
UPDATE_VECTOR_SIZE(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#undef UPDATE_VECTOR_SIZE
|
|
|
|
|
2020-02-10 13:54:57 +01:00
|
|
|
/**
|
|
|
|
* Use when the vector is used in the local scope of a function. It has a larger inline storage by
|
|
|
|
* default to make allocations less likely.
|
|
|
|
*/
|
2020-04-23 20:05:53 +02:00
|
|
|
template<typename T, uint InlineBufferCapacity = 20>
|
|
|
|
using ScopedVector = Vector<T, InlineBufferCapacity, GuardedAllocator>;
|
2019-09-12 14:23:21 +02:00
|
|
|
|
2020-06-09 10:27:24 +02:00
|
|
|
} /* namespace blender */
|
2019-09-13 21:12:26 +10:00
|
|
|
|
2020-04-21 17:31:56 +02:00
|
|
|
#endif /* __BLI_VECTOR_HH__ */
|