Files
42_INT_11_ft_containers/templates/vector.tpp
2022-06-09 14:14:13 +02:00

305 lines
7.1 KiB
C++

#define VT_TPL template <class T, class Allocator>
#define VT vector<T, Allocator>
namespace ft {
/*********************************************
* COPLIENS
*********************************************/
// constructors ------------------------------
VT_TPL VT::
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) {
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) {
assign(first, last);
return;
}
VT_TPL VT::
vector( vector const & src ) {
*this = src;
return;
}
// destructors -------------------------------
VT_TPL VT::
~vector() { return; }
// operator= ---------------------------------
VT_TPL VT & VT::
operator=( vector const & rhs ) {
//Base::operator=(rhs);
if ( this != &rhs )
{
_size = rhs.size();
}
return *this;
}
/*********************************************
* PUBLIC MEMBER FUNCTIONS
*********************************************/
/*************
* iterators :
*************/
// begin -------------------------------------
VT_TPL typename VT::iterator VT::
begin() { return _mem_ptr; }
//const_iterator begin() const;
// end ---------------------------------------
VT_TPL typename VT::iterator VT::
end() { return &_mem_ptr[_size]; }
//const_iterator end() const;
// rbegin ------------------------------------
//reverse_iterator rbegin();
//const_reverse_iterator rbegin() const;
// rend --------------------------------------
//reverse_iterator rend();
//const_reverse_iterator rend() const;
/************
* capacity :
************/
// size --------------------------------------
VT_TPL typename VT::size_type VT::
size( ) const { return _size; }
// max_size ----------------------------------
VT_TPL typename VT::size_type VT::
max_size() const { return (_allocator.max_size()); }
// resize ------------------------------------
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; }
// empty -------------------------------------
VT_TPL bool VT::
empty() const { return (_size == 0); }
// reserve -----------------------------------
VT_TPL void VT::
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)
{
for (size_type i = 0; i < _size; i++)
tmp_ptr[i] = _mem_ptr[i];
_destroy(begin(), end());
_allocator.deallocate(_mem_ptr, _capacity);
}
_mem_ptr = tmp_ptr;
}
/******************
* element access :
******************/
// operator[] --------------------------------
VT_TPL typename VT::reference VT::
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 ----------------------------------------
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 -------------------------------------
VT_TPL typename VT::reference VT::
front() { return (*_mem_ptr); }
VT_TPL typename VT::const_reference VT::
front() const { return (*_mem_ptr); }
// back --------------------------------------
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 :
*************/
// assign ------------------------------------
VT_TPL template <class InputIterator>
typename enable_if< !is_integral<InputIterator>::value,void >::type VT::
assign( InputIterator first, InputIterator last) {
InputIterator tmp = first;
unsigned int range = 0;
clear();
while (tmp++ != last)
range++;
if (range >= _capacity)
_increment_capacity(range);
while (first != last)
{
_allocator.construct(&_mem_ptr[_size], *first);
first++;
_size++;
}
}
VT_TPL void VT::
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 ---------------------------------
VT_TPL void VT::
push_back( const value_type & element ) {
if (_size >= _capacity)
_increment_capacity(1);
_allocator.construct(&_mem_ptr[_size], element);
_size++;
}
// pop_back ----------------------------------
VT_TPL void VT::
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);
//template <class InputIterator>
// void insert(iterator position, InputIterator first, InputIterator last);
// erase -------------------------------------
//iterator erase(iterator position);
//iterator erase(iterator first, iterator last);
// swap --------------------------------------
//void swap(vector& x);
// clear -------------------------------------
VT_TPL void VT::
clear() { _destroy(begin(), end()); _size = 0; }
/*********************************************
* PRIVATE MEMBER FUNCTIONS
*********************************************/
VT_TPL void VT::
_destroy(iterator first, iterator last) {
while (first != last)
{
_allocator.destroy(first);
first++;
}
}
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
*********************************************/
//std::string const vector::_bar = "bar";
} // namespace ft
#undef VT
#undef VT_TPL