-
Notifications
You must be signed in to change notification settings - Fork 1
/
deque_iterator.h
233 lines (202 loc) · 7.65 KB
/
deque_iterator.h
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//
// deque_iterator.h
// mini-stl
//
// Created by cz-mac on 2022/1/1.
//
#ifndef deque_iterator_h
#define deque_iterator_h
#include <cstddef>
#include "stl_iterator.h"
namespace MiniSTL {
// 缓冲区大小设定函数(在预设情况下传回可容纳的元素个数)
// 若n不为0,则传回n,表示由用户自定
// 若n为0则采用预设值 预设值根据sz(元素大小)而定
inline size_t __deque_buf_size(size_t sz) {
return sz < 512 ? size_t(512 / sz) : size_t(1);
}
template <class T, class Ref, class Ptr>
struct __deque_iterator {
// alias declarartions
typedef __deque_iterator<T, T &, T *> iterator;
typedef __deque_iterator<T, const T &, const T *> const_iterator;
typedef __deque_iterator self;
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef Ptr pointer;
typedef Ref reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T** map_pointer;
// data member
value_type *cur; // 当前缓冲区的当前元素
value_type *first; // 当前缓冲区头
value_type *last; // 当前缓冲区尾(含备用空间)
map_pointer node; // 指向管控中心
static size_t buffer_size() { return __deque_buf_size(sizeof(value_type)); }
// ctor
__deque_iterator()
: cur(nullptr), first(nullptr), last(nullptr), node(nullptr) {}
__deque_iterator(pointer x, map_pointer y)
: cur(x), first(*y), last(*y + buffer_size()), node(y) {}
__deque_iterator(const iterator &rhs)
: cur(rhs.cur), first(rhs.first), last(rhs.last), node(rhs.node) {}
void set_node(map_pointer new_node) {
node = new_node;
first = *new_node;
last = first + static_cast<difference_type>(buffer_size());
}
// dereference
reference operator*() const { return *cur; }
pointer operator->() const { return &(operator*()); }
// 对于自增自减少操作,实际上增的是cur域,只有跨段时才会真正改变自己的node域
self &operator++() {
++cur;
if (cur == last) { // 若已抵达尾端
set_node(node + 1);
cur = first;
}
return *this;
}
self operator++(int) {
self temp = *this;
++*this;
return temp;
}
self &operator--() {
if (cur == first) {
set_node(node - 1);
cur = last;
}
--cur;
return *this;
}
self operator--(int) {
self temp = *this;
--*this;
return temp;
}
// random access
self &operator += (difference_type n) {
difference_type offset = n + (cur - first);
if (offset >= 0 &&
offset < static_cast<difference_type>(buffer_size())) {
// 不需要跳转
cur += n;
} else {
// off_set小于0则必然需要跳转
difference_type node_offset =
offset > 0
? offset / static_cast<difference_type>(buffer_size())
: -static_cast<difference_type>((-offset - 1) /
buffer_size()) -
1;
set_node(node + node_offset);
cur = first +
(offset -
node_offset * static_cast<difference_type>(buffer_size()));
}
return *this;
}
self operator+(difference_type n) {
self temp = *this;
return temp += n;
}
self &operator-=(difference_type n) { return *this += -n; }
self operator-(difference_type n) {
self temp = *this;
return temp -= n;
}
reference operator[](difference_type n) { return *(*this + n); }
};
// 判断两个迭代器是否相等
template <class T, class Ref, class Ptr>
inline bool operator==(const __deque_iterator<T, Ref, Ptr> &lhs,
const __deque_iterator<T, Ref, Ptr> &rhs) {
return lhs.cur == rhs.cur;
}
// compare with const
template <class T, class RefL, class PtrL, class RefR, class PtrR>
inline bool operator==(const __deque_iterator<T, RefL, PtrL> &lhs,
const __deque_iterator<T, RefR, PtrR> &rhs) {
return lhs.cur == rhs.cur;
}
// 判断两个迭代器是否不相等
template <class T, class Ref, class Ptr>
inline bool operator!=(const __deque_iterator<T, Ref, Ptr> &lhs,
const __deque_iterator<T, Ref, Ptr> &rhs) {
return !(lhs == rhs);
}
template <class T, class RefL, class PtrL, class RefR, class PtrR>
inline bool operator!=(const __deque_iterator<T, RefL, PtrL> &lhs,
const __deque_iterator<T, RefR, PtrR> &rhs) {
return !(lhs == rhs);
}
// 判断两个迭代器 前后关系
template <class T, class Ref, class Ptr>
inline bool operator<(const __deque_iterator<T, Ref, Ptr> &lhs,
const __deque_iterator<T, Ref, Ptr> &rhs) {
return (lhs.node == rhs.node) ? (lhs.cur < rhs.cur) : (lhs.node < rhs.node);
}
template <class T, class RefL, class PtrL, class RefR, class PtrR>
inline bool operator<(const __deque_iterator<T, RefL, PtrL> &lhs,
const __deque_iterator<T, RefR, PtrR> &rhs) {
return (lhs.node == rhs.node) ? (lhs.cur < rhs.cur) : (lhs.node < rhs.node);
}
template <class T, class Ref, class Ptr>
inline bool operator>(const __deque_iterator<T, Ref, Ptr> &lhs,
const __deque_iterator<T, Ref, Ptr> &rhs) {
return rhs < lhs;
}
template <class T, class RefL, class PtrL, class RefR, class PtrR>
inline bool operator>(const __deque_iterator<T, RefL, PtrL> &lhs,
const __deque_iterator<T, RefR, PtrR> &rhs) {
return rhs < lhs;
}
template <class T, class Ref, class Ptr>
inline bool operator<=(const __deque_iterator<T, Ref, Ptr> &lhs,
const __deque_iterator<T, Ref, Ptr> &rhs) {
return !(rhs < lhs);
}
template <class T, class RefL, class PtrL, class RefR, class PtrR>
inline bool operator<=(const __deque_iterator<T, RefL, PtrL> &lhs,
const __deque_iterator<T, RefR, PtrR> &rhs) {
return !(rhs < lhs);
}
template <class T, class Ref, class Ptr>
inline bool operator>=(const __deque_iterator<T, Ref, Ptr> &lhs,
const __deque_iterator<T, Ref, Ptr> &rhs) {
return !(lhs < rhs);
}
template <class T, class RefL, class PtrL, class RefR, class PtrR>
inline bool operator>=(const __deque_iterator<T, RefL, PtrL> &lhs,
const __deque_iterator<T, RefR, PtrR> &rhs) {
return !(lhs < rhs);
}
// 迭代器距离 段间距离和段内距离
template <class T, class Ref, class Ptr>
inline typename __deque_iterator<T, Ref, Ptr>::difference_type operator-(
const __deque_iterator<T, Ref, Ptr> &lhs,
const __deque_iterator<T, Ref, Ptr> &rhs) {
return typename __deque_iterator<T, Ref, Ptr>::difference_type(
__deque_iterator<T, Ref, Ptr>::buffer_size() *
(lhs.node - rhs.node - 1) +
(lhs.cur - lhs.first) + (rhs.last - rhs.cur));
}
template <class T, class RefL, class PtrL, class RefR, class PtrR>
inline typename __deque_iterator<T, RefL, PtrL>::difference_type operator-(
const __deque_iterator<T, RefL, PtrL> &lhs,
const __deque_iterator<T, RefR, PtrR> &rhs) {
return typename __deque_iterator<T, RefL, PtrL>::difference_type(
__deque_iterator<T, RefL, PtrL>::buffer_size() *
(lhs.node - rhs.node - 1) +
(lhs.cur - lhs.first) + (rhs.last - rhs.cur));
}
// 迭代器向前走n步
template <class T, class Ref, class Ptr>
inline __deque_iterator<T, Ref, Ptr> operator+(
ptrdiff_t n, const __deque_iterator<T, Ref, Ptr> &x) {
return x + n;
}
} // namespace MiniSTL
#endif /* deque_iterator_h */