diff --git a/Makefile b/Makefile index 5adac0e..328f842 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,7 @@ EXT = cpp CFLAGS = -Wall -Wextra -Werror $(INCLUDES) CFLAGS += -std=c++98 +CFLAGS += -g3 VPATH = $(D_SRCS) diff --git a/headers/ftvector.hpp b/headers/ftvector.hpp index b33673e..fc3ac77 100644 --- a/headers/ftvector.hpp +++ b/headers/ftvector.hpp @@ -97,6 +97,7 @@ public: private: size_type _size; + size_type _capacity; value_type * _mem_ptr; allocator_type _allocator; diff --git a/srcs/ftvector.cpp b/srcs/ftvector.cpp index 4cee4ac..82a09fe 100644 --- a/srcs/ftvector.cpp +++ b/srcs/ftvector.cpp @@ -9,7 +9,8 @@ ftvector::ftvector() : _size(0) -//, _space(0) +, _capacity(0) +, _mem_ptr(NULL) { // std::cout << COPLIEN_COLOR "ftvector constructor" RESET "\n"; _allocator = allocator_type(); @@ -61,8 +62,10 @@ ftvector::size_type ftvector::size() const {return _size;} *********************************************/ void ftvector::push_back(const value_type & element) { -// if (_size == _space) - reserve(1); + if (_size >= _capacity) + { + reserve(std::min(_size + 1, _allocator.max_size() / 2) * 2); + } _allocator.construct(&_mem_ptr[_size], element); _size++; @@ -71,26 +74,34 @@ void ftvector::push_back(const value_type & element) { std::cout << "\n"; } +void ftvector::reserve(size_type new_cap) { + value_type * tmp_ptr; + + if (new_cap > _allocator.max_size()) + throw std::length_error("new_cap > max_size"); + if (_capacity == _allocator.max_size()) + throw std::length_error("capacity == max_size"); + if (new_cap <= _capacity) + return ; + + _capacity = new_cap; + tmp_ptr = _allocator.allocate(new_cap); + + if (_mem_ptr) + { + // TMP + for (size_type i = 0; i < _size; i++) + tmp_ptr[i] = _mem_ptr[i]; + // TMP END + _allocator.deallocate(_mem_ptr, _capacity); + } + _mem_ptr = tmp_ptr; +} + /********************************************* * PRIVATE MEMBER FUNCTIONS *********************************************/ -void ftvector::reserve(size_type add_space) { - - if (add_space == 0) - return ; - -// int * tmp_ptr; -// -// if (_space > 0 && add_space == 1) -// { -// add_space = _space * 2; -// } -// -// tmp_ptr = _allocator.allocate(add_space); - _mem_ptr = _allocator.allocate(15); -} - /********************************************* * NESTED CLASS *********************************************/