|
| 1 | +// Copyright (c) 2013, Facebook, Inc. All rights reserved. |
| 2 | +// This source code is licensed under the BSD-style license found in the |
| 3 | +// LICENSE file in the root directory of this source tree. An additional grant |
| 4 | +// of patent rights can be found in the PATENTS file in the same directory. |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include <algorithm> |
| 8 | +#include <cassert> |
| 9 | +#include <exception> |
| 10 | +#include <iterator> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +namespace rocksdb { |
| 14 | + |
| 15 | +// A vector that leverages pre-allocated stack-based array to achieve better |
| 16 | +// performance for array with small amount of items. |
| 17 | +// |
| 18 | +// The interface resembles that of vector, but with less features since we aim |
| 19 | +// to solve the problem that we have in hand, rather than implementing a |
| 20 | +// full-fledged generic container. |
| 21 | +// |
| 22 | +// Currently we don't support: |
| 23 | +// * reserve()/shrink_to_fit()/resize() |
| 24 | +// If used correctly, in most cases, people should not touch the |
| 25 | +// underlying vector at all. |
| 26 | +// * random insert()/erase(), please only use push_back()/pop_back(). |
| 27 | +// * No move/swap operations. Each autovector instance has a |
| 28 | +// stack-allocated array and if we want support move/swap operations, we |
| 29 | +// need to copy the arrays other than just swapping the pointers. In this |
| 30 | +// case we'll just explicitly forbid these operations since they may |
| 31 | +// lead users to make false assumption by thinking they are inexpensive |
| 32 | +// operations. |
| 33 | +// |
| 34 | +// Naming style of public methods almost follows that of the STL's. |
| 35 | +template <class T, size_t kSize = 8> |
| 36 | +class autovector { |
| 37 | + public: |
| 38 | + // General STL-style container member types. |
| 39 | + typedef T value_type; |
| 40 | + typedef typename std::vector<T>::difference_type difference_type; |
| 41 | + typedef typename std::vector<T>::size_type size_type; |
| 42 | + typedef value_type& reference; |
| 43 | + typedef const value_type& const_reference; |
| 44 | + typedef value_type* pointer; |
| 45 | + typedef const value_type* const_pointer; |
| 46 | + |
| 47 | + // This class is the base for regular/const iterator |
| 48 | + template <class TAutoVector, class TValueType> |
| 49 | + class iterator_impl { |
| 50 | + public: |
| 51 | + // -- iterator traits |
| 52 | + typedef iterator_impl<TAutoVector, TValueType> self_type; |
| 53 | + typedef TValueType value_type; |
| 54 | + typedef TValueType& reference; |
| 55 | + typedef TValueType* pointer; |
| 56 | + typedef typename TAutoVector::difference_type difference_type; |
| 57 | + typedef std::random_access_iterator_tag iterator_category; |
| 58 | + |
| 59 | + iterator_impl(TAutoVector* vect, size_t index) |
| 60 | + : vect_(vect) |
| 61 | + , index_(index) { |
| 62 | + }; |
| 63 | + iterator_impl(const iterator_impl&) = default; |
| 64 | + ~iterator_impl() { } |
| 65 | + iterator_impl& operator=(const iterator_impl&) = default; |
| 66 | + |
| 67 | + // -- Advancement |
| 68 | + // iterator++ |
| 69 | + self_type& operator++() { |
| 70 | + ++index_; |
| 71 | + return *this; |
| 72 | + } |
| 73 | + |
| 74 | + // ++iterator |
| 75 | + self_type operator++(int) { |
| 76 | + auto old = *this; |
| 77 | + ++index_; |
| 78 | + return old; |
| 79 | + } |
| 80 | + |
| 81 | + // iterator-- |
| 82 | + self_type& operator--() { |
| 83 | + --index_; |
| 84 | + return *this; |
| 85 | + } |
| 86 | + |
| 87 | + // --iterator |
| 88 | + self_type operator--(int) { |
| 89 | + auto old = *this; |
| 90 | + --index_; |
| 91 | + return old; |
| 92 | + } |
| 93 | + |
| 94 | + self_type operator-(difference_type len) { |
| 95 | + return self_type(vect_, index_ - len); |
| 96 | + } |
| 97 | + |
| 98 | + difference_type operator-(const self_type& other) { |
| 99 | + assert(vect_ == other.vect_); |
| 100 | + return index_ - other.index_; |
| 101 | + } |
| 102 | + |
| 103 | + self_type operator+(difference_type len) { |
| 104 | + return self_type(vect_, index_ + len); |
| 105 | + } |
| 106 | + |
| 107 | + self_type& operator+=(difference_type len) { |
| 108 | + index_ += len; |
| 109 | + return *this; |
| 110 | + } |
| 111 | + |
| 112 | + self_type& operator-=(difference_type len) { |
| 113 | + index_ -= len; |
| 114 | + return *this; |
| 115 | + } |
| 116 | + |
| 117 | + // -- Reference |
| 118 | + reference operator*() { |
| 119 | + assert(vect_->size() >= index_); |
| 120 | + return (*vect_)[index_]; |
| 121 | + } |
| 122 | + pointer operator->() { |
| 123 | + assert(vect_->size() >= index_); |
| 124 | + return &(*vect_)[index_]; |
| 125 | + } |
| 126 | + |
| 127 | + // -- Logical Operators |
| 128 | + bool operator==(const self_type& other) const { |
| 129 | + assert(vect_ == other.vect_); |
| 130 | + return index_ == other.index_; |
| 131 | + } |
| 132 | + |
| 133 | + bool operator!=(const self_type& other) const { |
| 134 | + return !(*this == other); |
| 135 | + } |
| 136 | + |
| 137 | + bool operator>(const self_type& other) const { |
| 138 | + assert(vect_ == other.vect_); |
| 139 | + return index_ > other.index_; |
| 140 | + } |
| 141 | + |
| 142 | + bool operator<(const self_type& other) const { |
| 143 | + assert(vect_ == other.vect_); |
| 144 | + return index_ < other.index_; |
| 145 | + } |
| 146 | + |
| 147 | + bool operator>=(const self_type& other) const { |
| 148 | + assert(vect_ == other.vect_); |
| 149 | + return index_ >= other.index_; |
| 150 | + } |
| 151 | + |
| 152 | + bool operator<=(const self_type& other) const { |
| 153 | + assert(vect_ == other.vect_); |
| 154 | + return index_ <= other.index_; |
| 155 | + } |
| 156 | + |
| 157 | + private: |
| 158 | + TAutoVector* vect_ = nullptr; |
| 159 | + size_t index_ = 0; |
| 160 | + }; |
| 161 | + |
| 162 | + typedef iterator_impl<autovector, value_type> iterator; |
| 163 | + typedef iterator_impl<const autovector, const value_type> const_iterator; |
| 164 | + typedef std::reverse_iterator<iterator> reverse_iterator; |
| 165 | + typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
| 166 | + |
| 167 | + autovector() = default; |
| 168 | + ~autovector() = default; |
| 169 | + |
| 170 | + // -- Immutable operations |
| 171 | + // Indicate if all data resides in in-stack data structure. |
| 172 | + bool only_in_stack() const { |
| 173 | + // If no element was inserted at all, the vector's capacity will be `0`. |
| 174 | + return vect_.capacity() == 0; |
| 175 | + } |
| 176 | + |
| 177 | + size_type size() const { |
| 178 | + return num_stack_items_ + vect_.size(); |
| 179 | + } |
| 180 | + |
| 181 | + bool empty() const { |
| 182 | + return size() == 0; |
| 183 | + } |
| 184 | + |
| 185 | + // will not check boundry |
| 186 | + const_reference operator[](size_type n) const { |
| 187 | + return n < kSize ? values_[n] : vect_[n - kSize]; |
| 188 | + } |
| 189 | + |
| 190 | + reference operator[](size_type n) { |
| 191 | + return n < kSize ? values_[n] : vect_[n - kSize]; |
| 192 | + } |
| 193 | + |
| 194 | + // will check boundry |
| 195 | + const_reference at(size_type n) const { |
| 196 | + if (n >= size()) { |
| 197 | + throw std::out_of_range("autovector: index out of range"); |
| 198 | + } |
| 199 | + return (*this)[n]; |
| 200 | + } |
| 201 | + |
| 202 | + reference at(size_type n) { |
| 203 | + if (n >= size()) { |
| 204 | + throw std::out_of_range("autovector: index out of range"); |
| 205 | + } |
| 206 | + return (*this)[n]; |
| 207 | + } |
| 208 | + |
| 209 | + reference front() { |
| 210 | + assert(!empty()); |
| 211 | + return *begin(); |
| 212 | + } |
| 213 | + |
| 214 | + const_reference front() const { |
| 215 | + assert(!empty()); |
| 216 | + return *begin(); |
| 217 | + } |
| 218 | + |
| 219 | + reference back() { |
| 220 | + assert(!empty()); |
| 221 | + return *(end() - 1); |
| 222 | + } |
| 223 | + |
| 224 | + const_reference back() const { |
| 225 | + assert(!empty()); |
| 226 | + return *(end() - 1); |
| 227 | + } |
| 228 | + |
| 229 | + // -- Mutable Operations |
| 230 | + void push_back(T&& item) { |
| 231 | + if (num_stack_items_ < kSize) { |
| 232 | + values_[num_stack_items_++] = std::move(item); |
| 233 | + } else { |
| 234 | + vect_.push_back(item); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + void push_back(const T& item) { |
| 239 | + push_back(value_type(item)); |
| 240 | + } |
| 241 | + |
| 242 | + template<class... Args> |
| 243 | + void emplace_back(Args&&... args) { |
| 244 | + push_back(value_type(args...)); |
| 245 | + } |
| 246 | + |
| 247 | + void pop_back() { |
| 248 | + assert(!empty()); |
| 249 | + if (!vect_.empty()) { |
| 250 | + vect_.pop_back(); |
| 251 | + } else { |
| 252 | + --num_stack_items_; |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + void clear() { |
| 257 | + num_stack_items_ = 0; |
| 258 | + vect_.clear(); |
| 259 | + } |
| 260 | + |
| 261 | + // -- Copy and Assignment |
| 262 | + autovector& assign(const autovector& other); |
| 263 | + |
| 264 | + autovector(const autovector& other) { |
| 265 | + assign(other); |
| 266 | + } |
| 267 | + |
| 268 | + autovector& operator=(const autovector& other) { |
| 269 | + return assign(other); |
| 270 | + } |
| 271 | + |
| 272 | + // move operation are disallowed since it is very hard to make sure both |
| 273 | + // autovectors are allocated from the same function stack. |
| 274 | + autovector& operator=(autovector&& other) = delete; |
| 275 | + autovector(autovector&& other) = delete; |
| 276 | + |
| 277 | + // -- Iterator Operations |
| 278 | + iterator begin() { |
| 279 | + return iterator(this, 0); |
| 280 | + } |
| 281 | + |
| 282 | + const_iterator begin() const { |
| 283 | + return const_iterator(this, 0); |
| 284 | + } |
| 285 | + |
| 286 | + iterator end() { |
| 287 | + return iterator(this, this->size()); |
| 288 | + } |
| 289 | + |
| 290 | + const_iterator end() const { |
| 291 | + return const_iterator(this, this->size()); |
| 292 | + } |
| 293 | + |
| 294 | + reverse_iterator rbegin() { |
| 295 | + return reverse_iterator(end()); |
| 296 | + } |
| 297 | + |
| 298 | + const_reverse_iterator rbegin() const { |
| 299 | + return const_reverse_iterator(end()); |
| 300 | + } |
| 301 | + |
| 302 | + reverse_iterator rend() { |
| 303 | + return reverse_iterator(begin()); |
| 304 | + } |
| 305 | + |
| 306 | + const_reverse_iterator rend() const { |
| 307 | + return const_reverse_iterator(begin()); |
| 308 | + } |
| 309 | + |
| 310 | + private: |
| 311 | + size_type num_stack_items_ = 0; // current number of items |
| 312 | + value_type values_[kSize]; // the first `kSize` items |
| 313 | + // used only if there are more than `kSize` items. |
| 314 | + std::vector<T> vect_; |
| 315 | +}; |
| 316 | + |
| 317 | +template <class T, size_t kSize> |
| 318 | +autovector<T, kSize>& autovector<T, kSize>::assign(const autovector& other) { |
| 319 | + // copy the internal vector |
| 320 | + vect_.assign(other.vect_.begin(), other.vect_.end()); |
| 321 | + |
| 322 | + // copy array |
| 323 | + num_stack_items_ = other.num_stack_items_; |
| 324 | + std::copy(other.values_, other.values_ + num_stack_items_, values_); |
| 325 | + |
| 326 | + return *this; |
| 327 | +} |
| 328 | + |
| 329 | +} // rocksdb |
0 commit comments