244 lines
5.6 KiB
C++
244 lines
5.6 KiB
C++
|
|
//#include "vector.hpp"
|
|
|
|
#define COPLIEN_COLOR B_CYAN
|
|
|
|
namespace ft {
|
|
|
|
/*********************************************
|
|
* CONSTRUCTORS
|
|
*********************************************/
|
|
|
|
// template <class InputIterator>
|
|
// vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());
|
|
|
|
template <class T, class Allocator>
|
|
vector<T, Allocator>::
|
|
vector( const Allocator & alloc )
|
|
: _size(0)
|
|
, _capacity(0)
|
|
, _mem_ptr(NULL)
|
|
, _allocator(alloc)
|
|
{
|
|
// std::cout << COPLIEN_COLOR "vector constructor" RESET "\n";
|
|
return;
|
|
}
|
|
|
|
template <class T, class Allocator>
|
|
vector<T, Allocator>::
|
|
vector( size_type n, const T & val, const Allocator & alloc )
|
|
: _size(0)
|
|
, _capacity(0)
|
|
, _mem_ptr(NULL)
|
|
, _allocator(alloc)
|
|
{
|
|
// std::cout << COPLIEN_COLOR "vector constructor" RESET "\n";
|
|
assign(n, val);
|
|
return;
|
|
}
|
|
|
|
template <class T, class Allocator>
|
|
vector<T, Allocator>::
|
|
vector( vector const & src )
|
|
{
|
|
// std::cout << COPLIEN_COLOR "vector copy constructor" RESET "\n";
|
|
*this = src;
|
|
return;
|
|
}
|
|
|
|
/*********************************************
|
|
* DESTRUCTORS
|
|
*********************************************/
|
|
|
|
template <class T, class Allocator>
|
|
vector<T, Allocator>::
|
|
~vector()
|
|
{
|
|
// std::cout << COPLIEN_COLOR "vector destructor" RESET "\n";
|
|
return;
|
|
}
|
|
|
|
/*********************************************
|
|
* OPERATORS
|
|
*********************************************/
|
|
|
|
template <class T, class Allocator>
|
|
vector<T, Allocator> & vector<T, Allocator>::
|
|
operator=( vector const & rhs )
|
|
{
|
|
// Base::operator=(rhs);
|
|
if ( this != &rhs )
|
|
{
|
|
_size = rhs.size();
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
//std::ostream & operator<<(std::ostream & o, vector const & rhs)
|
|
//{
|
|
// o << rhs.getFoo();
|
|
// return (o);
|
|
//}
|
|
|
|
|
|
/*********************************************
|
|
* PUBLIC MEMBER FUNCTIONS
|
|
*********************************************/
|
|
|
|
/*************
|
|
* iterators :
|
|
*************/
|
|
// begin -------------------------------------
|
|
template <class T, class Allocator>
|
|
typename vector<T, Allocator>::iterator vector<T, Allocator>::
|
|
begin()
|
|
{
|
|
return _mem_ptr;
|
|
}
|
|
// end ---------------------------------------
|
|
template <class T, class Allocator>
|
|
typename vector<T, Allocator>::iterator vector<T, Allocator>::
|
|
end()
|
|
{
|
|
return &_mem_ptr[_size];
|
|
}
|
|
// rbegin ------------------------------------
|
|
// rend --------------------------------------
|
|
|
|
/************
|
|
* capacity :
|
|
************/
|
|
// size --------------------------------------
|
|
template <class T, class Allocator>
|
|
typename vector<T, Allocator>::size_type vector<T, Allocator>::
|
|
size( ) const
|
|
{
|
|
return _size;
|
|
}
|
|
// max_size ----------------------------------
|
|
// resize ------------------------------------
|
|
// capacity ----------------------------------
|
|
template <class T, class Allocator>
|
|
typename vector<T, Allocator>::size_type vector<T, Allocator>::
|
|
capacity() const
|
|
{
|
|
return _capacity;
|
|
}
|
|
// empty -------------------------------------
|
|
// reserve -----------------------------------
|
|
template <class T, class Allocator>
|
|
void vector<T, Allocator>::
|
|
reserve( size_type new_cap )
|
|
{
|
|
value_type * tmp_ptr;
|
|
|
|
if (new_cap > _allocator.max_size())
|
|
throw std::length_error("reserve: new_cap > max_size");
|
|
if (_capacity == _allocator.max_size())
|
|
throw std::length_error("reserve: capacity == max_size");
|
|
if (new_cap <= _capacity)
|
|
return ;
|
|
|
|
_capacity = new_cap;
|
|
tmp_ptr = _allocator.allocate(new_cap);
|
|
|
|
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);
|
|
}
|
|
_mem_ptr = tmp_ptr;
|
|
}
|
|
|
|
/******************
|
|
* element access :
|
|
******************/
|
|
// operator[] --------------------------------
|
|
template <class T, class Allocator>
|
|
typename vector<T, Allocator>::reference vector<T, Allocator>::
|
|
operator[](size_type n)
|
|
{
|
|
return _mem_ptr[n];
|
|
}
|
|
// at ----------------------------------------
|
|
// front -------------------------------------
|
|
// back --------------------------------------
|
|
|
|
/*************
|
|
* modifiers :
|
|
*************/
|
|
// assign ------------------------------------
|
|
template <class T, class Allocator>
|
|
void vector<T, Allocator>::
|
|
assign( size_type n, const T & val )
|
|
{
|
|
if (n > _allocator.max_size())
|
|
throw std::length_error("assign: n > max_size");
|
|
|
|
value_type * tmp_ptr;
|
|
|
|
_destroy(begin(), end());
|
|
if (n > _capacity)
|
|
{
|
|
_capacity = n;
|
|
tmp_ptr = _allocator.allocate(n);
|
|
if (_mem_ptr)
|
|
_allocator.deallocate(_mem_ptr, _capacity);
|
|
_mem_ptr = tmp_ptr;
|
|
}
|
|
_size = n;
|
|
while (n)
|
|
_allocator.construct(&_mem_ptr[--n], val);
|
|
}
|
|
// push_back ---------------------------------
|
|
template <class T, class Allocator>
|
|
void vector<T, Allocator>::
|
|
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++;
|
|
}
|
|
// pop_back ----------------------------------
|
|
// insert ------------------------------------
|
|
// erase -------------------------------------
|
|
// swap --------------------------------------
|
|
// clear -------------------------------------
|
|
|
|
|
|
|
|
|
|
/*********************************************
|
|
* PRIVATE MEMBER FUNCTIONS
|
|
*********************************************/
|
|
|
|
template <class T, class Allocator>
|
|
void vector<T, Allocator>::
|
|
_destroy(iterator first, iterator last)
|
|
{
|
|
while (first != last)
|
|
{
|
|
_allocator.destroy(first);
|
|
first++;
|
|
}
|
|
}
|
|
|
|
/*********************************************
|
|
* NESTED CLASS
|
|
*********************************************/
|
|
|
|
//void vector::Class::function() {}
|
|
|
|
/*********************************************
|
|
* STATICS
|
|
*********************************************/
|
|
|
|
//std::string const vector::_bar = "bar";
|
|
|
|
}
|