1 /* |
1 /* |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 * |
3 * |
4 * Copyright 2015 Olaf Wintermann. All rights reserved. |
4 * Copyright 2016 Olaf Wintermann. All rights reserved. |
5 * |
5 * |
6 * Redistribution and use in source and binary forms, with or without |
6 * Redistribution and use in source and binary forms, with or without |
7 * modification, are permitted provided that the following conditions are met: |
7 * modification, are permitted provided that the following conditions are met: |
8 * |
8 * |
9 * 1. Redistributions of source code must retain the above copyright |
9 * 1. Redistributions of source code must retain the above copyright |
74 e = e->next; |
74 e = e->next; |
75 alfree(alloc, f); |
75 alfree(alloc, f); |
76 } |
76 } |
77 } |
77 } |
78 |
78 |
|
79 void ucx_list_free_content(UcxList* list, ucx_destructor destr) { |
|
80 while (list != NULL) { |
|
81 destr(list->data); |
|
82 list = list->next; |
|
83 } |
|
84 } |
|
85 |
79 UcxList *ucx_list_append(UcxList *l, void *data) { |
86 UcxList *ucx_list_append(UcxList *l, void *data) { |
80 return ucx_list_append_a(ucx_default_allocator(), l, data); |
87 return ucx_list_append_a(ucx_default_allocator(), l, data); |
81 } |
88 } |
82 |
89 |
83 UcxList *ucx_list_append_a(UcxAllocator *alloc, UcxList *l, void *data) { |
90 UcxList *ucx_list_append_a(UcxAllocator *alloc, UcxList *l, void *data) { |
97 nl->prev = NULL; |
104 nl->prev = NULL; |
98 return nl; |
105 return nl; |
99 } |
106 } |
100 } |
107 } |
101 |
108 |
|
109 UcxList *ucx_list_append_once(UcxList *l, void *data, |
|
110 cmp_func cmpfnc, void *cmpdata) { |
|
111 return ucx_list_append_once_a(ucx_default_allocator(), l, |
|
112 data, cmpfnc, cmpdata); |
|
113 } |
|
114 |
|
115 UcxList *ucx_list_append_once_a(UcxAllocator *alloc, UcxList *l, void *data, |
|
116 cmp_func cmpfnc, void *cmpdata) { |
|
117 |
|
118 UcxList *last = NULL; |
|
119 { |
|
120 UcxList *e = l; |
|
121 while (e) { |
|
122 if (cmpfnc(e->data, data, cmpdata) == 0) { |
|
123 return l; |
|
124 } |
|
125 last = e; |
|
126 e = e->next; |
|
127 } |
|
128 } |
|
129 |
|
130 UcxList *nl = ucx_list_append_a(alloc, NULL, data); |
|
131 if (!nl) { |
|
132 return NULL; |
|
133 } |
|
134 |
|
135 if (last == NULL) { |
|
136 return nl; |
|
137 } else { |
|
138 nl->prev = last; |
|
139 last->next = nl; |
|
140 return l; |
|
141 } |
|
142 } |
|
143 |
102 UcxList *ucx_list_prepend(UcxList *l, void *data) { |
144 UcxList *ucx_list_prepend(UcxList *l, void *data) { |
103 return ucx_list_prepend_a(ucx_default_allocator(), l, data); |
145 return ucx_list_prepend_a(ucx_default_allocator(), l, data); |
104 } |
146 } |
105 |
147 |
106 UcxList *ucx_list_prepend_a(UcxAllocator *alloc, UcxList *l, void *data) { |
148 UcxList *ucx_list_prepend_a(UcxAllocator *alloc, UcxList *l, void *data) { |
113 if (l) { |
155 if (l) { |
114 nl->next = l; |
156 nl->next = l; |
115 l->prev = nl; |
157 l->prev = nl; |
116 } |
158 } |
117 return nl; |
159 return nl; |
|
160 } |
|
161 |
|
162 UcxList *ucx_list_prepend_once(UcxList *l, void *data, |
|
163 cmp_func cmpfnc, void* cmpdata) { |
|
164 return ucx_list_prepend_once_a(ucx_default_allocator(), l, |
|
165 data, cmpfnc, cmpdata); |
|
166 } |
|
167 |
|
168 UcxList *ucx_list_prepend_once_a(UcxAllocator *alloc, UcxList *l, void *data, |
|
169 cmp_func cmpfnc, void *cmpdata) { |
|
170 |
|
171 if (l) { |
|
172 int found = 0; |
|
173 UcxList *first; |
|
174 { |
|
175 UcxList *e = l; |
|
176 while (e) { |
|
177 found |= (cmpfnc(e->data, data, cmpdata) == 0); |
|
178 first = e; |
|
179 e = e->prev; |
|
180 } |
|
181 } |
|
182 |
|
183 if (found) { |
|
184 return first; |
|
185 } else { |
|
186 UcxList *nl = ucx_list_append_a(alloc, NULL, data); |
|
187 nl->next = first; |
|
188 first->prev = nl; |
|
189 return nl; |
|
190 } |
|
191 } else { |
|
192 return ucx_list_append_a(alloc, NULL, data); |
|
193 } |
118 } |
194 } |
119 |
195 |
120 UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) { |
196 UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) { |
121 if (l1) { |
197 if (l1) { |
122 UcxList *last = ucx_list_last(l1); |
198 UcxList *last = ucx_list_last(l1); |