-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_access_iterator.hpp
132 lines (102 loc) · 3.61 KB
/
random_access_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
126
127
128
129
130
131
132
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* random_access_iterator.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariahi <ariahi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/23 18:14:08 by ariahi #+# #+# */
/* Updated: 2023/01/24 09:25:20 by ariahi ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef RANDOM_ACCESS_ITERATOR_HPP
#define RANDOM_ACCESS_ITERATOR_HPP
#include "iterator_traits.hpp"
#include <cstddef>
namespace ft
{
template <class T, class Category = ft::random_access_iterator_tag,
class Distance = ptrdiff_t, class Pointer = T*, class Reference = T&>
class random_access_iterator : ft::iterator<T, ft::random_access_iterator_tag>
{
public:
typedef T value_type;
typedef Distance difference_type;
typedef Pointer pointer;
typedef Reference reference;
typedef Category iterator_category;
private:
pointer _IT;
public:
random_access_iterator() {}
random_access_iterator(pointer p) : _IT(p) {}
random_access_iterator(const random_access_iterator &other) : _IT(other._IT) {}
~random_access_iterator() {}
random_access_iterator &operator=(const random_access_iterator &other)
{
_IT = other._IT;
return (*this);
}
reference operator*() const {return (*_IT);}
pointer operator->() const {return (_IT);}
reference operator[](difference_type n) const {return (_IT[n]);}
difference_type operator-(const random_access_iterator &other) const {return (_IT - other._IT);}
difference_type operator+(const random_access_iterator &other) const {return (_IT + other._IT);}
random_access_iterator &operator++()
{
_IT++;
return (*this);
}
random_access_iterator &operator--()
{
_IT--;
return (*this);
}
random_access_iterator operator++(int)
{
random_access_iterator tmp(*this);
_IT++;
return (tmp);
}
random_access_iterator operator--(int)
{
random_access_iterator tmp(*this);
_IT--;
return (tmp);
}
random_access_iterator &operator+=(const difference_type &n)
{
_IT += n;
return (*this);
}
random_access_iterator &operator-=(const difference_type &n)
{
_IT -= n;
return (*this);
}
random_access_iterator operator+(difference_type n) const
{
random_access_iterator tmp(*this);
tmp += n;
return (tmp);
}
random_access_iterator operator-(difference_type n) const
{
random_access_iterator tmp(*this);
tmp -= n;
return (tmp);
}
bool operator==(const random_access_iterator &other) const {return (_IT ==&(*other));}
bool operator!=(const random_access_iterator &other) const {return (_IT !=&(*other));}
bool operator>(const random_access_iterator &other) const {return (_IT >&(*other));}
bool operator<(const random_access_iterator &other) const {return (_IT <&(*other));}
bool operator<=(const random_access_iterator &other) const {return (_IT <=&(*other));}
bool operator>=(const random_access_iterator &other) const {return (_IT >=&(*other));}
};
template <class InputIterator, class Distance>
void advane(InputIterator &IT, Distance n)
{
IT += n;
}
} // namespace ft
#endif