Files
42_INT_11_ft_containers/srcs/ftvector.cpp
2022-06-02 18:18:38 +02:00

118 lines
2.6 KiB
C++

#include "ftvector.hpp"
#define COPLIEN_COLOR B_CYAN
/*********************************************
* CONSTRUCTORS
*********************************************/
ftvector::ftvector()
: _size(0)
, _capacity(0)
, _mem_ptr(NULL)
{
// std::cout << COPLIEN_COLOR "ftvector constructor" RESET "\n";
_allocator = allocator_type();
return;
}
ftvector::ftvector( ftvector const & src ) {
// std::cout << COPLIEN_COLOR "ftvector copy constructor" RESET "\n";
*this = src;
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
ftvector::~ftvector() {
// std::cout << COPLIEN_COLOR "ftvector destructor" RESET "\n";
return;
}
/*********************************************
* OPERATORS
*********************************************/
ftvector & ftvector::operator=( ftvector const & rhs ) {
// Base::operator=(rhs);
if ( this != &rhs )
{
_size = rhs.size();
}
return *this;
}
//std::ostream & operator<<(std::ostream & o, ftvector const & rhs)
//{
// o << rhs.getFoo();
// return (o);
//}
/*********************************************
* ACCESSORS
*********************************************/
ftvector::size_type ftvector::size() const {return _size;}
/*********************************************
* PUBLIC MEMBER FUNCTIONS
*********************************************/
void ftvector::push_back(const value_type & element) {
if (_size >= _capacity)
{
reserve(std::min(_size + 1, _allocator.max_size() / 2) * 2);
}
_allocator.construct(&_mem_ptr[_size], element);
_size++;
for (size_type i = 0; i < _size; i++)
std::cout << _mem_ptr[i] << "\n";
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
*********************************************/
/*********************************************
* NESTED CLASS
*********************************************/
//void ftvector::Class::function() {}
/*********************************************
* STATICS
*********************************************/
//std::string const ftvector::_bar = "bar";