You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
3.3 KiB
121 lines
3.3 KiB
#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;
|
|
|
|
protected:
|
|
Iterator current;
|
|
|
|
public:
|
|
reverse_iterator(Iterator value = NULL) : current(value) {}
|
|
|
|
template < typename T >
|
|
reverse_iterator(const reverse_iterator< T > &iter) : current(iter.base()) {}
|
|
|
|
reverse_iterator &operator=(const reverse_iterator &other) {
|
|
current = other.current;
|
|
return *this;
|
|
}
|
|
|
|
Iterator base(void) const { return current; }
|
|
|
|
reference operator*(void) const {
|
|
Iterator ret = current;
|
|
return *(--ret);
|
|
}
|
|
reverse_iterator operator+(const difference_type n) const {
|
|
return (reverse_iterator(current - n));
|
|
}
|
|
reverse_iterator &operator++(void) {
|
|
--current;
|
|
return *this;
|
|
}
|
|
reverse_iterator operator++(int) {
|
|
reverse_iterator ret(*this);
|
|
--(*this);
|
|
return ret;
|
|
}
|
|
reverse_iterator &operator+=(const difference_type n) {
|
|
current -= n;
|
|
return *this;
|
|
}
|
|
|
|
reverse_iterator operator-(const difference_type n) const {
|
|
return (reverse_iterator(current + n));
|
|
}
|
|
reverse_iterator &operator--(void) {
|
|
++current;
|
|
return *this;
|
|
}
|
|
reverse_iterator operator--(int) {
|
|
reverse_iterator ret(*this);
|
|
++(*this);
|
|
return ret;
|
|
}
|
|
reverse_iterator &operator-=(const difference_type n) {
|
|
current += 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.current - 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
|
|
|