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 2016 Olaf Wintermann. All rights reserved. |
4 * Copyright 2017 Mike Becker, 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 |
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
27 */ |
27 */ |
28 |
28 |
29 #include "list.h" |
29 #include "ucx/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); |
32 return ucx_list_clone_a(ucx_default_allocator(), l, fnc, data); |
33 } |
33 } |
34 |
34 |
203 } |
204 } |
204 |
205 |
205 return s; |
206 return s; |
206 } |
207 } |
207 |
208 |
208 static UcxList *ucx_list_sort_merge(int length, |
209 static UcxList *ucx_list_sort_merge(size_t length, |
209 UcxList* restrict ls, UcxList* restrict le, UcxList* restrict re, |
210 UcxList* ls, UcxList* le, UcxList* re, |
210 cmp_func fnc, void* data) { |
211 cmp_func fnc, void* data) { |
211 |
212 |
212 UcxList** sorted = (UcxList**) malloc(sizeof(UcxList*)*length); |
213 UcxList** sorted = (UcxList**) malloc(sizeof(UcxList*)*length); |
213 UcxList *rc, *lc; |
214 UcxList *rc, *lc; |
214 |
215 |
215 lc = ls; rc = le; |
216 lc = ls; rc = le; |
216 int n = 0; |
217 size_t n = 0; |
217 while (lc && lc != le && rc != re) { |
218 while (lc && lc != le && rc != re) { |
218 if (fnc(lc->data, rc->data, data) <= 0) { |
219 if (fnc(lc->data, rc->data, data) <= 0) { |
219 sorted[n] = lc; |
220 sorted[n] = lc; |
220 lc = lc->next; |
221 lc = lc->next; |
221 } else { |
222 } else { |
252 if (l == NULL) { |
253 if (l == NULL) { |
253 return NULL; |
254 return NULL; |
254 } |
255 } |
255 |
256 |
256 UcxList *lc; |
257 UcxList *lc; |
257 int ln = 1; |
258 size_t ln = 1; |
258 |
259 |
259 UcxList *restrict ls = l, *restrict le, *restrict re; |
260 UcxList *ls = l, *le, *re; |
260 |
261 |
261 // check how many elements are already sorted |
262 // check how many elements are already sorted |
262 lc = ls; |
263 lc = ls; |
263 while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) { |
264 while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) { |
264 lc = lc->next; |
265 lc = lc->next; |