nicolas-arnaud
2 years ago
4 changed files with 282 additions and 209 deletions
@ -0,0 +1,121 @@ |
|||||
|
#pragma once |
||||
|
#include "iterator.hpp" |
||||
|
|
||||
|
namespace ft { |
||||
|
template < class Iterator > class reverse_iterator { |
||||
|
public: |
||||
|
typedef Iterator iterator_type; |
||||
|
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; |
||||
|
|
||||
|
private: |
||||
|
Iterator _base; |
||||
|
|
||||
|
public: |
||||
|
reverse_iterator(Iterator value = NULL) : _base(value) {} |
||||
|
|
||||
|
template < typename T > |
||||
|
reverse_iterator(const reverse_iterator< T > &iter) : _base(iter.base()) {} |
||||
|
|
||||
|
reverse_iterator &operator=(const reverse_iterator &other) { |
||||
|
_base = other._base; |
||||
|
return *this; |
||||
|
} |
||||
|
|
||||
|
Iterator base(void) const { return _base; } |
||||
|
|
||||
|
reference operator*(void) const { |
||||
|
Iterator ret = _base; |
||||
|
return *(--ret); |
||||
|
} |
||||
|
reverse_iterator operator+(const difference_type n) const { |
||||
|
return (reverse_iterator(_base - n)); |
||||
|
} |
||||
|
reverse_iterator &operator++(void) { |
||||
|
--_base; |
||||
|
return *this; |
||||
|
} |
||||
|
reverse_iterator operator++(int) const { |
||||
|
reverse_iterator ret(*this); |
||||
|
--(*this); |
||||
|
return ret; |
||||
|
} |
||||
|
reverse_iterator &operator+=(const difference_type n) { |
||||
|
_base -= n; |
||||
|
return *this; |
||||
|
} |
||||
|
|
||||
|
reverse_iterator operator-(const difference_type n) const { |
||||
|
return (reverse_iterator(_base + n)); |
||||
|
} |
||||
|
reverse_iterator &operator--(void) { |
||||
|
++_base; |
||||
|
return *this; |
||||
|
} |
||||
|
reverse_iterator operator--(int) { |
||||
|
reverse_iterator ret(*this); |
||||
|
++(*this); |
||||
|
return ret; |
||||
|
} |
||||
|
reverse_iterator &operator-=(const difference_type n) { |
||||
|
_base += n; |
||||
|
return *this; |
||||
|
} |
||||
|
|
||||
|
pointer operator->(void) const { return &(operator*()); } |
||||
|
reference operator[](const difference_type n) const { return base()[-n-1]; } |
||||
|
|
||||
|
template < typename It > |
||||
|
friend reverse_iterator operator+(const int n, |
||||
|
const reverse_iterator< It > &op) { |
||||
|
return reverse_iterator(op._base - n); |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
template < typename It1, typename It2 > |
||||
|
bool operator==(const reverse_iterator< It1 > &lhs, |
||||
|
const reverse_iterator< It2 > &rhs) { |
||||
|
return (lhs.base() == rhs.base()); |
||||
|
} |
||||
|
|
||||
|
template < typename It1, typename It2 > |
||||
|
bool operator!=(const reverse_iterator< It1 > &lhs, |
||||
|
const reverse_iterator< It2 > &rhs) { |
||||
|
return (lhs.base() != rhs.base()); |
||||
|
} |
||||
|
|
||||
|
template < typename It1, typename It2 > |
||||
|
bool operator<(const reverse_iterator< It1 > &lhs, |
||||
|
const reverse_iterator< It2 > &rhs) { |
||||
|
return (lhs.base() > rhs.base()); |
||||
|
} |
||||
|
|
||||
|
template < typename It1, typename It2 > |
||||
|
bool operator<=(const reverse_iterator< It1 > &lhs, |
||||
|
const reverse_iterator< It2 > &rhs) { |
||||
|
return (lhs.base() >= rhs.base()); |
||||
|
} |
||||
|
|
||||
|
template < typename It1, typename It2 > |
||||
|
bool operator>(const reverse_iterator< It1 > &lhs, |
||||
|
const reverse_iterator< It2 > &rhs) { |
||||
|
return (lhs.base() < rhs.base()); |
||||
|
} |
||||
|
|
||||
|
template < typename It1, typename It2 > |
||||
|
bool operator>=(const reverse_iterator< It1 > &lhs, |
||||
|
const reverse_iterator< It2 > &rhs) { |
||||
|
return (lhs.base() <= rhs.base()); |
||||
|
} |
||||
|
|
||||
|
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(); |
||||
|
} |
||||
|
|
||||
|
} // namespace ft
|
Loading…
Reference in new issue