|
|
@ -86,14 +86,15 @@ public: |
|
|
|
}; |
|
|
|
} |
|
|
|
template < class It > |
|
|
|
void |
|
|
|
assign(It first, It last, |
|
|
|
void assign(It first, It last, |
|
|
|
typename ft::enable_if< !is_integral< It >::value, bool >::type = 0) { |
|
|
|
size_type count = ft::distance(first, last) - 1; |
|
|
|
resize(count); |
|
|
|
while (count-- > 0) { |
|
|
|
_alloc.destroy(_begin + count); |
|
|
|
_alloc.construct(_begin + count, *(--last)); |
|
|
|
size_t i = 0; |
|
|
|
while (i < count) { |
|
|
|
_alloc.destroy(_begin + i); |
|
|
|
_alloc.construct(_begin + i, *(first++)); |
|
|
|
i++; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -147,16 +148,23 @@ public: |
|
|
|
void reserve(size_type new_cap) { |
|
|
|
if (new_cap > max_size()) |
|
|
|
throw(std::length_error("vector::reserve: new_cap > max_size")); |
|
|
|
if (new_cap < _capacity) |
|
|
|
return ; |
|
|
|
Alloc new_alloc; |
|
|
|
pointer new_begin; |
|
|
|
new_begin = new_alloc.allocate(new_cap); |
|
|
|
for (size_type i = 0; i < _size; i++) |
|
|
|
size_t new_size; |
|
|
|
new_size = new_cap < _capacity ? new_cap : _size; |
|
|
|
for (size_type i = 0; i < new_size; i++) |
|
|
|
new_alloc.construct(new_begin + i, *(_begin + i)); |
|
|
|
clear(); |
|
|
|
_alloc.deallocate(_begin, _capacity); |
|
|
|
if (!empty()) |
|
|
|
clear(); |
|
|
|
if (_capacity) |
|
|
|
_alloc.deallocate(_begin, _capacity); |
|
|
|
_alloc = new_alloc; |
|
|
|
_begin = new_begin; |
|
|
|
_capacity = new_cap; |
|
|
|
_size = new_size; |
|
|
|
} |
|
|
|
size_type capacity(void) const { return _capacity; } |
|
|
|
|
|
|
@ -166,40 +174,39 @@ public: |
|
|
|
_alloc.destroy(_begin + i); |
|
|
|
_size = 0; |
|
|
|
} |
|
|
|
iterator insert(iterator pos, const_reference value) { |
|
|
|
|
|
|
|
iterator insert(const_iterator pos, const_reference value) { |
|
|
|
if (_size == _capacity) |
|
|
|
resize(_size + 1); |
|
|
|
iterator it = end() + 1; |
|
|
|
; |
|
|
|
while (--it >= pos) |
|
|
|
iterator it = end(); |
|
|
|
while (--it > pos) |
|
|
|
*it = *(it - 1); |
|
|
|
*it = value; |
|
|
|
return _begin; |
|
|
|
} |
|
|
|
iterator insert(iterator pos, size_type count, const_reference value) { |
|
|
|
iterator insert(const_iterator pos, size_type count, const_reference value) { |
|
|
|
if ((_size + count) > _capacity) |
|
|
|
resize(_size + count); |
|
|
|
iterator it = end() + count; |
|
|
|
iterator it = end(); |
|
|
|
while (--it >= (pos + count)) |
|
|
|
*it = *(it - count); |
|
|
|
while (it >= pos) { |
|
|
|
*it = value; |
|
|
|
it--; |
|
|
|
} |
|
|
|
return _begin; |
|
|
|
while (it >= pos) |
|
|
|
*(it--) = value; |
|
|
|
return it; |
|
|
|
} |
|
|
|
template < class It > iterator insert(iterator pos, It first, It last) { |
|
|
|
template < class It > iterator insert(const_iterator pos, It first, It last, |
|
|
|
typename ft::enable_if< !is_integral< It >::value, bool >::type = 0) { |
|
|
|
size_type count = ft::distance(first, last) - 1; |
|
|
|
if ((_size + count) > _capacity) |
|
|
|
resize(_size + count); |
|
|
|
resize(_size + count); |
|
|
|
iterator it = end(); |
|
|
|
while (--it >= (pos + count)) |
|
|
|
while (--it > (pos + count)) |
|
|
|
*it = *(it - count); |
|
|
|
while (it >= pos) { |
|
|
|
*(--it) = *(--last); |
|
|
|
while (it > pos) { |
|
|
|
*(it--) = *(last--); |
|
|
|
} |
|
|
|
return _begin; |
|
|
|
return it; |
|
|
|
} |
|
|
|
|
|
|
|
iterator erase(iterator pos) { |
|
|
|
while (pos < end()) { |
|
|
|
_alloc.destroy(pos.base()); |
|
|
@ -220,6 +227,7 @@ public: |
|
|
|
} |
|
|
|
return _begin; |
|
|
|
} |
|
|
|
|
|
|
|
void push_back(const_reference value) { |
|
|
|
if (_size == _capacity) |
|
|
|
resize(_size + 1); |
|
|
@ -240,8 +248,9 @@ public: |
|
|
|
_alloc.construct(_begin + _size++, value); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void swap(vector &other) { |
|
|
|
vector< T > tmp = other; |
|
|
|
vector< T > tmp(other); |
|
|
|
other = this; |
|
|
|
this = tmp; |
|
|
|
} |
|
|
|