27 */ |
27 */ |
28 |
28 |
29 #include "list.h" |
29 #include "list.h" |
30 |
30 |
31 UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data) { |
31 UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data) { |
|
32 return ucx_list_clone_a(ucx_default_allocator(), l, fnc, data); |
|
33 } |
|
34 |
|
35 UcxList *ucx_list_clone_a(UcxAllocator *alloc, UcxList *l, |
|
36 copy_func fnc, void *data) { |
32 UcxList *ret = NULL; |
37 UcxList *ret = NULL; |
33 while (l != NULL) { |
38 while (l) { |
34 if (fnc != NULL) { |
39 if (fnc) { |
35 ret = ucx_list_append(ret, fnc(l->data, data)); |
40 ret = ucx_list_append_a(alloc, ret, fnc(l->data, data)); |
36 } else { |
41 } else { |
37 ret = ucx_list_append(ret, l->data); |
42 ret = ucx_list_append_a(alloc, ret, l->data); |
38 } |
43 } |
39 l = l->next; |
44 l = l->next; |
40 } |
45 } |
41 return ret; |
46 return ret; |
42 } |
47 } |
57 |
62 |
58 return (l1 == NULL && l2 == NULL); |
63 return (l1 == NULL && l2 == NULL); |
59 } |
64 } |
60 |
65 |
61 void ucx_list_free(UcxList *l) { |
66 void ucx_list_free(UcxList *l) { |
|
67 ucx_list_free_a(ucx_default_allocator(), l); |
|
68 } |
|
69 |
|
70 void ucx_list_free_a(UcxAllocator *alloc, UcxList *l) { |
62 UcxList *e = l, *f; |
71 UcxList *e = l, *f; |
63 while (e != NULL) { |
72 while (e != NULL) { |
64 f = e; |
73 f = e; |
65 e = e->next; |
74 e = e->next; |
66 free(f); |
75 alloc->free(alloc->pool, f); |
67 } |
76 } |
68 } |
77 } |
69 |
78 |
70 UcxList *ucx_list_append(UcxList *l, void *data) { |
79 UcxList *ucx_list_append(UcxList *l, void *data) { |
71 UcxList *nl = (UcxList*) malloc(sizeof(UcxList)); |
80 return ucx_list_append_a(ucx_default_allocator(), l, data); |
72 if (nl == NULL) return NULL; |
81 } |
|
82 |
|
83 UcxList *ucx_list_append_a(UcxAllocator *alloc, UcxList *l, void *data) { |
|
84 UcxList *nl = (UcxList*) alloc->malloc(alloc->pool, sizeof(UcxList)); |
|
85 if (!nl) { |
|
86 return NULL; |
|
87 } |
73 |
88 |
74 nl->data = data; |
89 nl->data = data; |
75 nl->next = NULL; |
90 nl->next = NULL; |
76 if (l == NULL) { |
91 if (l) { |
77 nl->prev = NULL; |
|
78 return nl; |
|
79 } else { |
|
80 UcxList *t = ucx_list_last(l); |
92 UcxList *t = ucx_list_last(l); |
81 t->next = nl; |
93 t->next = nl; |
82 nl->prev = t; |
94 nl->prev = t; |
83 return l; |
95 return l; |
|
96 } else { |
|
97 nl->prev = NULL; |
|
98 return nl; |
84 } |
99 } |
85 } |
100 } |
86 |
101 |
87 UcxList *ucx_list_prepend(UcxList *l, void *data) { |
102 UcxList *ucx_list_prepend(UcxList *l, void *data) { |
88 UcxList *nl = ucx_list_append(NULL, data); |
103 return ucx_list_prepend_a(ucx_default_allocator(), l, data); |
89 if (nl == NULL) return NULL; |
104 } |
90 |
105 |
91 if (l != NULL) { |
106 UcxList *ucx_list_prepend_a(UcxAllocator *alloc, UcxList *l, void *data) { |
|
107 UcxList *nl = ucx_list_append_a(alloc, NULL, data); |
|
108 if (!nl) { |
|
109 return NULL; |
|
110 } |
|
111 l = ucx_list_first(l); |
|
112 |
|
113 if (l) { |
92 nl->next = l; |
114 nl->next = l; |
93 l->prev = nl; |
115 l->prev = nl; |
94 } |
116 } |
95 return nl; |
117 return nl; |
96 } |
118 } |
258 return l; |
280 return l; |
259 } |
281 } |
260 } |
282 } |
261 |
283 |
262 UcxList *ucx_list_first(const UcxList *l) { |
284 UcxList *ucx_list_first(const UcxList *l) { |
263 if (l == NULL) return NULL; |
285 if (!l) { |
|
286 return NULL; |
|
287 } |
264 |
288 |
265 const UcxList *e = l; |
289 const UcxList *e = l; |
266 while (e->prev != NULL) { |
290 while (e->prev) { |
267 e = e->prev; |
291 e = e->prev; |
268 } |
292 } |
269 return (UcxList *)e; |
293 return (UcxList *)e; |
270 } |
294 } |
271 |
295 |
272 UcxList *ucx_list_remove(UcxList *l, UcxList *e) { |
296 UcxList *ucx_list_remove(UcxList *l, UcxList *e) { |
|
297 return ucx_list_remove_a(ucx_default_allocator(), l, e); |
|
298 } |
|
299 |
|
300 UcxList *ucx_list_remove_a(UcxAllocator *alloc, UcxList *l, UcxList *e) { |
273 if (e->prev == NULL) { |
301 if (e->prev == NULL) { |
274 if(e->next != NULL) { |
302 if(e->next != NULL) { |
275 e->next->prev = NULL; |
303 e->next->prev = NULL; |
276 l = e->next; |
304 l = e->next; |
277 } else { |
305 } else { |