push back is working

This commit is contained in:
hugogogo
2022-06-02 18:18:38 +02:00
parent 0146ebcbf1
commit dea7818dfa
3 changed files with 32 additions and 19 deletions

View File

@@ -11,6 +11,7 @@ EXT = cpp
CFLAGS = -Wall -Wextra -Werror $(INCLUDES)
CFLAGS += -std=c++98
CFLAGS += -g3
VPATH = $(D_SRCS)

View File

@@ -97,6 +97,7 @@ public:
private:
size_type _size;
size_type _capacity;
value_type * _mem_ptr;
allocator_type _allocator;

View File

@@ -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
*********************************************/