Thu, 11 Oct 2012 11:42:31 +0200
added some qualifiers + removed pointer alias in mergesort
universe@10 | 1 | #include "list.h" |
olaf@2 | 2 | |
universe@67 | 3 | UcxList *restrict ucx_list_clone(UcxList *restrict l, |
universe@67 | 4 | copy_func fnc, void *data) { |
universe@18 | 5 | UcxList *ret = NULL; |
universe@18 | 6 | while (l != NULL) { |
universe@18 | 7 | if (fnc != NULL) { |
universe@18 | 8 | ret = ucx_list_append(ret, fnc(l->data, data)); |
universe@18 | 9 | } else { |
universe@18 | 10 | ret = ucx_list_append(ret, l->data); |
universe@18 | 11 | } |
universe@18 | 12 | l = l->next; |
universe@18 | 13 | } |
universe@18 | 14 | return ret; |
universe@18 | 15 | } |
universe@18 | 16 | |
universe@67 | 17 | int ucx_list_equals(const UcxList *l1, const UcxList *l2, |
universe@67 | 18 | cmp_func fnc, void* data) { |
universe@18 | 19 | if (l1 == l2) return 1; |
universe@18 | 20 | |
universe@18 | 21 | while (l1 != NULL && l2 != NULL) { |
universe@18 | 22 | if (fnc == NULL) { |
universe@18 | 23 | if (l1->data != l2->data) return 0; |
universe@18 | 24 | } else { |
universe@18 | 25 | if (fnc(l1->data, l2->data, data) != 0) return 0; |
universe@18 | 26 | } |
universe@18 | 27 | l1 = l1->next; |
universe@18 | 28 | l2 = l2->next; |
universe@18 | 29 | } |
universe@18 | 30 | |
universe@18 | 31 | return (l1 == NULL && l2 == NULL); |
universe@18 | 32 | } |
universe@18 | 33 | |
universe@10 | 34 | void ucx_list_free(UcxList *l) { |
universe@10 | 35 | UcxList *e = l, *f; |
universe@10 | 36 | while (e != NULL) { |
universe@10 | 37 | f = e; |
universe@10 | 38 | e = e->next; |
universe@10 | 39 | free(f); |
universe@10 | 40 | } |
universe@10 | 41 | } |
universe@10 | 42 | |
universe@10 | 43 | UcxList *ucx_list_append(UcxList *l, void *data) { |
universe@10 | 44 | UcxList *nl = (UcxList*) malloc(sizeof(UcxList)); |
universe@10 | 45 | if (nl == NULL) return NULL; |
universe@10 | 46 | |
universe@10 | 47 | nl->data = data; |
universe@10 | 48 | nl->next = NULL; |
universe@10 | 49 | if (l == NULL) { |
universe@10 | 50 | return nl; |
universe@10 | 51 | } else { |
universe@10 | 52 | UcxList *t = ucx_list_last(l); |
universe@10 | 53 | t->next = nl; |
universe@10 | 54 | return l; |
universe@10 | 55 | } |
universe@10 | 56 | } |
universe@10 | 57 | |
universe@10 | 58 | UcxList *ucx_list_prepend(UcxList *l, void *data) { |
universe@10 | 59 | UcxList *nl = ucx_list_append(NULL, data); |
universe@10 | 60 | if (nl == NULL) return NULL; |
universe@10 | 61 | |
universe@10 | 62 | if (l != NULL) { |
universe@10 | 63 | nl->next = l; |
universe@10 | 64 | } |
universe@10 | 65 | return nl; |
universe@10 | 66 | } |
universe@10 | 67 | |
universe@67 | 68 | UcxList *ucx_list_concat(UcxList *restrict l1, UcxList *restrict l2) { |
universe@10 | 69 | if (l1 == NULL) { |
universe@10 | 70 | return l2; |
universe@10 | 71 | } else { |
universe@10 | 72 | UcxList *last = ucx_list_last(l1); |
universe@10 | 73 | last->next = l2; |
universe@10 | 74 | return l1; |
universe@10 | 75 | } |
universe@10 | 76 | } |
universe@10 | 77 | |
universe@67 | 78 | UcxList *ucx_list_last(const UcxList *l) { |
universe@10 | 79 | if (l == NULL) return NULL; |
universe@10 | 80 | |
universe@67 | 81 | const UcxList *e = l; |
universe@10 | 82 | while (e->next != NULL) { |
universe@10 | 83 | e = e->next; |
universe@10 | 84 | } |
universe@67 | 85 | return (UcxList*)e; |
universe@10 | 86 | } |
universe@10 | 87 | |
universe@67 | 88 | UcxList *ucx_list_get(const UcxList *l, int index) { |
universe@10 | 89 | if (l == NULL) return NULL; |
universe@10 | 90 | |
universe@67 | 91 | const UcxList *e = l; |
universe@10 | 92 | while (e->next != NULL && index > 0) { |
universe@10 | 93 | e = e->next; |
universe@10 | 94 | index--; |
universe@10 | 95 | } |
universe@10 | 96 | |
universe@67 | 97 | return (UcxList*)(index == 0 ? e : NULL); |
universe@10 | 98 | } |
universe@10 | 99 | |
universe@67 | 100 | size_t ucx_list_size(const UcxList *l) { |
universe@10 | 101 | if (l == NULL) return 0; |
universe@10 | 102 | |
universe@67 | 103 | const UcxList *e = l; |
universe@10 | 104 | size_t s = 1; |
universe@10 | 105 | while (e->next != NULL) { |
universe@10 | 106 | e = e->next; |
universe@10 | 107 | s++; |
universe@10 | 108 | } |
universe@10 | 109 | |
universe@10 | 110 | return s; |
universe@10 | 111 | } |
universe@10 | 112 | |
universe@37 | 113 | UcxList *ucx_list_sort_merge(int length, |
universe@67 | 114 | UcxList* restrict ls, UcxList* restrict le, UcxList* restrict re, |
universe@37 | 115 | cmp_func fnc, void* data) { |
universe@37 | 116 | UcxList *sorted[length]; |
universe@37 | 117 | UcxList *rc, *lc; |
universe@35 | 118 | |
universe@67 | 119 | lc = ls; rc = le; |
universe@37 | 120 | int n = 0; |
universe@67 | 121 | while (lc && lc != le && rc != re) { |
universe@37 | 122 | if (fnc(lc->data, rc->data, data) <= 0) { |
universe@37 | 123 | sorted[n] = lc; |
universe@37 | 124 | lc = lc->next; |
universe@37 | 125 | } else { |
universe@37 | 126 | sorted[n] = rc; |
universe@37 | 127 | rc = rc->next; |
universe@35 | 128 | } |
universe@37 | 129 | n++; |
universe@37 | 130 | } |
universe@67 | 131 | while (lc && lc != le) { |
universe@37 | 132 | sorted[n] = lc; |
universe@37 | 133 | lc = lc->next; |
universe@37 | 134 | n++; |
universe@37 | 135 | } |
universe@67 | 136 | while (rc && rc != re) { |
universe@37 | 137 | sorted[n] = rc; |
universe@37 | 138 | rc = rc->next; |
universe@37 | 139 | n++; |
universe@35 | 140 | } |
universe@35 | 141 | |
universe@37 | 142 | // Update pointer |
universe@37 | 143 | for (int i = 0 ; i < length-1 ; i++) { |
universe@37 | 144 | sorted[i]->next = sorted[i+1]; |
universe@37 | 145 | } |
universe@37 | 146 | sorted[length-1]->next = NULL; |
universe@35 | 147 | |
universe@37 | 148 | return sorted[0]; |
universe@35 | 149 | } |
universe@35 | 150 | |
universe@36 | 151 | UcxList *ucx_list_sort(UcxList *l, cmp_func fnc, void *data) { |
universe@35 | 152 | if (l == NULL) { |
universe@35 | 153 | return NULL; |
universe@35 | 154 | } |
universe@37 | 155 | |
universe@37 | 156 | UcxList *lc; |
universe@37 | 157 | int ln = 1; |
universe@37 | 158 | |
universe@67 | 159 | UcxList *restrict ls = l, *restrict le, *restrict re; |
universe@37 | 160 | lc = ls; |
universe@37 | 161 | while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) { |
universe@37 | 162 | lc = lc->next; |
universe@37 | 163 | ln++; |
universe@37 | 164 | } |
universe@37 | 165 | le = lc->next; |
universe@37 | 166 | |
universe@67 | 167 | if (le == NULL) { |
universe@37 | 168 | return l; // this list is already sorted :) |
universe@37 | 169 | } else { |
universe@37 | 170 | UcxList *rc; |
universe@37 | 171 | int rn = 1; |
universe@67 | 172 | rc = le; |
universe@37 | 173 | while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) { |
universe@37 | 174 | rc = rc->next; |
universe@37 | 175 | rn++; |
universe@37 | 176 | } |
universe@37 | 177 | re = rc->next; |
universe@37 | 178 | |
universe@37 | 179 | // Something left? Sort it! |
universe@37 | 180 | UcxList *remainder = re; |
universe@37 | 181 | size_t remainder_length = ucx_list_size(remainder); |
universe@37 | 182 | if (remainder != NULL) { |
universe@37 | 183 | remainder = ucx_list_sort(remainder, fnc, data); |
universe@37 | 184 | } |
universe@37 | 185 | |
universe@37 | 186 | // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them |
universe@37 | 187 | UcxList *sorted = ucx_list_sort_merge(ln+rn, |
universe@67 | 188 | ls, le, re, |
universe@37 | 189 | fnc, data); |
universe@37 | 190 | |
universe@37 | 191 | // merge sorted list with (also sorted) remainder |
universe@37 | 192 | l = ucx_list_sort_merge(ln+rn+remainder_length, |
universe@67 | 193 | sorted, remainder, NULL, fnc, data); |
universe@37 | 194 | |
universe@35 | 195 | return l; |
universe@35 | 196 | } |
universe@35 | 197 | } |
universe@35 | 198 | |
universe@23 | 199 | /* list specific functions */ |
universe@23 | 200 | UcxList *ucx_list_remove(UcxList *l, UcxList *e) { |
universe@23 | 201 | if (e == l) { |
universe@23 | 202 | l = e->next; |
universe@23 | 203 | free(e); |
universe@23 | 204 | } else { |
universe@23 | 205 | UcxList *f = l; |
universe@23 | 206 | while (f->next != NULL && f->next != e) { |
universe@23 | 207 | f = f->next; |
universe@23 | 208 | } |
olaf@31 | 209 | /* perform remove if this element is found in this list */ |
universe@23 | 210 | if (f->next == e) { |
universe@23 | 211 | f->next = e->next; |
universe@23 | 212 | free(e); |
universe@23 | 213 | } |
universe@23 | 214 | } |
universe@23 | 215 | return l; |
universe@24 | 216 | } |