ucx/dlist.c

Fri, 12 Oct 2012 12:12:59 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 12 Oct 2012 12:12:59 +0200
changeset 73
f15c7d6aebb9
parent 69
fb59270b1de3
child 87
bd444539cced
permissions
-rw-r--r--

lists now sort on heap to prevent stack overflows

     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) {
   121     UcxDlist** sorted = (UcxDlist**) malloc(sizeof(UcxDlist*)*length);
   122     UcxDlist *rc, *lc;
   124     lc = ls; rc = le;
   125     int n = 0;
   126     while (lc && lc != le && rc != re) {
   127         if (fnc(lc->data, rc->data, data) <= 0) {
   128             sorted[n] = lc;
   129             lc = lc->next;
   130         } else {
   131             sorted[n] = rc;
   132             rc = rc->next;
   133         }
   134         n++;
   135     }
   136     while (lc && lc != le) {
   137         sorted[n] = lc;
   138         lc = lc->next;
   139         n++;
   140     }
   141     while (rc && rc != re) {
   142         sorted[n] = rc;
   143         rc = rc->next;
   144         n++;
   145     }
   147     // Update pointer
   148     sorted[0]->prev = NULL;
   149     for (int i = 0 ; i < length-1 ; i++) {
   150         sorted[i]->next = sorted[i+1];
   151         sorted[i+1]->prev = sorted[i];
   152     }
   153     sorted[length-1]->next = NULL;
   155     UcxDlist *ret = sorted[0];
   156     free(sorted);
   157     return ret;
   158 }
   160 UcxDlist *ucx_dlist_sort(UcxDlist *l, cmp_func fnc, void *data) {
   161     if (l == NULL) {
   162         return NULL;
   163     }
   165     UcxDlist *lc;
   166     int ln = 1;
   168     UcxDlist *restrict ls = l, *restrict le, *restrict re;
   169     lc = ls;
   170     while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) {
   171         lc = lc->next;
   172         ln++;
   173     }
   174     le = lc->next;
   176     if (le == NULL) {
   177         return l; // this list is already sorted :)
   178     } else {
   179         UcxDlist *rc;
   180         int rn = 1;
   181         rc = le;
   182         while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) {
   183             rc = rc->next;
   184             rn++;
   185         }
   186         re = rc->next;
   188         // Something left? Sort it!
   189         UcxDlist *remainder = re;
   190         size_t remainder_length = ucx_dlist_size(remainder);
   191         if (remainder != NULL) {
   192             remainder = ucx_dlist_sort(remainder, fnc, data);
   193         }
   195         // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
   196         UcxDlist *sorted = ucx_dlist_sort_merge(ln+rn,
   197                 ls, le, re,
   198                 fnc, data);
   200         // merge sorted list with (also sorted) remainder
   201         l = ucx_dlist_sort_merge(ln+rn+remainder_length,
   202                 sorted, remainder, NULL, fnc, data);
   204         return l;
   205     }
   206 }
   208 /* dlist specific functions */
   209 UcxDlist *ucx_dlist_first(const UcxDlist *l) {
   210     if (l == NULL) return NULL;
   212     const UcxDlist *e = l;
   213     while (e->prev != NULL) {
   214         e = e->prev;
   215     }
   216     return (UcxDlist *)e;
   217 }
   219 UcxDlist *ucx_dlist_remove(UcxDlist *l, UcxDlist *e) {
   220     if (e->prev == NULL) {
   221         if(e->next != NULL) {
   222             e->next->prev = NULL;
   223             l = e->next;
   224         } else {
   225             l = NULL;
   226         }
   228     } else {
   229         e->prev->next = e->next;
   230         e->next->prev = e->prev;
   231     }
   232     free(e);
   233     return l;
   234 }

mercurial