ucx/list.c

Wed, 17 Jul 2013 11:47:02 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 17 Jul 2013 11:47:02 +0200
changeset 114
5a0859739b76
parent 103
08018864fb91
permissions
-rw-r--r--

added doxyfile and documentation for ucx.h

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@10 29 #include "list.h"
olaf@2 30
universe@87 31 UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data) {
universe@18 32 UcxList *ret = NULL;
universe@18 33 while (l != NULL) {
universe@18 34 if (fnc != NULL) {
universe@18 35 ret = ucx_list_append(ret, fnc(l->data, data));
universe@18 36 } else {
universe@18 37 ret = ucx_list_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_list_equals(const UcxList *l1, const UcxList *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@10 61 void ucx_list_free(UcxList *l) {
universe@10 62 UcxList *e = l, *f;
universe@10 63 while (e != NULL) {
universe@10 64 f = e;
universe@10 65 e = e->next;
universe@10 66 free(f);
universe@10 67 }
universe@10 68 }
universe@10 69
universe@10 70 UcxList *ucx_list_append(UcxList *l, void *data) {
universe@10 71 UcxList *nl = (UcxList*) malloc(sizeof(UcxList));
universe@10 72 if (nl == NULL) return NULL;
universe@10 73
universe@10 74 nl->data = data;
universe@10 75 nl->next = NULL;
universe@10 76 if (l == NULL) {
universe@10 77 return nl;
universe@10 78 } else {
universe@10 79 UcxList *t = ucx_list_last(l);
universe@10 80 t->next = nl;
universe@10 81 return l;
universe@10 82 }
universe@10 83 }
universe@10 84
universe@10 85 UcxList *ucx_list_prepend(UcxList *l, void *data) {
universe@10 86 UcxList *nl = ucx_list_append(NULL, data);
universe@10 87 if (nl == NULL) return NULL;
universe@10 88
universe@10 89 if (l != NULL) {
universe@10 90 nl->next = l;
universe@10 91 }
universe@10 92 return nl;
universe@10 93 }
universe@10 94
universe@93 95 UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) {
universe@10 96 if (l1 == NULL) {
universe@10 97 return l2;
universe@10 98 } else {
universe@10 99 UcxList *last = ucx_list_last(l1);
universe@10 100 last->next = l2;
universe@10 101 return l1;
universe@10 102 }
universe@10 103 }
universe@10 104
universe@67 105 UcxList *ucx_list_last(const UcxList *l) {
universe@10 106 if (l == NULL) return NULL;
universe@10 107
universe@67 108 const UcxList *e = l;
universe@10 109 while (e->next != NULL) {
universe@10 110 e = e->next;
universe@10 111 }
universe@67 112 return (UcxList*)e;
universe@10 113 }
universe@10 114
universe@67 115 UcxList *ucx_list_get(const UcxList *l, int index) {
universe@10 116 if (l == NULL) return NULL;
universe@10 117
universe@67 118 const UcxList *e = l;
universe@10 119 while (e->next != NULL && index > 0) {
universe@10 120 e = e->next;
universe@10 121 index--;
universe@10 122 }
universe@10 123
universe@67 124 return (UcxList*)(index == 0 ? e : NULL);
universe@10 125 }
universe@10 126
universe@87 127 int ucx_list_contains(UcxList *l, void *elem, cmp_func fnc, void *cmpdata) {
universe@87 128 UCX_FOREACH(UcxList*, l, e) {
universe@87 129 if (!fnc(elem, e->data, cmpdata)) {
universe@87 130 return 1;
universe@87 131 }
universe@87 132 }
universe@87 133 return 0;
universe@87 134 }
universe@87 135
universe@67 136 size_t ucx_list_size(const UcxList *l) {
universe@10 137 if (l == NULL) return 0;
universe@10 138
universe@67 139 const UcxList *e = l;
universe@10 140 size_t s = 1;
universe@10 141 while (e->next != NULL) {
universe@10 142 e = e->next;
universe@10 143 s++;
universe@10 144 }
universe@10 145
universe@10 146 return s;
universe@10 147 }
universe@10 148
universe@37 149 UcxList *ucx_list_sort_merge(int length,
universe@67 150 UcxList* restrict ls, UcxList* restrict le, UcxList* restrict re,
universe@37 151 cmp_func fnc, void* data) {
universe@69 152
universe@73 153 UcxList** sorted = (UcxList**) malloc(sizeof(UcxList*)*length);
universe@37 154 UcxList *rc, *lc;
universe@35 155
universe@67 156 lc = ls; rc = le;
universe@37 157 int n = 0;
universe@67 158 while (lc && lc != le && rc != re) {
universe@37 159 if (fnc(lc->data, rc->data, data) <= 0) {
universe@37 160 sorted[n] = lc;
universe@37 161 lc = lc->next;
universe@37 162 } else {
universe@37 163 sorted[n] = rc;
universe@37 164 rc = rc->next;
universe@35 165 }
universe@37 166 n++;
universe@37 167 }
universe@67 168 while (lc && lc != le) {
universe@37 169 sorted[n] = lc;
universe@37 170 lc = lc->next;
universe@37 171 n++;
universe@37 172 }
universe@67 173 while (rc && rc != re) {
universe@37 174 sorted[n] = rc;
universe@37 175 rc = rc->next;
universe@37 176 n++;
universe@35 177 }
universe@35 178
universe@37 179 // Update pointer
universe@37 180 for (int i = 0 ; i < length-1 ; i++) {
universe@37 181 sorted[i]->next = sorted[i+1];
universe@37 182 }
universe@37 183 sorted[length-1]->next = NULL;
universe@35 184
universe@69 185 UcxList *ret = sorted[0];
universe@73 186 free(sorted);
universe@69 187 return ret;
universe@35 188 }
universe@35 189
universe@36 190 UcxList *ucx_list_sort(UcxList *l, cmp_func fnc, void *data) {
universe@35 191 if (l == NULL) {
universe@35 192 return NULL;
universe@35 193 }
universe@37 194
universe@37 195 UcxList *lc;
universe@37 196 int ln = 1;
universe@37 197
universe@67 198 UcxList *restrict ls = l, *restrict le, *restrict re;
universe@37 199 lc = ls;
universe@37 200 while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) {
universe@37 201 lc = lc->next;
universe@37 202 ln++;
universe@37 203 }
universe@37 204 le = lc->next;
universe@37 205
universe@67 206 if (le == NULL) {
universe@37 207 return l; // this list is already sorted :)
universe@37 208 } else {
universe@37 209 UcxList *rc;
universe@37 210 int rn = 1;
universe@67 211 rc = le;
universe@37 212 while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) {
universe@37 213 rc = rc->next;
universe@37 214 rn++;
universe@37 215 }
universe@37 216 re = rc->next;
universe@37 217
universe@37 218 // Something left? Sort it!
universe@37 219 UcxList *remainder = re;
universe@37 220 size_t remainder_length = ucx_list_size(remainder);
universe@37 221 if (remainder != NULL) {
universe@37 222 remainder = ucx_list_sort(remainder, fnc, data);
universe@37 223 }
universe@37 224
universe@37 225 // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
universe@37 226 UcxList *sorted = ucx_list_sort_merge(ln+rn,
universe@67 227 ls, le, re,
universe@37 228 fnc, data);
universe@37 229
universe@37 230 // merge sorted list with (also sorted) remainder
universe@37 231 l = ucx_list_sort_merge(ln+rn+remainder_length,
universe@67 232 sorted, remainder, NULL, fnc, data);
universe@37 233
universe@35 234 return l;
universe@35 235 }
universe@35 236 }
universe@35 237
universe@23 238 /* list specific functions */
universe@23 239 UcxList *ucx_list_remove(UcxList *l, UcxList *e) {
universe@23 240 if (e == l) {
universe@23 241 l = e->next;
universe@23 242 free(e);
universe@23 243 } else {
universe@23 244 UcxList *f = l;
universe@23 245 while (f->next != NULL && f->next != e) {
universe@23 246 f = f->next;
universe@23 247 }
olaf@31 248 /* perform remove if this element is found in this list */
universe@23 249 if (f->next == e) {
universe@23 250 f->next = e->next;
universe@23 251 free(e);
universe@23 252 }
universe@23 253 }
universe@23 254 return l;
universe@24 255 }

mercurial