Browse Source

save2 22-12-15

master
nicolas-arnaud 2 years ago
parent
commit
ff8142c50e
  1. 5
      includes/iterator.hpp
  2. 36
      includes/reverse_iterator.hpp
  3. 63
      includes/vector.hpp

5
includes/iterator.hpp

@ -72,9 +72,7 @@ public:
}
pointer base(void) { return _ptr; }
pointer operator->(void) { return _ptr; }
reference operator*(void) { return *_ptr; }
reference operator[](const difference_type n) { return *(_ptr + n); }
random_access_iterator operator+(const difference_type n) const {
return (random_access_iterator(_ptr + n));
@ -125,6 +123,9 @@ public:
return *this;
}
pointer operator->(void) { return _ptr; }
reference operator[](const difference_type n) { return *(_ptr + n); }
friend bool operator==(const random_access_iterator &left,
const random_access_iterator &right) {
return left._ptr == right._ptr;

36
includes/reverse_iterator.hpp

@ -8,51 +8,51 @@ public:
typedef typename Iterator::iterator_category iterator_category;
typedef typename Iterator::value_type value_type;
typedef typename Iterator::difference_type difference_type;
typedef typename Iterator::pointer *pointer;
typedef typename Iterator::reference &reference;
typedef typename Iterator::pointer pointer;
typedef typename Iterator::reference reference;
private:
Iterator _base;
protected:
Iterator current;
public:
reverse_iterator(Iterator value = NULL) : _base(value) {}
reverse_iterator(Iterator value = NULL) : current(value) {}
template < typename T >
reverse_iterator(const reverse_iterator< T > &iter) : _base(iter.base()) {}
reverse_iterator(const reverse_iterator< T > &iter) : current(iter.base()) {}
reverse_iterator &operator=(const reverse_iterator &other) {
_base = other._base;
current = other.current;
return *this;
}
Iterator base(void) const { return _base; }
Iterator base(void) const { return current; }
reference operator*(void) const {
Iterator ret = _base;
Iterator ret = current;
return *(--ret);
}
reverse_iterator operator+(const difference_type n) const {
return (reverse_iterator(_base - n));
return (reverse_iterator(current - n));
}
reverse_iterator &operator++(void) {
--_base;
--current;
return *this;
}
reverse_iterator operator++(int) const {
reverse_iterator operator++(int) {
reverse_iterator ret(*this);
--(*this);
return ret;
}
reverse_iterator &operator+=(const difference_type n) {
_base -= n;
current -= n;
return *this;
}
reverse_iterator operator-(const difference_type n) const {
return (reverse_iterator(_base + n));
return (reverse_iterator(current + n));
}
reverse_iterator &operator--(void) {
++_base;
++current;
return *this;
}
reverse_iterator operator--(int) {
@ -61,7 +61,7 @@ public:
return ret;
}
reverse_iterator &operator-=(const difference_type n) {
_base += n;
current += n;
return *this;
}
@ -71,7 +71,7 @@ public:
template < typename It >
friend reverse_iterator operator+(const int n,
const reverse_iterator< It > &op) {
return reverse_iterator(op._base - n);
return reverse_iterator(op.current - n);
}
};
@ -115,7 +115,7 @@ template < class It1, class It2 >
typename reverse_iterator< It1 >::difference_type
operator-(const reverse_iterator< It1 > &lhs,
const reverse_iterator< It2 > &rhs) {
return lhs.base() - rhs.base();
return -(lhs.base() - rhs.base());
}
} // namespace ft

63
includes/vector.hpp

@ -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;
}

Loading…
Cancel
Save