-
Notifications
You must be signed in to change notification settings - Fork 0
/
deque.h
383 lines (369 loc) · 12.5 KB
/
deque.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#ifndef __CCTL_DEQUE_H__
#define __CCTL_DEQUE_H__
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "cctl.h"
#define cctl_deque_chunk_max 512
#define chunk(TYPE) cctl_join(TYPE, chunk)
#define chunk_func(FUNC, TYPE) cctl_join(chunk(TYPE), FUNC)
#define chunk_struct(TYPE) cctl_join(chunk(TYPE), struct)
#define chunk_front(T, p_c) ((p_c)->p_data + (p_c)->begin)
#define chunk_back(T, p_c) ((p_c)->p_data + (p_c)->end)
#define chunk_imp_h(TYPE) \
typedef struct chunk_struct(TYPE) chunk(TYPE); \
\
struct chunk_struct(TYPE) { \
TYPE* p_data; \
size_t begin; \
size_t end; \
size_t size; \
size_t capacity; \
bool allocated; \
}; \
\
bool chunk_func(reserve, TYPE)(chunk(TYPE)* p_c, size_t size); \
void chunk_func(init, TYPE)(chunk(TYPE)* p_c); \
void chunk_func(clear, TYPE)(chunk(TYPE)* p_c); \
void chunk_func(free, TYPE)(chunk(TYPE)* p_c); \
bool chunk_func(push_front, TYPE)(chunk(TYPE)* p_c, TYPE item); \
bool chunk_func(push_back, TYPE)(chunk(TYPE)* p_c, TYPE item); \
bool chunk_func(pop_front, TYPE)(chunk(TYPE)* p_c); \
bool chunk_func(pop_back, TYPE)(chunk(TYPE)* p_c); \
TYPE* chunk_func(at, TYPE)(chunk(TYPE)* p_c, size_t index);
#define chunk_imp_c(TYPE) \
bool chunk_func(reserve, TYPE)(chunk(TYPE)* p_c, size_t size) { \
if (p_c->capacity == size) return true; \
size_t capacity = 1; \
if (size > 0) while (capacity < size) capacity <<= 1; \
if (p_c->capacity < size) { \
TYPE* p_temp = NULL; \
if (!(p_temp = (TYPE*) calloc(capacity, sizeof(TYPE)))) return false; \
if (p_c->allocated) { \
if (p_c->begin > p_c->end) { \
size_t temp_size = p_c->capacity - p_c->begin; \
memcpy(p_temp, p_c->p_data + p_c->begin, temp_size * sizeof(TYPE)); \
memcpy(p_temp + temp_size, p_c->p_data, (p_c->end + 1) * sizeof(TYPE)); \
} \
else memcpy(p_temp, p_c->p_data + p_c->begin, p_c->capacity * sizeof(TYPE)); \
free(p_c->p_data); \
} \
else p_c->allocated = true; \
p_c->p_data = p_temp; \
p_c->begin = 0; \
p_c->end = p_c->size - 1; \
p_c->capacity = capacity; \
} \
return true; \
} \
\
void chunk_func(init, TYPE)(chunk(TYPE)* p_c) { \
p_c->p_data = NULL; \
p_c->begin = 0; \
p_c->end = -1; \
p_c->size = 0; \
p_c->capacity = 0; \
p_c->allocated = false; \
return; \
} \
\
void chunk_func(clear, TYPE)(chunk(TYPE)* p_c) { \
memset(p_c->p_data, 0, p_c->capacity * sizeof(TYPE)); \
p_c->begin = 0; \
p_c->end = -1; \
p_c->size = 0; \
return; \
} \
\
void chunk_func(free, TYPE)(chunk(TYPE)* p_c) { \
free(p_c->p_data); \
chunk_func(init, TYPE)(p_c); \
return; \
} \
\
bool chunk_func(push_front, TYPE)(chunk(TYPE)* p_c, TYPE item) { \
if (chunk_func(reserve, TYPE)(p_c, p_c->size + 1)) { \
p_c->size++; \
if (p_c->begin == 0) p_c->begin = p_c->capacity; \
p_c->begin--; \
*(p_c->p_data + p_c->begin) = item; \
return true; \
} \
return false; \
} \
\
bool chunk_func(push_back, TYPE)(chunk(TYPE)* p_c, TYPE item) { \
if (chunk_func(reserve, TYPE)(p_c, p_c->size + 1)) { \
p_c->size++; \
if (p_c->end >= p_c->capacity - 1) p_c->end = -1; \
p_c->end++; \
*(p_c->p_data + p_c->end) = item; \
return true; \
} \
return false; \
} \
\
bool chunk_func(pop_front, TYPE)(chunk(TYPE)* p_c) { \
if (p_c->size == 0) return false; \
p_c->size--; \
memset(p_c->p_data + p_c->begin, 0, sizeof(TYPE)); \
if (p_c->begin == p_c->capacity - 1) p_c->begin = -1; \
p_c->begin++; \
return true; \
} \
\
bool chunk_func(pop_back, TYPE)(chunk(TYPE)* p_c) { \
if (p_c->size == 0) return false; \
p_c->size--; \
memset(p_c->p_data + p_c->end, 0, sizeof(TYPE)); \
if (p_c->end == 0) p_c->end = p_c->capacity; \
p_c->end--; \
return true; \
} \
\
TYPE* chunk_func(at, TYPE)(chunk(TYPE)* p_c, size_t index) { \
if (p_c->begin > p_c->end) { \
size_t temp = p_c->capacity - 1 - p_c->begin; \
if (index > temp) \
return p_c->p_data + index - temp - 1; \
} \
return p_c->p_data + p_c->begin + index; \
}
#define deque(TYPE) cctl_join(TYPE, deque)
#define deque_chunk_func(FUNC, TYPE) cctl_join(cctl_join(deque(TYPE), chunk), FUNC)
#define deque_func(FUNC, TYPE) cctl_join(deque(TYPE), FUNC)
#define deque_struct(TYPE) cctl_join(deque(TYPE), struct)
#define deque_init(TYPE, p_d) deque_func(init, TYPE)(p_d)
#define deque_resize(TYPE, p_d, size) deque_func(resize, TYPE)(p_d, size)
#define deque_reserve(TYPE, p_d, size) deque_func(reserve, TYPE)(p_d, size)
#define deque_clear(TYPE, p_d) deque_func(clear, TYPE)(p_d)
#define deque_free(TYPE, p_d) deque_func(free, TYPE)(p_d)
#define deque_push_back(TYPE, p_d, item) deque_func(push_back, TYPE)(p_d, item)
#define deque_pop_back(TYPE, p_d) deque_func(pop_back, TYPE)(p_d)
#define deque_at(TYPE, p_d, index) deque_func(at, TYPE)(p_d, index)
#define deque_chunk_front(TYPE, p_d) ((p_d)->p_data + (p_d)->chunk_begin)
#define deque_chunk_back(TYPE, p_d) ((p_d)->p_data + (p_d)->chunk_end)
#define deque_front(TYPE, p_d) ((p_d)->size ? chunk_front(TYPE, deque_chunk_front(TYPE, p_d)) : NULL)
#define deque_back(TYPE, p_d) ((p_d)->size ? chunk_back(TYPE, deque_chunk_back(TYPE, p_d)) : NULL)
#define deque_fd(TYPE) \
typedef struct deque_struct(TYPE) deque(TYPE);
#define deque_imp_h(TYPE) \
chunk_imp_h(TYPE); \
\
struct deque_struct(TYPE) { \
chunk(TYPE)* p_data; \
size_t chunk_begin; \
size_t chunk_end; \
size_t chunk_count; \
size_t chunk_capacity; \
bool allocated; \
size_t size; \
}; \
\
bool deque_chunk_func(reserve, TYPE)(deque(TYPE)* p_d, size_t size); \
bool deque_chunk_func(push_front, TYPE)(deque(TYPE)* p_d); \
bool deque_chunk_func(push_back, TYPE)(deque(TYPE)* p_d); \
bool deque_chunk_func(pop_front, TYPE)(deque(TYPE)* p_d); \
bool deque_chunk_func(pop_back, TYPE)(deque(TYPE)* p_d); \
chunk(TYPE)* deque_chunk_func(at, TYPE)(deque(TYPE)* p_d, size_t index); \
\
bool deque_func(resize, TYPE)(deque(TYPE)* p_d, size_t size); \
void deque_func(init, TYPE)(deque(TYPE)* p_d); \
void deque_func(clear, TYPE)(deque(TYPE)* p_d); \
void deque_func(free, TYPE)(deque(TYPE)* p_d); \
bool deque_func(push_front, TYPE)(deque(TYPE)* p_d, TYPE item); \
bool deque_func(push_back, TYPE)(deque(TYPE)* p_d, TYPE item); \
bool deque_func(pop_front, TYPE)(deque(TYPE)* p_d); \
bool deque_func(pop_back, TYPE)(deque(TYPE)* p_d); \
TYPE* deque_func(at, TYPE)(deque(TYPE)* p_d, size_t index);
#define deque_imp_c(TYPE) \
chunk_imp_c(TYPE); \
\
bool deque_chunk_func(reserve, TYPE)(deque(TYPE)* p_d, size_t size) { \
if (p_d->chunk_capacity == size) return true; \
size_t capacity = 1; \
if (size > 0) while (capacity < size) capacity <<= 1; \
if (p_d->chunk_capacity < size) { \
chunk(TYPE)* p_temp = NULL; \
if (!(p_temp = (chunk(TYPE)*) calloc(capacity, sizeof(chunk(TYPE))))) return false; \
if (p_d->allocated) { \
if (p_d->chunk_begin > p_d->chunk_end) { \
size_t temp_size = p_d->chunk_capacity - p_d->chunk_begin; \
memcpy(p_temp, p_d->p_data + p_d->chunk_begin, temp_size * sizeof(chunk(TYPE))); \
memcpy(p_temp + temp_size, p_d->p_data, (p_d->chunk_end + 1) * sizeof(chunk(TYPE))); \
} \
else memcpy(p_temp, p_d->p_data + p_d->chunk_begin, p_d->chunk_capacity * sizeof(chunk(TYPE))); \
free(p_d->p_data); \
} \
else p_d->allocated = true; \
p_d->p_data = p_temp; \
p_d->chunk_begin = 0; \
p_d->chunk_end = p_d->chunk_count - 1; \
p_d->chunk_capacity = capacity; \
} \
return true; \
} \
\
bool deque_chunk_func(push_front, TYPE)(deque(TYPE)* p_d) { \
chunk(TYPE) temp; \
chunk_func(init, TYPE)(&temp); \
if (deque_chunk_func(reserve, TYPE)(p_d, p_d->chunk_count + 1)) { \
p_d->chunk_count++; \
if (p_d->chunk_begin == 0) p_d->chunk_begin = p_d->chunk_capacity; \
p_d->chunk_begin--; \
*(p_d->p_data + p_d->chunk_begin) = temp; \
return true; \
} \
return false; \
} \
\
bool deque_chunk_func(push_back, TYPE)(deque(TYPE)* p_d) { \
chunk(TYPE) temp; \
chunk_func(init, TYPE)(&temp); \
if (deque_chunk_func(reserve, TYPE)(p_d, p_d->chunk_count + 1)) { \
p_d->chunk_count++; \
if (p_d->chunk_end >= p_d->chunk_capacity - 1) p_d->chunk_end = -1; \
p_d->chunk_end++; \
*(p_d->p_data + p_d->chunk_end) = temp; \
return true; \
} \
return false; \
} \
\
bool deque_chunk_func(pop_front, TYPE)(deque(TYPE)* p_d) { \
if (p_d->chunk_count == 0) return false; \
p_d->chunk_count--; \
chunk_func(free, TYPE)(deque_chunk_front(TYPE, p_d)); \
if (p_d->chunk_begin == p_d->chunk_capacity - 1) p_d->chunk_begin = -1; \
p_d->chunk_begin++; \
return true; \
} \
\
bool deque_chunk_func(pop_back, TYPE)(deque(TYPE)* p_d) { \
if (p_d->chunk_count == 0) return false; \
p_d->chunk_count--; \
chunk_func(free, TYPE)(deque_chunk_back(TYPE, p_d)); \
if (p_d->chunk_end == 0) p_d->chunk_end = p_d->chunk_capacity; \
p_d->chunk_end--; \
return true; \
} \
\
chunk(TYPE)* deque_chunk_func(at, TYPE)(deque(TYPE)* p_d, size_t index) { \
if (p_d->chunk_begin > p_d->chunk_end) { \
size_t temp = p_d->chunk_capacity - 1 - p_d->chunk_begin; \
if (index > temp) \
return p_d->p_data + index - temp - 1; \
} \
return p_d->p_data + p_d->chunk_begin + index; \
} \
\
bool deque_func(resize, TYPE)(deque(TYPE)* p_d, size_t new_size) { \
if (new_size > p_d->size) { \
TYPE none; \
memset(&none, 0, sizeof(TYPE)); \
for (size_t i = p_d->size; i < new_size; i++) { \
if (!deque_func(push_back, TYPE)(p_d, none)) return false; \
} \
} \
else if (new_size < p_d->size) { \
size_t temp = p_d->size; \
for (size_t i = new_size; i < temp; i++) { \
if (!(deque_func(pop_back, TYPE)(p_d))) return false; \
} \
} \
return true; \
} \
\
void deque_func(init, TYPE)(deque(TYPE)* p_d) { \
p_d->p_data = NULL; \
p_d->chunk_begin = 0; \
p_d->chunk_end = 0; \
p_d->chunk_count = 0; \
p_d->chunk_capacity = 0; \
p_d->allocated = false; \
p_d->size = 0; \
} \
\
void deque_func(clear, TYPE)(deque(TYPE)* p_d) { \
for (size_t i = 0; i < p_d->chunk_count; i++) { \
chunk_func(free, TYPE)(deque_chunk_func(at, TYPE)(p_d, i)); \
} \
p_d->chunk_count = 0; \
p_d->chunk_begin = 0; \
p_d->chunk_end = 0; \
p_d->size = 0; \
} \
\
void deque_func(free, TYPE)(deque(TYPE)* p_d) { \
for (size_t i = 0; i < p_d->chunk_count; i++) { \
chunk_func(free, TYPE)(deque_chunk_func(at, TYPE)(p_d, i)); \
} \
free(p_d->p_data); \
deque_func(init, TYPE)(p_d); \
} \
\
bool deque_func(push_front, TYPE)(deque(TYPE)* p_d, TYPE item) { \
if (p_d->chunk_count > 0) { \
if (deque_chunk_front(TYPE, p_d)->size >= cctl_deque_chunk_max) { \
if (!deque_chunk_func(push_front, TYPE)(p_d)) return false; \
} \
} \
else { \
if (!deque_chunk_func(push_front, TYPE)(p_d)) return false; \
} \
if (!chunk_func(push_front, TYPE)(deque_chunk_front(TYPE, p_d), item)) return false; \
p_d->size++; \
return true; \
} \
\
bool deque_func(push_back, TYPE)(deque(TYPE)* p_d, TYPE item) { \
if (p_d->chunk_count > 0) { \
if (deque_chunk_back(TYPE, p_d)->size >= cctl_deque_chunk_max) { \
if (!deque_chunk_func(push_back, TYPE)(p_d)) return false; \
} \
} \
else { \
if (!deque_chunk_func(push_back, TYPE)(p_d)) return false; \
} \
if (!chunk_func(push_back, TYPE)(deque_chunk_back(TYPE, p_d), item)) return false; \
p_d->size++; \
return true; \
} \
\
bool deque_func(pop_front, TYPE)(deque(TYPE)* p_d) { \
if (p_d->chunk_count == 0) return false; \
if (deque_chunk_front(TYPE, p_d)->size == 0) { \
if (!deque_chunk_func(pop_front, TYPE)(p_d)) return false; \
} \
if (!chunk_func(pop_front, TYPE)(deque_chunk_front(TYPE, p_d))) return false; \
if (deque_chunk_front(TYPE, p_d)->size == 0) { \
if (!deque_chunk_func(pop_front, TYPE)(p_d)) return false; \
} \
p_d->size--; \
return true; \
} \
\
bool deque_func(pop_back, TYPE)(deque(TYPE)* p_d) { \
if (p_d->chunk_count == 0) return false; \
if (deque_chunk_back(TYPE, p_d)->size == 0) { \
if (!deque_chunk_func(pop_back, TYPE)(p_d)) return false; \
} \
if (!chunk_func(pop_back, TYPE)(deque_chunk_back(TYPE, p_d))) return false; \
if (deque_chunk_back(TYPE, p_d)->size == 0) { \
if (!deque_chunk_func(pop_back, TYPE)(p_d)) return false; \
} \
p_d->size--; \
return true; \
} \
\
TYPE* deque_func(at, TYPE)(deque(TYPE)* p_d, size_t index) { \
size_t front_size = deque_chunk_front(TYPE, p_d)->size; \
size_t chunk_index; \
if (index < front_size) \
return chunk_func(at, TYPE)(deque_chunk_front(TYPE, p_d), index); \
index -= front_size; \
chunk_index = index / cctl_deque_chunk_max + 1; \
index %= cctl_deque_chunk_max; \
return chunk_func(at, TYPE)(deque_chunk_func(at, TYPE)(p_d, chunk_index), index); \
}
#endif