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.

122 lines
3.3 KiB

2 years ago
#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;
2 years ago
typedef typename Iterator::pointer pointer;
typedef typename Iterator::reference reference;
2 years ago
2 years ago
protected:
Iterator current;
2 years ago
public:
2 years ago
reverse_iterator(Iterator value = NULL) : current(value) {}
2 years ago
template < typename T >
2 years ago
reverse_iterator(const reverse_iterator< T > &iter) : current(iter.base()) {}
2 years ago
reverse_iterator &operator=(const reverse_iterator &other) {
2 years ago
current = other.current;
2 years ago
return *this;
}
2 years ago
Iterator base(void) const { return current; }
2 years ago
reference operator*(void) const {
2 years ago
Iterator ret = current;
2 years ago
return *(--ret);
}
reverse_iterator operator+(const difference_type n) const {
2 years ago
return (reverse_iterator(current - n));
2 years ago
}
reverse_iterator &operator++(void) {
2 years ago
--current;
2 years ago
return *this;
}
2 years ago
reverse_iterator operator++(int) {
2 years ago
reverse_iterator ret(*this);
--(*this);
return ret;
}
reverse_iterator &operator+=(const difference_type n) {
2 years ago
current -= n;
2 years ago
return *this;
}
reverse_iterator operator-(const difference_type n) const {
2 years ago
return (reverse_iterator(current + n));
2 years ago
}
reverse_iterator &operator--(void) {
2 years ago
++current;
2 years ago
return *this;
}
reverse_iterator operator--(int) {
reverse_iterator ret(*this);
++(*this);
return ret;
}
reverse_iterator &operator-=(const difference_type n) {
2 years ago
current += n;
2 years ago
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) {
2 years ago
return reverse_iterator(op.current - n);
2 years ago
}
};
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) {
2 years ago
return -(lhs.base() - rhs.base());
2 years ago
}
} // namespace ft