5#ifndef V8_BASE_SMALL_VECTOR_H_
6#define V8_BASE_SMALL_VECTOR_H_
21template <
typename T,
size_t kSize,
typename Allocator = std::allocator<T>>
30 const Allocator& allocator = Allocator())
35 const Allocator& allocator = Allocator())
37 resize(size, initial_value);
49 *
this = std::move(other);
53 *
this = std::move(other);
56 const Allocator& allocator = Allocator())
58 if (init.size() > capacity())
Grow(init.size());
60 std::uninitialized_move(init.begin(), init.end(),
begin_);
64 const Allocator& allocator = Allocator())
75 if (
this == &other)
return *
this;
76 size_t other_size = other.size();
82 std::uninitialized_copy(other.begin_, other.end_,
begin_);
84 std::copy(other.begin_, other.end_,
begin_);
87 std::min(
static_cast<ptrdiff_t
>(other_size),
end_ -
begin_);
88 std::copy(other.begin_, other.begin_ + to_copy,
begin_);
89 if (other.begin_ + to_copy < other.end_) {
90 std::uninitialized_copy(other.begin_ + to_copy, other.end_,
93 std::destroy_n(
begin_ + to_copy,
size() - to_copy);
101 if (
this == &other)
return *
this;
102 if (other.is_big()) {
109 size_t other_size = other.size();
111 std::move(other.begin_, other.end_,
begin_);
114 std::min(
static_cast<ptrdiff_t
>(other_size),
end_ -
begin_);
115 std::move(other.begin_, other.begin_ + to_move,
begin_);
116 if (other.begin_ + to_move < other.end_) {
117 std::uninitialized_move(other.begin_ + to_move, other.end_,
120 std::destroy_n(
begin_ + to_move,
size() - to_move);
125 other.reset_to_inline_storage();
139 auto rbegin()
const {
return std::make_reverse_iterator(
end_); }
142 auto rend()
const {
return std::make_reverse_iterator(
begin_); }
166 T&
at(
size_t index) {
176 const T&
at(
size_t index)
const {
183 template <
typename... Args>
186 void* storage =
end_;
188 new (storage)
T(std::forward<Args>(
args)...);
196 std::destroy_n(
end_, count);
200 return insert(
pos,
static_cast<size_t>(1), value);
205 size_t old_size =
size();
208 T* old_end =
begin_ + old_size;
210 std::move_backward(
pos, old_end,
end_);
211 std::fill_n(
pos, count, value);
214 template <
typename It>
218 size_t count = std::distance(
begin,
end);
219 size_t old_size =
size();
222 T* old_end =
begin_ + old_size;
224 std::move_backward(
pos, old_end,
end_);
230 return insert(
pos, values.begin(), values.end());
236 ptrdiff_t count =
end_ - erase_start;
238 std::destroy_n(
end_, count);
243 T* new_end =
begin_ + new_size;
245 if (new_end >
end_) {
246 std::uninitialized_default_construct(
end_, new_end);
248 std::destroy_n(new_end,
end_ - new_end);
254 void resize(
size_t new_size,
const T& initial_value) {
256 T* new_end =
begin_ + new_size;
257 if (new_end >
end_) {
258 std::uninitialized_fill(
end_, new_end, initial_value);
260 std::destroy_n(new_end,
end_ - new_end);
285 size_t new_capacity =
288 if (new_storage ==
nullptr) {
291 std::uninitialized_move(
begin_,
end_, new_storage);
294 end_ = new_storage + in_use;
299 return allocator_.allocate(number_of_elements);
SmallVector(const SmallVector &other, const Allocator &allocator) V8_NOEXCEPT
void erase(T *erase_start)
V8_INLINE SmallVector(size_t size, const T &initial_value, const Allocator &allocator=Allocator())
V8_NOINLINE V8_PRESERVE_MOST void Grow(size_t min_capacity)
V8_INLINE SmallVector(size_t size, const Allocator &allocator=Allocator())
char inline_storage_[sizeof(T) *kInlineSize]
T * insert(T *pos, It begin, It end)
void pop_back(size_t count=1)
T * insert(T *pos, std::initializer_list< T > values)
void resize(size_t new_size, const T &initial_value)
T * insert(T *pos, const T &value)
void emplace_back(Args &&... args)
V8_NOINLINE V8_PRESERVE_MOST void Grow()
static constexpr size_t kInlineSize
const T & operator[](size_t index) const
SmallVector & operator=(SmallVector &&other) V8_NOEXCEPT
SmallVector(const Allocator &allocator)
void reset_to_inline_storage()
V8_INLINE SmallVector(base::Vector< const T > init, const Allocator &allocator=Allocator())
const T * inline_storage_begin() const
SmallVector(SmallVector &&other) V8_NOEXCEPT
SmallVector(SmallVector &&other, const Allocator &allocator) V8_NOEXCEPT
V8_NO_UNIQUE_ADDRESS Allocator allocator_
T * inline_storage_begin()
V8_NOINLINE V8_PRESERVE_MOST void FreeStorage()
T * insert(T *pos, size_t count, const T &value)
Allocator get_allocator() const
T * AllocateDynamicStorage(size_t number_of_elements)
void reserve(size_t new_capacity)
T & operator[](size_t index)
static constexpr bool kHasTrivialElement
SmallVector(const SmallVector &other) V8_NOEXCEPT
const T & at(size_t index) const
V8_INLINE SmallVector(std::initializer_list< T > init, const Allocator &allocator=Allocator())
void resize(size_t new_size)
SmallVector & operator=(const SmallVector &other) V8_NOEXCEPT
constexpr size_t size() const
constexpr T * begin() const
constexpr T * end() const
constexpr size_t RoundUpToPowerOfTwo(size_t value)
void FatalOOM(OOMType type, const char *msg)
V8_BASE_EXPORT int const char va_list args
#define DCHECK_LE(v1, v2)
#define DCHECK_NE(v1, v2)
#define DCHECK_GE(v1, v2)
#define DCHECK_GT(v1, v2)
#define V8_NO_UNIQUE_ADDRESS
#define V8_UNLIKELY(condition)