src/list.c

Wed, 25 Jan 2023 19:19:29 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 25 Jan 2023 19:19:29 +0100
changeset 640
55cc3b373c5e
parent 630
ac5e7f789048
child 641
d402fead3386
permissions
-rw-r--r--

simplify list class - fixes #236

universe@503 1 /*
universe@503 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@503 3 *
universe@503 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@503 5 *
universe@503 6 * Redistribution and use in source and binary forms, with or without
universe@503 7 * modification, are permitted provided that the following conditions are met:
universe@503 8 *
universe@503 9 * 1. Redistributions of source code must retain the above copyright
universe@503 10 * notice, this list of conditions and the following disclaimer.
universe@503 11 *
universe@503 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@503 13 * notice, this list of conditions and the following disclaimer in the
universe@503 14 * documentation and/or other materials provided with the distribution.
universe@503 15 *
universe@503 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@503 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@503 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@503 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@503 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@503 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@503 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@503 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@503 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@503 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@503 26 * POSSIBILITY OF SUCH DAMAGE.
universe@503 27 */
universe@503 28
universe@503 29 #include "cx/list.h"
universe@503 30
universe@640 31 #include <string.h>
universe@640 32
universe@528 33 void cxListDestroy(CxList *list) {
universe@528 34 switch (list->content_destructor_type) {
universe@528 35 case CX_DESTRUCTOR_SIMPLE: {
universe@528 36 CxIterator iter = cxListBegin(list);
universe@528 37 cx_foreach(void*, elem, iter) {
universe@528 38 list->simple_destructor(elem);
universe@528 39 }
universe@528 40 break;
universe@503 41 }
universe@528 42 case CX_DESTRUCTOR_ADVANCED: {
universe@528 43 CxIterator iter = cxListBegin(list);
universe@528 44 cx_foreach(void*, elem, iter) {
universe@528 45 list->advanced_destructor.func(list->advanced_destructor.data, elem);
universe@528 46 }
universe@528 47 break;
universe@528 48 }
universe@528 49 case CX_DESTRUCTOR_NONE:
universe@528 50 break; // nothing
universe@503 51 }
universe@528 52
universe@524 53 list->cl->destructor(list);
universe@524 54 cxFree(list->allocator, list);
universe@503 55 }
universe@618 56
universe@618 57 int cxListCompare(
universe@618 58 CxList const *list,
universe@618 59 CxList const *other
universe@618 60 ) {
universe@618 61 if (list->cl->compare == other->cl->compare) {
universe@628 62 // same compare function, lists are compatible
universe@618 63 return list->cl->compare(list, other);
universe@618 64 } else {
universe@628 65 // different compare functions, use iterator
universe@618 66 if (list->size == other->size) {
universe@618 67 CxIterator left = cxListBegin(list);
universe@618 68 CxIterator right = cxListBegin(other);
universe@618 69 for (size_t i = 0; i < list->size; i++) {
universe@630 70 void *leftValue = cxIteratorCurrent(left);
universe@630 71 void *rightValue = cxIteratorCurrent(right);
universe@618 72 int d = list->cmpfunc(leftValue, rightValue);
universe@618 73 if (d != 0) {
universe@618 74 return d;
universe@618 75 }
universe@630 76 cxIteratorNext(left);
universe@630 77 cxIteratorNext(right);
universe@618 78 }
universe@618 79 return 0;
universe@618 80 } else {
universe@618 81 return list->size < other->size ? -1 : 1;
universe@618 82 }
universe@618 83 }
universe@618 84 }
universe@640 85
universe@640 86 CxMutIterator cxListMutIterator(
universe@640 87 CxList *list,
universe@640 88 size_t index
universe@640 89 ) {
universe@640 90 CxIterator it = list->cl->iterator(list, index);
universe@640 91 it.base.mutating = true;
universe@640 92
universe@640 93 // we know the iterators share the same memory layout
universe@640 94 CxMutIterator iter;
universe@640 95 memcpy(&iter, &it, sizeof(CxMutIterator));
universe@640 96 return iter;
universe@640 97 }

mercurial