9#include <initializer_list>
46 template <
typename T, std::
size_t N>
50 SmallVector() =
default;
56 for (
const auto &t : init)
65 : heap_(std::move(o.heap_)), size_(o.size_)
69 for (std::size_t i = 0; i < size_; ++i)
70 inline_[i] = std::move(o.inline_[i]);
76 SmallVector &operator=(SmallVector &&o)
noexcept
80 heap_ = std::move(o.heap_);
84 for (std::size_t i = 0; i < size_; ++i)
85 inline_[i] = std::move(o.inline_[i]);
94 SmallVector &
operator=(std::initializer_list<T> init)
98 for (
const auto &t : init)
104 std::size_t
size()
const {
return size_; }
106 bool empty()
const {
return size_ == 0; }
131 inline_[size_++] = value;
135 heap_.assign(inline_, inline_ + N);
136 heap_.push_back(value);
145 inline_[size_++] = std::move(value);
150 heap_.assign(std::make_move_iterator(inline_),
151 std::make_move_iterator(inline_ + N));
153 heap_.push_back(std::move(value));
157 T &operator[](std::size_t i) {
return data()[i]; }
158 const T &operator[](std::size_t i)
const {
return data()[i]; }
160 T *begin() {
return data(); }
161 T *end() {
return data() + size_; }
162 const T *begin()
const {
return data(); }
163 const T *end()
const {
return data() + size_; }
165 bool operator==(
const SmallVector &o)
const
167 if (size_ != o.size_)
169 for (std::size_t i = 0; i < size_; ++i)
170 if (!((*
this)[i] == o[i]))
174 bool operator!=(
const SmallVector &o)
const {
return !(*
this == o); }
178 T *data() {
return size_ <= N ? inline_ : heap_.data(); }
179 const T *data()
const {
return size_ <= N ? inline_ : heap_.data(); }
182 std::vector<T> heap_;
183 std::size_t size_{0};
A vector whose first N elements live inside the object.
Definition SmallVector.hpp:48
void reserve(std::size_t n)
Reserve storage for at least n elements.
Definition SmallVector.hpp:120
bool empty() const
true when there are no elements.
Definition SmallVector.hpp:106
void clear()
Remove all elements.
Definition SmallVector.hpp:109
SmallVector(std::initializer_list< T > init)
Construct from an initializer list.
Definition SmallVector.hpp:53
void push_back(T &&value)
Append a moved value.
Definition SmallVector.hpp:141
void push_back(const T &value)
Append a copy of value.
Definition SmallVector.hpp:127
SmallVector & operator=(std::initializer_list< T > init)
Replace contents with the given initializer list.
Definition SmallVector.hpp:94
std::size_t size() const
Number of elements currently stored.
Definition SmallVector.hpp:104