]> git.ameliathe1st.gay Git - voyage-au-centre-des-fichiers.git/commitdiff
Des fonctions de plus pour le vecteur
authorAmelia Coutard <eliottulio.coutard@gmail.com>
Sat, 30 Dec 2023 00:24:19 +0000 (01:24 +0100)
committerAmelia Coutard <eliottulio.coutard@gmail.com>
Sat, 30 Dec 2023 01:16:54 +0000 (02:16 +0100)
libcpp/vector.hpp

index 84577e6e11a9ea205a92f97ff08cda8c738dbc50..940358f5402f5e8c95942a7789be8340dfad6a52 100644 (file)
@@ -88,23 +88,37 @@ public:
                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_);