Sun, 13 Nov 2022 13:22:03 +0100
proposal for a low level array copy
universe@606 | 1 | /* |
universe@606 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@606 | 3 | * |
universe@606 | 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
universe@606 | 5 | * |
universe@606 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@606 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@606 | 8 | * |
universe@606 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@606 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@606 | 11 | * |
universe@606 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@606 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@606 | 14 | * documentation and/or other materials provided with the distribution. |
universe@606 | 15 | * |
universe@606 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@606 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@606 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@606 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@606 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@606 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@606 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@606 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@606 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@606 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@606 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@606 | 27 | */ |
universe@606 | 28 | |
universe@606 | 29 | #include "cx/array_list.h" |
universe@606 | 30 | |
universe@607 | 31 | /* LOW LEVEL ARRAY LIST FUNCTIONS */ |
universe@607 | 32 | |
universe@607 | 33 | |
universe@607 | 34 | |
universe@607 | 35 | /* HIGH LEVEL ARRAY LIST FUNCTIONS */ |
universe@607 | 36 | |
universe@607 | 37 | typedef struct { |
universe@607 | 38 | struct cx_list_s base; |
universe@607 | 39 | void *data; |
universe@607 | 40 | } cx_array_list; |
universe@607 | 41 | |
universe@607 | 42 | static void cx_arl_destructor(struct cx_list_s *list) { |
universe@607 | 43 | cx_array_list *arl = (cx_array_list*) list; |
universe@607 | 44 | cxFree(list->allocator, arl->data); |
universe@607 | 45 | } |
universe@607 | 46 | |
universe@607 | 47 | static int cx_arl_add( |
universe@607 | 48 | struct cx_list_s *list, |
universe@607 | 49 | void const *elem |
universe@607 | 50 | ) { |
universe@607 | 51 | return 1; |
universe@607 | 52 | } |
universe@607 | 53 | |
universe@607 | 54 | static int cx_arl_insert( |
universe@607 | 55 | struct cx_list_s *list, |
universe@607 | 56 | size_t index, |
universe@607 | 57 | void const *elem |
universe@607 | 58 | ) { |
universe@607 | 59 | return 1; |
universe@607 | 60 | } |
universe@607 | 61 | |
universe@607 | 62 | static int cx_arl_insert_iter( |
universe@607 | 63 | struct cx_iterator_s *iter, |
universe@607 | 64 | void const *elem, |
universe@607 | 65 | int prepend |
universe@607 | 66 | ) { |
universe@607 | 67 | return 1; |
universe@607 | 68 | } |
universe@607 | 69 | |
universe@607 | 70 | static int cx_arl_remove( |
universe@607 | 71 | struct cx_list_s *list, |
universe@607 | 72 | size_t index |
universe@607 | 73 | ) { |
universe@607 | 74 | return 1; |
universe@607 | 75 | } |
universe@607 | 76 | |
universe@607 | 77 | static void * cx_arl_at( |
universe@607 | 78 | struct cx_list_s const *list, |
universe@607 | 79 | size_t index |
universe@607 | 80 | ) { |
universe@607 | 81 | return NULL; |
universe@607 | 82 | } |
universe@607 | 83 | |
universe@607 | 84 | static size_t cx_arl_find( |
universe@607 | 85 | struct cx_list_s const *list, |
universe@607 | 86 | void const *elem |
universe@607 | 87 | ) { |
universe@607 | 88 | return 0; |
universe@607 | 89 | } |
universe@607 | 90 | |
universe@607 | 91 | static void cx_arl_sort(struct cx_list_s *list) { |
universe@607 | 92 | |
universe@607 | 93 | } |
universe@607 | 94 | |
universe@607 | 95 | static int cx_arl_compare( |
universe@607 | 96 | struct cx_list_s const *list, |
universe@607 | 97 | struct cx_list_s const *other |
universe@607 | 98 | ) { |
universe@607 | 99 | |
universe@607 | 100 | } |
universe@607 | 101 | |
universe@607 | 102 | static void cx_arl_reverse(struct cx_list_s *list) { |
universe@607 | 103 | |
universe@607 | 104 | } |
universe@607 | 105 | |
universe@607 | 106 | static struct cx_iterator_s cx_arl_iterator( |
universe@607 | 107 | struct cx_list_s *list, |
universe@607 | 108 | size_t index |
universe@607 | 109 | ) { |
universe@607 | 110 | struct cx_iterator_s iter; |
universe@607 | 111 | |
universe@607 | 112 | return iter; |
universe@607 | 113 | } |
universe@607 | 114 | |
universe@607 | 115 | static cx_list_class cx_array_list_class = { |
universe@607 | 116 | cx_arl_destructor, |
universe@607 | 117 | cx_arl_add, |
universe@607 | 118 | cx_arl_insert, |
universe@607 | 119 | cx_arl_insert_iter, |
universe@607 | 120 | cx_arl_remove, |
universe@607 | 121 | cx_arl_at, |
universe@607 | 122 | cx_arl_find, |
universe@607 | 123 | cx_arl_sort, |
universe@607 | 124 | cx_arl_compare, |
universe@607 | 125 | cx_arl_reverse, |
universe@607 | 126 | cx_arl_iterator, |
universe@607 | 127 | }; |
universe@607 | 128 | |
universe@606 | 129 | CxList *cxArrayListCreate( |
universe@606 | 130 | CxAllocator const *allocator, |
universe@606 | 131 | CxListComparator comparator, |
universe@606 | 132 | size_t item_size, |
universe@606 | 133 | size_t initial_capacity |
universe@606 | 134 | ) { |
universe@607 | 135 | cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list)); |
universe@607 | 136 | if (list == NULL) return NULL; |
universe@607 | 137 | |
universe@607 | 138 | list->data = cxCalloc(allocator, initial_capacity, item_size); |
universe@607 | 139 | if (list->data == NULL) { |
universe@607 | 140 | cxFree(allocator, list); |
universe@607 | 141 | return NULL; |
universe@607 | 142 | } |
universe@607 | 143 | |
universe@607 | 144 | list->base.cl = &cx_array_list_class; |
universe@607 | 145 | list->base.allocator = allocator; |
universe@607 | 146 | list->base.cmpfunc = comparator; |
universe@607 | 147 | list->base.itemsize = item_size; |
universe@607 | 148 | list->base.capacity = initial_capacity; |
universe@607 | 149 | |
universe@607 | 150 | return (CxList *) list; |
universe@606 | 151 | } |