|
|
|
@ -46,18 +46,7 @@ template <typename T, size_t N> |
|
|
|
|
class InlinedVector { |
|
|
|
|
public: |
|
|
|
|
InlinedVector() {} |
|
|
|
|
~InlinedVector() { |
|
|
|
|
for (size_t i = 0; i < size_ && i < N; ++i) { |
|
|
|
|
T& value = *reinterpret_cast<T*>(inline_ + i); |
|
|
|
|
value.~T(); |
|
|
|
|
} |
|
|
|
|
if (size_ > N) { // Avoid subtracting two signed values.
|
|
|
|
|
for (size_t i = 0; i < size_ - N; ++i) { |
|
|
|
|
dynamic_[i].~T(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
gpr_free(dynamic_); |
|
|
|
|
} |
|
|
|
|
~InlinedVector() { destroy_elements(); } |
|
|
|
|
|
|
|
|
|
// For now, we do not support copying.
|
|
|
|
|
InlinedVector(const InlinedVector&) = delete; |
|
|
|
@ -100,7 +89,27 @@ class InlinedVector { |
|
|
|
|
|
|
|
|
|
size_t size() const { return size_; } |
|
|
|
|
|
|
|
|
|
void clear() { |
|
|
|
|
destroy_elements(); |
|
|
|
|
dynamic_ = nullptr; |
|
|
|
|
size_ = 0; |
|
|
|
|
dynamic_capacity_ = 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
void destroy_elements() { |
|
|
|
|
for (size_t i = 0; i < size_ && i < N; ++i) { |
|
|
|
|
T& value = *reinterpret_cast<T*>(inline_ + i); |
|
|
|
|
value.~T(); |
|
|
|
|
} |
|
|
|
|
if (size_ > N) { // Avoid subtracting two signed values.
|
|
|
|
|
for (size_t i = 0; i < size_ - N; ++i) { |
|
|
|
|
dynamic_[i].~T(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
gpr_free(dynamic_); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
typename std::aligned_storage<sizeof(T)>::type inline_[N]; |
|
|
|
|
T* dynamic_ = nullptr; |
|
|
|
|
size_t size_ = 0; |
|
|
|
|