-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec_iterator.hpp
125 lines (95 loc) · 3.8 KB
/
vec_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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_iterator.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: graja <graja@student.42wolfsburg.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/25 13:25:31 by graja #+# #+# */
/* Updated: 2022/05/07 18:24:36 by graja ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef VEC_ITERATOR_H
# define VEC_ITERATOR_H
# include "iterator.hpp"
# include "utility.hpp"
namespace ft
{
template <typename T, bool is_const>
class V_iterator : public ft::iterator<std::random_access_iterator_tag, T>
{
public:
typedef T value_type;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef const value_type & const_reference;
typedef typename choose<is_const, const T &, T &>::type reference;
typedef typename choose<is_const, const T *, T *>::type pointer;
private:
pointer _p;
public:
V_iterator(void) :_p(NULL) {}
V_iterator(pointer x) :_p(x) {}
~V_iterator(void) {}
template<typename U, bool cval>
V_iterator(const V_iterator<U, cval> & mit) : _p(mit.getPtr()) {}
V_iterator & operator=(const V_iterator & right) {this->_p = right._p; return *this;}
pointer getPtr(void) const {return _p;}
V_iterator& operator++() {++_p;return *this;}
V_iterator operator++(int) {V_iterator tmp(*this); operator++(); return tmp;}
V_iterator& operator--() {--_p;return *this;}
V_iterator operator--(int) {V_iterator tmp(*this); operator--(); return tmp;}
V_iterator & operator+=(const size_type n) {_p = _p + n; return (*this);}
V_iterator & operator-=(const size_type n) {_p = _p - n; return (*this);}
V_iterator operator-(const size_type n)
{
V_iterator tmp(this->_p);
tmp._p = this->_p - n;
return (tmp);
}
V_iterator operator+(const size_type n)
{
V_iterator tmp(this->_p);
tmp._p = this->_p + n;
return (tmp);
}
template<typename U, bool cval>
difference_type operator-(const V_iterator<U, cval> & rhs) const
{return _p - rhs.getPtr();}
template<typename U, bool cval>
bool operator==(const V_iterator<U, cval> & rhs) const
{return _p == rhs.getPtr();}
template<typename U, bool cval>
bool operator!=(const V_iterator<U, cval> & rhs) const
{return _p != rhs.getPtr();}
template<typename U, bool cval>
bool operator>(const V_iterator<U, cval> & rhs) const
{return _p > rhs.getPtr();}
template<typename U, bool cval>
bool operator<(const V_iterator<U, cval> & rhs) const
{return _p < rhs.getPtr();}
template<typename U, bool cval>
bool operator>=(const V_iterator<U, cval> & rhs) const
{return _p >= rhs.getPtr();}
template<typename U, bool cval>
bool operator<=(const V_iterator<U, cval> & rhs) const
{return _p <= rhs.getPtr();}
reference operator*() {return *_p;}
reference operator->() {return *_p;}
value_type operator[](const size_type n) {return (*(_p + n));}
//non member overloads
friend V_iterator operator+(const size_type n, const V_iterator right)
{
V_iterator tmp(right);
tmp._p = right._p + n;
return (tmp);
}
friend V_iterator operator-(const size_type n, const V_iterator right)
{
V_iterator tmp(right);
tmp._p = right._p - n;
return (tmp);
}
};
} //end namespace
#endif