Fri, 12 Oct 2012 12:09:00 +0200
Merge with 6721482eaf8e8d35b1cd46f0a21285bfaa520d5a
1 #include "dlist.h"
3 UcxDlist *restrict ucx_dlist_clone(UcxDlist *restrict l,
4 copy_func fnc, void *data) {
5 UcxDlist *ret = NULL;
6 while (l != NULL) {
7 if (fnc != NULL) {
8 ret = ucx_dlist_append(ret, fnc(l->data, data));
9 } else {
10 ret = ucx_dlist_append(ret, l->data);
11 }
12 l = l->next;
13 }
14 return ret;
15 }
17 int ucx_dlist_equals(const UcxDlist *l1, const UcxDlist *l2,
18 cmp_func fnc, void* data) {
19 if (l1 == l2) return 1;
21 while (l1 != NULL && l2 != NULL) {
22 if (fnc == NULL) {
23 if (l1->data != l2->data) return 0;
24 } else {
25 if (fnc(l1->data, l2->data, data) != 0) return 0;
26 }
27 l1 = l1->next;
28 l2 = l2->next;
29 }
31 return (l1 == NULL && l2 == NULL);
32 }
34 void ucx_dlist_free(UcxDlist *l) {
35 UcxDlist *e = l, *f;
36 while (e != NULL) {
37 f = e;
38 e = e->next;
39 free(f);
40 }
41 }
43 UcxDlist *ucx_dlist_append(UcxDlist *l, void *data) {
44 UcxDlist *nl = (UcxDlist*) malloc(sizeof(UcxDlist));
45 if (nl == NULL) return NULL;
47 nl->data = data;
48 nl->next = NULL;
49 if (l == NULL) {
50 nl->prev = NULL;
51 return nl;
52 } else {
53 UcxDlist *t = ucx_dlist_last(l);
54 t->next = nl;
55 nl->prev = t;
56 return l;
57 }
58 }
60 UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data) {
61 UcxDlist *nl = ucx_dlist_append(NULL, data);
62 if (nl == NULL) return NULL;
64 if (l != NULL) {
65 nl->next = l;
66 l->prev = nl;
67 }
68 return nl;
69 }
71 UcxDlist *ucx_dlist_concat(UcxDlist *restrict l1, UcxDlist *restrict l2) {
72 if (l1 == NULL) {
73 return l2;
74 } else {
75 UcxDlist *last = ucx_dlist_last(l1);
76 last->next = l2;
77 l2->prev = last;
78 return l1;
79 }
80 }
82 UcxDlist *ucx_dlist_last(const UcxDlist *l) {
83 if (l == NULL) return NULL;
85 const UcxDlist *e = l;
86 while (e->next != NULL) {
87 e = e->next;
88 }
89 return (UcxDlist*)e;
90 }
92 UcxDlist *ucx_dlist_get(const UcxDlist *l, int index) {
93 if (l == NULL) return NULL;
95 const UcxDlist *e = l;
96 while (e->next != NULL && index > 0) {
97 e = e->next;
98 index--;
99 }
101 return (UcxDlist*)(index == 0 ? e : NULL);
102 }
104 size_t ucx_dlist_size(const UcxDlist *l) {
105 if (l == NULL) return 0;
107 const UcxDlist *e = l;
108 size_t s = 1;
109 while (e->next != NULL) {
110 e = e->next;
111 s++;
112 }
114 return s;
115 }
117 UcxDlist *ucx_dlist_sort_merge(int length,
118 UcxDlist* restrict ls, UcxDlist* restrict le, UcxDlist* restrict re,
119 cmp_func fnc, void* data) {
120 ucx_dynarray_new(UcxDlist*, sorted, length);
121 UcxDlist *rc, *lc;
123 lc = ls; rc = le;
124 int n = 0;
125 while (lc && lc != le && rc != re) {
126 if (fnc(lc->data, rc->data, data) <= 0) {
127 sorted[n] = lc;
128 lc = lc->next;
129 } else {
130 sorted[n] = rc;
131 rc = rc->next;
132 }
133 n++;
134 }
135 while (lc && lc != le) {
136 sorted[n] = lc;
137 lc = lc->next;
138 n++;
139 }
140 while (rc && rc != re) {
141 sorted[n] = rc;
142 rc = rc->next;
143 n++;
144 }
146 // Update pointer
147 sorted[0]->prev = NULL;
148 for (int i = 0 ; i < length-1 ; i++) {
149 sorted[i]->next = sorted[i+1];
150 sorted[i+1]->prev = sorted[i];
151 }
152 sorted[length-1]->next = NULL;
154 UcxDlist *ret = sorted[0];
155 ucx_dynarray_free(sorted);
156 return ret;
157 }
159 UcxDlist *ucx_dlist_sort(UcxDlist *l, cmp_func fnc, void *data) {
160 if (l == NULL) {
161 return NULL;
162 }
164 UcxDlist *lc;
165 int ln = 1;
167 UcxDlist *restrict ls = l, *restrict le, *restrict re;
168 lc = ls;
169 while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) {
170 lc = lc->next;
171 ln++;
172 }
173 le = lc->next;
175 if (le == NULL) {
176 return l; // this list is already sorted :)
177 } else {
178 UcxDlist *rc;
179 int rn = 1;
180 rc = le;
181 while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) {
182 rc = rc->next;
183 rn++;
184 }
185 re = rc->next;
187 // Something left? Sort it!
188 UcxDlist *remainder = re;
189 size_t remainder_length = ucx_dlist_size(remainder);
190 if (remainder != NULL) {
191 remainder = ucx_dlist_sort(remainder, fnc, data);
192 }
194 // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
195 UcxDlist *sorted = ucx_dlist_sort_merge(ln+rn,
196 ls, le, re,
197 fnc, data);
199 // merge sorted list with (also sorted) remainder
200 l = ucx_dlist_sort_merge(ln+rn+remainder_length,
201 sorted, remainder, NULL, fnc, data);
203 return l;
204 }
205 }
207 /* dlist specific functions */
208 UcxDlist *ucx_dlist_first(const UcxDlist *l) {
209 if (l == NULL) return NULL;
211 const UcxDlist *e = l;
212 while (e->prev != NULL) {
213 e = e->prev;
214 }
215 return (UcxDlist *)e;
216 }
218 UcxDlist *ucx_dlist_remove(UcxDlist *l, UcxDlist *e) {
219 if (e->prev == NULL) {
220 if(e->next != NULL) {
221 e->next->prev = NULL;
222 l = e->next;
223 } else {
224 l = NULL;
225 }
227 } else {
228 e->prev->next = e->next;
229 e->next->prev = e->prev;
230 }
231 free(e);
232 return l;
233 }