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