-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_iterator.hpp
93 lines (72 loc) · 3.23 KB
/
random_iterator.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#ifndef RANDOM_ITERATOR_HPP
# define RANDOM_ITERATOR_HPP
#include "iterator_traits.hpp"
//iterators
//https://leimao.github.io/blog/CPP-Const-Iterator/#Implementation-Without-Code-Duplication
namespace ft{
// iterator used by vector
template <class T>
class random_iterator
{
public:
typedef typename ft::iterator_traits<T>::iterator_category iterator_category;
typedef typename ft::iterator_traits<T>::difference_type difference_type;
typedef typename ft::iterator_traits<T>::value_type value_type;
typedef typename ft::iterator_traits<T>::pointer pointer;
typedef typename ft::iterator_traits<T>::reference reference;
random_iterator() : p(0) {}
random_iterator(pointer ptr) : p(ptr) {}
//conversion from non-const C to const T
template<class C>
random_iterator(const random_iterator<C> &it) : p(&(*it)) {}
random_iterator(random_iterator<const pointer> &it) : p(it.p) {}
reference operator*() const { return *this->p; }
pointer operator->() { return p; }
random_iterator& operator++() { p++; return *this; }
random_iterator operator++(int) {
random_iterator p_cpy = *this;
++(*this);
return (p_cpy);
}
random_iterator& operator--() { p--; return *this; }
random_iterator operator--(int) {
random_iterator p_cpy = *this;
--(*this);
return (p_cpy);
}
random_iterator& operator+=(difference_type i) { p += i; return (*this); }
random_iterator& operator-=(difference_type i) { p -= i; return (*this); }
random_iterator operator+(difference_type i) const { pointer p1; p1 = p + i; return (p1);};
random_iterator operator-(difference_type i) const { pointer p1; p1 = p - i; return (p1);};
reference operator[](difference_type n) const { return (p[n]);}
pointer getData(void) const { return p; };
private:
pointer p;
};
//T and U
template <class T, class U>
bool operator==(const random_iterator<T> &a, const random_iterator<U> &b){
return a.getData() == b.getData(); }
template <class T, class U>
typename ft::random_iterator<T>::difference_type operator-(const ft::random_iterator<T> &a, const ft::random_iterator<U> &b) { return (a.getData() - b.getData()); }
template <class T, class U>
bool operator<(const ft::random_iterator<T> &a, const ft::random_iterator<U> &b) { return (a.getData() < b.getData()); }
template <class T, class U>
bool operator>(const ft::random_iterator<T> &a, const ft::random_iterator<U> &b) { return b.getData() < a.getData(); }
template <class T, class U>
bool operator<=(const ft::random_iterator<T> &a, const ft::random_iterator<U> &b) { return (a.getData() <= b.getData()); }
template <class T, class U>
bool operator>=(const ft::random_iterator<T> &a, const ft::random_iterator<U> &b) { return b.getData() <= a.getData(); }
template <class T, class U>
bool operator!=(const ft::random_iterator<T> &a, const ft::random_iterator<U> &b){
return a.getData() != b.getData(); }
template <class T>
typename ft::random_iterator<T> operator+(typename ft::random_iterator<T>::difference_type n, const ft::random_iterator<T> &a) {
return (a.operator+(n));
}
template <class T>
typename ft::random_iterator<T> operator-(typename ft::random_iterator<T>::difference_type n, const ft::random_iterator<T> &a) {
return (a.operator-(n));
}
}
#endif