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) {
43 T& operator[](
size_t index) {
48 const T& operator[](
size_t index)
const {
49 if (index >= current_size) {
50 const T* out =
nullptr;
57 constexpr size_t size()
const {
61 constexpr bool empty()
const {
62 return current_size == 0;
66 constexpr size_t capacity()
const {
71 void push_back(
const T& value) {
72 if (current_size < N) {
73 void* mem = &mData[current_size];
79 void assign(
const T* values,
size_t count) {
81 for (
size_t i = 0; i < count; ++i) {
86 void assign(const_iterator begin, const_iterator end) {
88 for (const_iterator it = begin; it != end; ++it) {
95 if (current_size > 0) {
97 mData[current_size].~T();
103 while (current_size > 0) {
109 iterator erase(iterator pos) {
113 for (iterator p = pos; p != end() - 1; ++p) {
122 iterator erase(
const T& value) {
123 iterator it = find(value);
130 iterator find(
const T& value) {
131 for (iterator it = begin(); it != end(); ++it) {
139 template<
typename Predicate>
140 iterator find_if(Predicate pred) {
141 for (iterator it = begin(); it != end(); ++it) {
149 bool insert(iterator pos,
const T& value) {
150 if (current_size < N) {
152 for (iterator p = end(); p != pos; --p) {
163 const_iterator find(
const T& value)
const {
164 for (const_iterator it = begin(); it != end(); ++it) {
176 const_iterator data()
const {
180 bool has(
const T& value)
const {
181 return find(value) != end();
189 const T& front()
const {
194 return mData[current_size - 1];
197 const T& back()
const {
198 return mData[current_size - 1];
202 iterator begin() {
return &mData[0]; }
203 const_iterator begin()
const {
return &mData[0]; }
204 iterator end() {
return &mData[current_size]; }
205 const_iterator end()
const {
return &mData[current_size]; }
214 size_t mCapacity = 0;
222 HeapVector(
size_t size = 0,
const T& value = T()): mCapacity(size) {
223 mArray.reset(
new T[mCapacity]);
224 for (
size_t i = 0; i < size; ++i) {
230 assign(other.begin(), other.end());
233 if (
this != &other) {
234 assign(other.begin(), other.end());
244 void ensure_size(
size_t n) {
246 size_t new_capacity = (3*mCapacity) / 2;
247 if (new_capacity < n) {
251 for (
size_t i = 0; i < mSize; ++i) {
252 new_array[i] = mArray[i];
256 mArray.reset(new_array.release());
257 mCapacity = new_capacity;
261 void reserve(
size_t n) {
267 void resize(
size_t n) {
269 for (
size_t i = 0; i < n && i < mSize; ++i) {
270 temp.mArray[i] = mArray[i];
275 void resize(
size_t n,
const T& value) {
277 mArray.reset(
new T[n]);
278 for (
size_t i = 0; i < n; ++i) {
285 T& operator[](
size_t index) {
286 return mArray[index];
289 const T& operator[](
size_t index)
const {
290 return mArray[index];
294 size_t size()
const {
302 size_t capacity()
const {
307 void push_back(
const T& value) {
308 ensure_size(mSize + 1);
309 if (mSize < mCapacity) {
310 mArray[mSize] = value;
329 iterator begin() {
return &mArray[0]; }
331 iterator end() {
return &mArray[mSize]; }
339 const T& front()
const {
344 return mArray[mSize - 1];
347 const T& back()
const {
348 return mArray[mSize - 1];
353 for (
iterator it = begin(); it != end(); ++it) {
370 template<
typename Predicate>
372 for (
iterator it = begin(); it != end(); ++it) {
380 bool has(
const T& value)
const {
381 return find(value) != end();
384 bool erase(
iterator pos, T* out_value =
nullptr) {
385 if (pos == end() || empty()) {
391 while (pos != end() - 1) {
400 void erase(
const T& value) {
408 T* temp = mArray.release();
409 T* temp2 = other.mArray.release();
411 other.mArray.reset(temp);
421 return mSize >= mCapacity;
424 bool insert(
iterator pos,
const T& value) {
427 size_t target_idx = pos - begin();
429 auto last = end() - 1;
430 for (
size_t curr_idx = last - begin(); curr_idx > target_idx; --curr_idx) {
431 auto first = begin() + curr_idx - 1;
432 auto second = begin() + curr_idx;
438 void assign(
const T* values,
size_t count) {
439 assign(values, values + count);
444 reserve(end - begin);
454 const T* data()
const {
465 size_t mMaxSize = size_t(-1);
473 void setMaxSize(
size_t n) {
478 const bool needs_adjustment = mArray.size() > mMaxSize;
479 if (needs_adjustment) {
490 void reserve(
size_t n) {
495 bool insert(
const T& value, InsertResult* result =
nullptr) {
498 if (pos != end() && !mLess(value, *pos) && !mLess(*pos, value)) {
502 *result = InsertResult::kExists;
507 if (mArray.size() >= mMaxSize) {
510 *result = InsertResult::kMaxSize;
514 mArray.insert(pos, value);
523 iterator lower_bound(
const T& value) {
527 while (first != last) {
528 iterator mid = first + (last - first) / 2;
530 if (mLess(*mid, value)) {
546 if (pos != end() && !mLess(value, *pos) && !mLess(*pos, value)) {
556 bool has(
const T& value)
const {
557 return find(value) != end();
561 bool erase(
const T& value) {
564 return mArray.erase(it);
570 return mArray.erase(pos);
574 size_t size()
const {
return mArray.size(); }
575 bool empty()
const {
return mArray.empty(); }
576 size_t capacity()
const {
return mArray.capacity(); }
577 void clear() { mArray.clear(); }
579 if (mArray.size() >= mMaxSize) {
582 return mArray.full();
586 T& operator[](
size_t index) {
return mArray[index]; }
587 const T& operator[](
size_t index)
const {
return mArray[index]; }
589 T& front() {
return mArray.front(); }
590 const T& front()
const {
return mArray.front(); }
592 T& back() {
return mArray.back(); }
593 const T& back()
const {
return mArray.back(); }
596 iterator begin() {
return mArray.begin(); }
598 iterator end() {
return mArray.end(); }
602 T* data() {
return mArray.data(); }
603 const T* data()
const {
return mArray.data(); }