-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator.hpp
202 lines (188 loc) · 4.68 KB
/
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#pragma once
#include <iostream>
#include "iterator_traits.hpp"
#include "vector.hpp"
namespace ft
{
//?https://www.cplusplus.com/reference/iterator/InputIterator/
struct input_iterator_tag {};
//?https://www.cplusplus.com/reference/iterator/OutputIterator/
struct output_iterator_tag {};
//?https://www.cplusplus.com/reference/iterator/ForwardIterator/
struct forward_iterator_tag : public input_iterator_tag {};
//?https://www.cplusplus.com/reference/iterator/BidirectionalIterator/
struct bidirectional_iterator_tag : public forward_iterator_tag {};
//?https://www.cplusplus.com/reference/iterator/RandomAccessIterator/
struct random_access_iterator_tag : public bidirectional_iterator_tag {};
//?https://www.cplusplus.com/reference/iterator/iterator/
template <class Category, class T, class Distance = ptrdiff_t,
class Pointer = T*, class Reference = T&>
struct base_iterator
{
public:
typedef T value_type;
typedef Distance difference_type;
typedef Pointer pointer;
typedef Reference reference;
typedef Category iterator_category;
};
//?https://www.cplusplus.com/reference/iterator/
//?https://www.cplusplus.com/reference/iterator/RandomAccessIterator/
//!#Iterator
template <typename T>
class myiterator : public ft::base_iterator<std::random_access_iterator_tag, T>
{
public:
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef myiterator<const T> const_myiterator;
typedef ptrdiff_t difference_type;
myiterator(pointer abc = NULL)
{
_ptr = abc;
}
myiterator(myiterator const &it)
{
this->_ptr = it._ptr;
}
myiterator &operator=(myiterator const &it)
{
this->_ptr = it._ptr;
return (*this);
}
reference operator*()
{
return (*_ptr);
}
pointer base() const
{
return (_ptr);
}
pointer operator->() {return(_ptr);}
myiterator& operator++() { _ptr++; return *this;}
myiterator operator++(int)
{
myiterator abc(*this);
++(*this);
return abc;
}
myiterator& operator--()
{
_ptr--;
return (*this);
}
myiterator operator--(int)
{
myiterator abc = *this;
--(*this);
return abc;
}
myiterator &operator-=(const int &a)
{
for (int i = 0;i < a; i++)
(*this)--;
return (*this);
}
myiterator &operator+=(const int &a)
{
for (int i = 0;i < a; i++)
(*this)++;
return (*this);
}
myiterator operator+(const int &a)
{
myiterator<T> it(*this);
for (int i = 0;i < a;i++)
it._ptr++;
return (it);
}
template <typename S>
difference_type operator-(const myiterator<S> &first)
{
return (this->_ptr - first._ptr);
}
reference operator[](const int &a)
{
return _ptr[a];
}
template <typename S>
bool operator==(const myiterator<S> &it) const
{
return (this->_ptr == it.base());
}
template <typename S>
bool operator!=(const myiterator<S>& a)
{
if (this->_ptr != a._ptr)
return (true);
return (false);
}
template <typename S>
bool operator>(const myiterator<S>& a)
{
if (this->_ptr > a._ptr)
return (true);
return (false);
}
template <typename S>
bool operator<(const myiterator<S>& a)
{
if (this->_ptr < a._ptr)
return (true);
return (false);
}
template <typename S>
bool operator<=(const myiterator<S>& a)
{
if (this->_ptr <= a._ptr)
return (true);
return (false);
}
template <typename S>
bool operator>=(const myiterator<S>& a)
{
if (this->_ptr >= a._ptr)
return (true);
return (false);
}
//?https://en.cppreference.com/w/cpp/language/cast_operator
//?https://stackoverflow.com/questions/1307876/how-do-conversion-operators-work-in-c
//?https://www.youtube.com/watch?v=M2D11HXlgnE
//*Declares a user-defined conversion function that participates in all implicit and explicit conversions.
operator const_myiterator() const
{
return const_myiterator(_ptr);
}
operator myiterator<const T>()
{
return myiterator<const T>(_ptr);
}
protected:
pointer _ptr;
};
template <typename T>
myiterator<T> operator-(const myiterator<T> &it, const int &a)
{
myiterator<T> abc(it);
for (int i = 0; i < a; i++)
abc--;
return (abc);
}
template <typename T>
myiterator<T> operator+(const myiterator<T> &it, const int &a)
{
myiterator<T> abc(it);
for (int i = 0; i < a; i++)
abc++;
return (abc);
}
template <typename T>
myiterator<T> operator+(const int &a, const myiterator<T> &it)
{
myiterator<T> abc(it);
for(int i = 0; i < a; i++)
abc++;
return (abc);
}
}