src/linked_list.c

Sun, 07 Feb 2021 20:37:20 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 07 Feb 2021 20:37:20 +0100
changeset 401
e6a8f7fb0c45
parent 400
8cd274352419
child 402
a34b93911956
permissions
-rw-r--r--

copy list items when they are added to the list

/*
 * 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_next, ptrdiff_t loc_prev, void *newnode) {
    // 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 = newnode;
            return 0;
        }
    }

    void **next = CX_LL_PTR(last, loc_next);
    *next = newnode;
    if (loc_prev >= 0) {
        void **prev = CX_LL_PTR(newnode, loc_prev);
        *prev = last;
    }

    return 0;
}

/* HIGH LEVEL LINKED LIST IMPLEMENTATION */

typedef struct {
    void *begin;
    void *end;
} cx_linked_list;

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

    /*
     * Memory layout:
     * next : sizeof(void*)
     * prev : sizeof(void*)
     * data : itemsize
     */
    void *node = cxMalloc(allocator,2*sizeof(void*)+list->itemsize);
    if (node == NULL)
        return 1;

    memset(node, 0, sizeof(void*));
    memcpy(node+2, elem, list->itemsize);

    int ret = cx_linked_list_add(
            &listdata->begin,
            &listdata->end,
            0,
            sizeof(void*),
            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 = list->listdata;
    // TODO: implement using low level API
    return 1;
}

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

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


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

CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t itemsize) {
    CxList list = cxMalloc(allocator, sizeof(list));
    if (list == NULL)
        return NULL;

    list->cl = &cx_linked_list_class;
    list->data.allocator = allocator;
    list->data.cmpfunc = comparator;
    list->data.size = 0;
    list->data.itemsize = itemsize;
    list->data.capacity = SIZE_MAX;
    list->data.listdata = cxCalloc(allocator, 1, sizeof(cx_linked_list));
    if (list->data.listdata == NULL) {
        cxFree(allocator, list);
        return NULL;
    }
}

mercurial