ucx/dlist.c

Thu, 11 Oct 2012 11:42:31 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 11 Oct 2012 11:42:31 +0200
changeset 67
27e67e725d35
parent 37
ec296899d12f
child 69
fb59270b1de3
permissions
-rw-r--r--

added some qualifiers + removed pointer alias in mergesort

     1 #include "dlist.h"
     3 UcxDlist *restrict ucx_dlist_clone(UcxDlist *restrict l,
     4         copy_func fnc, void *data) {
     5     UcxDlist *ret = NULL;
     6     while (l != NULL) {
     7         if (fnc != NULL) {
     8             ret = ucx_dlist_append(ret, fnc(l->data, data));
     9         } else {
    10             ret = ucx_dlist_append(ret, l->data);
    11         }
    12         l = l->next;
    13     }
    14     return ret;
    15 }
    17 int ucx_dlist_equals(const UcxDlist *l1, const UcxDlist *l2,
    18         cmp_func fnc, void* data) {
    19     if (l1 == l2) return 1;
    21     while (l1 != NULL && l2 != NULL) {
    22         if (fnc == NULL) {
    23             if (l1->data != l2->data) return 0;
    24         } else {
    25             if (fnc(l1->data, l2->data, data) != 0) return 0;
    26         }
    27         l1 = l1->next;
    28         l2 = l2->next;
    29     }
    31     return (l1 == NULL && l2 == NULL);
    32 }
    34 void ucx_dlist_free(UcxDlist *l) {
    35     UcxDlist *e = l, *f;
    36     while (e != NULL) {
    37         f = e;
    38         e = e->next;
    39         free(f);
    40     }
    41 }
    43 UcxDlist *ucx_dlist_append(UcxDlist *l, void *data)  {
    44     UcxDlist *nl = (UcxDlist*) malloc(sizeof(UcxDlist));
    45     if (nl == NULL) return NULL;
    47     nl->data = data;
    48     nl->next = NULL;
    49     if (l == NULL) {
    50         nl->prev = NULL;
    51         return nl;
    52     } else {
    53         UcxDlist *t = ucx_dlist_last(l);
    54         t->next = nl;
    55         nl->prev = t;
    56         return l;
    57     }
    58 }
    60 UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data) {
    61     UcxDlist *nl = ucx_dlist_append(NULL, data);
    62     if (nl == NULL) return NULL;
    64     if (l != NULL) {
    65         nl->next = l;
    66         l->prev = nl;
    67     }
    68     return nl;
    69 }
    71 UcxDlist *ucx_dlist_concat(UcxDlist *restrict l1, UcxDlist *restrict l2) {
    72     if (l1 == NULL) {
    73         return l2;
    74     } else {
    75         UcxDlist *last = ucx_dlist_last(l1);
    76         last->next = l2;
    77         l2->prev = last;
    78         return l1;
    79     }
    80 }
    82 UcxDlist *ucx_dlist_last(const UcxDlist *l) {
    83     if (l == NULL) return NULL;
    85     const UcxDlist *e = l;
    86     while (e->next != NULL) {
    87         e = e->next;
    88     }
    89     return (UcxDlist*)e;
    90 }
    92 UcxDlist *ucx_dlist_get(const UcxDlist *l, int index) {
    93     if (l == NULL) return NULL;
    95     const UcxDlist *e = l;
    96     while (e->next != NULL && index > 0) {
    97         e = e->next;
    98         index--;
    99     }
   101     return (UcxDlist*)(index == 0 ? e : NULL);
   102 }
   104 size_t ucx_dlist_size(const UcxDlist *l) {
   105     if (l == NULL) return 0;
   107     const UcxDlist *e = l;
   108     size_t s = 1;
   109     while (e->next != NULL) {
   110         e = e->next;
   111         s++;
   112     }
   114     return s;
   115 }
   117 UcxDlist *ucx_dlist_sort_merge(int length,
   118         UcxDlist* restrict ls, UcxDlist* restrict le, UcxDlist* restrict re,
   119         cmp_func fnc, void* data) {
   120     UcxDlist *sorted[length];
   121     UcxDlist *rc, *lc;
   123     lc = ls; rc = le;
   124     int n = 0;
   125     while (lc && lc != le && rc != re) {
   126         if (fnc(lc->data, rc->data, data) <= 0) {
   127             sorted[n] = lc;
   128             lc = lc->next;
   129         } else {
   130             sorted[n] = rc;
   131             rc = rc->next;
   132         }
   133         n++;
   134     }
   135     while (lc && lc != le) {
   136         sorted[n] = lc;
   137         lc = lc->next;
   138         n++;
   139     }
   140     while (rc && rc != re) {
   141         sorted[n] = rc;
   142         rc = rc->next;
   143         n++;
   144     }
   146     // Update pointer
   147     sorted[0]->prev = NULL;
   148     for (int i = 0 ; i < length-1 ; i++) {
   149         sorted[i]->next = sorted[i+1];
   150         sorted[i+1]->prev = sorted[i];
   151     }
   152     sorted[length-1]->next = NULL;
   154     return sorted[0];
   155 }
   157 UcxDlist *ucx_dlist_sort(UcxDlist *l, cmp_func fnc, void *data) {
   158     if (l == NULL) {
   159         return NULL;
   160     }
   162     UcxDlist *lc;
   163     int ln = 1;
   165     UcxDlist *restrict ls = l, *restrict le, *restrict re;
   166     lc = ls;
   167     while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) {
   168         lc = lc->next;
   169         ln++;
   170     }
   171     le = lc->next;
   173     if (le == NULL) {
   174         return l; // this list is already sorted :)
   175     } else {
   176         UcxDlist *rc;
   177         int rn = 1;
   178         rc = le;
   179         while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) {
   180             rc = rc->next;
   181             rn++;
   182         }
   183         re = rc->next;
   185         // Something left? Sort it!
   186         UcxDlist *remainder = re;
   187         size_t remainder_length = ucx_dlist_size(remainder);
   188         if (remainder != NULL) {
   189             remainder = ucx_dlist_sort(remainder, fnc, data);
   190         }
   192         // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
   193         UcxDlist *sorted = ucx_dlist_sort_merge(ln+rn,
   194                 ls, le, re,
   195                 fnc, data);
   197         // merge sorted list with (also sorted) remainder
   198         l = ucx_dlist_sort_merge(ln+rn+remainder_length,
   199                 sorted, remainder, NULL, fnc, data);
   201         return l;
   202     }
   203 }
   205 /* dlist specific functions */
   206 UcxDlist *ucx_dlist_first(const UcxDlist *l) {
   207     if (l == NULL) return NULL;
   209     const UcxDlist *e = l;
   210     while (e->prev != NULL) {
   211         e = e->prev;
   212     }
   213     return (UcxDlist *)e;
   214 }
   216 UcxDlist *ucx_dlist_remove(UcxDlist *l, UcxDlist *e) {
   217     if (e->prev == NULL) {
   218         if(e->next != NULL) {
   219             e->next->prev = NULL;
   220             l = e->next;
   221         } else {
   222             l = NULL;
   223         }
   225     } else {
   226         e->prev->next = e->next;
   227         e->next->prev = e->prev;
   228     }
   229     free(e);
   230     return l;
   231 }

mercurial