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