ucx/dlist.c

changeset 122
540d99722f1f
parent 103
08018864fb91
equal deleted inserted replaced
121:311cac04d079 122:540d99722f1f
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
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
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "dlist.h"
30
31 UcxDlist *ucx_dlist_clone(UcxDlist *l, copy_func fnc, void *data) {
32 UcxDlist *ret = NULL;
33 while (l != NULL) {
34 if (fnc != NULL) {
35 ret = ucx_dlist_append(ret, fnc(l->data, data));
36 } else {
37 ret = ucx_dlist_append(ret, l->data);
38 }
39 l = l->next;
40 }
41 return ret;
42 }
43
44 int ucx_dlist_equals(const UcxDlist *l1, const UcxDlist *l2,
45 cmp_func fnc, void* data) {
46 if (l1 == l2) return 1;
47
48 while (l1 != NULL && l2 != NULL) {
49 if (fnc == NULL) {
50 if (l1->data != l2->data) return 0;
51 } else {
52 if (fnc(l1->data, l2->data, data) != 0) return 0;
53 }
54 l1 = l1->next;
55 l2 = l2->next;
56 }
57
58 return (l1 == NULL && l2 == NULL);
59 }
60
61 void ucx_dlist_free(UcxDlist *l) {
62 UcxDlist *e = l, *f;
63 while (e != NULL) {
64 f = e;
65 e = e->next;
66 free(f);
67 }
68 }
69
70 UcxDlist *ucx_dlist_append(UcxDlist *l, void *data) {
71 UcxDlist *nl = (UcxDlist*) malloc(sizeof(UcxDlist));
72 if (nl == NULL) return NULL;
73
74 nl->data = data;
75 nl->next = NULL;
76 if (l == NULL) {
77 nl->prev = NULL;
78 return nl;
79 } else {
80 UcxDlist *t = ucx_dlist_last(l);
81 t->next = nl;
82 nl->prev = t;
83 return l;
84 }
85 }
86
87 UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data) {
88 UcxDlist *nl = ucx_dlist_append(NULL, data);
89 if (nl == NULL) return NULL;
90
91 if (l != NULL) {
92 nl->next = l;
93 l->prev = nl;
94 }
95 return nl;
96 }
97
98 UcxDlist *ucx_dlist_concat(UcxDlist *l1, UcxDlist *l2) {
99 if (l1 == NULL) {
100 return l2;
101 } else {
102 UcxDlist *last = ucx_dlist_last(l1);
103 last->next = l2;
104 l2->prev = last;
105 return l1;
106 }
107 }
108
109 UcxDlist *ucx_dlist_last(const UcxDlist *l) {
110 if (l == NULL) return NULL;
111
112 const UcxDlist *e = l;
113 while (e->next != NULL) {
114 e = e->next;
115 }
116 return (UcxDlist*)e;
117 }
118
119 UcxDlist *ucx_dlist_get(const UcxDlist *l, int index) {
120 if (l == NULL) return NULL;
121
122 const UcxDlist *e = l;
123 while (e->next != NULL && index > 0) {
124 e = e->next;
125 index--;
126 }
127
128 return (UcxDlist*)(index == 0 ? e : NULL);
129 }
130
131 int ucx_dlist_contains(UcxDlist *l, void *elem, cmp_func fnc, void *cmpdata) {
132 UCX_FOREACH(l, e) {
133 if (!fnc(elem, e->data, cmpdata)) {
134 return 1;
135 }
136 }
137 return 0;
138 }
139
140 size_t ucx_dlist_size(const UcxDlist *l) {
141 if (l == NULL) return 0;
142
143 const UcxDlist *e = l;
144 size_t s = 1;
145 while (e->next != NULL) {
146 e = e->next;
147 s++;
148 }
149
150 return s;
151 }
152
153 UcxDlist *ucx_dlist_sort_merge(int length,
154 UcxDlist* restrict ls, UcxDlist* restrict le, UcxDlist* restrict re,
155 cmp_func fnc, void* data) {
156
157 UcxDlist** sorted = (UcxDlist**) malloc(sizeof(UcxDlist*)*length);
158 UcxDlist *rc, *lc;
159
160 lc = ls; rc = le;
161 int n = 0;
162 while (lc && lc != le && rc != re) {
163 if (fnc(lc->data, rc->data, data) <= 0) {
164 sorted[n] = lc;
165 lc = lc->next;
166 } else {
167 sorted[n] = rc;
168 rc = rc->next;
169 }
170 n++;
171 }
172 while (lc && lc != le) {
173 sorted[n] = lc;
174 lc = lc->next;
175 n++;
176 }
177 while (rc && rc != re) {
178 sorted[n] = rc;
179 rc = rc->next;
180 n++;
181 }
182
183 // Update pointer
184 sorted[0]->prev = NULL;
185 for (int i = 0 ; i < length-1 ; i++) {
186 sorted[i]->next = sorted[i+1];
187 sorted[i+1]->prev = sorted[i];
188 }
189 sorted[length-1]->next = NULL;
190
191 UcxDlist *ret = sorted[0];
192 free(sorted);
193 return ret;
194 }
195
196 UcxDlist *ucx_dlist_sort(UcxDlist *l, cmp_func fnc, void *data) {
197 if (l == NULL) {
198 return NULL;
199 }
200
201 UcxDlist *lc;
202 int ln = 1;
203
204 UcxDlist *restrict ls = l, *restrict le, *restrict re;
205 lc = ls;
206 while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) {
207 lc = lc->next;
208 ln++;
209 }
210 le = lc->next;
211
212 if (le == NULL) {
213 return l; // this list is already sorted :)
214 } else {
215 UcxDlist *rc;
216 int rn = 1;
217 rc = le;
218 while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) {
219 rc = rc->next;
220 rn++;
221 }
222 re = rc->next;
223
224 // Something left? Sort it!
225 UcxDlist *remainder = re;
226 size_t remainder_length = ucx_dlist_size(remainder);
227 if (remainder != NULL) {
228 remainder = ucx_dlist_sort(remainder, fnc, data);
229 }
230
231 // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
232 UcxDlist *sorted = ucx_dlist_sort_merge(ln+rn,
233 ls, le, re,
234 fnc, data);
235
236 // merge sorted list with (also sorted) remainder
237 l = ucx_dlist_sort_merge(ln+rn+remainder_length,
238 sorted, remainder, NULL, fnc, data);
239
240 return l;
241 }
242 }
243
244 /* dlist specific functions */
245 UcxDlist *ucx_dlist_first(const UcxDlist *l) {
246 if (l == NULL) return NULL;
247
248 const UcxDlist *e = l;
249 while (e->prev != NULL) {
250 e = e->prev;
251 }
252 return (UcxDlist *)e;
253 }
254
255 UcxDlist *ucx_dlist_remove(UcxDlist *l, UcxDlist *e) {
256 if (e->prev == NULL) {
257 if(e->next != NULL) {
258 e->next->prev = NULL;
259 l = e->next;
260 } else {
261 l = NULL;
262 }
263
264 } else {
265 e->prev->next = e->next;
266 e->next->prev = e->prev;
267 }
268 free(e);
269 return l;
270 }

mercurial