-
Notifications
You must be signed in to change notification settings - Fork 1
/
xx_queue.h
314 lines (276 loc) · 7.5 KB
/
xx_queue.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#pragma once
#include "xx_data.h"
#include "xx_string.h"
namespace xx {
// ring buffer FIFO queue, support random index visit, batch pop. performance better than std
//...............FR............... // Head == Tail
//......Head+++++++++++Tail....... // DataLen = Tail - Head
//++++++Tail...........Head+++++++ // DataLen = BufLen - Head + Tail
template <typename T, typename SizeType = int32_t>
struct Queue {
typedef T ChildType;
using S = SizeType;
T* buf;
SizeType cap;
SizeType head{}, tail{}; // TH..............................
explicit Queue(SizeType capacity = 8) noexcept {
if (capacity < 8) {
capacity = 8;
}
auto bufByteLen = Round2n(capacity * sizeof(T));
buf = AlignedAlloc<T>((size_t)bufByteLen); // buf can't be null
assert(buf);
cap = SizeType(bufByteLen / sizeof(T));
}
Queue(Queue&& o) noexcept : Queue() {
std::swap(buf, o.buf);
std::swap(cap, o.cap);
std::swap(head, o.head);
std::swap(tail, o.tail);
}
~Queue() noexcept {
assert(buf);
Clear();
AlignedFree<T>(buf);
buf = nullptr;
}
Queue(Queue const& o) = delete;
Queue& operator=(Queue const& o) = delete;
T& operator[](SizeType idx) const noexcept {
return At(idx);
}
T& At(SizeType idx) const noexcept {
assert(idx < Count());
if (head + idx < cap) {
return (T&)buf[head + idx];
} else {
return (T&)buf[head + idx - cap];
}
}
SizeType Count() const noexcept {
//......Head+++++++++++Tail.......
//...............FR...............
if (head <= tail) return tail - head;
// ++++++Tail...........Head++++++
else return tail + (cap - head);
}
bool Empty() const noexcept {
return head == tail;
}
void Clear() noexcept {
//........HT......................
if (head == tail) return;
if constexpr (!(std::is_standard_layout_v<T> && std::is_trivial_v<T>)) {
//......Head+++++++++++Tail......
if (head < tail) {
for (auto i = head; i < tail; ++i) {
buf[i].~T();
}
}
// ++++++Tail...........Head++++++
else {
for (SizeType i = 0; i < tail; ++i) {
buf[i].~T();
}
for (auto i = head; i < cap; ++i) {
buf[i].~T();
}
}
}
//........HT......................
head = tail = 0;
}
template<bool callByEmplace = false>
void Reserve(SizeType capacity) noexcept {
assert(capacity > 0);
if (capacity <= cap) return;
auto newBufByteLen = Round2n(capacity * sizeof(T));
auto newBuf = AlignedAlloc<T>((size_t)newBufByteLen);
assert(newBuf);
auto newBufLen = SizeType(newBufByteLen / sizeof(T));
// callByEmplace == true: ++++++++++++++TH++++++++++++++++
auto dataLen = callByEmplace ? cap : Count();
//......Head+++++++++++Tail.......
if (head < tail) {
if constexpr (xx::IsPod_v<T>) {
memcpy((void*)newBuf, buf + head, dataLen * sizeof(T));
} else {
for (size_t i = 0; i < dataLen; ++i) {
new (newBuf + i) T((T&&)buf[head + i]);
buf[head + i].~T();
}
}
}
// ++++++Tail...........Head+++++++
// ++++++++++++++TH++++++++++++++++
else {
//...Head++++++
auto frontDataLen = cap - head;
if constexpr (xx::IsPod_v<T>) {
memcpy((void*)newBuf, buf + head, frontDataLen * sizeof(T));
} else {
for (size_t i = 0; i < frontDataLen; ++i) {
new (newBuf + i) T((T&&)buf[head + i]);
buf[head + i].~T();
}
}
// ++++++Tail...
if constexpr (xx::IsPod_v<T>) {
memcpy((void*)(newBuf + frontDataLen), buf, tail * sizeof(T));
} else {
for (size_t i = 0; i < tail; ++i) {
new (newBuf + frontDataLen + i) T((T&&)buf[i]);
buf[i].~T();
}
}
}
// Head+++++++++++Tail.............
head = 0;
tail = dataLen;
AlignedFree<T>(buf);
buf = newBuf;
cap = newBufLen;
}
template<typename...Args>
T& Emplace(Args&&...args) noexcept {
auto idx = tail;
new (buf + tail++) T(std::forward<Args>(args)...);
if (tail == cap) { // cycle
tail = 0;
}
if (tail == head) { // no more space
idx = cap - 1;
Reserve<true>(cap * 2);
}
return buf[idx];
}
template<typename ...TS>
void Push(TS&& ...vs) noexcept {
(Emplace(std::forward<TS>(vs)), ...);
}
bool TryPop(T& outVal) noexcept {
if (head == tail) return false;
outVal = std::move(buf[head]);
Pop();
return true;
}
T& Top() const noexcept {
assert(head != tail);
return (T&)buf[head];
}
// ++head
void Pop() noexcept {
assert(head != tail);
if constexpr (!(std::is_standard_layout_v<T> && std::is_trivial_v<T>)) {
buf[head].~T();
}
++head;
if (head == cap) {
head = 0;
}
}
SizeType PopMulti(SizeType count) noexcept {
if (count <= 0) return 0;
auto dataLen = Count();
if (count >= dataLen) {
Clear();
return dataLen;
}
// count < dataLen
//......Head+++++++++++Tail......
if (head < tail) {
if constexpr (!(std::is_standard_layout_v<T> && std::is_trivial_v<T>)) {
//......Head+++++++++++count......
for (auto i = head; i < head + count; ++i) buf[i].~T();
}
head += count;
}
// ++++++Tail...........Head++++++
else {
auto frontDataLen = cap - head;
//...Head+++
if (count < frontDataLen) {
if constexpr (!(std::is_standard_layout_v<T> && std::is_trivial_v<T>)) {
for (auto i = head; i < head + count; ++i) buf[i].~T();
}
head += count;
} else {
if constexpr (!(std::is_standard_layout_v<T> && std::is_trivial_v<T>)) {
//...Head++++++
for (auto i = head; i < cap; ++i) buf[i].~T();
}
// <-Head
head = count - frontDataLen;
if constexpr (!(std::is_standard_layout_v<T> && std::is_trivial_v<T>)) {
// ++++++Head...
for (SizeType i = 0; i < head; ++i) buf[i].~T();
}
}
}
return count;
}
// [ tail-1 ]
T& Last() const noexcept {
assert(head != tail);
return (T&)buf[tail - 1 == (size_t)-1 ? cap - 1 : tail - 1];
}
void PopLast() noexcept {
assert(head != tail);
if constexpr (!(std::is_standard_layout_v<T> && std::is_trivial_v<T>)) {
buf[tail].~T();
}
--tail;
if (tail == (SizeType)-1) {
tail = cap - 1;
}
}
};
// mem moveable tag
template <typename T, typename SizeType>
struct IsPod<Queue<T, SizeType>, void> : std::true_type {};
// is checks
template<typename T> constexpr bool IsQueue_v = TemplateIsSame_v<std::remove_cvref_t<T>, Queue<AnyType>>;
// tostring
template<typename T>
struct StringFuncs<T, std::enable_if_t<IsQueue_v<T>>> {
static inline void Append(std::string& s, T const& in) {
s.push_back('[');
if (auto inLen = in.Count()) {
for (typename T::S i = 0; i < inLen; ++i) {
::xx::Append(s, in[i]);
s.push_back(',');
}
s[s.size() - 1] = ']';
} else {
s.push_back(']');
}
}
};
// serde
template<typename T>
struct DataFuncs<T, std::enable_if_t< (IsQueue_v<T>)>> {
using U = typename T::ChildType;
template<bool needReserve = true>
static inline void Write(Data& d, T const& in) {
auto siz = (size_t)in.Count();
d.WriteVarInteger<needReserve>(siz);
if (!siz) return;
for (size_t i = 0; i < siz; ++i) {
d.Write<needReserve>(in[(typename T::S)i]);
}
}
static inline int Read(Data_r& d, T& out) {
size_t siz = 0;
if (int r = d.ReadVarInteger(siz)) return r;
if (d.offset + siz > d.len) return __LINE__;
out.Clear();
if (siz == 0) return 0;
out.Reserve((typename T::S)siz);
for (size_t i = 0; i < siz; ++i) {
auto&& o = out.Emplace();
if (int r = d.Read(o)) return r;
}
return 0;
}
};
}