-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomAccessIterator.hpp
146 lines (90 loc) · 5.35 KB
/
RandomAccessIterator.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
#pragma once
#include "miscellaneous.hpp"
namespace ft
{
template <class T>
class RandomAccessIterator : ft::iterator<std::random_access_iterator_tag, T>
{
public :
typedef typename ft::iterator<std::random_access_iterator_tag, T>::iterator_category iterator_category;
typedef typename ft::iterator<std::random_access_iterator_tag, T>::value_type value_type;
typedef typename ft::iterator<std::random_access_iterator_tag, T>::difference_type difference_type;
typedef typename ft::iterator<std::random_access_iterator_tag, T>::pointer pointer;
typedef typename ft::iterator<std::random_access_iterator_tag, T>::reference reference;
/*
================================= Constructors =============================================================
*/
RandomAccessIterator(pointer ptr) { _pointer = ptr; }
RandomAccessIterator(const RandomAccessIterator& other) { _pointer = other._pointer;}
RandomAccessIterator() { _pointer = nullptr;}
~RandomAccessIterator() { }
/*
================================= Arithmitics =============================================================
*/
operator RandomAccessIterator<const value_type>() const { return RandomAccessIterator<const value_type>(_pointer); }
RandomAccessIterator& operator=(const RandomAccessIterator& other) { _pointer = other.base(); return (*this);}
RandomAccessIterator& operator+=(difference_type n) { _pointer += n; return (*this);}
RandomAccessIterator& operator-=(difference_type n) { _pointer -= n; return (*this);}
RandomAccessIterator& operator++() { _pointer++; return (*this); }
RandomAccessIterator operator++(int)
{
RandomAccessIterator tmp(*this);
_pointer++;
return (tmp);
}
RandomAccessIterator& operator--() { _pointer--; return (*this); }
RandomAccessIterator operator--(int)
{
RandomAccessIterator tmp(*this);
_pointer--;
return (tmp);
}
difference_type operator-(RandomAccessIterator it) const { return (this->_pointer - it.base()); }
RandomAccessIterator operator+(difference_type n) const { RandomAccessIterator it(this->_pointer + n); return (it);}
RandomAccessIterator operator-(difference_type n) const { RandomAccessIterator it(this->_pointer - n); return (it);}
friend RandomAccessIterator operator+(difference_type n, const RandomAccessIterator& input) { RandomAccessIterator it(input); return (it += n); }
// /*
// ================================= Access =============================================================
// */
reference operator[](difference_type n) const { return (_pointer[n]); }
reference operator*() { return (*_pointer); }
pointer operator->() { return (_pointer);}
// /*
// ================================= Comparision =============================================================
// */
template <typename U>
bool operator==(const RandomAccessIterator<U> & lhs) { return ( _pointer == lhs.base()); }
template <typename U>
bool operator!=(const RandomAccessIterator<U> & lhs) { return ( _pointer != lhs.base()); }
template <typename U>
bool operator>(const RandomAccessIterator<U> & lhs) { return ( _pointer > lhs.base()); }
template <typename U>
bool operator>=(const RandomAccessIterator<U> & lhs) { return ( _pointer >= lhs.base()); }
template <typename U>
bool operator<(const RandomAccessIterator<U> & lhs) { return ( _pointer < lhs.base()); }
template <typename U>
bool operator<=(const RandomAccessIterator<U> & lhs) { return ( _pointer <= lhs.base()); }
// /*
// ================================= Getter and Setter =============================================================
// */
pointer base() const { return this->_pointer;}
//void set_pointer(pointer ptr) const { this->_pointer = ptr;}
private :
pointer _pointer;
};
// /*
// ================================= Comparision =============================================================
// */std::cout << "haha\n";
// template <class T>
// bool operator== (const RandomAccessIterator<T>& rhs, const RandomAccessIterator<T>& lhs) { return (rhs.base() == lhs.base());}
// template <class T>
// bool operator!= (const RandomAccessIterator<T>& rhs, const RandomAccessIterator<T>& lhs) { return (rhs.base() != lhs.base());}
// template <class T>
// bool operator< (const RandomAccessIterator<T>& rhs, const RandomAccessIterator<T>& lhs) { return (rhs.base() < lhs.base());}
// template <class T>
// bool operator<= (const RandomAccessIterator<T>& rhs, const RandomAccessIterator<T>& lhs) { return (rhs.base() <= lhs.base());}
// template <class T>
// bool operator> (const RandomAccessIterator<T>& rhs, const RandomAccessIterator<T>& lhs) { return (rhs.base() > lhs.base());}
// template <class T>
// bool operator>= (const RandomAccessIterator<T>& rhs, const RandomAccessIterator<T>& lhs) { return (rhs.base() >= lhs.base());}
}