assert(!construction_failed());
return capacity_;
}
+ bool reserve(amy::size n) {
+ assert(!construction_failed());
+ if (n <= capacity_) {
+ return true;
+ }
+ T* new_data = (T*)amy::reallocate(allocator, data, capacity_ * amy::byte_size<T>(), n * amy::byte_size<T>(), amy::byte_align<T>());
+ if (new_data == amy::nil) {
+ return false;
+ }
+ data = new_data;
+ capacity_ = n;
+ return true;
+ }
bool push_back(const T& v) {
+ return emplace_back(v);
+ }
+ template <typename... Ts>
+ bool emplace_back(Ts... vs) {
assert(!construction_failed());
- if (size_ == capacity_) {
- if (allocator.expand(data, capacity_ * amy::byte_size<T>(), amy::byte_size<T>())) {
- capacity_++;
- } else if (auto new_data =
- amy::reallocate(allocator, data, capacity_ * amy::byte_size<T>(), capacity_ * 2 * amy::byte_size<T>(), amy::byte_align<T>())) {
- data = (T*)new_data;
- capacity_ *= 2;
- } else {
- return false;
- }
+ if (size_ == capacity_ && !reserve(2 * capacity_)) {
+ return false;
}
size_++;
- new(&data[size_ - 1]) T(v);
+ new(&data[size_ - 1]) T(vs...);
return true;
}
+ void pop_back() {
+ assert(size_ > 0);
+ data[size_ - 1].~T();
+ size_--;
+ }
T& operator[](amy::diff i) {
assert(!construction_failed());
assert(0 <= i && i < size_);