2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
33 // <editor-fold desc="Store Pointers Functionality">
35 static _Thread_local CxListComparator cx_pl_cmpfunc_impl;
37 static int cx_pl_cmpfunc(
41 void *const *lptr = l;
42 void *const *rptr = r;
43 void const *left = lptr == NULL ? NULL : *lptr;
44 void const *right = rptr == NULL ? NULL : *rptr;
45 return cx_pl_cmpfunc_impl(left, right);
48 static void cx_pl_hack_cmpfunc(struct cx_list_s const *list) {
49 // cast away const - this is the hacky thing
50 struct cx_list_s *l = (struct cx_list_s *) list;
51 cx_pl_cmpfunc_impl = l->cmpfunc;
52 l->cmpfunc = cx_pl_cmpfunc;
55 static void cx_pl_unhack_cmpfunc(struct cx_list_s const *list) {
56 // cast away const - this is the hacky thing
57 struct cx_list_s *l = (struct cx_list_s *) list;
58 l->cmpfunc = cx_pl_cmpfunc_impl;
61 static void cx_pl_destructor(struct cx_list_s *list) {
62 list->climpl->destructor(list);
65 static int cx_pl_insert_element(
66 struct cx_list_s *list,
70 return list->climpl->insert_element(list, index, &element);
73 static size_t cx_pl_insert_array(
74 struct cx_list_s *list,
79 return list->climpl->insert_array(list, index, array, n);
82 static int cx_pl_insert_iter(
83 struct cx_mut_iterator_s *iter,
87 struct cx_list_s *list = iter->src_handle;
88 return list->climpl->insert_iter(iter, &elem, prepend);
91 static int cx_pl_remove(
92 struct cx_list_s *list,
95 return list->climpl->remove(list, index);
98 static void cx_pl_clear(struct cx_list_s *list) {
99 list->climpl->clear(list);
102 static int cx_pl_swap(
103 struct cx_list_s *list,
107 return list->climpl->swap(list, i, j);
110 static void *cx_pl_at(
111 struct cx_list_s const *list,
114 void **ptr = list->climpl->at(list, index);
115 return ptr == NULL ? NULL : *ptr;
118 static size_t cx_pl_find(
119 struct cx_list_s const *list,
122 cx_pl_hack_cmpfunc(list);
123 size_t ret = list->climpl->find(list, &elem);
124 cx_pl_unhack_cmpfunc(list);
128 static void cx_pl_sort(struct cx_list_s *list) {
129 cx_pl_hack_cmpfunc(list);
130 list->climpl->sort(list);
131 cx_pl_unhack_cmpfunc(list);
134 static int cx_pl_compare(
135 struct cx_list_s const *list,
136 struct cx_list_s const *other
138 cx_pl_hack_cmpfunc(list);
139 int ret = list->climpl->compare(list, other);
140 cx_pl_unhack_cmpfunc(list);
144 static void cx_pl_reverse(struct cx_list_s *list) {
145 list->climpl->reverse(list);
148 static void *cx_pl_iter_current(void const *it) {
149 struct cx_iterator_s const *iter = it;
150 void **ptr = iter->base.current_impl(it);
151 return ptr == NULL ? NULL : *ptr;
154 static struct cx_iterator_s cx_pl_iterator(
155 struct cx_list_s const *list,
159 struct cx_iterator_s iter = list->climpl->iterator(list, index, backwards);
160 iter.base.current_impl = iter.base.current;
161 iter.base.current = cx_pl_iter_current;
165 static cx_list_class cx_pointer_list_class = {
167 cx_pl_insert_element,
181 void cxListStoreObjects(CxList *list) {
182 if (list->climpl != NULL) {
183 list->cl = list->climpl;
188 void cxListStorePointers(CxList *list) {
189 list->itemsize = sizeof(void *);
190 list->climpl = list->cl;
191 list->cl = &cx_pointer_list_class;
194 bool cxListIsStoringPointers(CxList const *list) {
195 return list->climpl != NULL;
200 void cx_list_invoke_destructor(
204 switch (list->content_destructor_type) {
205 case CX_DESTRUCTOR_SIMPLE: {
206 cx_list_invoke_simple_destructor(list, elem);
209 case CX_DESTRUCTOR_ADVANCED: {
210 cx_list_invoke_advanced_destructor(list, elem);
213 case CX_DESTRUCTOR_NONE:
218 void cx_list_invoke_simple_destructor(
222 if (cxListIsStoringPointers(list)) {
223 elem = *((void **) elem);
225 list->simple_destructor(elem);
228 void cx_list_invoke_advanced_destructor(
232 if (cxListIsStoringPointers(list)) {
233 elem = *((void **) elem);
235 list->advanced_destructor.func(list->advanced_destructor.data, elem);
238 void cxListDestroy(CxList *list) {
239 switch (list->content_destructor_type) {
240 case CX_DESTRUCTOR_SIMPLE: {
241 CxIterator iter = cxListIterator(list);
242 cx_foreach(void*, elem, iter) {
243 // already correctly resolved pointer - immediately invoke dtor
244 list->simple_destructor(elem);
248 case CX_DESTRUCTOR_ADVANCED: {
249 CxIterator iter = cxListIterator(list);
250 cx_foreach(void*, elem, iter) {
251 // already correctly resolved pointer - immediately invoke dtor
252 list->advanced_destructor.func(list->advanced_destructor.data, elem);
256 case CX_DESTRUCTOR_NONE:
260 list->cl->destructor(list);
261 cxFree(list->allocator, list);
268 if (list->cl->compare == other->cl->compare) {
269 // same compare function, lists are compatible
270 return list->cl->compare(list, other);
272 // different compare functions, use iterator
273 if (list->size == other->size) {
274 CxIterator left = cxListIterator(list);
275 CxIterator right = cxListIterator(other);
276 for (size_t i = 0; i < list->size; i++) {
277 void *leftValue = cxIteratorCurrent(left);
278 void *rightValue = cxIteratorCurrent(right);
279 int d = list->cmpfunc(leftValue, rightValue);
283 cxIteratorNext(left);
284 cxIteratorNext(right);
288 return list->size < other->size ? -1 : 1;
293 CxMutIterator cxListMutIteratorAt(
297 CxIterator it = list->cl->iterator(list, index, false);
298 it.base.mutating = true;
300 // we know the iterators share the same memory layout
302 memcpy(&iter, &it, sizeof(CxMutIterator));
306 CxMutIterator cxListMutBackwardsIteratorAt(
310 CxIterator it = list->cl->iterator(list, index, true);
311 it.base.mutating = true;
313 // we know the iterators share the same memory layout
315 memcpy(&iter, &it, sizeof(CxMutIterator));