ucx/list.c

Fri, 30 Nov 2012 21:18:13 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 30 Nov 2012 21:18:13 +0100
changeset 1
1bcaac272cdf
child 5
88625853ae74
permissions
-rw-r--r--

added existing source code

#include "list.h"

UcxList *restrict ucx_list_clone(UcxList *restrict l,
        copy_func fnc, void *data) {
    UcxList *ret = NULL;
    while (l != NULL) {
        if (fnc != NULL) {
            ret = ucx_list_append(ret, fnc(l->data, data));
        } else {
            ret = ucx_list_append(ret, l->data);
        }
        l = l->next;
    }
    return ret;
}

int ucx_list_equals(const UcxList *l1, const UcxList *l2,
        cmp_func fnc, void* data) {
    if (l1 == l2) return 1;
    
    while (l1 != NULL && l2 != NULL) {
        if (fnc == NULL) {
            if (l1->data != l2->data) return 0;
        } else {
            if (fnc(l1->data, l2->data, data) != 0) return 0;
        }
        l1 = l1->next;
        l2 = l2->next;
    }
    
    return (l1 == NULL && l2 == NULL);
}

void ucx_list_free(UcxList *l) {
    UcxList *e = l, *f;
    while (e != NULL) {
        f = e;
        e = e->next;
        free(f);
    }
}

UcxList *ucx_list_append(UcxList *l, void *data)  {
    UcxList *nl = (UcxList*) malloc(sizeof(UcxList));
    if (nl == NULL) return NULL;
    
    nl->data = data;
    nl->next = NULL;
    if (l == NULL) {
        return nl;
    } else {
        UcxList *t = ucx_list_last(l);
        t->next = nl;
        return l;
    }
}

UcxList *ucx_list_prepend(UcxList *l, void *data) {
    UcxList *nl = ucx_list_append(NULL, data);
    if (nl == NULL) return NULL;
    
    if (l != NULL) {
        nl->next = l;
    }
    return nl;
}

UcxList *ucx_list_concat(UcxList *restrict l1, UcxList *restrict l2) {
    if (l1 == NULL) {
        return l2;
    } else {
        UcxList *last = ucx_list_last(l1);
        last->next = l2;
        return l1;
    }
}

UcxList *ucx_list_last(const UcxList *l) {
    if (l == NULL) return NULL;
    
    const UcxList *e = l;
    while (e->next != NULL) {
        e = e->next;
    }
    return (UcxList*)e;
}

UcxList *ucx_list_get(const UcxList *l, int index) {
    if (l == NULL) return NULL;

    const UcxList *e = l;
    while (e->next != NULL && index > 0) {
        e = e->next;
        index--;
    }
    
    return (UcxList*)(index == 0 ? e : NULL);
}

size_t ucx_list_size(const UcxList *l) {
    if (l == NULL) return 0;
    
    const UcxList *e = l;
    size_t s = 1;
    while (e->next != NULL) {
        e = e->next;
        s++;
    }

    return s;
}

UcxList *ucx_list_sort_merge(int length,
        UcxList* restrict ls, UcxList* restrict le, UcxList* restrict re,
        cmp_func fnc, void* data) {

    UcxList** sorted = (UcxList**) malloc(sizeof(UcxList*)*length);
    UcxList *rc, *lc;

    lc = ls; rc = le;
    int n = 0;
    while (lc && lc != le && rc != re) {
        if (fnc(lc->data, rc->data, data) <= 0) {
            sorted[n] = lc;
            lc = lc->next;
        } else {
            sorted[n] = rc;
            rc = rc->next;
        }
        n++;
    }
    while (lc && lc != le) {
        sorted[n] = lc;
        lc = lc->next;
        n++;
    }
    while (rc && rc != re) {
        sorted[n] = rc;
        rc = rc->next;
        n++;
    }

    // Update pointer
    for (int i = 0 ; i < length-1 ; i++) {
        sorted[i]->next = sorted[i+1];
    }
    sorted[length-1]->next = NULL;

    UcxList *ret = sorted[0];
    free(sorted);
    return ret;
}

UcxList *ucx_list_sort(UcxList *l, cmp_func fnc, void *data) {
    if (l == NULL) {
        return NULL;
    }

    UcxList *lc;
    int ln = 1;

    UcxList *restrict ls = l, *restrict le, *restrict re;
    lc = ls;
    while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) {
        lc = lc->next;
        ln++;
    }
    le = lc->next;

    if (le == NULL) {
        return l; // this list is already sorted :)
    } else {
        UcxList *rc;
        int rn = 1;
        rc = le;
        while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) {
            rc = rc->next;
            rn++;
        }
        re = rc->next;

        // Something left? Sort it!
        UcxList *remainder = re;
        size_t remainder_length = ucx_list_size(remainder);
        if (remainder != NULL) {
            remainder = ucx_list_sort(remainder, fnc, data);
        }

        // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
        UcxList *sorted = ucx_list_sort_merge(ln+rn,
                ls, le, re,
                fnc, data);

        // merge sorted list with (also sorted) remainder
        l = ucx_list_sort_merge(ln+rn+remainder_length,
                sorted, remainder, NULL, fnc, data);

        return l;
    }
}

/* list specific functions */
UcxList *ucx_list_remove(UcxList *l, UcxList *e) {
    if (e == l) {
        l = e->next;
        free(e);
    } else {
        UcxList *f = l;
        while (f->next != NULL && f->next != e) {
            f = f->next;
        }
        /* perform remove if this element is found in this list */
        if (f->next == e) {
            f->next = e->next;
            free(e);
        }
    }
    return l;
}

mercurial