universe@103: /* universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@103: * universe@103: * Copyright 2013 Olaf Wintermann. All rights reserved. universe@103: * universe@103: * Redistribution and use in source and binary forms, with or without universe@103: * modification, are permitted provided that the following conditions are met: universe@103: * universe@103: * 1. Redistributions of source code must retain the above copyright universe@103: * notice, this list of conditions and the following disclaimer. universe@103: * universe@103: * 2. Redistributions in binary form must reproduce the above copyright universe@103: * notice, this list of conditions and the following disclaimer in the universe@103: * documentation and/or other materials provided with the distribution. universe@103: * universe@103: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@103: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@103: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@103: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@103: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@103: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@103: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@103: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@103: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@103: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@103: * POSSIBILITY OF SUCH DAMAGE. universe@103: */ universe@103: universe@4: #include "dlist.h" universe@4: universe@87: UcxDlist *ucx_dlist_clone(UcxDlist *l, copy_func fnc, void *data) { universe@18: UcxDlist *ret = NULL; universe@18: while (l != NULL) { universe@18: if (fnc != NULL) { universe@18: ret = ucx_dlist_append(ret, fnc(l->data, data)); universe@18: } else { universe@18: ret = ucx_dlist_append(ret, l->data); universe@18: } universe@18: l = l->next; universe@18: } universe@18: return ret; universe@18: } universe@18: universe@67: int ucx_dlist_equals(const UcxDlist *l1, const UcxDlist *l2, universe@67: cmp_func fnc, void* data) { universe@18: if (l1 == l2) return 1; universe@18: universe@18: while (l1 != NULL && l2 != NULL) { universe@18: if (fnc == NULL) { universe@18: if (l1->data != l2->data) return 0; universe@18: } else { universe@18: if (fnc(l1->data, l2->data, data) != 0) return 0; universe@18: } universe@18: l1 = l1->next; universe@18: l2 = l2->next; universe@18: } universe@18: universe@18: return (l1 == NULL && l2 == NULL); universe@18: } universe@18: universe@8: void ucx_dlist_free(UcxDlist *l) { universe@8: UcxDlist *e = l, *f; universe@8: while (e != NULL) { universe@8: f = e; universe@8: e = e->next; universe@8: free(f); universe@8: } universe@8: } universe@8: universe@7: UcxDlist *ucx_dlist_append(UcxDlist *l, void *data) { universe@8: UcxDlist *nl = (UcxDlist*) malloc(sizeof(UcxDlist)); universe@8: if (nl == NULL) return NULL; universe@7: universe@8: nl->data = data; universe@8: nl->next = NULL; universe@8: if (l == NULL) { universe@22: nl->prev = NULL; universe@8: return nl; universe@8: } else { universe@8: UcxDlist *t = ucx_dlist_last(l); universe@8: t->next = nl; universe@8: nl->prev = t; universe@8: return l; universe@8: } universe@7: } universe@7: universe@7: UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data) { universe@8: UcxDlist *nl = ucx_dlist_append(NULL, data); universe@8: if (nl == NULL) return NULL; universe@7: universe@8: if (l != NULL) { universe@8: nl->next = l; universe@8: l->prev = nl; universe@8: } universe@8: return nl; universe@7: } universe@7: universe@93: UcxDlist *ucx_dlist_concat(UcxDlist *l1, UcxDlist *l2) { universe@8: if (l1 == NULL) { universe@8: return l2; universe@8: } else { universe@8: UcxDlist *last = ucx_dlist_last(l1); universe@8: last->next = l2; universe@8: l2->prev = last; universe@8: return l1; universe@8: } universe@7: } universe@7: universe@67: UcxDlist *ucx_dlist_last(const UcxDlist *l) { universe@7: if (l == NULL) return NULL; universe@7: universe@67: const UcxDlist *e = l; universe@7: while (e->next != NULL) { universe@7: e = e->next; universe@7: } universe@67: return (UcxDlist*)e; universe@7: } universe@7: universe@67: UcxDlist *ucx_dlist_get(const UcxDlist *l, int index) { universe@8: if (l == NULL) return NULL; universe@8: universe@67: const UcxDlist *e = l; universe@8: while (e->next != NULL && index > 0) { universe@8: e = e->next; universe@8: index--; universe@8: } universe@7: universe@67: return (UcxDlist*)(index == 0 ? e : NULL); universe@7: } universe@7: universe@87: int ucx_dlist_contains(UcxDlist *l, void *elem, cmp_func fnc, void *cmpdata) { universe@87: UCX_FOREACH(UcxDlist*, l, e) { universe@87: if (!fnc(elem, e->data, cmpdata)) { universe@87: return 1; universe@87: } universe@87: } universe@87: return 0; universe@87: } universe@87: universe@67: size_t ucx_dlist_size(const UcxDlist *l) { universe@7: if (l == NULL) return 0; universe@7: universe@67: const UcxDlist *e = l; universe@7: size_t s = 1; universe@7: while (e->next != NULL) { universe@7: e = e->next; universe@7: s++; universe@7: } universe@7: universe@7: return s; universe@7: } universe@7: universe@37: UcxDlist *ucx_dlist_sort_merge(int length, universe@67: UcxDlist* restrict ls, UcxDlist* restrict le, UcxDlist* restrict re, universe@37: cmp_func fnc, void* data) { universe@73: universe@73: UcxDlist** sorted = (UcxDlist**) malloc(sizeof(UcxDlist*)*length); universe@37: UcxDlist *rc, *lc; universe@35: universe@67: lc = ls; rc = le; universe@37: int n = 0; universe@67: while (lc && lc != le && rc != re) { universe@37: if (fnc(lc->data, rc->data, data) <= 0) { universe@37: sorted[n] = lc; universe@37: lc = lc->next; universe@37: } else { universe@37: sorted[n] = rc; universe@37: rc = rc->next; universe@35: } universe@37: n++; universe@37: } universe@67: while (lc && lc != le) { universe@37: sorted[n] = lc; universe@37: lc = lc->next; universe@37: n++; universe@37: } universe@67: while (rc && rc != re) { universe@37: sorted[n] = rc; universe@37: rc = rc->next; universe@37: n++; universe@35: } universe@35: universe@37: // Update pointer universe@37: sorted[0]->prev = NULL; universe@37: for (int i = 0 ; i < length-1 ; i++) { universe@37: sorted[i]->next = sorted[i+1]; universe@37: sorted[i+1]->prev = sorted[i]; universe@37: } universe@37: sorted[length-1]->next = NULL; universe@35: universe@69: UcxDlist *ret = sorted[0]; universe@73: free(sorted); universe@69: return ret; universe@35: } universe@35: universe@36: UcxDlist *ucx_dlist_sort(UcxDlist *l, cmp_func fnc, void *data) { universe@35: if (l == NULL) { universe@35: return NULL; universe@35: } universe@37: universe@37: UcxDlist *lc; universe@37: int ln = 1; universe@37: universe@67: UcxDlist *restrict ls = l, *restrict le, *restrict re; universe@37: lc = ls; universe@37: while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) { universe@37: lc = lc->next; universe@37: ln++; universe@37: } universe@37: le = lc->next; universe@37: universe@67: if (le == NULL) { universe@37: return l; // this list is already sorted :) universe@37: } else { universe@37: UcxDlist *rc; universe@37: int rn = 1; universe@67: rc = le; universe@37: while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) { universe@37: rc = rc->next; universe@37: rn++; universe@37: } universe@37: re = rc->next; universe@37: universe@37: // Something left? Sort it! universe@37: UcxDlist *remainder = re; universe@37: size_t remainder_length = ucx_dlist_size(remainder); universe@37: if (remainder != NULL) { universe@37: remainder = ucx_dlist_sort(remainder, fnc, data); universe@37: } universe@37: universe@37: // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them universe@37: UcxDlist *sorted = ucx_dlist_sort_merge(ln+rn, universe@67: ls, le, re, universe@37: fnc, data); universe@37: universe@37: // merge sorted list with (also sorted) remainder universe@37: l = ucx_dlist_sort_merge(ln+rn+remainder_length, universe@67: sorted, remainder, NULL, fnc, data); universe@37: universe@35: return l; universe@35: } universe@35: } universe@35: universe@7: /* dlist specific functions */ universe@67: UcxDlist *ucx_dlist_first(const UcxDlist *l) { universe@8: if (l == NULL) return NULL; universe@7: universe@67: const UcxDlist *e = l; universe@8: while (e->prev != NULL) { universe@8: e = e->prev; universe@8: } universe@67: return (UcxDlist *)e; olaf@13: } universe@22: universe@22: UcxDlist *ucx_dlist_remove(UcxDlist *l, UcxDlist *e) { universe@22: if (e->prev == NULL) { olaf@31: if(e->next != NULL) { olaf@31: e->next->prev = NULL; olaf@31: l = e->next; olaf@31: } else { olaf@31: l = NULL; olaf@31: } olaf@31: universe@22: } else { universe@22: e->prev->next = e->next; universe@22: e->next->prev = e->prev; universe@22: } universe@22: free(e); universe@22: return l; universe@22: }