Wed, 08 Feb 2023 20:26:09 +0100
implement swap function for list elements - fixes #218
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
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.
15 *
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.
27 */
29 #include "cx/list.h"
31 #include <string.h>
33 // <editor-fold desc="Store Pointers Functionality">
35 static _Thread_local CxListComparator cx_pl_cmpfunc_impl;
37 static int cx_pl_cmpfunc(
38 void const *l,
39 void const *r
40 ) {
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);
46 }
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;
53 }
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;
59 }
61 static void cx_pl_destructor(struct cx_list_s *list) {
62 list->climpl->destructor(list);
63 }
65 static int cx_pl_insert_element(
66 struct cx_list_s *list,
67 size_t index,
68 void const *element
69 ) {
70 return list->climpl->insert_element(list, index, &element);
71 }
73 static size_t cx_pl_insert_array(
74 struct cx_list_s *list,
75 size_t index,
76 void const *array,
77 size_t n
78 ) {
79 return list->climpl->insert_array(list, index, array, n);
80 }
82 static int cx_pl_insert_iter(
83 struct cx_mut_iterator_s *iter,
84 void const *elem,
85 int prepend
86 ) {
87 struct cx_list_s *list = iter->src_handle;
88 return list->climpl->insert_iter(iter, &elem, prepend);
89 }
91 static int cx_pl_remove(
92 struct cx_list_s *list,
93 size_t index
94 ) {
95 return list->climpl->remove(list, index);
96 }
98 static int cx_pl_swap(
99 struct cx_list_s *list,
100 size_t i,
101 size_t j
102 ) {
103 return list->climpl->swap(list, i, j);
104 }
106 static void *cx_pl_at(
107 struct cx_list_s const *list,
108 size_t index
109 ) {
110 void **ptr = list->climpl->at(list, index);
111 return ptr == NULL ? NULL : *ptr;
112 }
114 static size_t cx_pl_find(
115 struct cx_list_s const *list,
116 void const *elem
117 ) {
118 cx_pl_hack_cmpfunc(list);
119 size_t ret = list->climpl->find(list, &elem);
120 cx_pl_unhack_cmpfunc(list);
121 return ret;
122 }
124 static void cx_pl_sort(struct cx_list_s *list) {
125 cx_pl_hack_cmpfunc(list);
126 list->climpl->sort(list);
127 cx_pl_unhack_cmpfunc(list);
128 }
130 static int cx_pl_compare(
131 struct cx_list_s const *list,
132 struct cx_list_s const *other
133 ) {
134 cx_pl_hack_cmpfunc(list);
135 int ret = list->climpl->compare(list, other);
136 cx_pl_unhack_cmpfunc(list);
137 return ret;
138 }
140 static void cx_pl_reverse(struct cx_list_s *list) {
141 list->climpl->reverse(list);
142 }
144 static void *cx_pl_iter_current(void const *it) {
145 struct cx_iterator_s const *iter = it;
146 void **ptr = iter->base.current_impl(it);
147 return ptr == NULL ? NULL : *ptr;
148 }
150 static struct cx_iterator_s cx_pl_iterator(
151 struct cx_list_s const *list,
152 size_t index
153 ) {
154 struct cx_iterator_s iter = list->climpl->iterator(list, index);
155 iter.base.current_impl = iter.base.current;
156 iter.base.current = cx_pl_iter_current;
157 return iter;
158 }
160 static cx_list_class cx_pointer_list_class = {
161 cx_pl_destructor,
162 cx_pl_insert_element,
163 cx_pl_insert_array,
164 cx_pl_insert_iter,
165 cx_pl_remove,
166 cx_pl_swap,
167 cx_pl_at,
168 cx_pl_find,
169 cx_pl_sort,
170 cx_pl_compare,
171 cx_pl_reverse,
172 cx_pl_iterator,
173 };
175 void cxListStoreObjects(CxList *list) {
176 if (list->climpl != NULL) {
177 list->cl = list->climpl;
178 list->climpl = NULL;
179 }
180 }
182 void cxListStorePointers(CxList *list) {
183 list->itemsize = sizeof(void *);
184 list->climpl = list->cl;
185 list->cl = &cx_pointer_list_class;
186 }
188 bool cxListIsStoringPointers(CxList *list) {
189 return list->climpl != NULL;
190 }
192 // </editor-fold>
194 void cxListDestroy(CxList *list) {
195 switch (list->content_destructor_type) {
196 case CX_DESTRUCTOR_SIMPLE: {
197 CxIterator iter = cxListBegin(list);
198 cx_foreach(void*, elem, iter) {
199 list->simple_destructor(elem);
200 }
201 break;
202 }
203 case CX_DESTRUCTOR_ADVANCED: {
204 CxIterator iter = cxListBegin(list);
205 cx_foreach(void*, elem, iter) {
206 list->advanced_destructor.func(list->advanced_destructor.data, elem);
207 }
208 break;
209 }
210 case CX_DESTRUCTOR_NONE:
211 break; // nothing
212 }
214 list->cl->destructor(list);
215 cxFree(list->allocator, list);
216 }
218 int cxListCompare(
219 CxList const *list,
220 CxList const *other
221 ) {
222 if (list->cl->compare == other->cl->compare) {
223 // same compare function, lists are compatible
224 return list->cl->compare(list, other);
225 } else {
226 // different compare functions, use iterator
227 if (list->size == other->size) {
228 CxIterator left = cxListBegin(list);
229 CxIterator right = cxListBegin(other);
230 for (size_t i = 0; i < list->size; i++) {
231 void *leftValue = cxIteratorCurrent(left);
232 void *rightValue = cxIteratorCurrent(right);
233 int d = list->cmpfunc(leftValue, rightValue);
234 if (d != 0) {
235 return d;
236 }
237 cxIteratorNext(left);
238 cxIteratorNext(right);
239 }
240 return 0;
241 } else {
242 return list->size < other->size ? -1 : 1;
243 }
244 }
245 }
247 CxMutIterator cxListMutIterator(
248 CxList *list,
249 size_t index
250 ) {
251 CxIterator it = list->cl->iterator(list, index);
252 it.base.mutating = true;
254 // we know the iterators share the same memory layout
255 CxMutIterator iter;
256 memcpy(&iter, &it, sizeof(CxMutIterator));
257 return iter;
258 }