2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2013-12-12 23:32:56 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
2016-11-16 04:05:36 +00:00
|
|
|
#include <initializer_list>
|
2013-12-12 23:32:56 +00:00
|
|
|
#include <iterator>
|
2016-11-16 04:05:36 +00:00
|
|
|
#include <stdexcept>
|
2013-12-12 23:32:56 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2022-03-18 23:35:51 +00:00
|
|
|
#include "port/lang.h"
|
2020-02-20 20:07:53 +00:00
|
|
|
#include "rocksdb/rocksdb_namespace.h"
|
|
|
|
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2013-12-12 23:32:56 +00:00
|
|
|
|
|
|
|
// A vector that leverages pre-allocated stack-based array to achieve better
|
|
|
|
// performance for array with small amount of items.
|
|
|
|
//
|
|
|
|
// The interface resembles that of vector, but with less features since we aim
|
|
|
|
// to solve the problem that we have in hand, rather than implementing a
|
|
|
|
// full-fledged generic container.
|
|
|
|
//
|
|
|
|
// Currently we don't support:
|
2022-06-04 03:54:48 +00:00
|
|
|
// * shrink_to_fit()
|
2013-12-12 23:32:56 +00:00
|
|
|
// If used correctly, in most cases, people should not touch the
|
|
|
|
// underlying vector at all.
|
|
|
|
// * random insert()/erase(), please only use push_back()/pop_back().
|
|
|
|
// * No move/swap operations. Each autovector instance has a
|
|
|
|
// stack-allocated array and if we want support move/swap operations, we
|
|
|
|
// need to copy the arrays other than just swapping the pointers. In this
|
|
|
|
// case we'll just explicitly forbid these operations since they may
|
|
|
|
// lead users to make false assumption by thinking they are inexpensive
|
|
|
|
// operations.
|
|
|
|
//
|
|
|
|
// Naming style of public methods almost follows that of the STL's.
|
|
|
|
template <class T, size_t kSize = 8>
|
|
|
|
class autovector {
|
|
|
|
public:
|
|
|
|
// General STL-style container member types.
|
2021-09-07 18:31:12 +00:00
|
|
|
using value_type = T;
|
|
|
|
using difference_type = typename std::vector<T>::difference_type;
|
|
|
|
using size_type = typename std::vector<T>::size_type;
|
|
|
|
using reference = value_type&;
|
|
|
|
using const_reference = const value_type&;
|
|
|
|
using pointer = value_type*;
|
|
|
|
using const_pointer = const value_type*;
|
2013-12-12 23:32:56 +00:00
|
|
|
|
|
|
|
// This class is the base for regular/const iterator
|
|
|
|
template <class TAutoVector, class TValueType>
|
|
|
|
class iterator_impl {
|
|
|
|
public:
|
|
|
|
// -- iterator traits
|
2021-09-07 18:31:12 +00:00
|
|
|
using self_type = iterator_impl<TAutoVector, TValueType>;
|
|
|
|
using value_type = TValueType;
|
|
|
|
using reference = TValueType&;
|
|
|
|
using pointer = TValueType*;
|
|
|
|
using difference_type = typename TAutoVector::difference_type;
|
|
|
|
using iterator_category = std::random_access_iterator_tag;
|
2013-12-12 23:32:56 +00:00
|
|
|
|
|
|
|
iterator_impl(TAutoVector* vect, size_t index)
|
2023-12-09 01:21:52 +00:00
|
|
|
: vect_(vect), index_(index){}
|
2013-12-12 23:32:56 +00:00
|
|
|
iterator_impl(const iterator_impl&) = default;
|
2014-01-02 19:26:57 +00:00
|
|
|
~iterator_impl() {}
|
2013-12-12 23:32:56 +00:00
|
|
|
iterator_impl& operator=(const iterator_impl&) = default;
|
|
|
|
|
|
|
|
// -- Advancement
|
2014-08-14 21:56:11 +00:00
|
|
|
// ++iterator
|
2013-12-12 23:32:56 +00:00
|
|
|
self_type& operator++() {
|
|
|
|
++index_;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-08-14 21:56:11 +00:00
|
|
|
// iterator++
|
2013-12-12 23:32:56 +00:00
|
|
|
self_type operator++(int) {
|
|
|
|
auto old = *this;
|
|
|
|
++index_;
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
2014-08-14 21:56:11 +00:00
|
|
|
// --iterator
|
2013-12-12 23:32:56 +00:00
|
|
|
self_type& operator--() {
|
|
|
|
--index_;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-08-14 21:56:11 +00:00
|
|
|
// iterator--
|
2013-12-12 23:32:56 +00:00
|
|
|
self_type operator--(int) {
|
|
|
|
auto old = *this;
|
|
|
|
--index_;
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
2017-05-22 17:53:11 +00:00
|
|
|
self_type operator-(difference_type len) const {
|
2013-12-12 23:32:56 +00:00
|
|
|
return self_type(vect_, index_ - len);
|
|
|
|
}
|
|
|
|
|
2017-05-22 17:53:11 +00:00
|
|
|
difference_type operator-(const self_type& other) const {
|
2013-12-12 23:32:56 +00:00
|
|
|
assert(vect_ == other.vect_);
|
|
|
|
return index_ - other.index_;
|
|
|
|
}
|
|
|
|
|
2017-05-22 17:53:11 +00:00
|
|
|
self_type operator+(difference_type len) const {
|
2013-12-12 23:32:56 +00:00
|
|
|
return self_type(vect_, index_ + len);
|
|
|
|
}
|
|
|
|
|
|
|
|
self_type& operator+=(difference_type len) {
|
|
|
|
index_ += len;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
self_type& operator-=(difference_type len) {
|
|
|
|
index_ -= len;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- Reference
|
2019-11-23 02:12:35 +00:00
|
|
|
reference operator*() const {
|
2013-12-12 23:32:56 +00:00
|
|
|
assert(vect_->size() >= index_);
|
|
|
|
return (*vect_)[index_];
|
|
|
|
}
|
2017-05-22 17:53:11 +00:00
|
|
|
|
2019-11-23 02:12:35 +00:00
|
|
|
pointer operator->() const {
|
2017-05-22 17:53:11 +00:00
|
|
|
assert(vect_->size() >= index_);
|
|
|
|
return &(*vect_)[index_];
|
|
|
|
}
|
|
|
|
|
2022-10-26 19:08:20 +00:00
|
|
|
reference operator[](difference_type len) const { return *(*this + len); }
|
2019-11-20 19:27:05 +00:00
|
|
|
|
2013-12-12 23:32:56 +00:00
|
|
|
// -- Logical Operators
|
|
|
|
bool operator==(const self_type& other) const {
|
|
|
|
assert(vect_ == other.vect_);
|
|
|
|
return index_ == other.index_;
|
|
|
|
}
|
|
|
|
|
2014-01-02 19:26:57 +00:00
|
|
|
bool operator!=(const self_type& other) const { return !(*this == other); }
|
2013-12-12 23:32:56 +00:00
|
|
|
|
|
|
|
bool operator>(const self_type& other) const {
|
|
|
|
assert(vect_ == other.vect_);
|
|
|
|
return index_ > other.index_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const self_type& other) const {
|
|
|
|
assert(vect_ == other.vect_);
|
|
|
|
return index_ < other.index_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator>=(const self_type& other) const {
|
|
|
|
assert(vect_ == other.vect_);
|
|
|
|
return index_ >= other.index_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<=(const self_type& other) const {
|
|
|
|
assert(vect_ == other.vect_);
|
|
|
|
return index_ <= other.index_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
TAutoVector* vect_ = nullptr;
|
|
|
|
size_t index_ = 0;
|
|
|
|
};
|
|
|
|
|
2021-09-07 18:31:12 +00:00
|
|
|
using iterator = iterator_impl<autovector, value_type>;
|
|
|
|
using const_iterator = iterator_impl<const autovector, const value_type>;
|
|
|
|
using reverse_iterator = std::reverse_iterator<iterator>;
|
|
|
|
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
2013-12-12 23:32:56 +00:00
|
|
|
|
2019-03-20 17:38:54 +00:00
|
|
|
autovector() : values_(reinterpret_cast<pointer>(buf_)) {}
|
2016-11-16 04:05:36 +00:00
|
|
|
|
2019-03-20 17:38:54 +00:00
|
|
|
autovector(std::initializer_list<T> init_list)
|
|
|
|
: values_(reinterpret_cast<pointer>(buf_)) {
|
2016-11-16 04:05:36 +00:00
|
|
|
for (const T& item : init_list) {
|
|
|
|
push_back(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 17:38:54 +00:00
|
|
|
~autovector() { clear(); }
|
2013-12-12 23:32:56 +00:00
|
|
|
|
|
|
|
// -- Immutable operations
|
|
|
|
// Indicate if all data resides in in-stack data structure.
|
|
|
|
bool only_in_stack() const {
|
|
|
|
// If no element was inserted at all, the vector's capacity will be `0`.
|
|
|
|
return vect_.capacity() == 0;
|
|
|
|
}
|
|
|
|
|
2014-01-02 19:26:57 +00:00
|
|
|
size_type size() const { return num_stack_items_ + vect_.size(); }
|
2013-12-12 23:32:56 +00:00
|
|
|
|
2014-05-08 20:50:49 +00:00
|
|
|
// resize does not guarantee anything about the contents of the newly
|
|
|
|
// available elements
|
|
|
|
void resize(size_type n) {
|
|
|
|
if (n > kSize) {
|
|
|
|
vect_.resize(n - kSize);
|
2019-03-20 17:38:54 +00:00
|
|
|
while (num_stack_items_ < kSize) {
|
|
|
|
new ((void*)(&values_[num_stack_items_++])) value_type();
|
|
|
|
}
|
2014-05-08 20:50:49 +00:00
|
|
|
num_stack_items_ = kSize;
|
|
|
|
} else {
|
|
|
|
vect_.clear();
|
2019-03-20 17:38:54 +00:00
|
|
|
while (num_stack_items_ < n) {
|
|
|
|
new ((void*)(&values_[num_stack_items_++])) value_type();
|
|
|
|
}
|
|
|
|
while (num_stack_items_ > n) {
|
|
|
|
values_[--num_stack_items_].~value_type();
|
|
|
|
}
|
2014-05-08 20:50:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-02 19:26:57 +00:00
|
|
|
bool empty() const { return size() == 0; }
|
2013-12-12 23:32:56 +00:00
|
|
|
|
2022-06-04 03:54:48 +00:00
|
|
|
size_type capacity() const { return kSize + vect_.capacity(); }
|
|
|
|
|
|
|
|
void reserve(size_t cap) {
|
|
|
|
if (cap > kSize) {
|
|
|
|
vect_.reserve(cap - kSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(cap <= capacity());
|
|
|
|
}
|
|
|
|
|
2013-12-12 23:32:56 +00:00
|
|
|
const_reference operator[](size_type n) const {
|
2015-04-29 17:52:31 +00:00
|
|
|
assert(n < size());
|
2019-03-20 17:38:54 +00:00
|
|
|
if (n < kSize) {
|
|
|
|
return values_[n];
|
|
|
|
}
|
|
|
|
return vect_[n - kSize];
|
2013-12-12 23:32:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reference operator[](size_type n) {
|
2015-04-29 17:52:31 +00:00
|
|
|
assert(n < size());
|
2019-03-20 17:38:54 +00:00
|
|
|
if (n < kSize) {
|
|
|
|
return values_[n];
|
|
|
|
}
|
|
|
|
return vect_[n - kSize];
|
2013-12-12 23:32:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const_reference at(size_type n) const {
|
2014-12-04 19:41:56 +00:00
|
|
|
assert(n < size());
|
2013-12-12 23:32:56 +00:00
|
|
|
return (*this)[n];
|
|
|
|
}
|
|
|
|
|
|
|
|
reference at(size_type n) {
|
2014-12-04 19:41:56 +00:00
|
|
|
assert(n < size());
|
2013-12-12 23:32:56 +00:00
|
|
|
return (*this)[n];
|
|
|
|
}
|
|
|
|
|
|
|
|
reference front() {
|
|
|
|
assert(!empty());
|
|
|
|
return *begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
const_reference front() const {
|
|
|
|
assert(!empty());
|
|
|
|
return *begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
reference back() {
|
|
|
|
assert(!empty());
|
|
|
|
return *(end() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const_reference back() const {
|
|
|
|
assert(!empty());
|
|
|
|
return *(end() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- Mutable Operations
|
|
|
|
void push_back(T&& item) {
|
|
|
|
if (num_stack_items_ < kSize) {
|
2019-03-20 17:38:54 +00:00
|
|
|
new ((void*)(&values_[num_stack_items_])) value_type();
|
2013-12-12 23:32:56 +00:00
|
|
|
values_[num_stack_items_++] = std::move(item);
|
|
|
|
} else {
|
|
|
|
vect_.push_back(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-01 23:13:49 +00:00
|
|
|
void push_back(const T& item) {
|
|
|
|
if (num_stack_items_ < kSize) {
|
2019-03-20 17:38:54 +00:00
|
|
|
new ((void*)(&values_[num_stack_items_])) value_type();
|
2015-07-01 23:13:49 +00:00
|
|
|
values_[num_stack_items_++] = item;
|
2015-07-13 19:11:05 +00:00
|
|
|
} else {
|
2015-07-01 23:13:49 +00:00
|
|
|
vect_.push_back(item);
|
|
|
|
}
|
|
|
|
}
|
2013-12-12 23:32:56 +00:00
|
|
|
|
2014-01-02 19:26:57 +00:00
|
|
|
template <class... Args>
|
2022-08-23 21:58:16 +00:00
|
|
|
#if _LIBCPP_STD_VER > 14
|
|
|
|
reference emplace_back(Args&&... args) {
|
|
|
|
if (num_stack_items_ < kSize) {
|
|
|
|
return *(new ((void*)(&values_[num_stack_items_++]))
|
2022-10-26 19:08:20 +00:00
|
|
|
value_type(std::forward<Args>(args)...));
|
2022-08-23 21:58:16 +00:00
|
|
|
} else {
|
|
|
|
return vect_.emplace_back(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2013-12-12 23:32:56 +00:00
|
|
|
void emplace_back(Args&&... args) {
|
2018-11-13 22:37:08 +00:00
|
|
|
if (num_stack_items_ < kSize) {
|
2019-03-20 17:38:54 +00:00
|
|
|
new ((void*)(&values_[num_stack_items_++]))
|
|
|
|
value_type(std::forward<Args>(args)...);
|
2018-11-13 22:37:08 +00:00
|
|
|
} else {
|
|
|
|
vect_.emplace_back(std::forward<Args>(args)...);
|
|
|
|
}
|
2013-12-12 23:32:56 +00:00
|
|
|
}
|
2022-08-23 21:58:16 +00:00
|
|
|
#endif
|
|
|
|
|
2013-12-12 23:32:56 +00:00
|
|
|
void pop_back() {
|
|
|
|
assert(!empty());
|
|
|
|
if (!vect_.empty()) {
|
|
|
|
vect_.pop_back();
|
|
|
|
} else {
|
2019-03-20 17:38:54 +00:00
|
|
|
values_[--num_stack_items_].~value_type();
|
2013-12-12 23:32:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
2019-03-20 17:38:54 +00:00
|
|
|
while (num_stack_items_ > 0) {
|
|
|
|
values_[--num_stack_items_].~value_type();
|
|
|
|
}
|
2013-12-12 23:32:56 +00:00
|
|
|
vect_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- Copy and Assignment
|
|
|
|
autovector& assign(const autovector& other);
|
|
|
|
|
2014-01-02 19:26:57 +00:00
|
|
|
autovector(const autovector& other) { assign(other); }
|
2013-12-12 23:32:56 +00:00
|
|
|
|
2014-01-02 19:26:57 +00:00
|
|
|
autovector& operator=(const autovector& other) { return assign(other); }
|
2013-12-12 23:32:56 +00:00
|
|
|
|
2022-03-18 23:35:51 +00:00
|
|
|
autovector(autovector&& other) noexcept { *this = std::move(other); }
|
|
|
|
autovector& operator=(autovector&& other);
|
|
|
|
|
2013-12-12 23:32:56 +00:00
|
|
|
// -- Iterator Operations
|
2014-01-02 19:26:57 +00:00
|
|
|
iterator begin() { return iterator(this, 0); }
|
2013-12-12 23:32:56 +00:00
|
|
|
|
2014-01-02 19:26:57 +00:00
|
|
|
const_iterator begin() const { return const_iterator(this, 0); }
|
2013-12-12 23:32:56 +00:00
|
|
|
|
2014-01-02 19:26:57 +00:00
|
|
|
iterator end() { return iterator(this, this->size()); }
|
2013-12-12 23:32:56 +00:00
|
|
|
|
2014-01-02 19:26:57 +00:00
|
|
|
const_iterator end() const { return const_iterator(this, this->size()); }
|
2013-12-12 23:32:56 +00:00
|
|
|
|
2014-01-02 19:26:57 +00:00
|
|
|
reverse_iterator rbegin() { return reverse_iterator(end()); }
|
2013-12-12 23:32:56 +00:00
|
|
|
|
|
|
|
const_reverse_iterator rbegin() const {
|
|
|
|
return const_reverse_iterator(end());
|
|
|
|
}
|
|
|
|
|
2014-01-02 19:26:57 +00:00
|
|
|
reverse_iterator rend() { return reverse_iterator(begin()); }
|
2013-12-12 23:32:56 +00:00
|
|
|
|
|
|
|
const_reverse_iterator rend() const {
|
|
|
|
return const_reverse_iterator(begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-01-02 19:26:57 +00:00
|
|
|
size_type num_stack_items_ = 0; // current number of items
|
2019-03-20 17:38:54 +00:00
|
|
|
alignas(alignof(
|
|
|
|
value_type)) char buf_[kSize *
|
|
|
|
sizeof(value_type)]; // the first `kSize` items
|
|
|
|
pointer values_;
|
2013-12-12 23:32:56 +00:00
|
|
|
// used only if there are more than `kSize` items.
|
|
|
|
std::vector<T> vect_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T, size_t kSize>
|
2022-03-18 23:35:51 +00:00
|
|
|
autovector<T, kSize>& autovector<T, kSize>::assign(
|
|
|
|
const autovector<T, kSize>& other) {
|
2019-03-20 17:38:54 +00:00
|
|
|
values_ = reinterpret_cast<pointer>(buf_);
|
2013-12-12 23:32:56 +00:00
|
|
|
// copy the internal vector
|
|
|
|
vect_.assign(other.vect_.begin(), other.vect_.end());
|
|
|
|
|
|
|
|
// copy array
|
|
|
|
num_stack_items_ = other.num_stack_items_;
|
2024-04-04 19:33:05 +00:00
|
|
|
for (size_t i = 0; i < num_stack_items_; ++i) {
|
|
|
|
new ((void*)(&values_[i])) value_type();
|
|
|
|
}
|
2013-12-12 23:32:56 +00:00
|
|
|
std::copy(other.values_, other.values_ + num_stack_items_, values_);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
2022-03-18 23:35:51 +00:00
|
|
|
|
|
|
|
template <class T, size_t kSize>
|
|
|
|
autovector<T, kSize>& autovector<T, kSize>::operator=(
|
|
|
|
autovector<T, kSize>&& other) {
|
|
|
|
values_ = reinterpret_cast<pointer>(buf_);
|
|
|
|
vect_ = std::move(other.vect_);
|
|
|
|
size_t n = other.num_stack_items_;
|
|
|
|
num_stack_items_ = n;
|
|
|
|
other.num_stack_items_ = 0;
|
|
|
|
for (size_t i = 0; i < n; ++i) {
|
2024-04-04 19:33:05 +00:00
|
|
|
new ((void*)(&values_[i])) value_type();
|
2022-03-18 23:35:51 +00:00
|
|
|
values_[i] = std::move(other.values_[i]);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|