22 char mRaw[N *
sizeof(T)];
25 size_t current_size = 0;
29 typedef const T* const_iterator;
33 FixedVector(
const T (&values)[N]) : current_size(N) {
38 FixedVector(
const T (&values)[M]) : current_size(M) {
39 static_assert(M <= N,
"Too many elements for FixedVector");
49 T& operator[](
size_t index) {
54 const T& operator[](
size_t index)
const {
55 if (index >= current_size) {
56 const T* out =
nullptr;
63 constexpr size_t size()
const {
67 constexpr bool empty()
const {
68 return current_size == 0;
72 constexpr size_t capacity()
const {
77 void push_back(
const T& value) {
78 if (current_size < N) {
79 void* mem = &mData[current_size];
85 void assign(
const T* values,
size_t count) {
87 for (
size_t i = 0; i < count; ++i) {
92 void assign(const_iterator begin, const_iterator end) {
94 for (const_iterator it = begin; it != end; ++it) {
101 if (current_size > 0) {
103 mData[current_size].~T();
109 while (current_size > 0) {
115 iterator erase(iterator pos) {
119 for (iterator p = pos; p != end() - 1; ++p) {
128 iterator erase(
const T& value) {
129 iterator it = find(value);
136 iterator find(
const T& value) {
137 for (iterator it = begin(); it != end(); ++it) {
145 template<
typename Predicate>
146 iterator find_if(Predicate pred) {
147 for (iterator it = begin(); it != end(); ++it) {
155 bool insert(iterator pos,
const T& value) {
156 if (current_size < N) {
158 for (iterator p = end(); p != pos; --p) {
169 const_iterator find(
const T& value)
const {
170 for (const_iterator it = begin(); it != end(); ++it) {
182 const_iterator data()
const {
186 bool has(
const T& value)
const {
187 return find(value) != end();
195 const T& front()
const {
200 return mData[current_size - 1];
203 const T& back()
const {
204 return mData[current_size - 1];
208 iterator begin() {
return &mData[0]; }
209 const_iterator begin()
const {
return &mData[0]; }
210 iterator end() {
return &mData[current_size]; }
211 const_iterator end()
const {
return &mData[current_size]; }
220 size_t mCapacity = 0;
228 HeapVector(
size_t size = 0,
const T& value = T()): mCapacity(size) {
229 mArray.reset(
new T[mCapacity]);
230 for (
size_t i = 0; i < size; ++i) {
236 assign(other.begin(), other.end());
239 if (
this != &other) {
240 assign(other.begin(), other.end());
250 void ensure_size(
size_t n) {
252 size_t new_capacity = (3*mCapacity) / 2;
253 if (new_capacity < n) {
257 for (
size_t i = 0; i < mSize; ++i) {
258 new_array[i] = mArray[i];
262 mArray.reset(new_array.release());
263 mCapacity = new_capacity;
267 void reserve(
size_t n) {
273 void resize(
size_t n) {
278 for (
size_t i = 0; i < n && i < mSize; ++i) {
279 temp.mArray[i] = mArray[i];
284 void resize(
size_t n,
const T& value) {
286 mArray.reset(
new T[n]);
287 for (
size_t i = 0; i < n; ++i) {
294 T& operator[](
size_t index) {
295 return mArray[index];
298 const T& operator[](
size_t index)
const {
299 return mArray[index];
303 size_t size()
const {
311 size_t capacity()
const {
316 void push_back(
const T& value) {
317 ensure_size(mSize + 1);
318 if (mSize < mCapacity) {
319 mArray[mSize] = value;
338 iterator begin() {
return &mArray[0]; }
340 iterator end() {
return &mArray[mSize]; }
348 const T& front()
const {
353 return mArray[mSize - 1];
356 const T& back()
const {
357 return mArray[mSize - 1];
362 for (
iterator it = begin(); it != end(); ++it) {
379 template<
typename Predicate>
381 for (
iterator it = begin(); it != end(); ++it) {
389 bool has(
const T& value)
const {
390 return find(value) != end();
393 bool erase(
iterator pos, T* out_value =
nullptr) {
394 if (pos == end() || empty()) {
400 while (pos != end() - 1) {
409 void erase(
const T& value) {
417 T* temp = mArray.release();
418 size_t temp_size = mSize;
419 size_t temp_capacity = mCapacity;
420 T* temp2 = other.mArray.release();
422 other.mArray.reset(temp);
424 mCapacity = other.mCapacity;
425 other.mSize = temp_size;
426 other.mCapacity = temp_capacity;
436 return mSize >= mCapacity;
439 bool insert(
iterator pos,
const T& value) {
442 size_t target_idx = pos - begin();
444 auto last = end() - 1;
445 for (
size_t curr_idx = last - begin(); curr_idx > target_idx; --curr_idx) {
446 auto first = begin() + curr_idx - 1;
447 auto second = begin() + curr_idx;
453 void assign(
const T* values,
size_t count) {
454 assign(values, values + count);
459 reserve(end - begin);
469 const T* data()
const {
474 if (size() != other.size()) {
477 for (
size_t i = 0; i < size(); ++i) {
478 if (mArray[i] != other.mArray[i]) {
486 return !(*
this == other);
496 size_t mMaxSize = size_t(-1);
504 void setMaxSize(
size_t n) {
509 const bool needs_adjustment = mArray.size() > mMaxSize;
510 if (needs_adjustment) {
521 void reserve(
size_t n) {
526 bool insert(
const T& value, InsertResult* result =
nullptr) {
529 if (pos != end() && !mLess(value, *pos) && !mLess(*pos, value)) {
533 *result = InsertResult::kExists;
538 if (mArray.size() >= mMaxSize) {
541 *result = InsertResult::kMaxSize;
545 mArray.insert(pos, value);
554 iterator lower_bound(
const T& value) {
558 while (first != last) {
559 iterator mid = first + (last - first) / 2;
561 if (mLess(*mid, value)) {
577 if (pos != end() && !mLess(value, *pos) && !mLess(*pos, value)) {
584 mArray.swap(other.mArray);
591 bool has(
const T& value)
const {
592 return find(value) != end();
596 bool erase(
const T& value) {
599 return mArray.erase(it);
605 return mArray.erase(pos);
609 size_t size()
const {
return mArray.size(); }
610 bool empty()
const {
return mArray.empty(); }
611 size_t capacity()
const {
return mArray.capacity(); }
612 void clear() { mArray.clear(); }
614 if (mArray.size() >= mMaxSize) {
617 return mArray.full();
621 T& operator[](
size_t index) {
return mArray[index]; }
622 const T& operator[](
size_t index)
const {
return mArray[index]; }
624 T& front() {
return mArray.front(); }
625 const T& front()
const {
return mArray.front(); }
627 T& back() {
return mArray.back(); }
628 const T& back()
const {
return mArray.back(); }
631 iterator begin() {
return mArray.begin(); }
633 iterator end() {
return mArray.end(); }
637 T* data() {
return mArray.data(); }
638 const T* data()
const {
return mArray.data(); }