-
Notifications
You must be signed in to change notification settings - Fork 4
/
selist.c
332 lines (306 loc) · 9.03 KB
/
selist.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
/*
Copyright (c) 2010-2015, Enno Rehling <enno@eressea.de>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/
#include "selist.h"
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#define LIST_MAXSIZE 14 /* max. number of elements unrolled into one node */
#define LIST_LIMIT 7 /* this many or fewer number in a node => attempt merge */
/* The total size of this struct is 64 bytes on a 32-bit system with
* normal alignment. YMMV, so on a 64-bit system, twiddle the
* constants above */
struct selist {
struct selist *next;
int num_elements;
void *elements[LIST_MAXSIZE];
};
bool selist_find(struct selist **qlp, int *index, const void *value, int(*match)(const void *, const void *))
{
for (; *qlp; selist_advance(qlp, index, 1)) {
void *x = selist_get(*qlp, *index);
if (match ? match(value, x) : value == x) {
return true;
}
}
return false;
}
bool selist_contains(selist *ql, const void *value, int(*equal)(const void *, const void *))
{
int i = 0;
return selist_find(&ql, &i, value, equal);
}
void *selist_get(const selist * ql, int i)
{
assert(ql);
return (i < ql->num_elements) ? ql->elements[i] : selist_get(ql->next, i - ql->num_elements);
}
void *selist_replace(selist * ql, int i, void *data)
{
assert(ql);
if (i < ql->num_elements) {
void *orig = ql->elements[i];
ql->elements[i] = data;
return orig;
}
else {
return selist_replace(ql->next, i - ql->num_elements, data);
}
}
int selist_length(const selist * ql)
{
int size = 0;
const selist *it;
for (it=ql; it; it = it->next) {
size += it->num_elements;
}
return size;
}
int selist_empty(const selist * ql)
{
return !ql;
}
selist * selist_push(selist ** qlp, void *data)
{
selist *ql = NULL;
while (*qlp && ((*qlp)->next || (*qlp)->num_elements == LIST_MAXSIZE)) {
qlp = &(*qlp)->next;
}
if (!*qlp) {
ql = (selist *)malloc(sizeof(selist));
if (ql) {
ql->num_elements = 0;
ql->next = 0;
*qlp = ql;
}
}
else {
ql = *qlp;
}
if (ql) {
ql->elements[ql->num_elements++] = data;
}
return ql;
}
void * selist_pop(selist **qlp)
{
while (*qlp) {
selist *ql = *qlp;
if (ql->next == NULL) {
void * result;
int i = ql->num_elements - 1;
assert(i >= 0);
result = selist_get(ql, i);
selist_delete(qlp, i);
return result;
}
qlp = &ql->next;
}
return NULL;
}
int selist_delete(selist ** qlp, int i)
{
selist *ql = *qlp;
if (i < 0)
return EINVAL;
if (ql && i >= ql->num_elements) {
return selist_delete(&ql->next, i - ql->num_elements);
}
else if (ql) {
if (i + 1 < ql->num_elements) {
size_t len = ((size_t)ql->num_elements - i - 1) * sizeof(void *);
memmove(ql->elements + i, ql->elements + i + 1, len);
}
--ql->num_elements;
if (ql->num_elements == 0) {
*qlp = ql->next;
free(ql);
}
else if (ql->next && ql->num_elements <= LIST_LIMIT) {
selist *qn = ql->next;
if (ql->num_elements + qn->num_elements > LIST_MAXSIZE) {
size_t len = ((size_t)qn->num_elements - 1) * sizeof(void *);
ql->elements[ql->num_elements] = qn->elements[0];
--qn->num_elements;
++ql->num_elements;
memmove(qn->elements, qn->elements + 1, len);
}
else {
size_t len = (size_t)qn->num_elements * sizeof(void *);
memcpy(ql->elements + ql->num_elements, qn->elements, len);
ql->num_elements += qn->num_elements;
ql->next = qn->next;
free(qn);
}
}
}
return 0;
}
int selist_insert(selist ** qlp, int i, void *data)
{
selist *ql = *qlp;
if (ql) {
if (i >= LIST_MAXSIZE) {
return selist_insert(&ql->next, i - ql->num_elements, data);
}
else if (ql->num_elements < LIST_MAXSIZE && i<=ql->num_elements) {
if (i<ql->num_elements) {
size_t len = (size_t)(ql->num_elements - i) * sizeof(void *);
memmove(ql->elements + i + 1, ql->elements + i, len);
}
ql->elements[i] = data;
++ql->num_elements;
}
else {
selist *qn = (selist *)malloc(sizeof(selist));
if (qn) {
qn->next = ql->next;
ql->next = qn;
qn->num_elements = ql->num_elements - LIST_LIMIT;
ql->num_elements = LIST_LIMIT;
memcpy(qn->elements, ql->elements + ql->num_elements, //TODO: V512 http://www.viva64.com/en/V512 A call of the 'memcpy' function will lead to underflow of the buffer 'qn->elements'.
LIST_LIMIT * sizeof(void *));
}
if (i <= ql->num_elements) {
return selist_insert(qlp, i, data);
}
else {
return selist_insert(&ql->next, i - ql->num_elements, data);
}
}
}
else if (i == 0) {
selist_push(qlp, data);
}
else {
return EINVAL;
}
return 0;
}
void selist_foreach_ex(struct selist *ql, selist_cbex cb, void *more)
{
for (; ql; ql = ql->next) {
int i;
for (i = 0; i != ql->num_elements; ++i) {
if (!cb(ql->elements[i], more)) break;
}
}
}
void selist_foreach(struct selist *ql, selist_cb cb)
{
for (; ql; ql = ql->next) {
int i;
for (i = 0; i != ql->num_elements; ++i) {
cb(ql->elements[i]);
}
}
}
int selist_advance(struct selist **iterator, int *index, int stride)
{
selist *ql = *iterator;
int i = *index;
if (!ql || i < 0 || stride < 0) {
return ERANGE;
}
else if (i + stride < ql->num_elements) {
*index = i + stride;
return 0;
}
else {
*index = i - ql->num_elements + stride;
*iterator = ql->next;
return selist_advance(iterator, index, 0);
}
}
void selist_free(struct selist *ql)
{
while (ql) {
selist * qn = ql;
ql = ql->next;
free(qn);
}
}
void selist_map_reduce(struct selist *ql, void(*mapfunc)(void *entry, void *data), void(*reducefunc)(void *data, void *result), void *data, void *result)
{
int qi;
for (qi = 0; ql; selist_advance(&ql, &qi, 1)) {
mapfunc(selist_get(ql, qi), data);
reducefunc(data, result);
}
}
static int cmp_voidptr(const void *a, const void *b)
{
if (a<b) return -1;
if (b<a) return 1;
return 0;
}
bool selist_set_insert(struct selist **qlp, void *data, cmp_cb cmp)
{
if (*qlp) {
selist *ql = *qlp;
if (cmp==NULL) cmp = cmp_voidptr;
if (ql->num_elements > 0 && cmp(ql->elements[ql->num_elements - 1], data) < 0) {
if (ql->num_elements == LIST_MAXSIZE || (ql->next
&& cmp(ql->next->elements[0], data) <= 0)) {
return selist_set_insert(&ql->next, data, cmp);
}
else {
ql->elements[ql->num_elements++] = data;
}
return true;
}
else {
int i;
/* TODO: OPT | binary search */
for (i = 0; i != ql->num_elements; ++i) {
int cmpi = cmp(data, ql->elements[i]);
if (cmpi < 0) {
selist_insert(qlp, i, data);
return true;
}
if (cmpi == 0) {
return false;
}
}
}
}
selist_push(qlp, data);
return true;
}
bool selist_set_find(struct selist **qlp, int *qip, const void *data, cmp_cb cmp)
{
selist *ql = *qlp;
int qi;
while (ql && cmp(ql->elements[ql->num_elements - 1], data) < 0) {
ql = ql->next;
}
if (!ql)
return false;
if (cmp==NULL) cmp = cmp_voidptr;
/* TODO: OPT | binary search */
for (qi = 0; qi != ql->num_elements; ++qi) {
int cmpi = cmp(ql->elements[qi], data);
if (cmpi > 0) {
return false;
}
if (cmpi == 0) {
if (qip) {
*qip = qi;
*qlp = ql;
}
return true;
}
}
return false;
}