-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.hpp
178 lines (157 loc) · 5.13 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
#ifndef _ASP_ITERATOR_HPP_
#define _ASP_ITERATOR_HPP_
#include <iterator>
#include "basic_param.hpp"
#include "basic_io.hpp"
#include "iterator_traits.hpp"
namespace asp {
template <typename _Tp, typename _Pointer, typename _Reference> struct base_iterator;
template <typename _Tp, typename _Pointer = _Tp*, typename _Reference = _Tp&> struct base_iterator {
// typedef typename _Tp value_type;
// typedef typename _Pointer pointer;
// typedef typename _Reference reference;
typedef asp::fixed_iterator iterator_category;
using value_type = _Tp;
using pointer = _Pointer;
using reference = _Reference;
using difference_type = asp::difference_type;
typedef base_iterator<_Tp> self;
base_iterator() = default;
base_iterator(const pointer _p) : _ptr(_p) {}
base_iterator(const self& _s) : _ptr(_s._ptr) {}
self& operator=(const self& _s) {
this->_ptr = _s._ptr;
}
reference operator*() {
return *_ptr;
}
const reference operator*() const {
return *_ptr;
}
pointer operator->() {
return _ptr;
}
const pointer operator->() const {
return _ptr;
}
const pointer& base() const {
return _ptr;
}
friend bool operator==(const self& lhs, const self& rhs) {
return lhs._ptr == rhs._ptr;
}
friend bool operator!=(const self& lhs, const self& rhs) {
return lhs._ptr != rhs._ptr;
}
value_type* _ptr = nullptr;
template <typename _T, typename _P, typename _R> friend std::ostream&
operator<<(std::ostream& os, const base_iterator<_T, _P, _R>& _bi);
};
template <typename _Tp>
struct normal_iterator;
template <typename _Tp>
struct normal_iterator : public base_iterator <_Tp> {
typedef asp::random_access_iterator_tag iterator_category;
// typedef value_type = base_iterator<_Tp>::value_type;
// typedef pointer = base_iterator<_Tp>::pointer;
// typedef reference = base_iterator<_Tp>::reference;
typedef typename base_iterator<_Tp>::value_type value_type;
typedef typename base_iterator<_Tp>::pointer pointer;
typedef typename base_iterator<_Tp>::reference reference;
typedef base_iterator<_Tp> base;
typedef normal_iterator<value_type> self;
typedef typename base::difference_type difference_type;
using base_iterator<_Tp>::_ptr;
normal_iterator() = default;
normal_iterator(const pointer _p): base(_p) {}
normal_iterator(const self& rhs): base(rhs._ptr) {}
self& operator=(const self& rhs) {
_ptr = rhs._ptr;
}
// @iterator to @const_iterator conversion
template <typename _Iter> normal_iterator(const normal_iterator<_Iter>& _i)
: base(_i._ptr) {}
self& operator++() {
++_ptr;
return *this;
}
self operator++(int) {
self _tmp = *this;
++_ptr;
return _tmp;
}
self& operator--() {
--_ptr;
return *this;
}
self operator--(int) {
self _tmp = *this;
--_ptr;
return _tmp;
}
self operator+(int _n) {
self _tmp = *this;
_tmp._ptr += _n;
return _tmp;
}
self operator-(int _n) {
self _tmp = *this;
_tmp._ptr -= _n;
return _tmp;
}
friend difference_type operator-(const self& last, const self& first) {
return last._ptr - first._ptr;
}
// private:
// value_type* _ptr = nullptr;
};
template <typename _Iterator> static difference_type _distance(_Iterator& first, _Iterator& last, asp::random_access_iterator_tag) {
return last - first;
}
template <typename _Iterator> static difference_type _distance(_Iterator& first, _Iterator& last, asp::bidirectional_iterator_tag) {
difference_type _n = 0;
while (first != last) {
++first;
++_n;
}
return _n;
}
template <typename _Iterator, typename _UnknownIteraterTag> static difference_type _distance(_Iterator& first, _Iterator& last, _UnknownIteraterTag) {
return std::distance(first, last);
}
template <typename _Tp> static void _advance(normal_iterator<_Tp>& _p, difference_type _n, asp::random_access_iterator_tag) {
_p += _n;
}
template <typename _Iterator> static void _advance(_Iterator& _p, difference_type _n, asp::bidirectional_iterator_tag) {
if (_n > 0) {
while (_n--) {
++_p;
}
}
else if (_n < 0) {
while (_n++) {
--_p;
}
}
}
template <typename _Iterator, typename _UnknownIteraterTag> static void _advance(_Iterator& _p, difference_type _n, _UnknownIteraterTag) {
std::advance(_p, _n);
}
template <typename _Iterator> difference_type distance(_Iterator first, _Iterator last) {
return _distance(first, last, asp::_A_iterator_category(first));
}
template <typename _Iterator> void advance(_Iterator& _p, difference_type _n) {
_advance(_p, _n, asp::_A_iterator_category(_p));
}
template <typename _Iterator> void iter_swap(_Iterator _a, _Iterator _b) {
std::swap(*_a, *_b);
}
/// output
template <typename _T, typename _P, typename _R> auto
operator<<(std::ostream& os, const base_iterator<_T, _P, _R>& _bi)
-> std::ostream& {
os << obj_string::_M_obj_2_string(*_bi);
return os;
}
};
#endif