1 |
|
---|
2 | /* singularly linked-list */
|
---|
3 |
|
---|
4 | #include <stdlib.h>
|
---|
5 |
|
---|
6 | #include "globals.h"
|
---|
7 | #include "module-datastruct-llist.h"
|
---|
8 |
|
---|
9 | static void _destroy(LLIST *l)
|
---|
10 | {
|
---|
11 | if (!l) return;
|
---|
12 |
|
---|
13 | add_garbage(l);
|
---|
14 | }
|
---|
15 |
|
---|
16 | LLIST *ll_create()
|
---|
17 | {
|
---|
18 | LLIST *l = calloc(1, sizeof(LLIST));
|
---|
19 |
|
---|
20 | return l;
|
---|
21 | }
|
---|
22 |
|
---|
23 | void ll_destroy(LLIST *l)
|
---|
24 | {
|
---|
25 | if (!l) return;
|
---|
26 | ll_clear(l);
|
---|
27 |
|
---|
28 | _destroy(l);
|
---|
29 | }
|
---|
30 |
|
---|
31 | void ll_destroy_data(LLIST *l)
|
---|
32 | {
|
---|
33 | if (!l) return;
|
---|
34 | ll_clear_data(l);
|
---|
35 |
|
---|
36 | _destroy(l);
|
---|
37 | }
|
---|
38 |
|
---|
39 | void ll_clear(LLIST *l)
|
---|
40 | {
|
---|
41 | if (!l) return;
|
---|
42 |
|
---|
43 | LL_ITER *it = ll_iter_create(l);
|
---|
44 | while (ll_iter_next(it))
|
---|
45 | ll_iter_remove(it);
|
---|
46 | ll_iter_release(it);
|
---|
47 | l->count = 0;
|
---|
48 | }
|
---|
49 |
|
---|
50 | void ll_clear_data(LLIST *l)
|
---|
51 | {
|
---|
52 | if (!l) return;
|
---|
53 |
|
---|
54 | LL_ITER *it = ll_iter_create(l);
|
---|
55 | while (ll_iter_next(it))
|
---|
56 | ll_iter_remove_data(it);
|
---|
57 | ll_iter_release(it);
|
---|
58 | l->count = 0;
|
---|
59 | }
|
---|
60 |
|
---|
61 | LL_NODE* ll_append(LLIST *l, void *obj)
|
---|
62 | {
|
---|
63 | if (l && obj) {
|
---|
64 | LL_NODE *new = calloc(1, sizeof(LL_NODE));
|
---|
65 | LL_NODE *n = l->initial;
|
---|
66 |
|
---|
67 | new->obj = obj;
|
---|
68 |
|
---|
69 | if (n) {
|
---|
70 | while (n->nxt) n = n->nxt;
|
---|
71 | n->nxt = new;
|
---|
72 | } else
|
---|
73 | l->initial = new;
|
---|
74 |
|
---|
75 | l->count++;
|
---|
76 |
|
---|
77 | return new;
|
---|
78 | }
|
---|
79 |
|
---|
80 | return NULL;
|
---|
81 | }
|
---|
82 |
|
---|
83 | LL_NODE *ll_prepend(LLIST *l, void *obj)
|
---|
84 | {
|
---|
85 | if (l && obj) {
|
---|
86 | LL_NODE *new = calloc(1, sizeof(LL_NODE));
|
---|
87 |
|
---|
88 | new->obj = obj;
|
---|
89 | new->nxt = l->initial;
|
---|
90 |
|
---|
91 | l->initial = new;
|
---|
92 | l->count++;
|
---|
93 |
|
---|
94 | return new;
|
---|
95 | }
|
---|
96 |
|
---|
97 | return NULL;
|
---|
98 | }
|
---|
99 |
|
---|
100 | LL_ITER *ll_iter_create(LLIST *l)
|
---|
101 | {
|
---|
102 | if (!l) return NULL;
|
---|
103 |
|
---|
104 | LL_ITER *it = calloc(1, sizeof(LL_ITER));
|
---|
105 |
|
---|
106 | it->l = l;
|
---|
107 |
|
---|
108 | return it;
|
---|
109 | }
|
---|
110 |
|
---|
111 | LL_ITER *ll_iter_create_s(LLIST *l, LL_ITER *it)
|
---|
112 | {
|
---|
113 | if (!l) return NULL;
|
---|
114 | it->l = l;
|
---|
115 | return it;
|
---|
116 | }
|
---|
117 |
|
---|
118 | void ll_iter_release(LL_ITER *it)
|
---|
119 | {
|
---|
120 | add_garbage(it);
|
---|
121 | }
|
---|
122 |
|
---|
123 | void *ll_iter_next(LL_ITER *it)
|
---|
124 | {
|
---|
125 | if (it) {
|
---|
126 | if (it->cur) {
|
---|
127 | it->prv = it->cur;
|
---|
128 | it->cur = it->cur->nxt;
|
---|
129 | } else if (it->l->initial && !it->prv)
|
---|
130 | it->cur = it->l->initial;
|
---|
131 |
|
---|
132 | if (it->cur)
|
---|
133 | return it->cur->obj;
|
---|
134 | }
|
---|
135 |
|
---|
136 | return NULL;
|
---|
137 | }
|
---|
138 |
|
---|
139 | void *ll_iter_peek(LL_ITER *it, int offset)
|
---|
140 | {
|
---|
141 | LL_NODE *n = it->cur;
|
---|
142 | int i;
|
---|
143 |
|
---|
144 | for (i = 0; i < offset; i++)
|
---|
145 | if (n)
|
---|
146 | n = n->nxt;
|
---|
147 | else
|
---|
148 | return NULL;
|
---|
149 |
|
---|
150 | if (!n)
|
---|
151 | return NULL;
|
---|
152 |
|
---|
153 | return n->obj;
|
---|
154 | }
|
---|
155 |
|
---|
156 | void ll_iter_reset(LL_ITER *it)
|
---|
157 | {
|
---|
158 | if (it) {
|
---|
159 | it->prv = NULL;
|
---|
160 | it->cur = NULL;
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | void ll_iter_insert(LL_ITER *it, void *obj)
|
---|
165 | {
|
---|
166 | if (it && obj) {
|
---|
167 | if (!it->cur || !it->cur->nxt)
|
---|
168 | ll_append(it->l, obj);
|
---|
169 | else {
|
---|
170 | LL_NODE *n = calloc(1, sizeof(LL_NODE));
|
---|
171 |
|
---|
172 | n->obj = obj;
|
---|
173 | n->nxt = it->cur->nxt;
|
---|
174 | it->cur->nxt = n;
|
---|
175 |
|
---|
176 | it->l->count++;
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | void *ll_iter_remove(LL_ITER *it)
|
---|
182 | {
|
---|
183 | if (it) {
|
---|
184 | LL_NODE *del = it->cur;
|
---|
185 |
|
---|
186 | if (del) {
|
---|
187 | void *obj = del->obj;
|
---|
188 | LL_NODE *prv = it->prv;
|
---|
189 |
|
---|
190 | if (prv)
|
---|
191 | prv->nxt = del->nxt;
|
---|
192 | else
|
---|
193 | it->l->initial = del->nxt;
|
---|
194 |
|
---|
195 | it->l->count--;
|
---|
196 |
|
---|
197 | ll_iter_reset(it);
|
---|
198 | while (prv && ll_iter_next(it))
|
---|
199 | if (it->cur == prv)
|
---|
200 | break;
|
---|
201 |
|
---|
202 | add_garbage(del);
|
---|
203 | return obj;
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | return NULL;
|
---|
208 | }
|
---|
209 |
|
---|
210 | void ll_iter_remove_data(LL_ITER *it)
|
---|
211 | {
|
---|
212 | void *obj = ll_iter_remove(it);
|
---|
213 | add_garbage(obj);
|
---|
214 | }
|
---|
215 |
|
---|
216 | int ll_count(LLIST *l)
|
---|
217 | {
|
---|
218 | if (!l)
|
---|
219 | return 0;
|
---|
220 |
|
---|
221 | return l->count;
|
---|
222 | }
|
---|
223 |
|
---|
224 | void *ll_has_elements(LLIST *l) {
|
---|
225 | if (!l || !l->initial)
|
---|
226 | return NULL;
|
---|
227 | return l->initial->obj;
|
---|
228 | }
|
---|
229 |
|
---|
230 | int ll_contains(LLIST *l, void *obj)
|
---|
231 | {
|
---|
232 | if (!l || !obj)
|
---|
233 | return 0;
|
---|
234 | LL_ITER *it = ll_iter_create(l);
|
---|
235 | void *data;
|
---|
236 | while ((data=ll_iter_next(it))) {
|
---|
237 | if (data==obj)
|
---|
238 | break;
|
---|
239 | }
|
---|
240 | ll_iter_release(it);
|
---|
241 | return (data==obj);
|
---|
242 | }
|
---|
243 |
|
---|
244 | void ll_remove(LLIST *l, void *obj)
|
---|
245 | {
|
---|
246 | LL_ITER *it = ll_iter_create(l);
|
---|
247 | void *data;
|
---|
248 | while ((data=ll_iter_next(it))) {
|
---|
249 | if (data==obj)
|
---|
250 | ll_iter_remove(it);
|
---|
251 | }
|
---|
252 | ll_iter_release(it);
|
---|
253 | }
|
---|
254 |
|
---|
255 | void ll_remove_data(LLIST *l, void *obj)
|
---|
256 | {
|
---|
257 | LL_ITER *it = ll_iter_create(l);
|
---|
258 | void *data;
|
---|
259 | while ((data=ll_iter_next(it))) {
|
---|
260 | if (data==obj)
|
---|
261 | ll_iter_remove_data(it);
|
---|
262 | }
|
---|
263 | ll_iter_release(it);
|
---|
264 | }
|
---|
265 |
|
---|
266 | // removes all elements from l where elements are in elements_to_remove
|
---|
267 | int ll_remove_all(LLIST *l, LLIST *elements_to_remove)
|
---|
268 | {
|
---|
269 | int count = 0;
|
---|
270 | LL_ITER *it1 = ll_iter_create(l);
|
---|
271 | LL_ITER *it2 = ll_iter_create(elements_to_remove);
|
---|
272 |
|
---|
273 | void *data1, *data2;
|
---|
274 | while ((data1=ll_iter_next(it1))) {
|
---|
275 | ll_iter_reset(it2);
|
---|
276 | while ((data2=ll_iter_next(it2))) {
|
---|
277 | if (data1 == data2) {
|
---|
278 | ll_iter_remove(it1);
|
---|
279 | count++;
|
---|
280 | break;
|
---|
281 | }
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 | ll_iter_release(it2);
|
---|
286 | ll_iter_release(it1);
|
---|
287 |
|
---|
288 | return count;
|
---|
289 | }
|
---|