Mon, 18 Dec 2023 18:22:53 +0100
add cxListFindRemove and cx_linked_list_find_node
resolves #339
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/array_list.h"
30 #include "cx/compare.h"
31 #include <assert.h>
32 #include <string.h>
34 // LOW LEVEL ARRAY LIST FUNCTIONS
36 enum cx_array_copy_result cx_array_copy(
37 void **target,
38 size_t *size,
39 size_t *capacity,
40 size_t index,
41 void const *src,
42 size_t elem_size,
43 size_t elem_count,
44 struct cx_array_reallocator_s *reallocator
45 ) {
46 // assert pointers
47 assert(target != NULL);
48 assert(size != NULL);
49 assert(src != NULL);
51 // determine capacity
52 size_t cap = capacity == NULL ? *size : *capacity;
54 // check if resize is required
55 size_t minsize = index + elem_count;
56 size_t newsize = *size < minsize ? minsize : *size;
57 bool needrealloc = newsize > cap;
59 // reallocate if possible
60 if (needrealloc) {
61 // a reallocator and a capacity variable must be available
62 if (reallocator == NULL || capacity == NULL) {
63 return CX_ARRAY_COPY_REALLOC_NOT_SUPPORTED;
64 }
66 // check, if we need to repair the src pointer
67 uintptr_t targetaddr = (uintptr_t) *target;
68 uintptr_t srcaddr = (uintptr_t) src;
69 bool repairsrc = targetaddr <= srcaddr
70 && srcaddr < targetaddr + cap * elem_size;
72 // calculate new capacity (next number divisible by 16)
73 cap = newsize - (newsize % 16) + 16;
74 assert(cap > newsize);
76 // perform reallocation
77 void *newmem = reallocator->realloc(
78 *target, cap, elem_size, reallocator
79 );
80 if (newmem == NULL) {
81 return CX_ARRAY_COPY_REALLOC_FAILED;
82 }
84 // repair src pointer, if necessary
85 if (repairsrc) {
86 src = ((char *) newmem) + (srcaddr - targetaddr);
87 }
89 // store new pointer and capacity
90 *target = newmem;
91 *capacity = cap;
92 }
94 // determine target pointer
95 char *start = *target;
96 start += index * elem_size;
98 // copy elements and set new size
99 memmove(start, src, elem_count * elem_size);
100 *size = newsize;
102 // return successfully
103 return CX_ARRAY_COPY_SUCCESS;
104 }
106 #ifndef CX_ARRAY_SWAP_SBO_SIZE
107 #define CX_ARRAY_SWAP_SBO_SIZE 128
108 #endif
110 void cx_array_swap(
111 void *arr,
112 size_t elem_size,
113 size_t idx1,
114 size_t idx2
115 ) {
116 assert(arr != NULL);
118 // short circuit
119 if (idx1 == idx2) return;
121 char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE];
122 void *tmp;
124 // decide if we can use the local buffer
125 if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) {
126 tmp = malloc(elem_size);
127 // we don't want to enforce error handling
128 if (tmp == NULL) abort();
129 } else {
130 tmp = sbo_mem;
131 }
133 // calculate memory locations
134 char *left = arr, *right = arr;
135 left += idx1 * elem_size;
136 right += idx2 * elem_size;
138 // three-way swap
139 memcpy(tmp, left, elem_size);
140 memcpy(left, right, elem_size);
141 memcpy(right, tmp, elem_size);
143 // free dynamic memory, if it was needed
144 if (tmp != sbo_mem) {
145 free(tmp);
146 }
147 }
149 // HIGH LEVEL ARRAY LIST FUNCTIONS
151 typedef struct {
152 struct cx_list_s base;
153 void *data;
154 size_t capacity;
155 struct cx_array_reallocator_s reallocator;
156 } cx_array_list;
158 static void *cx_arl_realloc(
159 void *array,
160 size_t capacity,
161 size_t elem_size,
162 struct cx_array_reallocator_s *alloc
163 ) {
164 // retrieve the pointer to the list allocator
165 CxAllocator const *al = alloc->ptr1;
167 // use the list allocator to reallocate the memory
168 return cxRealloc(al, array, capacity * elem_size);
169 }
171 static void cx_arl_destructor(struct cx_list_s *list) {
172 cx_array_list *arl = (cx_array_list *) list;
174 char *ptr = arl->data;
176 if (list->simple_destructor) {
177 for (size_t i = 0; i < list->size; i++) {
178 cx_invoke_simple_destructor(list, ptr);
179 ptr += list->item_size;
180 }
181 }
182 if (list->advanced_destructor) {
183 for (size_t i = 0; i < list->size; i++) {
184 cx_invoke_advanced_destructor(list, ptr);
185 ptr += list->item_size;
186 }
187 }
189 cxFree(list->allocator, arl->data);
190 cxFree(list->allocator, list);
191 }
193 static size_t cx_arl_insert_array(
194 struct cx_list_s *list,
195 size_t index,
196 void const *array,
197 size_t n
198 ) {
199 // out of bounds and special case check
200 if (index > list->size || n == 0) return 0;
202 // get a correctly typed pointer to the list
203 cx_array_list *arl = (cx_array_list *) list;
205 // do we need to move some elements?
206 if (index < list->size) {
207 char const *first_to_move = (char const *) arl->data;
208 first_to_move += index * list->item_size;
209 size_t elems_to_move = list->size - index;
210 size_t start_of_moved = index + n;
212 if (CX_ARRAY_COPY_SUCCESS != cx_array_copy(
213 &arl->data,
214 &list->size,
215 &arl->capacity,
216 start_of_moved,
217 first_to_move,
218 list->item_size,
219 elems_to_move,
220 &arl->reallocator
221 )) {
222 // if moving existing elems is unsuccessful, abort
223 return 0;
224 }
225 }
227 // note that if we had to move the elements, the following operation
228 // is guaranteed to succeed, because we have the memory already allocated
229 // therefore, it is impossible to leave this function with an invalid array
231 // place the new elements
232 if (CX_ARRAY_COPY_SUCCESS == cx_array_copy(
233 &arl->data,
234 &list->size,
235 &arl->capacity,
236 index,
237 array,
238 list->item_size,
239 n,
240 &arl->reallocator
241 )) {
242 return n;
243 } else {
244 // array list implementation is "all or nothing"
245 return 0;
246 }
247 }
249 static int cx_arl_insert_element(
250 struct cx_list_s *list,
251 size_t index,
252 void const *element
253 ) {
254 return 1 != cx_arl_insert_array(list, index, element, 1);
255 }
257 static int cx_arl_insert_iter(
258 struct cx_mut_iterator_s *iter,
259 void const *elem,
260 int prepend
261 ) {
262 struct cx_list_s *list = iter->src_handle;
263 if (iter->index < list->size) {
264 int result = cx_arl_insert_element(
265 list,
266 iter->index + 1 - prepend,
267 elem
268 );
269 if (result == 0 && prepend != 0) {
270 iter->index++;
271 iter->elem_handle = ((char *) iter->elem_handle) + list->item_size;
272 }
273 return result;
274 } else {
275 int result = cx_arl_insert_element(list, list->size, elem);
276 iter->index = list->size;
277 return result;
278 }
279 }
281 static int cx_arl_remove(
282 struct cx_list_s *list,
283 size_t index
284 ) {
285 cx_array_list *arl = (cx_array_list *) list;
287 // out-of-bounds check
288 if (index >= list->size) {
289 return 1;
290 }
292 // content destruction
293 cx_invoke_destructor(list, ((char *) arl->data) + index * list->item_size);
295 // short-circuit removal of last element
296 if (index == list->size - 1) {
297 list->size--;
298 return 0;
299 }
301 // just move the elements starting at index to the left
302 int result = cx_array_copy(
303 &arl->data,
304 &list->size,
305 &arl->capacity,
306 index,
307 ((char *) arl->data) + (index + 1) * list->item_size,
308 list->item_size,
309 list->size - index - 1,
310 &arl->reallocator
311 );
312 if (result == 0) {
313 // decrease the size
314 list->size--;
315 }
316 return result;
317 }
319 static void cx_arl_clear(struct cx_list_s *list) {
320 if (list->size == 0) return;
322 cx_array_list *arl = (cx_array_list *) list;
323 char *ptr = arl->data;
325 if (list->simple_destructor) {
326 for (size_t i = 0; i < list->size; i++) {
327 cx_invoke_simple_destructor(list, ptr);
328 ptr += list->item_size;
329 }
330 }
331 if (list->advanced_destructor) {
332 for (size_t i = 0; i < list->size; i++) {
333 cx_invoke_advanced_destructor(list, ptr);
334 ptr += list->item_size;
335 }
336 }
338 memset(arl->data, 0, list->size * list->item_size);
339 list->size = 0;
340 }
342 static int cx_arl_swap(
343 struct cx_list_s *list,
344 size_t i,
345 size_t j
346 ) {
347 if (i >= list->size || j >= list->size) return 1;
348 cx_array_list *arl = (cx_array_list *) list;
349 cx_array_swap(arl->data, list->item_size, i, j);
350 return 0;
351 }
353 static void *cx_arl_at(
354 struct cx_list_s const *list,
355 size_t index
356 ) {
357 if (index < list->size) {
358 cx_array_list const *arl = (cx_array_list const *) list;
359 char *space = arl->data;
360 return space + index * list->item_size;
361 } else {
362 return NULL;
363 }
364 }
366 static ssize_t cx_arl_find_remove(
367 struct cx_list_s *list,
368 void const *elem,
369 bool remove
370 ) {
371 assert(list->cmpfunc != NULL);
372 assert(list->size < SIZE_MAX / 2);
373 char *cur = ((cx_array_list const *) list)->data;
375 for (ssize_t i = 0; i < (ssize_t) list->size; i++) {
376 if (0 == list->cmpfunc(elem, cur)) {
377 if (remove) {
378 if (0 == cx_arl_remove(list, i)) {
379 return i;
380 } else {
381 return -1;
382 }
383 } else {
384 return i;
385 }
386 }
387 cur += list->item_size;
388 }
390 return -1;
391 }
393 static void cx_arl_sort(struct cx_list_s *list) {
394 assert(list->cmpfunc != NULL);
395 qsort(((cx_array_list *) list)->data,
396 list->size,
397 list->item_size,
398 list->cmpfunc
399 );
400 }
402 static int cx_arl_compare(
403 struct cx_list_s const *list,
404 struct cx_list_s const *other
405 ) {
406 assert(list->cmpfunc != NULL);
407 if (list->size == other->size) {
408 char const *left = ((cx_array_list const *) list)->data;
409 char const *right = ((cx_array_list const *) other)->data;
410 for (size_t i = 0; i < list->size; i++) {
411 int d = list->cmpfunc(left, right);
412 if (d != 0) {
413 return d;
414 }
415 left += list->item_size;
416 right += other->item_size;
417 }
418 return 0;
419 } else {
420 return list->size < other->size ? -1 : 1;
421 }
422 }
424 static void cx_arl_reverse(struct cx_list_s *list) {
425 if (list->size < 2) return;
426 void *data = ((cx_array_list const *) list)->data;
427 size_t half = list->size / 2;
428 for (size_t i = 0; i < half; i++) {
429 cx_array_swap(data, list->item_size, i, list->size - 1 - i);
430 }
431 }
433 static bool cx_arl_iter_valid(void const *it) {
434 struct cx_iterator_s const *iter = it;
435 struct cx_list_s const *list = iter->src_handle;
436 return iter->index < list->size;
437 }
439 static void *cx_arl_iter_current(void const *it) {
440 struct cx_iterator_s const *iter = it;
441 return iter->elem_handle;
442 }
444 static void cx_arl_iter_next(void *it) {
445 struct cx_iterator_base_s *itbase = it;
446 if (itbase->remove) {
447 struct cx_mut_iterator_s *iter = it;
448 itbase->remove = false;
449 cx_arl_remove(iter->src_handle, iter->index);
450 } else {
451 struct cx_iterator_s *iter = it;
452 iter->index++;
453 iter->elem_handle =
454 ((char *) iter->elem_handle)
455 + ((struct cx_list_s const *) iter->src_handle)->item_size;
456 }
457 }
459 static void cx_arl_iter_prev(void *it) {
460 struct cx_iterator_base_s *itbase = it;
461 struct cx_mut_iterator_s *iter = it;
462 cx_array_list *const list = iter->src_handle;
463 if (itbase->remove) {
464 itbase->remove = false;
465 cx_arl_remove(iter->src_handle, iter->index);
466 }
467 iter->index--;
468 if (iter->index < list->base.size) {
469 iter->elem_handle = ((char *) list->data)
470 + iter->index * list->base.item_size;
471 }
472 }
474 static bool cx_arl_iter_flag_rm(void *it) {
475 struct cx_iterator_base_s *iter = it;
476 if (iter->mutating) {
477 iter->remove = true;
478 return true;
479 } else {
480 return false;
481 }
482 }
484 static struct cx_iterator_s cx_arl_iterator(
485 struct cx_list_s const *list,
486 size_t index,
487 bool backwards
488 ) {
489 struct cx_iterator_s iter;
491 iter.index = index;
492 iter.src_handle = list;
493 iter.elem_handle = cx_arl_at(list, index);
494 iter.base.valid = cx_arl_iter_valid;
495 iter.base.current = cx_arl_iter_current;
496 iter.base.next = backwards ? cx_arl_iter_prev : cx_arl_iter_next;
497 iter.base.flag_removal = cx_arl_iter_flag_rm;
498 iter.base.remove = false;
499 iter.base.mutating = false;
501 return iter;
502 }
504 static cx_list_class cx_array_list_class = {
505 cx_arl_destructor,
506 cx_arl_insert_element,
507 cx_arl_insert_array,
508 cx_arl_insert_iter,
509 cx_arl_remove,
510 cx_arl_clear,
511 cx_arl_swap,
512 cx_arl_at,
513 cx_arl_find_remove,
514 cx_arl_sort,
515 cx_arl_compare,
516 cx_arl_reverse,
517 cx_arl_iterator,
518 };
520 CxList *cxArrayListCreate(
521 CxAllocator const *allocator,
522 cx_compare_func comparator,
523 size_t item_size,
524 size_t initial_capacity
525 ) {
526 if (allocator == NULL) {
527 allocator = cxDefaultAllocator;
528 }
530 cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
531 if (list == NULL) return NULL;
533 list->base.cl = &cx_array_list_class;
534 list->base.allocator = allocator;
535 list->capacity = initial_capacity;
537 if (item_size > 0) {
538 list->base.item_size = item_size;
539 list->base.cmpfunc = comparator;
540 } else {
541 item_size = sizeof(void *);
542 list->base.cmpfunc = comparator == NULL ? cx_cmp_ptr : comparator;
543 cxListStorePointers((CxList *) list);
544 }
546 // allocate the array after the real item_size is known
547 list->data = cxCalloc(allocator, initial_capacity, item_size);
548 if (list->data == NULL) {
549 cxFree(allocator, list);
550 return NULL;
551 }
553 // configure the reallocator
554 list->reallocator.realloc = cx_arl_realloc;
555 list->reallocator.ptr1 = (void *) allocator;
557 return (CxList *) list;
558 }