src/ucx/list.c

Tue, 23 Aug 2016 15:55:02 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 15:55:02 +0200
changeset 46
534a4ef4143d
parent 39
ac35daceb24c
permissions
-rw-r--r--

refactors highlighter_t and removes abstraction overhead

39
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2015 Olaf Wintermann. All rights reserved.
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "list.h"
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 return ucx_list_clone_a(ucx_default_allocator(), l, fnc, data);
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 UcxList *ucx_list_clone_a(UcxAllocator *alloc, UcxList *l,
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 copy_func fnc, void *data) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 UcxList *ret = NULL;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 while (l) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 if (fnc) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 ret = ucx_list_append_a(alloc, ret, fnc(l->data, data));
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 } else {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 ret = ucx_list_append_a(alloc, ret, l->data);
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 l = l->next;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 return ret;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 int ucx_list_equals(const UcxList *l1, const UcxList *l2,
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 cmp_func fnc, void* data) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 if (l1 == l2) return 1;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 while (l1 != NULL && l2 != NULL) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 if (fnc == NULL) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 if (l1->data != l2->data) return 0;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 } else {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 if (fnc(l1->data, l2->data, data) != 0) return 0;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 l1 = l1->next;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 l2 = l2->next;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 return (l1 == NULL && l2 == NULL);
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 void ucx_list_free(UcxList *l) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 ucx_list_free_a(ucx_default_allocator(), l);
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 void ucx_list_free_a(UcxAllocator *alloc, UcxList *l) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 UcxList *e = l, *f;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 while (e != NULL) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 f = e;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 e = e->next;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 alfree(alloc, f);
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 void ucx_list_free_content(UcxList* list, ucx_destructor destr) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 while (list != NULL) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 destr(list->data);
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 list = list->next;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 UcxList *ucx_list_append(UcxList *l, void *data) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 return ucx_list_append_a(ucx_default_allocator(), l, data);
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 UcxList *ucx_list_append_a(UcxAllocator *alloc, UcxList *l, void *data) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 UcxList *nl = (UcxList*) almalloc(alloc, sizeof(UcxList));
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 if (!nl) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 return NULL;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 nl->data = data;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 nl->next = NULL;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 if (l) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 UcxList *t = ucx_list_last(l);
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 t->next = nl;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 nl->prev = t;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 return l;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 } else {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 nl->prev = NULL;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 return nl;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 UcxList *ucx_list_prepend(UcxList *l, void *data) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 return ucx_list_prepend_a(ucx_default_allocator(), l, data);
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 UcxList *ucx_list_prepend_a(UcxAllocator *alloc, UcxList *l, void *data) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 UcxList *nl = ucx_list_append_a(alloc, NULL, data);
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 if (!nl) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 return NULL;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 l = ucx_list_first(l);
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 if (l) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 nl->next = l;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 l->prev = nl;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 return nl;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 if (l1) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 UcxList *last = ucx_list_last(l1);
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 last->next = l2;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 if (l2) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 l2->prev = last;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 return l1;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 } else {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 return l2;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 UcxList *ucx_list_last(const UcxList *l) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 if (l == NULL) return NULL;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 const UcxList *e = l;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144 while (e->next != NULL) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 e = e->next;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 return (UcxList*)e;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 ssize_t index = 0;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 while (list) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 if (list == elem) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154 return index;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 list = list->next;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 index++;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159 return -1;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 UcxList *ucx_list_get(const UcxList *l, size_t index) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163 if (l == NULL) return NULL;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 const UcxList *e = l;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166 while (e->next && index > 0) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167 e = e->next;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168 index--;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
171 return (UcxList*)(index == 0 ? e : NULL);
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 ssize_t ucx_list_find(UcxList *l, void *elem, cmp_func fnc, void *cmpdata) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 ssize_t index = 0;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 UCX_FOREACH(e, l) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 if (fnc) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178 if (fnc(elem, e->data, cmpdata) == 0) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179 return index;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181 } else {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 if (elem == e->data) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183 return index;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
186 index++;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188 return -1;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 int ucx_list_contains(UcxList *l, void *elem, cmp_func fnc, void *cmpdata) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192 return ucx_list_find(l, elem, fnc, cmpdata) > -1;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195 size_t ucx_list_size(const UcxList *l) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 if (l == NULL) return 0;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 const UcxList *e = l;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 size_t s = 1;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200 while (e->next != NULL) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
201 e = e->next;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
202 s++;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
205 return s;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208 static UcxList *ucx_list_sort_merge(int length,
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
209 UcxList* restrict ls, UcxList* restrict le, UcxList* restrict re,
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
210 cmp_func fnc, void* data) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
211
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
212 UcxList** sorted = (UcxList**) malloc(sizeof(UcxList*)*length);
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
213 UcxList *rc, *lc;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
214
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
215 lc = ls; rc = le;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
216 int n = 0;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
217 while (lc && lc != le && rc != re) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
218 if (fnc(lc->data, rc->data, data) <= 0) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
219 sorted[n] = lc;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
220 lc = lc->next;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
221 } else {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
222 sorted[n] = rc;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
223 rc = rc->next;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
224 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
225 n++;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
226 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
227 while (lc && lc != le) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
228 sorted[n] = lc;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
229 lc = lc->next;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
230 n++;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
231 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
232 while (rc && rc != re) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
233 sorted[n] = rc;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
234 rc = rc->next;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
235 n++;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
236 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
237
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
238 // Update pointer
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
239 sorted[0]->prev = NULL;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
240 for (int i = 0 ; i < length-1 ; i++) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
241 sorted[i]->next = sorted[i+1];
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
242 sorted[i+1]->prev = sorted[i];
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
243 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
244 sorted[length-1]->next = NULL;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
245
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
246 UcxList *ret = sorted[0];
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
247 free(sorted);
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
248 return ret;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
249 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
250
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
251 UcxList *ucx_list_sort(UcxList *l, cmp_func fnc, void *data) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
252 if (l == NULL) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
253 return NULL;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
254 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
255
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
256 UcxList *lc;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
257 int ln = 1;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
258
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
259 UcxList *restrict ls = l, *restrict le, *restrict re;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
260
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
261 // check how many elements are already sorted
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
262 lc = ls;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
263 while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
264 lc = lc->next;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
265 ln++;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
266 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
267 le = lc->next;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
268
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
269 if (le == NULL) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
270 return l; // this list is already sorted :)
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
271 } else {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
272 UcxList *rc;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
273 int rn = 1;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
274 rc = le;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
275 // skip already sorted elements
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
276 while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
277 rc = rc->next;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
278 rn++;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
279 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
280 re = rc->next;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
281
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
282 // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
283 UcxList *sorted = ucx_list_sort_merge(ln+rn,
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
284 ls, le, re,
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
285 fnc, data);
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
286
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
287 // Something left? Sort it!
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
288 size_t remainder_length = ucx_list_size(re);
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
289 if (remainder_length > 0) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
290 UcxList *remainder = ucx_list_sort(re, fnc, data);
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
291
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
292 // merge sorted list with (also sorted) remainder
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
293 l = ucx_list_sort_merge(ln+rn+remainder_length,
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
294 sorted, remainder, NULL, fnc, data);
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
295 } else {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
296 // no remainder - we've got our sorted list
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
297 l = sorted;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
298 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
299
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
300 return l;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
301 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
302 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
303
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
304 UcxList *ucx_list_first(const UcxList *l) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
305 if (!l) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
306 return NULL;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
307 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
308
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
309 const UcxList *e = l;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
310 while (e->prev) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
311 e = e->prev;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
312 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
313 return (UcxList *)e;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
314 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
315
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
316 UcxList *ucx_list_remove(UcxList *l, UcxList *e) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
317 return ucx_list_remove_a(ucx_default_allocator(), l, e);
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
318 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
319
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
320 UcxList *ucx_list_remove_a(UcxAllocator *alloc, UcxList *l, UcxList *e) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
321 if (l == e) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
322 l = e->next;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
323 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
324
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
325 if (e->next) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
326 e->next->prev = e->prev;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
327 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
328
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
329 if (e->prev) {
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
330 e->prev->next = e->next;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
331 }
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
332
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
333 alfree(alloc, e);
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
334 return l;
ac35daceb24c adds UCX + changes how the input file is read (uses an consecutive memory area now)
Mike Becker <universe@uap-core.de>
parents:
diff changeset
335 }

mercurial