-
Notifications
You must be signed in to change notification settings - Fork 2
/
stack.c
277 lines (200 loc) · 4.34 KB
/
stack.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
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#define AEM_INTERNAL
#include <aem/log.h>
#include <aem/memory.h>
#include "stack.h"
struct aem_stack *aem_stack_new(void)
{
struct aem_stack *stk = malloc(sizeof(*stk));
if (!stk) {
aem_logf_ctx(AEM_LOG_ERROR, "malloc() failed: %s", strerror(errno));
return NULL;
}
*stk = AEM_STACK_EMPTY;
return stk;
}
struct aem_stack *aem_stack_init_prealloc(struct aem_stack *stk, size_t maxn)
{
if (!stk)
return NULL;
*stk = AEM_STACK_EMPTY;
if (maxn)
aem_stack_reserve_total(stk, maxn);
return stk;
}
void aem_stack_free(struct aem_stack *stk)
{
if (!stk)
return;
aem_stack_dtor(stk);
free(stk);
}
void aem_stack_dtor(struct aem_stack *stk)
{
if (!stk)
return;
free(stk->s);
*stk = AEM_STACK_EMPTY;
}
void **aem_stack_release(struct aem_stack *stk, size_t *n_p)
{
if (!stk) {
if (n_p)
*n_p = 0;
return NULL;
}
void **s = aem_stack_shrinkwrap(stk);
if (n_p)
*n_p = stk->n;
free(stk);
return s;
}
void *aem_stack_shrinkwrap(struct aem_stack *stk)
{
if (!stk)
return NULL;
size_t maxn_new = stk->n;
if (AEM_ARRAY_RESIZE(stk->s, maxn_new)) {
aem_logf_ctx(AEM_LOG_ERROR, "realloc() failed: %s", strerror(errno));
} else {
stk->maxn = maxn_new;
}
return stk->s;
}
int aem_stack_reserve(struct aem_stack *stk, size_t len)
{
aem_assert(stk);
return aem_stack_reserve_total(stk, stk->n + len);
}
int aem_stack_reserve_total(struct aem_stack *stk, size_t maxn)
{
aem_assert(stk);
return AEM_ARRAY_GROW(stk->s, maxn, stk->maxn);
}
void aem_stack_push(struct aem_stack *stk, void *s)
{
aem_assert(stk);
aem_stack_reserve(stk, 1);
stk->s[stk->n++] = s;
}
void aem_stack_pushn(struct aem_stack *restrict stk, size_t n, void *const *restrict s)
{
if (!n)
return;
aem_assert(stk);
aem_assert(s);
aem_stack_reserve(stk, n);
memcpy(&stk->s[stk->n], s, n*sizeof(void*));
stk->n += n;
}
void aem_stack_pushv(struct aem_stack *stk, size_t n, ...)
{
aem_assert(stk);
va_list ap;
va_start(ap, n);
aem_stack_reserve(stk, n);
for (size_t i = 0; i < n; i++)
aem_stack_push(stk, va_arg(ap, void*));
va_end(ap);
}
size_t aem_stack_transfer(struct aem_stack *restrict dest, struct aem_stack *restrict src, size_t n)
{
if (!n)
return 0;
aem_assert(dest);
aem_assert(src);
if (src->n < n)
return 0;
size_t new_top = src->n - n;
aem_stack_pushn(dest, n, &src->s[new_top]);
aem_stack_trunc(src, new_top);
return n;
}
void *aem_stack_pop(struct aem_stack *stk)
{
if (!stk)
return NULL;
if (!stk->n)
return NULL;
return stk->s[--stk->n];
}
void *aem_stack_peek(struct aem_stack *stk)
{
if (!stk)
return NULL;
if (!stk->n)
return NULL;
return aem_stack_index(stk, stk->n-1);
}
void *aem_stack_index_end(struct aem_stack *stk, size_t i)
{
if (!stk)
return NULL;
return aem_stack_index(stk, stk->n - 1 - i);
}
void **aem_stack_index_p(struct aem_stack *stk, size_t i)
{
aem_assert(stk);
//aem_stack_reserve_total(stk, i+1)
// Push NULLs until i is a valid index.
while (i >= stk->n)
aem_stack_push(stk, NULL);
return &stk->s[i];
}
void aem_stack_assign(struct aem_stack *stk, size_t i, void *s)
{
aem_assert(stk);
void **p = aem_stack_index_p(stk, i);
aem_assert(p);
*p = s;
}
size_t aem_stack_assign_empty(struct aem_stack *stk, void *s)
{
aem_assert(stk);
// Skip over elements until we find one that's NULL (or nonexistent)
size_t i = 0;
while (aem_stack_index(stk, i))
i++;
aem_stack_assign(stk, i, s);
return i;
}
void *aem_stack_remove(struct aem_stack *stk, size_t i)
{
aem_assert(stk);
if (i >= stk->n)
return NULL;
// Replace object with NULL
void *p = stk->s[i];
stk->s[i] = NULL;
// Decrease vector size as much as possible.
// Pop all trailing NULL elements.
while (stk->n && !stk->s[stk->n-1])
stk->n--;
return p;
}
int aem_stack_insert(struct aem_stack *stk, size_t i, void *p)
{
aem_assert(stk);
if (i > stk->n)
return 1;
aem_stack_reserve(stk, 1);
memmove(&stk->s[i+1], &stk->s[i], (stk->n - i)*sizeof(p));
stk->s[i] = p;
stk->n++;
return 0;
}
int aem_stack_insert_end(struct aem_stack *stk, size_t i, void *p)
{
aem_assert(stk);
if (i > stk->n)
return 1;
return aem_stack_insert(stk, stk->n - i, p);
}
void aem_stack_qsort(struct aem_stack *stk, int (*compar)(const void *p1, const void *p2))
{
aem_assert(stk);
qsort(stk->s, stk->n, sizeof(*stk->s), compar);
}