Sun, 20 Nov 2022 21:08:36 +0100
use //-style single line comments everywhere
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 int cx_arl_insert(
189 struct cx_list_s *list,
190 size_t index,
191 void const *elem
192 ) {
193 if (index > list->size) {
194 return 1;
195 } else if (index == list->size) {
196 return cx_arl_add(list, elem);
197 } else {
198 cx_array_list *arl = (cx_array_list *) list;
200 // move elements starting at index to the right
201 if (cx_array_copy(
202 &arl->data,
203 &list->size,
204 &list->capacity,
205 index + 1,
206 ((char *) arl->data) + index * list->itemsize,
207 list->itemsize,
208 list->size - index,
209 &arl->reallocator
210 )) {
211 return 1;
212 }
214 // place the element
215 memcpy(((char *) arl->data) + index * list->itemsize,
216 elem, list->itemsize);
218 return 0;
219 }
220 }
222 static int cx_arl_insert_iter(
223 struct cx_iterator_s *iter,
224 void const *elem,
225 int prepend
226 ) {
227 struct cx_list_s *list = iter->src_handle;
228 if (iter->index < list->size) {
229 int result = cx_arl_insert(
230 list,
231 iter->index + 1 - prepend,
232 elem
233 );
234 if (result == 0 && prepend != 0) {
235 iter->index++;
236 iter->elem_handle = ((char *) iter->elem_handle) + list->itemsize;
237 }
238 return result;
239 } else {
240 int result = cx_arl_add(list, elem);
241 iter->index = list->size;
242 return result;
243 }
244 }
246 static int cx_arl_remove(
247 struct cx_list_s *list,
248 size_t index
249 ) {
250 // out-of-bounds check
251 if (index >= list->size) {
252 return 1;
253 }
255 // short-circuit removal of last element
256 if (index == list->size - 1) {
257 list->size--;
258 return 0;
259 }
261 // just move the elements starting at index to the left
262 cx_array_list *arl = (cx_array_list *) list;
263 int result = cx_array_copy(
264 &arl->data,
265 &list->size,
266 &list->capacity,
267 index,
268 ((char *) arl->data) + (index + 1) * list->itemsize,
269 list->itemsize,
270 list->size - index - 1,
271 &arl->reallocator
272 );
273 if (result == 0) {
274 // decrease the size
275 list->size--;
276 }
277 return result;
278 }
280 static void *cx_arl_at(
281 struct cx_list_s const *list,
282 size_t index
283 ) {
284 if (index < list->size) {
285 cx_array_list const *arl = (cx_array_list const *) list;
286 char *space = arl->data;
287 return space + index * list->itemsize;
288 } else {
289 return NULL;
290 }
291 }
293 static size_t cx_arl_find(
294 struct cx_list_s const *list,
295 void const *elem
296 ) {
297 char *cur = ((cx_array_list const *) list)->data;
299 for (size_t i = 0; i < list->size; i++) {
300 if (0 == list->cmpfunc(elem, cur)) {
301 return i;
302 }
303 cur += list->itemsize;
304 }
306 return list->size;
307 }
309 static void cx_arl_sort(struct cx_list_s *list) {
310 qsort(((cx_array_list *) list)->data,
311 list->size,
312 list->itemsize,
313 list->cmpfunc
314 );
315 }
317 static int cx_arl_compare(
318 struct cx_list_s const *list,
319 struct cx_list_s const *other
320 ) {
321 if (list->size == other->size) {
322 char const *left = ((cx_array_list const *) list)->data;
323 char const *right = ((cx_array_list const *) other)->data;
324 for (size_t i = 0; i < list->size; i++) {
325 int d = list->cmpfunc(left, right);
326 if (d != 0) {
327 return d;
328 }
329 left += list->itemsize;
330 right += other->itemsize;
331 }
332 return 0;
333 } else {
334 return list->size < other->size ? -1 : 1;
335 }
336 }
338 static void cx_arl_reverse(struct cx_list_s *list) {
339 if (list->size < 2) return;
340 void *data = ((cx_array_list const *) list)->data;
341 size_t half = list->size / 2;
342 for (size_t i = 0; i < half; i++) {
343 cx_array_swap(data, list->itemsize, i, list->size - 1 - i);
344 }
345 }
347 static bool cx_arl_iter_valid(struct cx_iterator_s const *iter) {
348 struct cx_list_s const *list = iter->src_handle;
349 return iter->index < list->size;
350 }
352 static void *cx_arl_iter_current(struct cx_iterator_s const *iter) {
353 return iter->elem_handle;
354 }
356 static void cx_arl_iter_next(struct cx_iterator_s *iter) {
357 if (iter->remove) {
358 iter->remove = false;
359 cx_arl_remove(iter->src_handle, iter->index);
360 } else {
361 iter->index++;
362 iter->elem_handle =
363 ((char *) iter->elem_handle)
364 + ((struct cx_list_s const *) iter->src_handle)->itemsize;
365 }
366 }
368 static struct cx_iterator_s cx_arl_iterator(
369 struct cx_list_s *list,
370 size_t index
371 ) {
372 struct cx_iterator_s iter;
374 iter.index = index;
375 iter.src_handle = list;
376 iter.elem_handle = cx_arl_at(list, index);
377 iter.valid = cx_arl_iter_valid;
378 iter.current = cx_arl_iter_current;
379 iter.next = cx_arl_iter_next;
380 iter.remove = false;
382 return iter;
383 }
385 static cx_list_class cx_array_list_class = {
386 cx_arl_destructor,
387 cx_arl_add,
388 cx_arl_insert,
389 cx_arl_insert_iter,
390 cx_arl_remove,
391 cx_arl_at,
392 cx_arl_find,
393 cx_arl_sort,
394 cx_arl_compare,
395 cx_arl_reverse,
396 cx_arl_iterator,
397 };
399 CxList *cxArrayListCreate(
400 CxAllocator const *allocator,
401 CxListComparator comparator,
402 size_t item_size,
403 size_t initial_capacity
404 ) {
405 cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
406 if (list == NULL) return NULL;
408 list->data = cxCalloc(allocator, initial_capacity, item_size);
409 if (list->data == NULL) {
410 cxFree(allocator, list);
411 return NULL;
412 }
414 list->base.cl = &cx_array_list_class;
415 list->base.allocator = allocator;
416 list->base.cmpfunc = comparator;
417 list->base.itemsize = item_size;
418 list->base.capacity = initial_capacity;
420 // configure the reallocator
421 list->reallocator.realloc = cx_arl_realloc;
422 list->reallocator.ptr1 = (void *) allocator;
424 return (CxList *) list;
425 }