1 #ifndef KALLISTO_TINYVECTOR_H 2 #define KALLISTO_TINYVECTOR_H 8 template<
class T,
int N = (1 + (48 /
sizeof(T)))>
22 for (
auto &x : l) push_back(x);
46 reserve(o.capacity());
48 for (
auto &x : o) push_back(x);
57 reserve(o.capacity());
59 for (
auto &x : o) push_back(x);
66 const size_t sz = o.size();
68 for (
size_t i = 0; i < sz; i++) arr.data[i] = std::move(o.arr.data[i]);
76 vec.data = o.vec.data;
77 vec.size = o.vec.size;
89 reserve(o.capacity());
91 for (
auto &x : o) push_back(x);
104 const size_t sz = o.size();
106 for (
size_t i = 0; i < sz; i++) arr.data[i] = std::move(o.arr.data[i]);
114 vec.data = o.vec.data;
115 vec.size = o.vec.size;
117 o.vec.data =
nullptr;
130 typedef T
const* const_iterator;
132 iterator begin() {
return getPointer(); }
133 const_iterator begin()
const {
return getPointer(); }
134 iterator end() {
return getPointer() + size(); }
135 const_iterator end()
const {
return (getPointer() + size()); }
137 inline bool isShort()
const {
return short_; }
139 inline T* getPointer() {
141 if (isShort())
return &arr.data[0];
145 inline const T* getPointer()
const {
147 if (isShort())
return &arr.data[0];
151 inline size_t size()
const {
153 if (isShort())
return arr.size;
157 inline bool empty()
const {
return (size()==0); }
161 if (size() == o.size()) {
163 for (
auto it=begin(), oit = o.begin(); it != end(); ++it, ++oit) {
165 if (*it != *oit)
return false;
176 return !operator==(o);
179 inline size_t capacity()
const {
181 if (isShort())
return N;
185 T& operator[](
size_t i) {
187 return *(begin() + i);
190 const T& operator[](
size_t i)
const {
192 return *(begin() + i);
195 void push_back(
const T& value) {
197 if (size() >= capacity()) _reserve_and_copy(std::max((
size_t) 2, 3 * size() / 2));
201 if (isShort()) ++arr.size;
205 void insert(
const T& value,
const size_t position) {
207 if (size() >= capacity()) _reserve_and_copy(std::max((
size_t) 2, 3 * size() / 2));
209 T* data = getPointer();
211 memmove(&data[position + 1], &data[position], (size() - position) *
sizeof(T));
213 data[position] = value;
215 if (isShort()) ++arr.size;
219 void remove(
const size_t position) {
221 T* data = getPointer();
223 if (position != size() - 1)
224 memmove(&data[position], &data[position + 1], (size() - position - 1) *
sizeof(T));
226 if (isShort()) --arr.size;
241 for (
auto it = begin(); it != end(); ++it) it->~T();
248 inline void reserve(
size_t sz) {
250 if (sz > capacity()) _reserve_and_copy(sz);
253 void _reserve_and_copy(
size_t sz) {
255 if (sz <= capacity())
return;
257 size_t old_size = size();
259 T* newdata =
new T[sz];
262 for (
auto it = begin(); it != end(); ++it, ++i) newdata[i] = std::move(*it);
264 if (!isShort())
delete[] vec.data;
Definition: TinyVector.hpp:9