src/linked_list.c

Sun, 14 Feb 2021 11:30:47 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 14 Feb 2021 11:30:47 +0100
changeset 410
76b76f0f046b
parent 409
5d167af0eadb
child 412
af766caea48d
permissions
-rw-r--r--

add test config and let the test binary return nonzero on test failures

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "cx/linked_list.h"
#include <stdint.h>
#include <string.h>

/* LOW LEVEL LINKED LIST FUNCTIONS */

#define CX_LL_PTR(cur, off) ((void**)(((char*)cur)+off))

void *cx_linked_list_last(void **begin, void **end, ptrdiff_t loc_next) {
    if (end != NULL) {
        return *end;
    } else {
        if (begin == NULL || *begin == NULL)
            return NULL;

        void *cur = *begin;
        void *last;
        do {
            last = cur;
        } while ((cur = *CX_LL_PTR(cur, loc_next)) != NULL);

        return last;
    }
}

int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) {
    // TODO: how do we report error messages?
    if (loc_next < 0 || (begin == NULL && end == NULL)) {
        return 1;
    }

    void *last = cx_linked_list_last(begin, end, loc_next);
    if (last == NULL) {
        if (begin == NULL) {
            return 1;
        } else {
            *begin = new_node;
            return 0;
        }
    }

    void **next = CX_LL_PTR(last, loc_next);
    *next = new_node;
    if (end != NULL) {
        *end = cx_linked_list_last(&new_node, NULL, loc_next);
    }
    if (loc_prev >= 0) {
        void **prev = CX_LL_PTR(new_node, loc_prev);
        *prev = last;
    }

    return 0;
}

/* HIGH LEVEL LINKED LIST IMPLEMENTATION */

struct cx_linked_list_node {
    void *prev;
    void *next;
    char payload[];
};

typedef struct {
    void *begin;
    void *end;
    ptrdiff_t loc_prev;
    ptrdiff_t loc_next;
} cx_linked_list;

int cx_ll_add(cx_list *list, void *elem) {
    cx_linked_list *listdata = (cx_linked_list *) list->listdata;
    CxAllocator allocator = list->allocator;

    struct cx_linked_list_node *node = cxMalloc(allocator,
                                                sizeof(struct cx_linked_list_node) + list->itemsize);
    if (node == NULL)
        return 1;

    node->next = NULL;
    memcpy(node->payload, elem, list->itemsize);

    int ret = cx_linked_list_add(
            &listdata->begin,
            &listdata->end,
            offsetof(struct cx_linked_list_node, prev),
            offsetof(struct cx_linked_list_node, next),
            node
    );
    if (ret == 0) {
        list->size++;
        return 0;
    } else {
        return ret;
    }
}

int cx_ll_insert(cx_list *list, size_t index, void *elem) {
    cx_linked_list *listdata = (cx_linked_list *) list->listdata;
    // TODO: implement using low level API
    return 1;
}

void *cx_ll_remove(cx_list *list, size_t index) {
    cx_linked_list *listdata = (cx_linked_list *) list->listdata;
    // TODO: implement using low level API
    return NULL;
}

size_t cx_ll_find(cx_list *list, void *elem) {
    cx_linked_list *listdata = (cx_linked_list *) list->listdata;
    // TODO: implement using low level API
    return 0;
}

void *cx_ll_last(cx_list *list) {
    cx_linked_list *listdata = (cx_linked_list *) list->listdata;
    struct cx_linked_list_node *last = cx_linked_list_last(
            NULL, &listdata->end, offsetof(struct cx_linked_list_node, next));
    return &last->payload;
}

cx_list_class cx_linked_list_class = {
        cx_ll_add,
        cx_ll_insert,
        cx_ll_remove,
        cx_ll_find,
        cx_ll_last
};

CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) {
    CxLinkedListDesc desc;
    desc.item_size = item_size;
    desc.begin = NULL;
    desc.loc_prev = offsetof(struct cx_linked_list_node, prev);
    desc.loc_next = offsetof(struct cx_linked_list_node, next);

    return cxLinkedListWrap(allocator, comparator, desc);
}

CxList cxLinkedListWrap(CxAllocator allocator, CxListComparator comparator, CxLinkedListDesc desc) {
    CxList list = cxMalloc(allocator, sizeof(list) + sizeof(cx_linked_list));
    if (list == NULL)
        return NULL;

    list->cl = &cx_linked_list_class;
    list->data.allocator = allocator;
    list->data.cmpfunc = comparator;
    list->data.itemsize = desc.item_size;
    list->data.capacity = SIZE_MAX;

    cx_linked_list *ll = (cx_linked_list *) list->data.listdata;
    ll->begin = desc.begin;
    ll->loc_prev = desc.loc_prev;
    ll->loc_next = desc.loc_next;
    cxLinkedListRecalculateSize(list);

    return list;
}

void cxLinkedListDestroy(CxList list) {

}

size_t cxLinkedListRecalculateSize(CxList list) {
    cx_linked_list *ll = (cx_linked_list *) list->data.listdata;

    if (ll->begin == NULL) {
        list->data.size = 0;
        ll->end = NULL;
        return 0;
    } else {
        void *cur = ll->begin;
        void *last;
        size_t size;
        do {
            last = cur;
            size++;
        } while ((cur = *CX_LL_PTR(cur, ll->loc_next)) != NULL);
        ll->end = last;
        list->data.size = size;
        return size;
    }
}

mercurial