added resize and _increment_capacity

This commit is contained in:
hugogogo
2022-06-09 14:14:13 +02:00
parent b385f6220a
commit 8326f7f283
3 changed files with 348 additions and 135 deletions

View File

@@ -5,66 +5,52 @@
namespace ft {
/*********************************************
* CONSTRUCTORS
* COPLIENS
*********************************************/
// constructors ------------------------------
VT_TPL VT::
vector( const Allocator & alloc )
: _size(0)
, _capacity(0)
, _mem_ptr(NULL)
, _allocator(alloc)
{
vector( const Allocator & alloc )
: _size(0)
, _capacity(0)
, _mem_ptr(NULL)
, _allocator(alloc) {
return;
}
VT_TPL VT::
vector( size_type n, const T & val, const Allocator & alloc )
: _size(0)
, _capacity(0)
, _mem_ptr(NULL)
, _allocator(alloc)
{
vector( size_type n, const T & val, const Allocator & alloc )
: _size(0)
, _capacity(0)
, _mem_ptr(NULL)
, _allocator(alloc) {
assign(n, val);
return;
}
VT_TPL template <class InputIterator> VT::
vector(InputIterator first, InputIterator last, const Allocator & alloc)
: _size(0)
, _capacity(0)
, _mem_ptr(NULL)
, _allocator(alloc)
{
: _size(0)
, _capacity(0)
, _mem_ptr(NULL)
, _allocator(alloc) {
assign(first, last);
return;
}
VT_TPL VT::
vector( vector const & src )
{
vector( vector const & src ) {
*this = src;
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
// destructors -------------------------------
VT_TPL VT::
~vector()
{
return;
}
/*********************************************
* OPERATORS
*********************************************/
~vector() { return; }
// operator= ---------------------------------
VT_TPL VT & VT::
operator=( vector const & rhs )
{
// Base::operator=(rhs);
operator=( vector const & rhs ) {
//Base::operator=(rhs);
if ( this != &rhs )
{
_size = rhs.size();
@@ -72,12 +58,6 @@ operator=( vector const & rhs )
return *this;
}
//std::ostream & operator<<(std::ostream & o, vector const & rhs)
//{
// o << rhs.getFoo();
// return (o);
//}
/*********************************************
* PUBLIC MEMBER FUNCTIONS
@@ -106,26 +86,40 @@ VT_TPL typename VT::iterator VT::
************/
// size --------------------------------------
VT_TPL typename VT::size_type VT::
size( ) const
{
return _size;
}
size( ) const { return _size; }
// max_size ----------------------------------
//size_type max_size() const;
VT_TPL typename VT::size_type VT::
max_size() const { return (_allocator.max_size()); }
// resize ------------------------------------
//void resize(size_type n, value_type val = value_type());
VT_TPL void VT::
resize(size_type n, value_type val) {
if (n > _size)
{
if (n > _capacity)
_increment_capacity(n);
while (_size != n)
{
_allocator.construct(&_mem_ptr[_size], val);
++_size;
}
}
else if (n < _size)
{
while (_size != n)
_allocator.destroy(&_mem_ptr[--_size]);
}
}
// capacity ----------------------------------
VT_TPL typename VT::size_type VT::
capacity() const
{
return _capacity;
}
capacity() const { return _capacity; }
// empty -------------------------------------
//bool empty() const;
VT_TPL bool VT::
empty() const { return (_size == 0); }
// reserve -----------------------------------
VT_TPL void VT::
reserve( size_type new_cap )
{
reserve( size_type new_cap ) {
value_type * tmp_ptr;
if (new_cap > _allocator.max_size())
@@ -140,10 +134,8 @@ VT_TPL void VT::
if (_mem_ptr)
{
// TMP replacing assign(first, last)
for (size_type i = 0; i < _size; i++)
tmp_ptr[i] = _mem_ptr[i];
// TMP END
_destroy(begin(), end());
_allocator.deallocate(_mem_ptr, _capacity);
}
@@ -155,20 +147,34 @@ VT_TPL void VT::
******************/
// operator[] --------------------------------
VT_TPL typename VT::reference VT::
operator[](size_type n)
{
return _mem_ptr[n];
}
//const_reference operator[](size_type n) const;
operator[](size_type n) { return _mem_ptr[n]; }
VT_TPL typename VT::const_reference VT::
operator[](size_type n) const { return _mem_ptr[n]; }
// at ----------------------------------------
//reference at(size_type n);
//const_reference at(size_type n) const;
VT_TPL typename VT::reference VT::
at(size_type n) {
if (n >= _size)
throw std::out_of_range("vector out of range");
return (_mem_ptr[n]);
}
VT_TPL typename VT::const_reference VT::
at(size_type n) const {
if (n >= _size)
throw std::out_of_range("vector out of range");
return (_mem_ptr[n]);
}
// front -------------------------------------
//reference front();
//const_reference front() const;
VT_TPL typename VT::reference VT::
front() { return (*_mem_ptr); }
VT_TPL typename VT::const_reference VT::
front() const { return (*_mem_ptr); }
// back --------------------------------------
//reference back();
//const_reference back() const;
VT_TPL typename VT::reference VT::
back() { return (_mem_ptr[_size - 1]); }
VT_TPL typename VT::const_reference VT::
back() const { return (_mem_ptr[_size - 1]); }
/*************
* modifiers :
@@ -176,8 +182,8 @@ VT_TPL typename VT::reference VT::
// assign ------------------------------------
VT_TPL template <class InputIterator>
typename enable_if< !is_integral<InputIterator>::value,void >::type VT::
assign( InputIterator first, InputIterator last)
{
assign( InputIterator first, InputIterator last) {
InputIterator tmp = first;
unsigned int range = 0;
@@ -186,7 +192,7 @@ typename enable_if< !is_integral<InputIterator>::value,void >::type VT::
while (tmp++ != last)
range++;
if (range >= _capacity)
reserve(range);
_increment_capacity(range);
while (first != last)
{
_allocator.construct(&_mem_ptr[_size], *first);
@@ -195,8 +201,8 @@ typename enable_if< !is_integral<InputIterator>::value,void >::type VT::
}
}
VT_TPL void VT::
assign( size_type n, const T & val )
{
assign( size_type n, const T & val ) {
if (n > _allocator.max_size())
throw std::length_error("assign: n > max_size");
@@ -217,25 +223,16 @@ VT_TPL void VT::
}
// push_back ---------------------------------
VT_TPL void VT::
push_back( const value_type & element )
{
push_back( const value_type & element ) {
if (_size >= _capacity)
{
if (_size == 0)
reserve(1);
else
reserve(std::min(_size * 2, _allocator.max_size()));
}
_increment_capacity(1);
_allocator.construct(&_mem_ptr[_size], element);
_size++;
}
// pop_back ----------------------------------
VT_TPL void VT::
pop_back()
{
_allocator.destroy(end() - 1);
_size--;
}
pop_back() { _allocator.destroy(end() - 1); _size--; }
// insert ------------------------------------
//iterator insert(iterator position, const value_type& val);
//void insert(iterator position, size_type n, const value_type& val);
@@ -248,11 +245,7 @@ VT_TPL void VT::
//void swap(vector& x);
// clear -------------------------------------
VT_TPL void VT::
clear()
{
_destroy(begin(), end());
_size = 0;
}
clear() { _destroy(begin(), end()); _size = 0; }
@@ -262,8 +255,8 @@ VT_TPL void VT::
*********************************************/
VT_TPL void VT::
_destroy(iterator first, iterator last)
{
_destroy(iterator first, iterator last) {
while (first != last)
{
_allocator.destroy(first);
@@ -271,12 +264,34 @@ VT_TPL void VT::
}
}
VT_TPL void VT::
_increment_capacity(size_type n) {
size_type res;
res = std::max(_size * 2, n);
reserve(std::min(res, _allocator.max_size()));
}
/*********************************************
* OPERATORS
*********************************************/
//std::ostream & operator<<(std::ostream & o, vector const & rhs)
//{
// o << rhs.getFoo();
// return (o);
//}
/*********************************************
* NESTED CLASS
*********************************************/
//void vector::Class::function() {}
/*********************************************
* STATICS
*********************************************/