|
| 1 | +#pragma once |
| 2 | + |
| 3 | +// Yet another replacement for std::vector, this time for use in graphics queues. |
| 4 | +// Its major difference is that you can append uninitialized structures and initialize them after. |
| 5 | +// This is not allows by std::vector but is very useful for our sometimes oversized unions. |
| 6 | +// Also, copies during resize are done by memcpy, not by any move constructor or similar. |
| 7 | + |
| 8 | +#include <cstdlib> |
| 9 | +#include <cstring> |
| 10 | + |
| 11 | +template<class T> |
| 12 | +class FastVec { |
| 13 | +public: |
| 14 | + FastVec() {} |
| 15 | + FastVec(size_t initialCapacity) { |
| 16 | + capacity_ = initialCapacity; |
| 17 | + data_ = (T *)malloc(initialCapacity * sizeof(T)); |
| 18 | + } |
| 19 | + ~FastVec() { if (data_) free(data_); } |
| 20 | + |
| 21 | + T *push_back() { |
| 22 | + if (size_ < capacity_) { |
| 23 | + size_++; |
| 24 | + return data_ + size_ - 1; |
| 25 | + } else { |
| 26 | + T *oldData = data_; |
| 27 | + size_t newCapacity = capacity_ * 2; |
| 28 | + if (newCapacity < 16) { |
| 29 | + newCapacity = 16; |
| 30 | + } |
| 31 | + data_ = (T *)malloc(sizeof(T) * newCapacity); |
| 32 | + if (capacity_ != 0) { |
| 33 | + memcpy(data_, oldData, sizeof(T) * size_); |
| 34 | + free(oldData); |
| 35 | + } |
| 36 | + size_++; |
| 37 | + capacity_ = newCapacity; |
| 38 | + return data_ + size_ - 1; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + void push_back(const T &t) { |
| 43 | + T *dest = push_back(); |
| 44 | + *dest = t; |
| 45 | + } |
| 46 | + |
| 47 | + // Move constructor |
| 48 | + FastVec(FastVec &&other) { |
| 49 | + data_ = other.data_; |
| 50 | + size_ = other.size_; |
| 51 | + capacity_ = other.capacity_; |
| 52 | + other.data_ = nullptr; |
| 53 | + other.size_ = 0; |
| 54 | + other.capacity_ = 0; |
| 55 | + } |
| 56 | + |
| 57 | + FastVec &operator=(FastVec &&other) { |
| 58 | + if (this != &other) { |
| 59 | + delete[] data_; |
| 60 | + data_ = other.data_; |
| 61 | + size_ = other.size_; |
| 62 | + capacity_ = other.capacity_; |
| 63 | + other.data_ = nullptr; |
| 64 | + other.size_ = 0; |
| 65 | + other.capacity_ = 0; |
| 66 | + } |
| 67 | + return *this; |
| 68 | + } |
| 69 | + |
| 70 | + // No copy constructor. |
| 71 | + FastVec(const FastVec &other) = delete; |
| 72 | + FastVec &operator=(const FastVec &other) = delete; |
| 73 | + |
| 74 | + size_t size() const { return size_; } |
| 75 | + size_t capacity() const { return capacity_; } |
| 76 | + void clear() { size_ = 0; } |
| 77 | + |
| 78 | + T *begin() { return data_; } |
| 79 | + T *end() { return data_ + size_; } |
| 80 | + const T *begin() const { return data_; } |
| 81 | + const T *end() const { return data_ + size_; } |
| 82 | + |
| 83 | + // Out of bounds (past size() - 1) is undefined behavior. |
| 84 | + T &operator[] (const size_t index) { return data_[index]; } |
| 85 | + const T &operator[] (const size_t index) const { return data_[index]; } |
| 86 | + |
| 87 | + // These two are invalid if empty(). |
| 88 | + const T &back() const { return (*this)[size() - 1]; } |
| 89 | + const T &front() const { return (*this)[0]; } |
| 90 | + |
| 91 | +private: |
| 92 | + T *data_ = nullptr; |
| 93 | + size_t size_ = 0; |
| 94 | + size_t capacity_ = 0; |
| 95 | +}; |
0 commit comments