-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslist.c
More file actions
177 lines (134 loc) · 3.49 KB
/
slist.c
File metadata and controls
177 lines (134 loc) · 3.49 KB
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
#include <assert.h>
#include "slist.h"
slist *slist_cons(slist *item, slist *next)
{
assert(item != NULL);
item->next = next;
return item;
}
slist *slist_uncons(slist **pitem)
{
slist *item = *pitem;
if( item != NULL ) {
*pitem = item->next;
}
return item;
}
slist* slist_pool(void *mem, size_t chunk_size, size_t mem_size)
{
slist *pool = slist_nil();
return slist_pool_(pool, mem, chunk_size, mem_size);
}
slist* slist_pool_(slist *root, void *mem, size_t chunk_size, size_t mem_size)
{
const size_t chunks = mem_size / chunk_size;
char *p = (char *) mem;
char *e = (char *) mem + chunks * chunk_size;
for( ; p < e; p += chunk_size ) {
root = slist_cons((slist *) p, root);
}
return root;
}
slist* slist_alloc( slist **pool
, void *init_ctx
, void (*init)(void *init_ctx, char *item_value))
{
slist *item = slist_uncons(pool);
if( item != NULL ) {
item->next = (slist *) slist_nil();
if( init != NULL ) {
init(init_ctx, item->value);
}
}
return item;
}
size_t slist_length(slist *head)
{
size_t length = 0;
while( head != NULL ) {
head = head->next;
++length;
}
return length;
}
void slist_foreach(slist *e, void *cc, void (*cb)(void*, void*) ) {
slist *it = e;
for(; it; it = it->next) {
cb(cc, it->value);
}
}
void slist_filt_destructive( slist **xs
, void *cc
, bool (*cb)(void*, void*)
, void *ccd
, void (*destroy)(void*, slist*)
) {
slist *new_l = slist_nil();
slist *phead = slist_nil();
if( !xs )
return;
while( xs ) {
slist *i = slist_uncons(xs);
if( i == NULL ) break;
i->next = slist_nil();
if( cb && cb(cc, i->value) ) {
if( new_l == NULL ) {
new_l = slist_cons(i, new_l);
phead = new_l;
continue;
}
phead->next = i;
phead = phead->next;
} else {
if( destroy ) {
destroy(ccd, i);
}
}
}
*xs = new_l;
}
void slist_partition_destructive( slist **xs
, slist **notmatch
, void *cc
, bool (*filt)(void*, void*)
) {
if( !xs )
return;
if( !notmatch )
return;
slist *new_l = slist_nil();
slist *phead = slist_nil();
slist *new_l2 = slist_nil();
slist *phead2 = slist_nil();
while( xs ) {
slist *i = slist_uncons(xs);
if( i == NULL ) break;
i->next = slist_nil();
if( filt && filt(cc, i->value) ) {
if( new_l == NULL ) {
new_l = slist_cons(i, new_l);
phead = new_l;
continue;
}
phead->next = i;
phead = phead->next;
} else {
if( new_l2 == NULL ) {
new_l2 = slist_cons(i, new_l2);
phead2 = new_l2;
continue;
}
phead2->next = i;
phead2 = phead2->next;
}
}
*xs = new_l;
*notmatch = new_l2;
}
void slist_reverse(slist **r) {
slist *hd = 0;
while( r && *r ) {
hd = slist_cons(slist_uncons(r), hd);
}
*r = hd;
}