src/array_list.c

Sun, 21 May 2023 14:56:10 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 21 May 2023 14:56:10 +0200
changeset 708
1caed6c9ba68
parent 699
35b2b99ee523
child 735
b686d0c98c62
permissions
-rw-r--r--

fix inconsistent destructor requirements for list and map classes

universe@606 1 /*
universe@606 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@606 3 *
universe@606 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@606 5 *
universe@606 6 * Redistribution and use in source and binary forms, with or without
universe@606 7 * modification, are permitted provided that the following conditions are met:
universe@606 8 *
universe@606 9 * 1. Redistributions of source code must retain the above copyright
universe@606 10 * notice, this list of conditions and the following disclaimer.
universe@606 11 *
universe@606 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@606 13 * notice, this list of conditions and the following disclaimer in the
universe@606 14 * documentation and/or other materials provided with the distribution.
universe@606 15 *
universe@606 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@606 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@606 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@606 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@606 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@606 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@606 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@606 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@606 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@606 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@606 26 * POSSIBILITY OF SUCH DAMAGE.
universe@606 27 */
universe@606 28
universe@606 29 #include "cx/array_list.h"
universe@610 30 #include <assert.h>
universe@610 31 #include <string.h>
universe@606 32
universe@628 33 // LOW LEVEL ARRAY LIST FUNCTIONS
universe@607 34
universe@612 35 enum cx_array_copy_result cx_array_copy(
universe@610 36 void **target,
universe@610 37 size_t *size,
universe@610 38 size_t *capacity,
universe@610 39 size_t index,
universe@610 40 void const *src,
universe@610 41 size_t elem_size,
universe@610 42 size_t elem_count,
universe@610 43 struct cx_array_reallocator_s *reallocator
universe@610 44 ) {
universe@628 45 // assert pointers
universe@610 46 assert(target != NULL);
universe@610 47 assert(size != NULL);
universe@610 48 assert(src != NULL);
universe@607 49
universe@628 50 // determine capacity
universe@610 51 size_t cap = capacity == NULL ? *size : *capacity;
universe@610 52
universe@628 53 // check if resize is required
universe@627 54 size_t minsize = index + elem_count;
universe@627 55 size_t newsize = *size < minsize ? minsize : *size;
universe@610 56 bool needrealloc = newsize > cap;
universe@610 57
universe@628 58 // reallocate if possible
universe@610 59 if (needrealloc) {
universe@628 60 // a reallocator and a capacity variable must be available
universe@610 61 if (reallocator == NULL || capacity == NULL) {
universe@610 62 return CX_ARRAY_COPY_REALLOC_NOT_SUPPORTED;
universe@610 63 }
universe@610 64
universe@628 65 // check, if we need to repair the src pointer
universe@611 66 uintptr_t targetaddr = (uintptr_t) *target;
universe@611 67 uintptr_t srcaddr = (uintptr_t) src;
universe@611 68 bool repairsrc = targetaddr <= srcaddr
universe@611 69 && srcaddr < targetaddr + cap * elem_size;
universe@611 70
universe@628 71 // calculate new capacity (next number divisible by 16)
universe@625 72 cap = newsize - (newsize % 16) + 16;
universe@625 73 assert(cap > newsize);
universe@610 74
universe@628 75 // perform reallocation
universe@610 76 void *newmem = reallocator->realloc(
universe@610 77 *target, cap, elem_size, reallocator
universe@610 78 );
universe@610 79 if (newmem == NULL) {
universe@610 80 return CX_ARRAY_COPY_REALLOC_FAILED;
universe@610 81 }
universe@610 82
universe@628 83 // repair src pointer, if necessary
universe@611 84 if (repairsrc) {
universe@611 85 src = ((char *) newmem) + (srcaddr - targetaddr);
universe@611 86 }
universe@611 87
universe@628 88 // store new pointer and capacity
universe@610 89 *target = newmem;
universe@610 90 *capacity = cap;
universe@610 91 }
universe@610 92
universe@628 93 // determine target pointer
universe@610 94 char *start = *target;
universe@610 95 start += index * elem_size;
universe@610 96
universe@628 97 // copy elements and set new size
universe@611 98 memmove(start, src, elem_count * elem_size);
universe@610 99 *size = newsize;
universe@610 100
universe@628 101 // return successfully
universe@610 102 return CX_ARRAY_COPY_SUCCESS;
universe@610 103 }
universe@607 104
universe@643 105 #ifndef CX_ARRAY_SWAP_SBO_SIZE
universe@623 106 #define CX_ARRAY_SWAP_SBO_SIZE 512
universe@643 107 #endif
universe@623 108
universe@623 109 void cx_array_swap(
universe@623 110 void *arr,
universe@623 111 size_t elem_size,
universe@623 112 size_t idx1,
universe@623 113 size_t idx2
universe@623 114 ) {
universe@660 115 assert(arr != NULL);
universe@660 116
universe@628 117 // short circuit
universe@623 118 if (idx1 == idx2) return;
universe@623 119
universe@623 120 char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE];
universe@623 121 void *tmp;
universe@623 122
universe@628 123 // decide if we can use the local buffer
universe@623 124 if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) {
universe@623 125 tmp = malloc(elem_size);
universe@628 126 // we don't want to enforce error handling
universe@623 127 if (tmp == NULL) abort();
universe@623 128 } else {
universe@623 129 tmp = sbo_mem;
universe@623 130 }
universe@623 131
universe@628 132 // calculate memory locations
universe@623 133 char *left = arr, *right = arr;
universe@623 134 left += idx1 * elem_size;
universe@623 135 right += idx2 * elem_size;
universe@623 136
universe@628 137 // three-way swap
universe@623 138 memcpy(tmp, left, elem_size);
universe@623 139 memcpy(left, right, elem_size);
universe@623 140 memcpy(right, tmp, elem_size);
universe@623 141
universe@628 142 // free dynamic memory, if it was needed
universe@623 143 if (tmp != sbo_mem) {
universe@623 144 free(tmp);
universe@623 145 }
universe@623 146 }
universe@623 147
universe@628 148 // HIGH LEVEL ARRAY LIST FUNCTIONS
universe@607 149
universe@607 150 typedef struct {
universe@607 151 struct cx_list_s base;
universe@607 152 void *data;
universe@677 153 size_t capacity;
universe@610 154 struct cx_array_reallocator_s reallocator;
universe@607 155 } cx_array_list;
universe@607 156
universe@610 157 static void *cx_arl_realloc(
universe@610 158 void *array,
universe@610 159 size_t capacity,
universe@610 160 size_t elem_size,
universe@610 161 struct cx_array_reallocator_s *alloc
universe@610 162 ) {
universe@628 163 // retrieve the pointer to the list allocator
universe@610 164 CxAllocator const *al = alloc->ptr1;
universe@610 165
universe@628 166 // use the list allocator to reallocate the memory
universe@610 167 return cxRealloc(al, array, capacity * elem_size);
universe@610 168 }
universe@610 169
universe@607 170 static void cx_arl_destructor(struct cx_list_s *list) {
universe@610 171 cx_array_list *arl = (cx_array_list *) list;
universe@708 172
universe@708 173 char *ptr = arl->data;
universe@708 174
universe@708 175 if (list->simple_destructor) {
universe@708 176 for (size_t i = 0; i < list->size; i++) {
universe@708 177 cx_invoke_simple_destructor(list, ptr);
universe@708 178 ptr += list->item_size;
universe@708 179 }
universe@708 180 }
universe@708 181 if (list->advanced_destructor) {
universe@708 182 for (size_t i = 0; i < list->size; i++) {
universe@708 183 cx_invoke_advanced_destructor(list, ptr);
universe@708 184 ptr += list->item_size;
universe@708 185 }
universe@708 186 }
universe@708 187
universe@607 188 cxFree(list->allocator, arl->data);
universe@708 189 cxFree(list->allocator, list);
universe@607 190 }
universe@607 191
universe@638 192 static size_t cx_arl_insert_array(
universe@629 193 struct cx_list_s *list,
universe@638 194 size_t index,
universe@629 195 void const *array,
universe@629 196 size_t n
universe@629 197 ) {
universe@638 198 // out of bounds and special case check
universe@638 199 if (index > list->size || n == 0) return 0;
universe@638 200
universe@638 201 // get a correctly typed pointer to the list
universe@629 202 cx_array_list *arl = (cx_array_list *) list;
universe@638 203
universe@638 204 // do we need to move some elements?
universe@638 205 if (index < list->size) {
universe@638 206 char const *first_to_move = (char const *) arl->data;
universe@677 207 first_to_move += index * list->item_size;
universe@638 208 size_t elems_to_move = list->size - index;
universe@638 209 size_t start_of_moved = index + n;
universe@638 210
universe@638 211 if (CX_ARRAY_COPY_SUCCESS != cx_array_copy(
universe@638 212 &arl->data,
universe@638 213 &list->size,
universe@677 214 &arl->capacity,
universe@638 215 start_of_moved,
universe@638 216 first_to_move,
universe@677 217 list->item_size,
universe@638 218 elems_to_move,
universe@638 219 &arl->reallocator
universe@638 220 )) {
universe@638 221 // if moving existing elems is unsuccessful, abort
universe@638 222 return 0;
universe@638 223 }
universe@638 224 }
universe@638 225
universe@638 226 // note that if we had to move the elements, the following operation
universe@638 227 // is guaranteed to succeed, because we have the memory already allocated
universe@638 228 // therefore, it is impossible to leave this function with an invalid array
universe@638 229
universe@638 230 // place the new elements
universe@629 231 if (CX_ARRAY_COPY_SUCCESS == cx_array_copy(
universe@629 232 &arl->data,
universe@629 233 &list->size,
universe@677 234 &arl->capacity,
universe@638 235 index,
universe@629 236 array,
universe@677 237 list->item_size,
universe@629 238 n,
universe@629 239 &arl->reallocator
universe@629 240 )) {
universe@629 241 return n;
universe@629 242 } else {
universe@629 243 // array list implementation is "all or nothing"
universe@629 244 return 0;
universe@629 245 }
universe@629 246 }
universe@629 247
universe@641 248 static int cx_arl_insert_element(
universe@641 249 struct cx_list_s *list,
universe@641 250 size_t index,
universe@641 251 void const *element
universe@641 252 ) {
universe@641 253 return 1 != cx_arl_insert_array(list, index, element, 1);
universe@641 254 }
universe@641 255
universe@607 256 static int cx_arl_insert_iter(
universe@630 257 struct cx_mut_iterator_s *iter,
universe@607 258 void const *elem,
universe@607 259 int prepend
universe@607 260 ) {
universe@619 261 struct cx_list_s *list = iter->src_handle;
universe@619 262 if (iter->index < list->size) {
universe@641 263 int result = cx_arl_insert_element(
universe@619 264 list,
universe@619 265 iter->index + 1 - prepend,
universe@641 266 elem
universe@619 267 );
universe@619 268 if (result == 0 && prepend != 0) {
universe@619 269 iter->index++;
universe@677 270 iter->elem_handle = ((char *) iter->elem_handle) + list->item_size;
universe@619 271 }
universe@619 272 return result;
universe@619 273 } else {
universe@641 274 int result = cx_arl_insert_element(list, list->size, elem);
universe@619 275 iter->index = list->size;
universe@619 276 return result;
universe@619 277 }
universe@607 278 }
universe@607 279
universe@607 280 static int cx_arl_remove(
universe@607 281 struct cx_list_s *list,
universe@607 282 size_t index
universe@607 283 ) {
universe@664 284 cx_array_list *arl = (cx_array_list *) list;
universe@664 285
universe@628 286 // out-of-bounds check
universe@613 287 if (index >= list->size) {
universe@613 288 return 1;
universe@613 289 }
universe@613 290
universe@664 291 // content destruction
universe@678 292 cx_invoke_destructor(list, ((char *) arl->data) + index * list->item_size);
universe@664 293
universe@628 294 // short-circuit removal of last element
universe@624 295 if (index == list->size - 1) {
universe@624 296 list->size--;
universe@624 297 return 0;
universe@624 298 }
universe@613 299
universe@628 300 // just move the elements starting at index to the left
universe@613 301 int result = cx_array_copy(
universe@613 302 &arl->data,
universe@613 303 &list->size,
universe@677 304 &arl->capacity,
universe@613 305 index,
universe@677 306 ((char *) arl->data) + (index + 1) * list->item_size,
universe@677 307 list->item_size,
universe@626 308 list->size - index - 1,
universe@613 309 &arl->reallocator
universe@613 310 );
universe@613 311 if (result == 0) {
universe@628 312 // decrease the size
universe@613 313 list->size--;
universe@613 314 }
universe@613 315 return result;
universe@607 316 }
universe@607 317
universe@664 318 static void cx_arl_clear(struct cx_list_s *list) {
universe@664 319 if (list->size == 0) return;
universe@664 320
universe@664 321 cx_array_list *arl = (cx_array_list *) list;
universe@664 322 char *ptr = arl->data;
universe@664 323
universe@677 324 if (list->simple_destructor) {
universe@677 325 for (size_t i = 0; i < list->size; i++) {
universe@677 326 cx_invoke_simple_destructor(list, ptr);
universe@677 327 ptr += list->item_size;
universe@664 328 }
universe@677 329 }
universe@677 330 if (list->advanced_destructor) {
universe@677 331 for (size_t i = 0; i < list->size; i++) {
universe@677 332 cx_invoke_advanced_destructor(list, ptr);
universe@677 333 ptr += list->item_size;
universe@664 334 }
universe@664 335 }
universe@666 336
universe@677 337 memset(arl->data, 0, list->size * list->item_size);
universe@666 338 list->size = 0;
universe@664 339 }
universe@664 340
universe@647 341 static int cx_arl_swap(
universe@647 342 struct cx_list_s *list,
universe@647 343 size_t i,
universe@647 344 size_t j
universe@647 345 ) {
universe@647 346 if (i >= list->size || j >= list->size) return 1;
universe@647 347 cx_array_list *arl = (cx_array_list *) list;
universe@677 348 cx_array_swap(arl->data, list->item_size, i, j);
universe@647 349 return 0;
universe@647 350 }
universe@647 351
universe@610 352 static void *cx_arl_at(
universe@607 353 struct cx_list_s const *list,
universe@607 354 size_t index
universe@607 355 ) {
universe@610 356 if (index < list->size) {
universe@610 357 cx_array_list const *arl = (cx_array_list const *) list;
universe@610 358 char *space = arl->data;
universe@677 359 return space + index * list->item_size;
universe@610 360 } else {
universe@610 361 return NULL;
universe@610 362 }
universe@607 363 }
universe@607 364
universe@699 365 static ssize_t cx_arl_find(
universe@607 366 struct cx_list_s const *list,
universe@607 367 void const *elem
universe@607 368 ) {
universe@660 369 assert(list->cmpfunc != NULL);
universe@699 370 assert(list->size < SIZE_MAX / 2);
universe@614 371 char *cur = ((cx_array_list const *) list)->data;
universe@614 372
universe@699 373 for (ssize_t i = 0; i < (ssize_t) list->size; i++) {
universe@614 374 if (0 == list->cmpfunc(elem, cur)) {
universe@614 375 return i;
universe@614 376 }
universe@677 377 cur += list->item_size;
universe@614 378 }
universe@614 379
universe@699 380 return -1;
universe@607 381 }
universe@607 382
universe@607 383 static void cx_arl_sort(struct cx_list_s *list) {
universe@660 384 assert(list->cmpfunc != NULL);
universe@615 385 qsort(((cx_array_list *) list)->data,
universe@615 386 list->size,
universe@677 387 list->item_size,
universe@615 388 list->cmpfunc
universe@615 389 );
universe@607 390 }
universe@607 391
universe@607 392 static int cx_arl_compare(
universe@607 393 struct cx_list_s const *list,
universe@607 394 struct cx_list_s const *other
universe@607 395 ) {
universe@660 396 assert(list->cmpfunc != NULL);
universe@622 397 if (list->size == other->size) {
universe@622 398 char const *left = ((cx_array_list const *) list)->data;
universe@622 399 char const *right = ((cx_array_list const *) other)->data;
universe@622 400 for (size_t i = 0; i < list->size; i++) {
universe@622 401 int d = list->cmpfunc(left, right);
universe@622 402 if (d != 0) {
universe@622 403 return d;
universe@622 404 }
universe@677 405 left += list->item_size;
universe@677 406 right += other->item_size;
universe@622 407 }
universe@622 408 return 0;
universe@622 409 } else {
universe@622 410 return list->size < other->size ? -1 : 1;
universe@622 411 }
universe@607 412 }
universe@607 413
universe@607 414 static void cx_arl_reverse(struct cx_list_s *list) {
universe@623 415 if (list->size < 2) return;
universe@623 416 void *data = ((cx_array_list const *) list)->data;
universe@623 417 size_t half = list->size / 2;
universe@623 418 for (size_t i = 0; i < half; i++) {
universe@677 419 cx_array_swap(data, list->item_size, i, list->size - 1 - i);
universe@623 420 }
universe@607 421 }
universe@607 422
universe@630 423 static bool cx_arl_iter_valid(void const *it) {
universe@630 424 struct cx_iterator_s const *iter = it;
universe@616 425 struct cx_list_s const *list = iter->src_handle;
universe@616 426 return iter->index < list->size;
universe@616 427 }
universe@616 428
universe@630 429 static void *cx_arl_iter_current(void const *it) {
universe@630 430 struct cx_iterator_s const *iter = it;
universe@616 431 return iter->elem_handle;
universe@616 432 }
universe@616 433
universe@630 434 static void cx_arl_iter_next(void *it) {
universe@630 435 struct cx_iterator_base_s *itbase = it;
universe@630 436 if (itbase->remove) {
universe@630 437 struct cx_mut_iterator_s *iter = it;
universe@630 438 itbase->remove = false;
universe@616 439 cx_arl_remove(iter->src_handle, iter->index);
universe@616 440 } else {
universe@630 441 struct cx_iterator_s *iter = it;
universe@616 442 iter->index++;
universe@620 443 iter->elem_handle =
universe@620 444 ((char *) iter->elem_handle)
universe@677 445 + ((struct cx_list_s const *) iter->src_handle)->item_size;
universe@616 446 }
universe@616 447 }
universe@616 448
universe@655 449 static void cx_arl_iter_prev(void *it) {
universe@655 450 struct cx_iterator_base_s *itbase = it;
universe@655 451 struct cx_mut_iterator_s *iter = it;
universe@655 452 cx_array_list *const list = iter->src_handle;
universe@655 453 if (itbase->remove) {
universe@655 454 itbase->remove = false;
universe@655 455 cx_arl_remove(iter->src_handle, iter->index);
universe@655 456 }
universe@655 457 iter->index--;
universe@655 458 if (iter->index < list->base.size) {
universe@655 459 iter->elem_handle = ((char *) list->data)
universe@677 460 + iter->index * list->base.item_size;
universe@655 461 }
universe@655 462 }
universe@655 463
universe@630 464 static bool cx_arl_iter_flag_rm(void *it) {
universe@630 465 struct cx_iterator_base_s *iter = it;
universe@630 466 if (iter->mutating) {
universe@630 467 iter->remove = true;
universe@630 468 return true;
universe@630 469 } else {
universe@630 470 return false;
universe@630 471 }
universe@630 472 }
universe@630 473
universe@607 474 static struct cx_iterator_s cx_arl_iterator(
universe@630 475 struct cx_list_s const *list,
universe@655 476 size_t index,
universe@655 477 bool backwards
universe@607 478 ) {
universe@607 479 struct cx_iterator_s iter;
universe@607 480
universe@616 481 iter.index = index;
universe@616 482 iter.src_handle = list;
universe@616 483 iter.elem_handle = cx_arl_at(list, index);
universe@630 484 iter.base.valid = cx_arl_iter_valid;
universe@630 485 iter.base.current = cx_arl_iter_current;
universe@655 486 iter.base.next = backwards ? cx_arl_iter_prev : cx_arl_iter_next;
universe@630 487 iter.base.flag_removal = cx_arl_iter_flag_rm;
universe@630 488 iter.base.remove = false;
universe@630 489 iter.base.mutating = false;
universe@616 490
universe@607 491 return iter;
universe@607 492 }
universe@607 493
universe@607 494 static cx_list_class cx_array_list_class = {
universe@607 495 cx_arl_destructor,
universe@641 496 cx_arl_insert_element,
universe@638 497 cx_arl_insert_array,
universe@607 498 cx_arl_insert_iter,
universe@607 499 cx_arl_remove,
universe@664 500 cx_arl_clear,
universe@647 501 cx_arl_swap,
universe@607 502 cx_arl_at,
universe@607 503 cx_arl_find,
universe@607 504 cx_arl_sort,
universe@607 505 cx_arl_compare,
universe@607 506 cx_arl_reverse,
universe@607 507 cx_arl_iterator,
universe@607 508 };
universe@607 509
universe@670 510 CxList *cxArrayListCreate(
universe@606 511 CxAllocator const *allocator,
universe@677 512 cx_compare_func comparator,
universe@606 513 size_t item_size,
universe@606 514 size_t initial_capacity
universe@606 515 ) {
universe@670 516 if (allocator == NULL) {
universe@670 517 allocator = cxDefaultAllocator;
universe@670 518 }
universe@670 519
universe@607 520 cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
universe@607 521 if (list == NULL) return NULL;
universe@607 522
universe@607 523 list->base.cl = &cx_array_list_class;
universe@607 524 list->base.allocator = allocator;
universe@607 525 list->base.cmpfunc = comparator;
universe@677 526 list->capacity = initial_capacity;
universe@607 527
universe@667 528 if (item_size > 0) {
universe@677 529 list->base.item_size = item_size;
universe@667 530 } else {
universe@678 531 item_size = sizeof(void *);
universe@667 532 cxListStorePointers((CxList *) list);
universe@667 533 }
universe@667 534
universe@676 535 // allocate the array after the real item_size is known
universe@676 536 list->data = cxCalloc(allocator, initial_capacity, item_size);
universe@676 537 if (list->data == NULL) {
universe@676 538 cxFree(allocator, list);
universe@676 539 return NULL;
universe@676 540 }
universe@676 541
universe@628 542 // configure the reallocator
universe@610 543 list->reallocator.realloc = cx_arl_realloc;
universe@610 544 list->reallocator.ptr1 = (void *) allocator;
universe@610 545
universe@607 546 return (CxList *) list;
universe@606 547 }

mercurial