Mon, 21 Jul 2014 13:18:32 +0200
added some string tests
universe@103 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@103 | 3 | * |
universe@177 | 4 | * Copyright 2014 Olaf Wintermann. All rights reserved. |
universe@103 | 5 | * |
universe@103 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@103 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@103 | 8 | * |
universe@103 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@103 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@103 | 11 | * |
universe@103 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@103 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@103 | 14 | * documentation and/or other materials provided with the distribution. |
universe@103 | 15 | * |
universe@103 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@103 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@103 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@103 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@103 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@103 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@103 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@103 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@103 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@103 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@103 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@103 | 27 | */ |
universe@103 | 28 | |
universe@122 | 29 | #include "list.h" |
universe@4 | 30 | |
universe@122 | 31 | UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data) { |
universe@125 | 32 | return ucx_list_clone_a(ucx_default_allocator(), l, fnc, data); |
universe@125 | 33 | } |
universe@125 | 34 | |
universe@125 | 35 | UcxList *ucx_list_clone_a(UcxAllocator *alloc, UcxList *l, |
universe@125 | 36 | copy_func fnc, void *data) { |
universe@122 | 37 | UcxList *ret = NULL; |
universe@125 | 38 | while (l) { |
universe@125 | 39 | if (fnc) { |
universe@125 | 40 | ret = ucx_list_append_a(alloc, ret, fnc(l->data, data)); |
universe@18 | 41 | } else { |
universe@125 | 42 | ret = ucx_list_append_a(alloc, ret, l->data); |
universe@18 | 43 | } |
universe@18 | 44 | l = l->next; |
universe@18 | 45 | } |
universe@18 | 46 | return ret; |
universe@18 | 47 | } |
universe@18 | 48 | |
universe@122 | 49 | int ucx_list_equals(const UcxList *l1, const UcxList *l2, |
universe@67 | 50 | cmp_func fnc, void* data) { |
universe@18 | 51 | if (l1 == l2) return 1; |
universe@18 | 52 | |
universe@18 | 53 | while (l1 != NULL && l2 != NULL) { |
universe@18 | 54 | if (fnc == NULL) { |
universe@18 | 55 | if (l1->data != l2->data) return 0; |
universe@18 | 56 | } else { |
universe@18 | 57 | if (fnc(l1->data, l2->data, data) != 0) return 0; |
universe@18 | 58 | } |
universe@18 | 59 | l1 = l1->next; |
universe@18 | 60 | l2 = l2->next; |
universe@18 | 61 | } |
universe@18 | 62 | |
universe@18 | 63 | return (l1 == NULL && l2 == NULL); |
universe@18 | 64 | } |
universe@18 | 65 | |
universe@122 | 66 | void ucx_list_free(UcxList *l) { |
universe@125 | 67 | ucx_list_free_a(ucx_default_allocator(), l); |
universe@125 | 68 | } |
universe@125 | 69 | |
universe@125 | 70 | void ucx_list_free_a(UcxAllocator *alloc, UcxList *l) { |
universe@122 | 71 | UcxList *e = l, *f; |
universe@8 | 72 | while (e != NULL) { |
universe@8 | 73 | f = e; |
universe@8 | 74 | e = e->next; |
universe@173 | 75 | alfree(alloc, f); |
universe@8 | 76 | } |
universe@8 | 77 | } |
universe@8 | 78 | |
universe@122 | 79 | UcxList *ucx_list_append(UcxList *l, void *data) { |
universe@125 | 80 | return ucx_list_append_a(ucx_default_allocator(), l, data); |
universe@125 | 81 | } |
universe@125 | 82 | |
universe@125 | 83 | UcxList *ucx_list_append_a(UcxAllocator *alloc, UcxList *l, void *data) { |
universe@173 | 84 | UcxList *nl = (UcxList*) almalloc(alloc, sizeof(UcxList)); |
universe@125 | 85 | if (!nl) { |
universe@125 | 86 | return NULL; |
universe@125 | 87 | } |
universe@7 | 88 | |
universe@8 | 89 | nl->data = data; |
universe@8 | 90 | nl->next = NULL; |
universe@125 | 91 | if (l) { |
universe@122 | 92 | UcxList *t = ucx_list_last(l); |
universe@8 | 93 | t->next = nl; |
universe@8 | 94 | nl->prev = t; |
universe@8 | 95 | return l; |
universe@125 | 96 | } else { |
universe@125 | 97 | nl->prev = NULL; |
universe@125 | 98 | return nl; |
universe@8 | 99 | } |
universe@7 | 100 | } |
universe@7 | 101 | |
universe@122 | 102 | UcxList *ucx_list_prepend(UcxList *l, void *data) { |
universe@125 | 103 | return ucx_list_prepend_a(ucx_default_allocator(), l, data); |
universe@125 | 104 | } |
universe@125 | 105 | |
universe@125 | 106 | UcxList *ucx_list_prepend_a(UcxAllocator *alloc, UcxList *l, void *data) { |
universe@125 | 107 | UcxList *nl = ucx_list_append_a(alloc, NULL, data); |
universe@125 | 108 | if (!nl) { |
universe@125 | 109 | return NULL; |
universe@125 | 110 | } |
universe@125 | 111 | l = ucx_list_first(l); |
universe@7 | 112 | |
universe@125 | 113 | if (l) { |
universe@8 | 114 | nl->next = l; |
universe@8 | 115 | l->prev = nl; |
universe@8 | 116 | } |
universe@8 | 117 | return nl; |
universe@7 | 118 | } |
universe@7 | 119 | |
universe@122 | 120 | UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) { |
universe@128 | 121 | if (l1) { |
universe@122 | 122 | UcxList *last = ucx_list_last(l1); |
universe@8 | 123 | last->next = l2; |
universe@172 | 124 | if (l2) { |
universe@172 | 125 | l2->prev = last; |
universe@172 | 126 | } |
universe@8 | 127 | return l1; |
universe@128 | 128 | } else { |
universe@128 | 129 | return l2; |
universe@8 | 130 | } |
universe@7 | 131 | } |
universe@7 | 132 | |
universe@122 | 133 | UcxList *ucx_list_last(const UcxList *l) { |
universe@7 | 134 | if (l == NULL) return NULL; |
universe@7 | 135 | |
universe@122 | 136 | const UcxList *e = l; |
universe@7 | 137 | while (e->next != NULL) { |
universe@7 | 138 | e = e->next; |
universe@7 | 139 | } |
universe@122 | 140 | return (UcxList*)e; |
universe@7 | 141 | } |
universe@7 | 142 | |
universe@123 | 143 | ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem) { |
universe@123 | 144 | ssize_t index = 0; |
universe@123 | 145 | while (list) { |
universe@123 | 146 | if (list == elem) { |
universe@123 | 147 | return index; |
universe@123 | 148 | } |
universe@123 | 149 | list = list->next; |
universe@123 | 150 | index++; |
universe@123 | 151 | } |
universe@123 | 152 | return -1; |
universe@123 | 153 | } |
universe@123 | 154 | |
universe@172 | 155 | UcxList *ucx_list_get(const UcxList *l, size_t index) { |
universe@8 | 156 | if (l == NULL) return NULL; |
universe@8 | 157 | |
universe@122 | 158 | const UcxList *e = l; |
universe@128 | 159 | while (e->next && index > 0) { |
universe@8 | 160 | e = e->next; |
universe@8 | 161 | index--; |
universe@8 | 162 | } |
universe@7 | 163 | |
universe@122 | 164 | return (UcxList*)(index == 0 ? e : NULL); |
universe@7 | 165 | } |
universe@7 | 166 | |
universe@123 | 167 | ssize_t ucx_list_find(UcxList *l, void *elem, cmp_func fnc, void *cmpdata) { |
universe@123 | 168 | ssize_t index = 0; |
universe@123 | 169 | UCX_FOREACH(e, l) { |
universe@128 | 170 | if (fnc) { |
universe@128 | 171 | if (fnc(elem, e->data, cmpdata) == 0) { |
universe@128 | 172 | return index; |
universe@128 | 173 | } |
universe@128 | 174 | } else { |
universe@128 | 175 | if (elem == e->data) { |
universe@128 | 176 | return index; |
universe@128 | 177 | } |
universe@123 | 178 | } |
universe@123 | 179 | index++; |
universe@123 | 180 | } |
universe@123 | 181 | return -1; |
universe@123 | 182 | } |
universe@123 | 183 | |
universe@122 | 184 | int ucx_list_contains(UcxList *l, void *elem, cmp_func fnc, void *cmpdata) { |
universe@123 | 185 | return ucx_list_find(l, elem, fnc, cmpdata) > -1; |
universe@87 | 186 | } |
universe@87 | 187 | |
universe@122 | 188 | size_t ucx_list_size(const UcxList *l) { |
universe@7 | 189 | if (l == NULL) return 0; |
universe@7 | 190 | |
universe@122 | 191 | const UcxList *e = l; |
universe@7 | 192 | size_t s = 1; |
universe@7 | 193 | while (e->next != NULL) { |
universe@7 | 194 | e = e->next; |
universe@7 | 195 | s++; |
universe@7 | 196 | } |
universe@7 | 197 | |
universe@7 | 198 | return s; |
universe@7 | 199 | } |
universe@7 | 200 | |
universe@172 | 201 | static UcxList *ucx_list_sort_merge(int length, |
universe@122 | 202 | UcxList* restrict ls, UcxList* restrict le, UcxList* restrict re, |
universe@37 | 203 | cmp_func fnc, void* data) { |
universe@73 | 204 | |
universe@122 | 205 | UcxList** sorted = (UcxList**) malloc(sizeof(UcxList*)*length); |
universe@122 | 206 | UcxList *rc, *lc; |
universe@35 | 207 | |
universe@67 | 208 | lc = ls; rc = le; |
universe@37 | 209 | int n = 0; |
universe@67 | 210 | while (lc && lc != le && rc != re) { |
universe@37 | 211 | if (fnc(lc->data, rc->data, data) <= 0) { |
universe@37 | 212 | sorted[n] = lc; |
universe@37 | 213 | lc = lc->next; |
universe@37 | 214 | } else { |
universe@37 | 215 | sorted[n] = rc; |
universe@37 | 216 | rc = rc->next; |
universe@35 | 217 | } |
universe@37 | 218 | n++; |
universe@37 | 219 | } |
universe@67 | 220 | while (lc && lc != le) { |
universe@37 | 221 | sorted[n] = lc; |
universe@37 | 222 | lc = lc->next; |
universe@37 | 223 | n++; |
universe@37 | 224 | } |
universe@67 | 225 | while (rc && rc != re) { |
universe@37 | 226 | sorted[n] = rc; |
universe@37 | 227 | rc = rc->next; |
universe@37 | 228 | n++; |
universe@35 | 229 | } |
universe@35 | 230 | |
universe@37 | 231 | // Update pointer |
universe@37 | 232 | sorted[0]->prev = NULL; |
universe@37 | 233 | for (int i = 0 ; i < length-1 ; i++) { |
universe@37 | 234 | sorted[i]->next = sorted[i+1]; |
universe@37 | 235 | sorted[i+1]->prev = sorted[i]; |
universe@37 | 236 | } |
universe@37 | 237 | sorted[length-1]->next = NULL; |
universe@35 | 238 | |
universe@122 | 239 | UcxList *ret = sorted[0]; |
universe@73 | 240 | free(sorted); |
universe@69 | 241 | return ret; |
universe@35 | 242 | } |
universe@35 | 243 | |
universe@122 | 244 | UcxList *ucx_list_sort(UcxList *l, cmp_func fnc, void *data) { |
universe@35 | 245 | if (l == NULL) { |
universe@35 | 246 | return NULL; |
universe@35 | 247 | } |
universe@37 | 248 | |
universe@122 | 249 | UcxList *lc; |
universe@37 | 250 | int ln = 1; |
universe@37 | 251 | |
universe@122 | 252 | UcxList *restrict ls = l, *restrict le, *restrict re; |
universe@172 | 253 | |
universe@172 | 254 | // check how many elements are already sorted |
universe@37 | 255 | lc = ls; |
universe@37 | 256 | while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) { |
universe@37 | 257 | lc = lc->next; |
universe@37 | 258 | ln++; |
universe@37 | 259 | } |
universe@37 | 260 | le = lc->next; |
universe@37 | 261 | |
universe@67 | 262 | if (le == NULL) { |
universe@37 | 263 | return l; // this list is already sorted :) |
universe@37 | 264 | } else { |
universe@122 | 265 | UcxList *rc; |
universe@37 | 266 | int rn = 1; |
universe@67 | 267 | rc = le; |
universe@172 | 268 | // skip already sorted elements |
universe@37 | 269 | while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) { |
universe@37 | 270 | rc = rc->next; |
universe@37 | 271 | rn++; |
universe@37 | 272 | } |
universe@37 | 273 | re = rc->next; |
universe@37 | 274 | |
universe@37 | 275 | // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them |
universe@122 | 276 | UcxList *sorted = ucx_list_sort_merge(ln+rn, |
universe@67 | 277 | ls, le, re, |
universe@37 | 278 | fnc, data); |
universe@172 | 279 | |
universe@172 | 280 | // Something left? Sort it! |
universe@172 | 281 | size_t remainder_length = ucx_list_size(re); |
universe@172 | 282 | if (remainder_length > 0) { |
universe@172 | 283 | UcxList *remainder = ucx_list_sort(re, fnc, data); |
universe@37 | 284 | |
universe@172 | 285 | // merge sorted list with (also sorted) remainder |
universe@172 | 286 | l = ucx_list_sort_merge(ln+rn+remainder_length, |
universe@172 | 287 | sorted, remainder, NULL, fnc, data); |
universe@172 | 288 | } else { |
universe@172 | 289 | // no remainder - we've got our sorted list |
universe@172 | 290 | l = sorted; |
universe@172 | 291 | } |
universe@37 | 292 | |
universe@35 | 293 | return l; |
universe@35 | 294 | } |
universe@35 | 295 | } |
universe@35 | 296 | |
universe@122 | 297 | UcxList *ucx_list_first(const UcxList *l) { |
universe@125 | 298 | if (!l) { |
universe@125 | 299 | return NULL; |
universe@125 | 300 | } |
universe@7 | 301 | |
universe@122 | 302 | const UcxList *e = l; |
universe@125 | 303 | while (e->prev) { |
universe@8 | 304 | e = e->prev; |
universe@8 | 305 | } |
universe@122 | 306 | return (UcxList *)e; |
olaf@13 | 307 | } |
universe@22 | 308 | |
universe@122 | 309 | UcxList *ucx_list_remove(UcxList *l, UcxList *e) { |
universe@125 | 310 | return ucx_list_remove_a(ucx_default_allocator(), l, e); |
universe@125 | 311 | } |
universe@125 | 312 | |
universe@125 | 313 | UcxList *ucx_list_remove_a(UcxAllocator *alloc, UcxList *l, UcxList *e) { |
universe@161 | 314 | if (l == e) { |
universe@161 | 315 | l = e->next; |
universe@161 | 316 | } |
universe@161 | 317 | |
universe@161 | 318 | if (e->next) { |
universe@22 | 319 | e->next->prev = e->prev; |
universe@22 | 320 | } |
universe@161 | 321 | |
universe@161 | 322 | if (e->prev) { |
universe@161 | 323 | e->prev->next = e->next; |
universe@161 | 324 | } |
universe@161 | 325 | |
universe@173 | 326 | alfree(alloc, e); |
universe@22 | 327 | return l; |
universe@22 | 328 | } |