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