diff --git a/.cache/clangd/index/ft_container.hpp.AE6A49ED28E963FF.idx b/.cache/clangd/index/ft_container.hpp.AE6A49ED28E963FF.idx new file mode 100644 index 0000000..73e1a8c Binary files /dev/null and b/.cache/clangd/index/ft_container.hpp.AE6A49ED28E963FF.idx differ diff --git a/.cache/clangd/index/iterator.hpp.B6BE3296FFA2AF9F.idx b/.cache/clangd/index/iterator.hpp.B6BE3296FFA2AF9F.idx new file mode 100644 index 0000000..0924e83 Binary files /dev/null and b/.cache/clangd/index/iterator.hpp.B6BE3296FFA2AF9F.idx differ diff --git a/.cache/clangd/index/tester.cpp.133FD4700F7AEE0A.idx b/.cache/clangd/index/tester.cpp.133FD4700F7AEE0A.idx new file mode 100644 index 0000000..02746c4 Binary files /dev/null and b/.cache/clangd/index/tester.cpp.133FD4700F7AEE0A.idx differ diff --git a/.cache/clangd/index/vector.hpp.8C3201A34AC2A736.idx b/.cache/clangd/index/vector.hpp.8C3201A34AC2A736.idx new file mode 100644 index 0000000..d847911 Binary files /dev/null and b/.cache/clangd/index/vector.hpp.8C3201A34AC2A736.idx differ diff --git a/includes/vector.hpp b/includes/vector.hpp index a472722..471e3bf 100644 --- a/includes/vector.hpp +++ b/includes/vector.hpp @@ -1,6 +1,7 @@ #pragma once #include "iterator.hpp" #include +#include namespace ft { @@ -62,7 +63,18 @@ public: _alloc.deallocate(_begin, _capacity); } - vector &operator=(const vector &other); + vector &operator=(const vector &other) { + clear(); + _alloc.deallocate(_begin, _capacity); + _alloc = other._alloc; + _capacity = other._capacity; + _size = other._size; + _begin = _alloc.allocate(_capacity); + for (size_type i = 0; i < _size; i++) + _alloc.construct(_begin + i, *(other._begin + i)); + return (*this); + } + void assign(size_type count, const T &value); template < class It > void assign(It first, It last); allocator_type get_allocator(void) const { return _alloc; } @@ -97,11 +109,24 @@ public: ft::rev_iterator< cst_iterator > rend(void) const; // CAPACITY: - bool empty(void) const; - size_type size(void) const; - size_type max_size(void) const; - void reserve(size_type new_cap); - size_type capacity(void) const; + bool empty(void) const { return _size == 0; } + size_type size(void) const { return _size; } + size_type max_size(void) const { return _alloc.max_size(); } + void reserve(size_type new_cap) { + if (new_cap > max_size()) + throw(std::length_error("vector::reserve: new_cap > max_size")); + Allocator new_alloc; + pointer new_begin; + new_begin = new_alloc.allocate(new_cap); + for (size_type i = 0; i < _size; i++) + new_alloc.construct(new_begin + i, *(_begin + i)); + clear(); + _alloc.deallocate(_begin, _capacity); + _alloc = new_alloc; + _begin = new_begin; + _capacity = new_cap; + } + size_type capacity(void) const { return _capacity; } // MODIFIERS: void clear(void) { @@ -112,14 +137,13 @@ public: iterator insert(cst_iterator pos, const T &value); iterator insert(cst_iterator pos, size_type count, const T &value); template < class It > iterator insert(cst_iterator pos, It first, It last); - iterator erase(iterator pos); - iterator erase(iterator first, iterator last); - void push_back(const T& value); - void pop_back(void); - void resize(size_type count); - void resize(size_type count, T value = T()); - void swap( vector& other); - + iterator erase(iterator pos); + iterator erase(iterator first, iterator last); + void push_back(const T &value); + void pop_back(void); + void resize(size_type count); + void resize(size_type count, T value = T()); + void swap(vector &other); }; } // namespace ft