forked from sysprog21/lab0-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
605 lines (532 loc) · 15.9 KB
/
queue.c
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "queue.h"
/* Notice: sometimes, Cppcheck would find the potential NULL pointer bugs,
* but some of them cannot occur. You can suppress them by adding the
* following line.
* cppcheck-suppress nullPointer
*/
typedef int (*list_cmp_func_t)(void *,
const struct list_head *,
const struct list_head *);
/* Create an empty queue */
struct list_head *q_new()
{
struct list_head *head = malloc(sizeof(struct list_head));
if (!head)
return NULL;
INIT_LIST_HEAD(head);
return head;
}
/* Free all storage used by queue */
void q_free(struct list_head *head)
{
if (!head)
return;
element_t *curr = NULL, *tmp = NULL;
list_for_each_entry_safe (curr, tmp, head, list) {
q_release_element(curr);
}
free(head);
}
/* Insert an element at head of queue */
bool q_insert_head(struct list_head *head, char *s)
{
if (!head)
return false;
element_t *new_element = malloc(sizeof(element_t));
if (!new_element)
return false;
new_element->value = strdup(s);
if (!new_element->value) {
free(new_element);
return false;
}
list_add(&new_element->list, head);
return true;
}
/* Insert an element at tail of queue */
bool q_insert_tail(struct list_head *head, char *s)
{
if (!head)
return false;
element_t *new_element = malloc(sizeof(element_t));
if (!new_element)
return false;
new_element->value = strdup(s);
if (!new_element->value) {
free(new_element);
return false;
}
list_add_tail(&new_element->list, head);
return true;
}
/* Remove an element from head of queue */
element_t *q_remove_head(struct list_head *head, char *sp, size_t bufsize)
{
if (!head || list_empty(head))
return NULL;
element_t *target = list_first_entry(head, element_t, list);
if (sp) {
int orig_len = strlen(target->value);
int copy_len = (orig_len < bufsize - 1) ? orig_len : bufsize - 1;
strncpy(sp, target->value, copy_len);
sp[copy_len] = '\0';
}
list_del(&target->list);
return target;
}
/* Remove an element from tail of queue */
element_t *q_remove_tail(struct list_head *head, char *sp, size_t bufsize)
{
if (!head || list_empty(head))
return NULL;
element_t *target = list_last_entry(head, element_t, list);
if (sp) {
int orig_len = strlen(target->value);
int copy_len = (orig_len < bufsize - 1) ? orig_len : bufsize - 1;
strncpy(sp, target->value, copy_len);
sp[copy_len] = '\0';
}
list_del(&target->list);
return target;
}
/* Return number of elements in queue */
int q_size(struct list_head *head)
{
if (!head)
return 0;
int size = 0;
struct list_head *curr;
list_for_each (curr, head)
size++;
return size;
}
/* Delete the middle node in queue */
bool q_delete_mid(struct list_head *head)
{
if (!head || list_empty(head))
return false;
struct list_head *currNext = head->next, *currPrev = head->prev;
while (currNext != currPrev && currNext->next != currPrev) {
currNext = currNext->next;
currPrev = currPrev->prev;
}
element_t *del = list_entry(currNext, element_t, list);
list_del(&del->list);
free(del->value);
free(del);
return true;
}
static void q_delete_dup_free_helper(struct list_head *del)
{
if (!del)
return;
element_t *del_e = list_entry(del, element_t, list);
list_del_init(&del_e->list);
free(del_e->value);
free(del_e);
}
/* Delete all nodes that have duplicate string */
bool q_delete_dup(struct list_head *head)
{
if (!head)
return false;
// Need to sort the list first
q_sort(head, 0);
struct list_head **indir = &head->next;
struct list_head *del = NULL;
element_t *e = NULL, *next_e = NULL;
while (*indir != head && (*indir)->next != head) {
e = list_entry(*indir, element_t, list);
next_e = list_entry((*indir)->next, element_t, list);
if (!strcmp(e->value, next_e->value)) {
while (*indir != head && (*indir)->next != head) {
e = list_entry(*indir, element_t, list);
next_e = list_entry((*indir)->next, element_t, list);
if (!strcmp(e->value, next_e->value)) {
del = *indir;
*indir = (*indir)->next;
q_delete_dup_free_helper(del);
del = *indir;
} else {
*indir = (*indir)->next;
break;
}
}
q_delete_dup_free_helper(del);
del = NULL;
} else
indir = &(*indir)->next;
}
return true;
}
/* Swap every two adjacent nodes */
void q_swap(struct list_head *head)
{
struct list_head *curr = head->next;
while (curr != head && curr->next != head) {
list_move(curr, curr->next);
curr = curr->next;
}
}
/* Reverse elements in queue */
void q_reverse(struct list_head *head)
{
if (!head || list_empty(head))
return;
struct list_head *curr = head, *nxt = NULL;
while (nxt != head) {
nxt = curr->next;
curr->next = curr->prev;
curr->prev = nxt;
curr = nxt;
}
}
/* Reverse the nodes of the list k at a time */
void q_reverseK(struct list_head *head, int k)
{
if (!head || list_empty(head))
return;
int count = 1;
struct list_head *curr = head->next, *prev = head, *nxt = curr->next;
LIST_HEAD(tmp_list);
while (curr != head) {
if (count % k == 0) {
list_cut_position(&tmp_list, prev, curr);
q_reverse(&tmp_list);
list_splice_init(&tmp_list, prev);
prev = nxt->prev;
}
count++;
curr = nxt;
nxt = nxt->next;
}
}
static void merge(struct list_head *head,
struct list_head *left,
struct list_head *right,
bool descend)
{
while (!list_empty(left) && !list_empty(right)) {
element_t *l = list_first_entry(left, element_t, list);
element_t *r = list_first_entry(right, element_t, list);
if ((strcmp(l->value, r->value) <= 0) ^ descend)
list_move_tail(left->next, head);
else
list_move_tail(right->next, head);
}
if (!list_empty(left))
list_splice_tail_init(left, head);
if (!list_empty(right))
list_splice_tail_init(right, head);
}
static struct list_head *get_list_mid(struct list_head *head)
{
struct list_head **indir = &head;
for (struct list_head *fast = head->next;
fast != head && fast->next != head; fast = fast->next->next)
indir = &(*indir)->next;
return *indir;
}
static void merge_sort(struct list_head *head, bool descend)
{
if (!head || list_empty(head) || list_is_singular(head))
return;
LIST_HEAD(left);
LIST_HEAD(right);
struct list_head *mid = get_list_mid(head);
list_cut_position(&left, head, mid);
list_splice_init(head, &right);
merge_sort(&left, descend);
merge_sort(&right, descend);
merge(head, &left, &right, descend);
}
/* Sort elements of queue in ascending/descending order */
void q_sort(struct list_head *head, bool descend)
{
merge_sort(head, descend);
}
/* Remove every node which has a node with a strictly less value anywhere to
* the right side of it */
int q_ascend(struct list_head *head)
{
if (!head || list_empty(head))
return 0;
LIST_HEAD(descend_list);
element_t *curr_e = NULL, *last_e = NULL;
struct list_head *curr = NULL, *tmp = NULL;
list_for_each_safe (curr, tmp, head) {
curr_e = list_entry(curr, element_t, list);
while (q_size(&descend_list) > 0) {
last_e = list_last_entry(&descend_list, element_t, list);
if (strcmp(curr_e->value, last_e->value) < 0) {
list_del_init(&last_e->list);
free(last_e->value);
free(last_e);
} else
break;
}
list_move_tail(curr, &descend_list);
}
list_splice_init(&descend_list, head);
return q_size(head);
}
/* Remove every node which has a node with a strictly greater value anywhere to
* the right side of it */
int q_descend(struct list_head *head)
{
if (!head || list_empty(head))
return 0;
LIST_HEAD(descend_list);
element_t *curr_e = NULL, *last_e = NULL;
struct list_head *curr = NULL, *tmp = NULL;
list_for_each_safe (curr, tmp, head) {
curr_e = list_entry(curr, element_t, list);
while (q_size(&descend_list) > 0) {
last_e = list_last_entry(&descend_list, element_t, list);
if (strcmp(curr_e->value, last_e->value) > 0) {
list_del_init(&last_e->list);
free(last_e->value);
free(last_e);
} else
break;
}
list_move_tail(curr, &descend_list);
}
list_splice_init(&descend_list, head);
return q_size(head);
}
/* Merge all the queues into one sorted queue, which is in ascending/descending
* order */
int q_merge(struct list_head *head, bool descend)
{
if (!head || list_empty(head))
return 0;
queue_contex_t *qc_first = list_first_entry(head, queue_contex_t, chain);
queue_contex_t *curr = NULL;
list_for_each_entry (curr, head, chain) {
if (qc_first == curr)
continue;
list_splice_init(curr->q, qc_first->q);
qc_first->size += curr->size;
curr->size = 0;
}
q_sort(qc_first->q, descend);
return qc_first->size;
}
static int compare(void *priv,
const struct list_head *a,
const struct list_head *b)
{
if (a == b)
return 0;
element_t *a_e = list_entry(a, element_t, list);
element_t *b_e = list_entry(b, element_t, list);
int res = strcmp(a_e->value, b_e->value);
if (priv)
*((int *) priv) += 1;
return res;
}
static inline size_t run_size(struct list_head *head)
{
if (!head)
return 0;
if (!head->next)
return 1;
return (size_t) (head->next->prev);
}
struct pair {
struct list_head *head, *next;
};
static size_t stk_size;
static struct list_head *merge_run(void *priv,
list_cmp_func_t cmp,
struct list_head *a,
struct list_head *b)
{
struct list_head *head = NULL;
struct list_head **tail = &head;
for (;;) {
/* if equal, take 'a' -- important for sort stability */
if (cmp(priv, a, b) <= 0) {
*tail = a;
tail = &(*tail)->next;
a = a->next;
if (!a) {
*tail = b;
break;
}
} else {
*tail = b;
tail = &(*tail)->next;
b = b->next;
if (!b) {
*tail = a;
break;
}
}
}
return head;
}
static void build_prev_link(struct list_head *head,
struct list_head *tail,
struct list_head *list)
{
tail->next = list;
do {
list->prev = tail;
tail = list;
list = list->next;
} while (list);
/* The final links to make a circular doubly-linked list */
tail->next = head;
head->prev = tail;
}
static void merge_final(void *priv,
list_cmp_func_t cmp,
struct list_head *head,
struct list_head *a,
struct list_head *b)
{
struct list_head *tail = head;
for (;;) {
/* if equal, take 'a' -- important for sort stability */
if (cmp(priv, a, b) <= 0) {
tail->next = a;
a->prev = tail;
tail = a;
a = a->next;
if (!a)
break;
} else {
tail->next = b;
b->prev = tail;
tail = b;
b = b->next;
if (!b) {
b = a;
break;
}
}
}
/* Finish linking remainder of list b on to tail */
build_prev_link(head, tail, b);
}
static struct pair find_run(void *priv,
struct list_head *list,
list_cmp_func_t cmp)
{
size_t len = 1;
struct list_head *next = list->next, *head = list;
struct pair result;
if (!next) {
result.head = head, result.next = next;
return result;
}
if (cmp(priv, list, next) > 0) {
/* decending run, also reverse the list */
struct list_head *prev = NULL;
do {
len++;
list->next = prev;
prev = list;
list = next;
next = list->next;
head = list;
} while (next && cmp(priv, list, next) > 0);
list->next = prev;
} else {
do {
len++;
list = next;
next = list->next;
} while (next && cmp(priv, list, next) <= 0);
list->next = NULL;
}
head->prev = NULL;
head->next->prev = (struct list_head *) len;
result.head = head, result.next = next;
return result;
}
static struct list_head *merge_at(void *priv,
list_cmp_func_t cmp,
struct list_head *at)
{
size_t len = run_size(at) + run_size(at->prev);
struct list_head *prev = at->prev->prev;
struct list_head *list = merge_run(priv, cmp, at->prev, at);
list->prev = prev;
list->next->prev = (struct list_head *) len;
--stk_size;
return list;
}
static struct list_head *merge_force_collapse(void *priv,
list_cmp_func_t cmp,
struct list_head *tp)
{
while (stk_size >= 3) {
if (run_size(tp->prev->prev) < run_size(tp)) {
tp->prev = merge_at(priv, cmp, tp->prev);
} else {
tp = merge_at(priv, cmp, tp);
}
}
return tp;
}
static struct list_head *merge_collapse(void *priv,
list_cmp_func_t cmp,
struct list_head *tp)
{
int n;
while ((n = stk_size) >= 2) {
if ((n >= 3 &&
run_size(tp->prev->prev) <= run_size(tp->prev) + run_size(tp)) ||
(n >= 4 && run_size(tp->prev->prev->prev) <=
run_size(tp->prev->prev) + run_size(tp->prev))) {
if (run_size(tp->prev->prev) < run_size(tp)) {
tp->prev = merge_at(priv, cmp, tp->prev);
} else {
tp = merge_at(priv, cmp, tp);
}
} else if (run_size(tp->prev) <= run_size(tp)) {
tp = merge_at(priv, cmp, tp);
} else {
break;
}
}
return tp;
}
void timsort(void *priv, struct list_head *head, bool descend)
{
if (!head || list_empty(head) || list_is_singular(head))
return;
stk_size = 0;
list_cmp_func_t cmp = compare;
struct list_head *list = head->next, *tp = NULL;
if (head == head->prev)
return;
/* Convert to a null-terminated singly-linked list. */
head->prev->next = NULL;
do {
/* Find next run */
struct pair result = find_run(priv, list, cmp);
result.head->prev = tp;
tp = result.head;
list = result.next;
stk_size++;
tp = merge_collapse(priv, cmp, tp);
} while (list);
/* End of input; merge together all the runs. */
tp = merge_force_collapse(priv, cmp, tp);
/* The final merge; rebuild prev links */
struct list_head *stk0 = tp, *stk1 = stk0->prev;
while (stk1 && stk1->prev)
stk0 = stk0->prev, stk1 = stk1->prev;
if (stk_size <= 1) {
build_prev_link(head, head, stk0);
return;
}
merge_final(priv, cmp, head, stk1, stk0);
}