ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
SmallVector.hpp
Go to the documentation of this file.
1#pragma once
2
7
8#include <cstddef>
9#include <initializer_list>
10#include <iterator>
11#include <utility>
12#include <vector>
13
14namespace ZenitUI
15{
16
20
46 template <typename T, std::size_t N>
47 class SmallVector
48 {
49 public:
50 SmallVector() = default;
51
53 SmallVector(std::initializer_list<T> init)
54 {
55 reserve(init.size());
56 for (const auto &t : init)
57 push_back(t);
58 }
59
60 SmallVector(const SmallVector &) = default;
61 SmallVector &operator=(const SmallVector &) = default;
62 ~SmallVector() = default;
63
64 SmallVector(SmallVector &&o) noexcept
65 : heap_(std::move(o.heap_)), size_(o.size_)
66 {
67 if (size_ <= N)
68 {
69 for (std::size_t i = 0; i < size_; ++i)
70 inline_[i] = std::move(o.inline_[i]);
71 }
72 o.size_ = 0;
73 o.heap_.clear();
74 }
75
76 SmallVector &operator=(SmallVector &&o) noexcept
77 {
78 if (this != &o)
79 {
80 heap_ = std::move(o.heap_);
81 size_ = o.size_;
82 if (size_ <= N)
83 {
84 for (std::size_t i = 0; i < size_; ++i)
85 inline_[i] = std::move(o.inline_[i]);
86 }
87 o.size_ = 0;
88 o.heap_.clear();
89 }
90 return *this;
91 }
92
94 SmallVector &operator=(std::initializer_list<T> init)
95 {
96 clear();
97 reserve(init.size());
98 for (const auto &t : init)
99 push_back(t);
100 return *this;
101 }
102
104 std::size_t size() const { return size_; }
106 bool empty() const { return size_ == 0; }
107
109 void clear()
110 {
111 size_ = 0;
112 heap_.clear();
113 }
114
120 void reserve(std::size_t n)
121 {
122 if (n > N)
123 heap_.reserve(n);
124 }
125
127 void push_back(const T &value)
128 {
129 if (size_ < N)
130 {
131 inline_[size_++] = value;
132 return;
133 }
134 if (size_ == N)
135 heap_.assign(inline_, inline_ + N);
136 heap_.push_back(value);
137 ++size_;
138 }
139
141 void push_back(T &&value)
142 {
143 if (size_ < N)
144 {
145 inline_[size_++] = std::move(value);
146 return;
147 }
148 if (size_ == N)
149 {
150 heap_.assign(std::make_move_iterator(inline_),
151 std::make_move_iterator(inline_ + N));
152 }
153 heap_.push_back(std::move(value));
154 ++size_;
155 }
156
157 T &operator[](std::size_t i) { return data()[i]; }
158 const T &operator[](std::size_t i) const { return data()[i]; }
159
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_; }
164
165 bool operator==(const SmallVector &o) const
166 {
167 if (size_ != o.size_)
168 return false;
169 for (std::size_t i = 0; i < size_; ++i)
170 if (!((*this)[i] == o[i]))
171 return false;
172 return true;
173 }
174 bool operator!=(const SmallVector &o) const { return !(*this == o); }
175
176 private:
178 T *data() { return size_ <= N ? inline_ : heap_.data(); }
179 const T *data() const { return size_ <= N ? inline_ : heap_.data(); }
180
181 T inline_[N]{};
182 std::vector<T> heap_;
183 std::size_t size_{0};
184 };
185
187} // namespace ZenitUI
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