497 lines
11 KiB
C++
497 lines
11 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;
|
|
}
|
|
// copy constructor --------------------------
|
|
VT_TPL VT::
|
|
vector( vector const & src )
|
|
: _size(0)
|
|
, _capacity(0)
|
|
, _mem_ptr(NULL)
|
|
, _allocator(src._allocator) {
|
|
|
|
*this = src;
|
|
return;
|
|
}
|
|
// destructors -------------------------------
|
|
VT_TPL VT::
|
|
~vector() {
|
|
|
|
clear();
|
|
_allocator.deallocate(_mem_ptr, _capacity);
|
|
return;
|
|
}
|
|
// operator= ---------------------------------
|
|
VT_TPL VT & VT::
|
|
operator=( vector const & rhs ) {
|
|
|
|
vector new_vector;
|
|
|
|
if ( this != &rhs )
|
|
{
|
|
new_vector.reserve(_capacity);
|
|
new_vector.assign(rhs.begin(), rhs.end());
|
|
swap(new_vector);
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
|
|
|
|
/*************
|
|
* iterators :
|
|
*************/
|
|
// begin -------------------------------------
|
|
VT_TPL typename VT::iterator VT::
|
|
begin() { return _mem_ptr; }
|
|
VT_TPL typename VT::const_iterator VT::
|
|
begin() const { return _mem_ptr; }
|
|
// end ---------------------------------------
|
|
VT_TPL typename VT::iterator VT::
|
|
end() { return &_mem_ptr[_size]; }
|
|
VT_TPL typename VT::const_iterator VT::
|
|
end() const { return &_mem_ptr[_size]; }
|
|
// rbegin ------------------------------------
|
|
VT_TPL typename VT::reverse_iterator VT::
|
|
rbegin() { return reverse_iterator(end()); }
|
|
VT_TPL typename VT::const_reverse_iterator VT::
|
|
rbegin() const { return const_reverse_iterator(end()); }
|
|
// rend --------------------------------------
|
|
VT_TPL typename VT::reverse_iterator VT::
|
|
rend() { return reverse_iterator(begin()); }
|
|
VT_TPL typename VT::const_reverse_iterator VT::
|
|
rend() const { return const_reverse_iterator(begin()); }
|
|
|
|
|
|
|
|
/************
|
|
* 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;
|
|
value_type * old_ptr = _mem_ptr;
|
|
iterator first = begin();
|
|
iterator last = end();
|
|
|
|
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);
|
|
|
|
_mem_ptr = tmp_ptr;
|
|
if (old_ptr)
|
|
{
|
|
_size = 0;
|
|
assign(first, last);
|
|
_destroy(begin(), end());
|
|
_allocator.deallocate(old_ptr, _capacity);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/******************
|
|
* 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 ------------------------------------
|
|
VT_TPL typename VT::iterator VT::
|
|
insert(iterator position, const value_type& val) {
|
|
|
|
difference_type distance;
|
|
iterator it;
|
|
|
|
if (_size + 1 > _capacity)
|
|
{
|
|
distance = position - begin();
|
|
_increment_capacity(1);
|
|
position = begin() + distance;
|
|
}
|
|
it = end();
|
|
if (position != it)
|
|
{
|
|
_allocator.construct(it, *(it - 1));
|
|
while (it-- != position)
|
|
*(it + 1) = *it;
|
|
_allocator.destroy(position);
|
|
}
|
|
_allocator.construct(position, val);
|
|
_size++;
|
|
return (position);
|
|
}
|
|
VT_TPL void VT::
|
|
insert(iterator position, size_type n, const value_type& val) {
|
|
|
|
difference_type distance;
|
|
iterator it_end;
|
|
iterator it;
|
|
|
|
if (_size + n > _capacity)
|
|
{
|
|
distance = position - begin();
|
|
_increment_capacity(n);
|
|
position = begin() + distance;
|
|
}
|
|
|
|
it_end = end();
|
|
if (position != it_end)
|
|
{
|
|
it = it_end;
|
|
while (it + n != it_end && it != position)
|
|
{
|
|
it--;
|
|
_allocator.construct(it + n, *it);
|
|
}
|
|
while (it != position)
|
|
{
|
|
it--;
|
|
*(it + n) = *it;
|
|
}
|
|
_destroy(position, std::min(position + n, it_end));
|
|
}
|
|
|
|
for (size_type i = 0; i < n; i++, position++)
|
|
_allocator.construct(position, val);
|
|
_size += n;
|
|
}
|
|
VT_TPL template <class InputIterator>
|
|
typename enable_if< !is_integral<InputIterator>::value,void >::type VT::
|
|
insert(iterator position, InputIterator first, InputIterator last) {
|
|
|
|
difference_type dist;
|
|
difference_type n;
|
|
iterator it_end;
|
|
iterator it;
|
|
|
|
n = std::distance(first, last);
|
|
if (_size + n > _capacity)
|
|
{
|
|
dist = position - begin();
|
|
_increment_capacity(n);
|
|
position = begin() + dist;
|
|
}
|
|
|
|
it_end = end();
|
|
if (position != it_end)
|
|
{
|
|
it = it_end;
|
|
while (it + n != it_end && it != position)
|
|
{
|
|
it--;
|
|
_allocator.construct(it + n, *it);
|
|
}
|
|
while (it != position)
|
|
{
|
|
it--;
|
|
*(it + n) = *it;
|
|
}
|
|
_destroy(position, std::min(position + n, it_end));
|
|
}
|
|
|
|
while (first != last)
|
|
{
|
|
_allocator.construct(position, *first);
|
|
++position;
|
|
++first;
|
|
}
|
|
|
|
_size += n;
|
|
}
|
|
// erase -------------------------------------
|
|
VT_TPL typename VT::iterator VT::
|
|
erase(iterator position) {
|
|
|
|
iterator i = position;
|
|
iterator it_end = end() - 1;
|
|
|
|
while (i != it_end)
|
|
{
|
|
*i = *(i + 1);
|
|
++i;
|
|
}
|
|
_allocator.destroy(it_end);
|
|
_size -= 1;
|
|
return (position);
|
|
}
|
|
VT_TPL typename VT::iterator VT::
|
|
erase(iterator first, iterator last) {
|
|
|
|
iterator it_end = end();
|
|
difference_type diff = std::distance(first, last);
|
|
|
|
if (diff <= 0)
|
|
return (first);
|
|
|
|
it_end = end();
|
|
while (last != it_end)
|
|
{
|
|
*first = *last;
|
|
first++;
|
|
last++;
|
|
}
|
|
_destroy(it_end - diff, it_end);
|
|
_size -= diff;
|
|
|
|
return (first);
|
|
}
|
|
// swap --------------------------------------
|
|
VT_TPL void VT::
|
|
swap(vector& x) {
|
|
|
|
T* tmp_mem_ptr;
|
|
size_type tmp_size;
|
|
size_type tmp_capacity;
|
|
|
|
tmp_mem_ptr = x._mem_ptr;
|
|
tmp_size = x._size;
|
|
tmp_capacity = x._capacity;
|
|
|
|
x._mem_ptr = _mem_ptr;
|
|
x._size = _size;
|
|
x._capacity = _capacity;
|
|
|
|
_mem_ptr = tmp_mem_ptr;
|
|
_size = tmp_size;
|
|
_capacity = tmp_capacity;
|
|
}
|
|
// clear -------------------------------------
|
|
VT_TPL void VT::
|
|
clear() { _destroy(begin(), end()); _size = 0; }
|
|
|
|
|
|
|
|
/*************
|
|
* allocator :
|
|
*************/
|
|
// get_allocator -----------------------------
|
|
VT_TPL typename VT::allocator_type VT::
|
|
get_allocator() const { return (_allocator); }
|
|
|
|
|
|
|
|
/*********************************************
|
|
* 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()));
|
|
}
|
|
|
|
|
|
|
|
/************************
|
|
* non-member functions :
|
|
************************/
|
|
// operator == -------------------------------
|
|
VT_TPL
|
|
bool operator== (const VT & lhs, const VT & rhs) {
|
|
|
|
if (lhs.size() != rhs.size())
|
|
return false;
|
|
return ft::equal(lhs.begin(), lhs.end(), rhs.begin());
|
|
}
|
|
// operator < --------------------------------
|
|
VT_TPL
|
|
bool operator< (const VT & lhs, const VT & rhs) {
|
|
|
|
return ft::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
|
|
}
|
|
// operator != -------------------------------
|
|
VT_TPL
|
|
bool operator!= (const VT & lhs, const VT & rhs) { return !(lhs == rhs); }
|
|
// operator <= -------------------------------
|
|
VT_TPL
|
|
bool operator<= (const VT & lhs, const VT & rhs) { return !(lhs > rhs); }
|
|
// operator > --------------------------------
|
|
VT_TPL
|
|
bool operator> (const VT & lhs, const VT & rhs) { return (rhs < lhs); }
|
|
// operator >= -------------------------------
|
|
VT_TPL
|
|
bool operator>= (const VT & lhs, const VT & rhs) { return !(lhs < rhs); }
|
|
// swap (vector) -------------------------------
|
|
VT_TPL
|
|
void swap (VT & lhs, VT & rhs) { lhs.swap(rhs); }
|
|
|
|
} // namespace ft
|
|
|
|
#undef VT
|
|
#undef VT_TPL
|