Sun, 20 Nov 2022 17:06:00 +0100
#219: cx_arl_remove short-circuit for last element
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 newsize = index + elem_count;
56 bool needrealloc = newsize > cap;
58 /* reallocate if possible */
59 if (needrealloc) {
60 /* a reallocator and a capacity variable must be available */
61 if (reallocator == NULL || capacity == NULL) {
62 return CX_ARRAY_COPY_REALLOC_NOT_SUPPORTED;
63 }
65 /* check, if we need to repair the src pointer */
66 uintptr_t targetaddr = (uintptr_t) *target;
67 uintptr_t srcaddr = (uintptr_t) src;
68 bool repairsrc = targetaddr <= srcaddr
69 && srcaddr < targetaddr + cap * elem_size;
71 /* increase capacity linearly */
72 cap += 16;
74 /* perform reallocation */
75 void *newmem = reallocator->realloc(
76 *target, cap, elem_size, reallocator
77 );
78 if (newmem == NULL) {
79 return CX_ARRAY_COPY_REALLOC_FAILED;
80 }
82 /* repair src pointer, if necessary */
83 if (repairsrc) {
84 src = ((char *) newmem) + (srcaddr - targetaddr);
85 }
87 /* store new pointer and capacity */
88 *target = newmem;
89 *capacity = cap;
90 }
92 /* determine target pointer */
93 char *start = *target;
94 start += index * elem_size;
96 /* copy elements and set new size */
97 memmove(start, src, elem_count * elem_size);
98 *size = newsize;
100 /* return successfully */
101 return CX_ARRAY_COPY_SUCCESS;
102 }
104 #define CX_ARRAY_SWAP_SBO_SIZE 512
106 void cx_array_swap(
107 void *arr,
108 size_t elem_size,
109 size_t idx1,
110 size_t idx2
111 ) {
112 /* short circuit */
113 if (idx1 == idx2) return;
115 char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE];
116 void *tmp;
118 /* decide if we can use the local buffer */
119 if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) {
120 tmp = malloc(elem_size);
121 /* we don't want to enforce error handling */
122 if (tmp == NULL) abort();
123 } else {
124 tmp = sbo_mem;
125 }
127 /* calculate memory locations */
128 char *left = arr, *right = arr;
129 left += idx1 * elem_size;
130 right += idx2 * elem_size;
132 /* three-way swap */
133 memcpy(tmp, left, elem_size);
134 memcpy(left, right, elem_size);
135 memcpy(right, tmp, elem_size);
137 /* free dynamic memory, if it was needed */
138 if (tmp != sbo_mem) {
139 free(tmp);
140 }
141 }
143 /* HIGH LEVEL ARRAY LIST FUNCTIONS */
145 typedef struct {
146 struct cx_list_s base;
147 void *data;
148 struct cx_array_reallocator_s reallocator;
149 } cx_array_list;
151 static void *cx_arl_realloc(
152 void *array,
153 size_t capacity,
154 size_t elem_size,
155 struct cx_array_reallocator_s *alloc
156 ) {
157 /* retrieve the pointer to the list allocator */
158 CxAllocator const *al = alloc->ptr1;
160 /* use the list allocator to reallocate the memory */
161 return cxRealloc(al, array, capacity * elem_size);
162 }
164 static void cx_arl_destructor(struct cx_list_s *list) {
165 cx_array_list *arl = (cx_array_list *) list;
166 cxFree(list->allocator, arl->data);
167 }
169 static int cx_arl_add(
170 struct cx_list_s *list,
171 void const *elem
172 ) {
173 cx_array_list *arl = (cx_array_list *) list;
174 return cx_array_copy(
175 &arl->data,
176 &list->size,
177 &list->capacity,
178 list->size,
179 elem,
180 list->itemsize,
181 1,
182 &arl->reallocator
183 );
184 }
186 static int cx_arl_insert(
187 struct cx_list_s *list,
188 size_t index,
189 void const *elem
190 ) {
191 if (index > list->size) {
192 return 1;
193 } else if (index == list->size) {
194 return cx_arl_add(list, elem);
195 } else {
196 cx_array_list *arl = (cx_array_list *) list;
198 /* move elements starting at index to the right */
199 if (cx_array_copy(
200 &arl->data,
201 &list->size,
202 &list->capacity,
203 index + 1,
204 ((char *) arl->data) + index * list->itemsize,
205 list->itemsize,
206 list->size - index,
207 &arl->reallocator
208 )) {
209 return 1;
210 }
212 /* place the element */
213 memcpy(((char *) arl->data) + index * list->itemsize,
214 elem, list->itemsize);
216 return 0;
217 }
218 }
220 static int cx_arl_insert_iter(
221 struct cx_iterator_s *iter,
222 void const *elem,
223 int prepend
224 ) {
225 struct cx_list_s *list = iter->src_handle;
226 if (iter->index < list->size) {
227 int result = cx_arl_insert(
228 list,
229 iter->index + 1 - prepend,
230 elem
231 );
232 if (result == 0 && prepend != 0) {
233 iter->index++;
234 iter->elem_handle = ((char *) iter->elem_handle) + list->itemsize;
235 }
236 return result;
237 } else {
238 int result = cx_arl_add(list, elem);
239 iter->index = list->size;
240 return result;
241 }
242 }
244 static int cx_arl_remove(
245 struct cx_list_s *list,
246 size_t index
247 ) {
248 /* out-of-bounds check */
249 if (index >= list->size) {
250 return 1;
251 }
253 /* short-circuit removal of last element */
254 if (index == list->size - 1) {
255 list->size--;
256 return 0;
257 }
259 /* just move the elements starting at index to the left */
260 cx_array_list *arl = (cx_array_list *) list;
261 int result = cx_array_copy(
262 &arl->data,
263 &list->size,
264 &list->capacity,
265 index,
266 ((char *) arl->data) + (index + 1) * list->itemsize,
267 list->itemsize,
268 list->size - index,
269 &arl->reallocator
270 );
271 if (result == 0) {
272 /* decrease the size */
273 list->size--;
274 }
275 return result;
276 }
278 static void *cx_arl_at(
279 struct cx_list_s const *list,
280 size_t index
281 ) {
282 if (index < list->size) {
283 cx_array_list const *arl = (cx_array_list const *) list;
284 char *space = arl->data;
285 return space + index * list->itemsize;
286 } else {
287 return NULL;
288 }
289 }
291 static size_t cx_arl_find(
292 struct cx_list_s const *list,
293 void const *elem
294 ) {
295 char *cur = ((cx_array_list const *) list)->data;
297 for (size_t i = 0; i < list->size; i++) {
298 if (0 == list->cmpfunc(elem, cur)) {
299 return i;
300 }
301 cur += list->itemsize;
302 }
304 return list->size;
305 }
307 static void cx_arl_sort(struct cx_list_s *list) {
308 qsort(((cx_array_list *) list)->data,
309 list->size,
310 list->itemsize,
311 list->cmpfunc
312 );
313 }
315 static int cx_arl_compare(
316 struct cx_list_s const *list,
317 struct cx_list_s const *other
318 ) {
319 if (list->size == other->size) {
320 char const *left = ((cx_array_list const *) list)->data;
321 char const *right = ((cx_array_list const *) other)->data;
322 for (size_t i = 0; i < list->size; i++) {
323 int d = list->cmpfunc(left, right);
324 if (d != 0) {
325 return d;
326 }
327 left += list->itemsize;
328 right += other->itemsize;
329 }
330 return 0;
331 } else {
332 return list->size < other->size ? -1 : 1;
333 }
334 }
336 static void cx_arl_reverse(struct cx_list_s *list) {
337 if (list->size < 2) return;
338 void *data = ((cx_array_list const *) list)->data;
339 size_t half = list->size / 2;
340 for (size_t i = 0; i < half; i++) {
341 cx_array_swap(data, list->itemsize, i, list->size - 1 - i);
342 }
343 }
345 static bool cx_arl_iter_valid(struct cx_iterator_s const *iter) {
346 struct cx_list_s const *list = iter->src_handle;
347 return iter->index < list->size;
348 }
350 static void *cx_arl_iter_current(struct cx_iterator_s const *iter) {
351 return iter->elem_handle;
352 }
354 static void cx_arl_iter_next(struct cx_iterator_s *iter) {
355 if (iter->remove) {
356 iter->remove = false;
357 cx_arl_remove(iter->src_handle, iter->index);
358 } else {
359 iter->index++;
360 iter->elem_handle =
361 ((char *) iter->elem_handle)
362 + ((struct cx_list_s const *) iter->src_handle)->itemsize;
363 }
364 }
366 static struct cx_iterator_s cx_arl_iterator(
367 struct cx_list_s *list,
368 size_t index
369 ) {
370 struct cx_iterator_s iter;
372 iter.index = index;
373 iter.src_handle = list;
374 iter.elem_handle = cx_arl_at(list, index);
375 iter.valid = cx_arl_iter_valid;
376 iter.current = cx_arl_iter_current;
377 iter.next = cx_arl_iter_next;
378 iter.remove = false;
380 return iter;
381 }
383 static cx_list_class cx_array_list_class = {
384 cx_arl_destructor,
385 cx_arl_add,
386 cx_arl_insert,
387 cx_arl_insert_iter,
388 cx_arl_remove,
389 cx_arl_at,
390 cx_arl_find,
391 cx_arl_sort,
392 cx_arl_compare,
393 cx_arl_reverse,
394 cx_arl_iterator,
395 };
397 CxList *cxArrayListCreate(
398 CxAllocator const *allocator,
399 CxListComparator comparator,
400 size_t item_size,
401 size_t initial_capacity
402 ) {
403 cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
404 if (list == NULL) return NULL;
406 list->data = cxCalloc(allocator, initial_capacity, item_size);
407 if (list->data == NULL) {
408 cxFree(allocator, list);
409 return NULL;
410 }
412 list->base.cl = &cx_array_list_class;
413 list->base.allocator = allocator;
414 list->base.cmpfunc = comparator;
415 list->base.itemsize = item_size;
416 list->base.capacity = initial_capacity;
418 /* configure the reallocator */
419 list->reallocator.realloc = cx_arl_realloc;
420 list->reallocator.ptr1 = (void *) allocator;
422 return (CxList *) list;
423 }