Mon, 23 Jan 2023 20:22:11 +0100
add cxListInsertArray() - fixes #224
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 <assert.h>
31 #include <string.h>
32 #include <stdint.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 #define CX_ARRAY_SWAP_SBO_SIZE 512
108 void cx_array_swap(
109 void *arr,
110 size_t elem_size,
111 size_t idx1,
112 size_t idx2
113 ) {
114 // short circuit
115 if (idx1 == idx2) return;
117 char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE];
118 void *tmp;
120 // decide if we can use the local buffer
121 if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) {
122 tmp = malloc(elem_size);
123 // we don't want to enforce error handling
124 if (tmp == NULL) abort();
125 } else {
126 tmp = sbo_mem;
127 }
129 // calculate memory locations
130 char *left = arr, *right = arr;
131 left += idx1 * elem_size;
132 right += idx2 * elem_size;
134 // three-way swap
135 memcpy(tmp, left, elem_size);
136 memcpy(left, right, elem_size);
137 memcpy(right, tmp, elem_size);
139 // free dynamic memory, if it was needed
140 if (tmp != sbo_mem) {
141 free(tmp);
142 }
143 }
145 // HIGH LEVEL ARRAY LIST FUNCTIONS
147 typedef struct {
148 struct cx_list_s base;
149 void *data;
150 struct cx_array_reallocator_s reallocator;
151 } cx_array_list;
153 static void *cx_arl_realloc(
154 void *array,
155 size_t capacity,
156 size_t elem_size,
157 struct cx_array_reallocator_s *alloc
158 ) {
159 // retrieve the pointer to the list allocator
160 CxAllocator const *al = alloc->ptr1;
162 // use the list allocator to reallocate the memory
163 return cxRealloc(al, array, capacity * elem_size);
164 }
166 static void cx_arl_destructor(struct cx_list_s *list) {
167 cx_array_list *arl = (cx_array_list *) list;
168 cxFree(list->allocator, arl->data);
169 }
171 static int cx_arl_add(
172 struct cx_list_s *list,
173 void const *elem
174 ) {
175 cx_array_list *arl = (cx_array_list *) list;
176 return cx_array_copy(
177 &arl->data,
178 &list->size,
179 &list->capacity,
180 list->size,
181 elem,
182 list->itemsize,
183 1,
184 &arl->reallocator
185 );
186 }
188 static size_t cx_arl_insert_array(
189 struct cx_list_s *list,
190 size_t index,
191 void const *array,
192 size_t n
193 ) {
194 // out of bounds and special case check
195 if (index > list->size || n == 0) return 0;
197 // get a correctly typed pointer to the list
198 cx_array_list *arl = (cx_array_list *) list;
200 // do we need to move some elements?
201 if (index < list->size) {
202 char const *first_to_move = (char const *) arl->data;
203 first_to_move += index * list->itemsize;
204 size_t elems_to_move = list->size - index;
205 size_t start_of_moved = index + n;
207 if (CX_ARRAY_COPY_SUCCESS != cx_array_copy(
208 &arl->data,
209 &list->size,
210 &list->capacity,
211 start_of_moved,
212 first_to_move,
213 list->itemsize,
214 elems_to_move,
215 &arl->reallocator
216 )) {
217 // if moving existing elems is unsuccessful, abort
218 return 0;
219 }
220 }
222 // note that if we had to move the elements, the following operation
223 // is guaranteed to succeed, because we have the memory already allocated
224 // therefore, it is impossible to leave this function with an invalid array
226 // place the new elements
227 if (CX_ARRAY_COPY_SUCCESS == cx_array_copy(
228 &arl->data,
229 &list->size,
230 &list->capacity,
231 index,
232 array,
233 list->itemsize,
234 n,
235 &arl->reallocator
236 )) {
237 return n;
238 } else {
239 // array list implementation is "all or nothing"
240 return 0;
241 }
242 }
244 static size_t cx_arl_add_array(
245 struct cx_list_s *list,
246 void const *array,
247 size_t n
248 ) {
249 return cx_arl_insert_array(list, list->size, array, n);
250 }
252 static int cx_arl_insert(
253 struct cx_list_s *list,
254 size_t index,
255 void const *elem
256 ) {
257 if (index > list->size) {
258 return 1;
259 } else if (index == list->size) {
260 return cx_arl_add(list, elem);
261 } else {
262 cx_array_list *arl = (cx_array_list *) list;
264 // move elements starting at index to the right
265 if (cx_array_copy(
266 &arl->data,
267 &list->size,
268 &list->capacity,
269 index + 1,
270 ((char *) arl->data) + index * list->itemsize,
271 list->itemsize,
272 list->size - index,
273 &arl->reallocator
274 )) {
275 return 1;
276 }
278 // place the element
279 memcpy(((char *) arl->data) + index * list->itemsize,
280 elem, list->itemsize);
282 return 0;
283 }
284 }
286 static int cx_arl_insert_iter(
287 struct cx_mut_iterator_s *iter,
288 void const *elem,
289 int prepend
290 ) {
291 struct cx_list_s *list = iter->src_handle;
292 if (iter->index < list->size) {
293 int result = cx_arl_insert(
294 list,
295 iter->index + 1 - prepend,
296 elem
297 );
298 if (result == 0 && prepend != 0) {
299 iter->index++;
300 iter->elem_handle = ((char *) iter->elem_handle) + list->itemsize;
301 }
302 return result;
303 } else {
304 int result = cx_arl_add(list, elem);
305 iter->index = list->size;
306 return result;
307 }
308 }
310 static int cx_arl_remove(
311 struct cx_list_s *list,
312 size_t index
313 ) {
314 // out-of-bounds check
315 if (index >= list->size) {
316 return 1;
317 }
319 // short-circuit removal of last element
320 if (index == list->size - 1) {
321 list->size--;
322 return 0;
323 }
325 // just move the elements starting at index to the left
326 cx_array_list *arl = (cx_array_list *) list;
327 int result = cx_array_copy(
328 &arl->data,
329 &list->size,
330 &list->capacity,
331 index,
332 ((char *) arl->data) + (index + 1) * list->itemsize,
333 list->itemsize,
334 list->size - index - 1,
335 &arl->reallocator
336 );
337 if (result == 0) {
338 // decrease the size
339 list->size--;
340 }
341 return result;
342 }
344 static void *cx_arl_at(
345 struct cx_list_s const *list,
346 size_t index
347 ) {
348 if (index < list->size) {
349 cx_array_list const *arl = (cx_array_list const *) list;
350 char *space = arl->data;
351 return space + index * list->itemsize;
352 } else {
353 return NULL;
354 }
355 }
357 static size_t cx_arl_find(
358 struct cx_list_s const *list,
359 void const *elem
360 ) {
361 char *cur = ((cx_array_list const *) list)->data;
363 for (size_t i = 0; i < list->size; i++) {
364 if (0 == list->cmpfunc(elem, cur)) {
365 return i;
366 }
367 cur += list->itemsize;
368 }
370 return list->size;
371 }
373 static void cx_arl_sort(struct cx_list_s *list) {
374 qsort(((cx_array_list *) list)->data,
375 list->size,
376 list->itemsize,
377 list->cmpfunc
378 );
379 }
381 static int cx_arl_compare(
382 struct cx_list_s const *list,
383 struct cx_list_s const *other
384 ) {
385 if (list->size == other->size) {
386 char const *left = ((cx_array_list const *) list)->data;
387 char const *right = ((cx_array_list const *) other)->data;
388 for (size_t i = 0; i < list->size; i++) {
389 int d = list->cmpfunc(left, right);
390 if (d != 0) {
391 return d;
392 }
393 left += list->itemsize;
394 right += other->itemsize;
395 }
396 return 0;
397 } else {
398 return list->size < other->size ? -1 : 1;
399 }
400 }
402 static void cx_arl_reverse(struct cx_list_s *list) {
403 if (list->size < 2) return;
404 void *data = ((cx_array_list const *) list)->data;
405 size_t half = list->size / 2;
406 for (size_t i = 0; i < half; i++) {
407 cx_array_swap(data, list->itemsize, i, list->size - 1 - i);
408 }
409 }
411 static bool cx_arl_iter_valid(void const *it) {
412 struct cx_iterator_s const *iter = it;
413 struct cx_list_s const *list = iter->src_handle;
414 return iter->index < list->size;
415 }
417 static void *cx_arl_iter_current(void const *it) {
418 struct cx_iterator_s const *iter = it;
419 return iter->elem_handle;
420 }
422 static void cx_arl_iter_next(void *it) {
423 struct cx_iterator_base_s *itbase = it;
424 if (itbase->remove) {
425 struct cx_mut_iterator_s *iter = it;
426 itbase->remove = false;
427 cx_arl_remove(iter->src_handle, iter->index);
428 } else {
429 struct cx_iterator_s *iter = it;
430 iter->index++;
431 iter->elem_handle =
432 ((char *) iter->elem_handle)
433 + ((struct cx_list_s const *) iter->src_handle)->itemsize;
434 }
435 }
437 static bool cx_arl_iter_flag_rm(void *it) {
438 struct cx_iterator_base_s *iter = it;
439 if (iter->mutating) {
440 iter->remove = true;
441 return true;
442 } else {
443 return false;
444 }
445 }
447 static struct cx_iterator_s cx_arl_iterator(
448 struct cx_list_s const *list,
449 size_t index
450 ) {
451 struct cx_iterator_s iter;
453 iter.index = index;
454 iter.src_handle = list;
455 iter.elem_handle = cx_arl_at(list, index);
456 iter.base.valid = cx_arl_iter_valid;
457 iter.base.current = cx_arl_iter_current;
458 iter.base.next = cx_arl_iter_next;
459 iter.base.flag_removal = cx_arl_iter_flag_rm;
460 iter.base.remove = false;
461 iter.base.mutating = false;
463 return iter;
464 }
466 static struct cx_mut_iterator_s cx_arl_mut_iterator(
467 struct cx_list_s *list,
468 size_t index
469 ) {
470 CxIterator it = cx_arl_iterator(list, index);
471 it.base.mutating = true;
473 // we know the iterators share the same memory layout
474 CxMutIterator iter;
475 memcpy(&iter, &it, sizeof(CxMutIterator));
476 return iter;
477 }
479 static cx_list_class cx_array_list_class = {
480 cx_arl_destructor,
481 cx_arl_add,
482 cx_arl_add_array,
483 cx_arl_insert,
484 cx_arl_insert_array,
485 cx_arl_insert_iter,
486 cx_arl_remove,
487 cx_arl_at,
488 cx_arl_find,
489 cx_arl_sort,
490 cx_arl_compare,
491 cx_arl_reverse,
492 cx_arl_iterator,
493 cx_arl_mut_iterator,
494 };
496 CxList *cxArrayListCreate(
497 CxAllocator const *allocator,
498 CxListComparator comparator,
499 size_t item_size,
500 size_t initial_capacity
501 ) {
502 cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
503 if (list == NULL) return NULL;
505 list->data = cxCalloc(allocator, initial_capacity, item_size);
506 if (list->data == NULL) {
507 cxFree(allocator, list);
508 return NULL;
509 }
511 list->base.cl = &cx_array_list_class;
512 list->base.allocator = allocator;
513 list->base.cmpfunc = comparator;
514 list->base.itemsize = item_size;
515 list->base.capacity = initial_capacity;
517 // configure the reallocator
518 list->reallocator.realloc = cx_arl_realloc;
519 list->reallocator.ptr1 = (void *) allocator;
521 return (CxList *) list;
522 }