forked from yanjiew1/linux23q1-timsort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timsort_dynamic_stack(single_linked_list).c
238 lines (211 loc) · 5.05 KB
/
timsort_dynamic_stack(single_linked_list).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
#include "list.h"
#include "list_sort.h"
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#ifndef likely
#define likely(x) __builtin_expect(!!(x), 1)
#endif
#ifndef unlikely
#define unlikely(x) __builtin_expect(!!(x), 0)
#endif
#define MAX_MERGE_PENDING 85
struct run {
struct list_head *list;
size_t len;
struct run* next;
// struct run* prev;
};
int stk_size = 0;
static struct list_head *merge(void *priv, list_cmp_func_t cmp,
struct list_head *a, struct list_head *b)
{
struct list_head *head, **tail = &head;
for (;;) {
/* if equal, take 'a' -- important for sort stability */
if (cmp(priv, a, b) <= 0) {
*tail = a;
tail = &a->next;
a = a->next;
if (!a) {
*tail = b;
break;
}
} else {
*tail = b;
tail = &b->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;
uint8_t count = 0;
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 list_head *find_run(void *priv, struct list_head *list,
size_t *len, list_cmp_func_t cmp)
{
*len = 1;
struct list_head *next = list->next;
if (unlikely(next == NULL))
return NULL;
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;
} 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;
}
return next;
}
static void merge_at(void *priv, list_cmp_func_t cmp, struct run *at)
{
(*(at->next)).list = merge(priv, cmp, (*(at->next)).list, ((*at).list));
(*(at->next)).len += (*at).len;
}
static struct run *merge_force_collapse(void *priv, list_cmp_func_t cmp,
struct run *stk, struct run *tp)
{
while (stk_size >= 3) {
if ((*(tp->next->next)).len < (*tp).len) {
merge_at(priv, cmp, &(*(tp->next)));
tp->next->list = tp->list;
tp->next->len = tp->len;
} else {
merge_at(priv, cmp, &(*(tp)));
}
// tp--;
struct run* tmp = tp;
tp = tp->next;
free(tmp);
stk_size--;
}
return tp;
}
static struct run *merge_collapse(void *priv, list_cmp_func_t cmp,
struct run *stk, struct run *tp)
{
int n;
while ((n = stk_size) >= 2) {
if ((n >= 3 && (*(tp->next->next)).len <= (*(tp->next)).len + (*tp).len) ||
(n >= 4 && (*(tp->next->next->next)).len <= (*(tp->next->next)).len + (*(tp->next)).len)) {
if ((*(tp->next->next)).len < (*tp).len) {
merge_at(priv, cmp, &(*(tp->next)));
tp->next->list = tp->list;
tp->next->len = tp->len;
} else {
merge_at(priv, cmp, &(*(tp)));
}
} else if ((*(tp->next)).len <= (*tp).len) {
merge_at(priv, cmp, &(*(tp)));
} else {
break;
}
// tp--;
struct run* tmp = tp;
tp = tp->next;
free(tmp);
stk_size--;
}
return tp;
}
void inplace_timsort(void *priv, struct list_head *head, list_cmp_func_t cmp)
{
struct list_head *list = head->next;
// struct run stk[MAX_MERGE_PENDING], *tp = stk - 1;
struct run *stk = NULL;
struct run *tp = NULL;
stk_size = 0;
if (head == head->prev)
return;
/* Convert to a null-terminated singly-linked list. */
head->prev->next = NULL;
do {
// tp++;
/* Find next run */
struct run *new_tp = malloc(sizeof(struct run));
if(!new_tp){
printf("malloc failed\n");
exit(1);
}
new_tp->next = tp;
tp = new_tp;
stk_size++;
tp->list = list;
list = find_run(priv, list, &tp->len, cmp);
tp = merge_collapse(priv, cmp, stk, tp);
} while (list);
/* End of input; merge together all the runs. */
tp = merge_force_collapse(priv, cmp, stk, tp);
/* The final merge; rebuild prev links */
// if (tp > stk) {
// merge_final(priv, cmp, head, stk[0].list, stk[1].list);
// } else {
// build_prev_link(head, head, stk->list);
// }
struct run *stk0 = tp;
struct run *stk1 = tp->next;
while(stk1 && stk1->next) {
stk0 = stk0->next;
stk1 = stk1->next;
}
if (stk_size > 1) {
merge_final(priv, cmp, head, (*stk1).list, (*(stk0)).list);
} else {
build_prev_link(head, head, stk1->list);
}
}