Mon, 23 Jan 2023 20:22:11 +0100
add cxListInsertArray() - fixes #224
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@611 | 32 | #include <stdint.h> |
universe@606 | 33 | |
universe@628 | 34 | // LOW LEVEL ARRAY LIST FUNCTIONS |
universe@607 | 35 | |
universe@612 | 36 | enum cx_array_copy_result cx_array_copy( |
universe@610 | 37 | void **target, |
universe@610 | 38 | size_t *size, |
universe@610 | 39 | size_t *capacity, |
universe@610 | 40 | size_t index, |
universe@610 | 41 | void const *src, |
universe@610 | 42 | size_t elem_size, |
universe@610 | 43 | size_t elem_count, |
universe@610 | 44 | struct cx_array_reallocator_s *reallocator |
universe@610 | 45 | ) { |
universe@628 | 46 | // assert pointers |
universe@610 | 47 | assert(target != NULL); |
universe@610 | 48 | assert(size != NULL); |
universe@610 | 49 | assert(src != NULL); |
universe@607 | 50 | |
universe@628 | 51 | // determine capacity |
universe@610 | 52 | size_t cap = capacity == NULL ? *size : *capacity; |
universe@610 | 53 | |
universe@628 | 54 | // check if resize is required |
universe@627 | 55 | size_t minsize = index + elem_count; |
universe@627 | 56 | size_t newsize = *size < minsize ? minsize : *size; |
universe@610 | 57 | bool needrealloc = newsize > cap; |
universe@610 | 58 | |
universe@628 | 59 | // reallocate if possible |
universe@610 | 60 | if (needrealloc) { |
universe@628 | 61 | // a reallocator and a capacity variable must be available |
universe@610 | 62 | if (reallocator == NULL || capacity == NULL) { |
universe@610 | 63 | return CX_ARRAY_COPY_REALLOC_NOT_SUPPORTED; |
universe@610 | 64 | } |
universe@610 | 65 | |
universe@628 | 66 | // check, if we need to repair the src pointer |
universe@611 | 67 | uintptr_t targetaddr = (uintptr_t) *target; |
universe@611 | 68 | uintptr_t srcaddr = (uintptr_t) src; |
universe@611 | 69 | bool repairsrc = targetaddr <= srcaddr |
universe@611 | 70 | && srcaddr < targetaddr + cap * elem_size; |
universe@611 | 71 | |
universe@628 | 72 | // calculate new capacity (next number divisible by 16) |
universe@625 | 73 | cap = newsize - (newsize % 16) + 16; |
universe@625 | 74 | assert(cap > newsize); |
universe@610 | 75 | |
universe@628 | 76 | // perform reallocation |
universe@610 | 77 | void *newmem = reallocator->realloc( |
universe@610 | 78 | *target, cap, elem_size, reallocator |
universe@610 | 79 | ); |
universe@610 | 80 | if (newmem == NULL) { |
universe@610 | 81 | return CX_ARRAY_COPY_REALLOC_FAILED; |
universe@610 | 82 | } |
universe@610 | 83 | |
universe@628 | 84 | // repair src pointer, if necessary |
universe@611 | 85 | if (repairsrc) { |
universe@611 | 86 | src = ((char *) newmem) + (srcaddr - targetaddr); |
universe@611 | 87 | } |
universe@611 | 88 | |
universe@628 | 89 | // store new pointer and capacity |
universe@610 | 90 | *target = newmem; |
universe@610 | 91 | *capacity = cap; |
universe@610 | 92 | } |
universe@610 | 93 | |
universe@628 | 94 | // determine target pointer |
universe@610 | 95 | char *start = *target; |
universe@610 | 96 | start += index * elem_size; |
universe@610 | 97 | |
universe@628 | 98 | // copy elements and set new size |
universe@611 | 99 | memmove(start, src, elem_count * elem_size); |
universe@610 | 100 | *size = newsize; |
universe@610 | 101 | |
universe@628 | 102 | // return successfully |
universe@610 | 103 | return CX_ARRAY_COPY_SUCCESS; |
universe@610 | 104 | } |
universe@607 | 105 | |
universe@623 | 106 | #define CX_ARRAY_SWAP_SBO_SIZE 512 |
universe@623 | 107 | |
universe@623 | 108 | void cx_array_swap( |
universe@623 | 109 | void *arr, |
universe@623 | 110 | size_t elem_size, |
universe@623 | 111 | size_t idx1, |
universe@623 | 112 | size_t idx2 |
universe@623 | 113 | ) { |
universe@628 | 114 | // short circuit |
universe@623 | 115 | if (idx1 == idx2) return; |
universe@623 | 116 | |
universe@623 | 117 | char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE]; |
universe@623 | 118 | void *tmp; |
universe@623 | 119 | |
universe@628 | 120 | // decide if we can use the local buffer |
universe@623 | 121 | if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) { |
universe@623 | 122 | tmp = malloc(elem_size); |
universe@628 | 123 | // we don't want to enforce error handling |
universe@623 | 124 | if (tmp == NULL) abort(); |
universe@623 | 125 | } else { |
universe@623 | 126 | tmp = sbo_mem; |
universe@623 | 127 | } |
universe@623 | 128 | |
universe@628 | 129 | // calculate memory locations |
universe@623 | 130 | char *left = arr, *right = arr; |
universe@623 | 131 | left += idx1 * elem_size; |
universe@623 | 132 | right += idx2 * elem_size; |
universe@623 | 133 | |
universe@628 | 134 | // three-way swap |
universe@623 | 135 | memcpy(tmp, left, elem_size); |
universe@623 | 136 | memcpy(left, right, elem_size); |
universe@623 | 137 | memcpy(right, tmp, elem_size); |
universe@623 | 138 | |
universe@628 | 139 | // free dynamic memory, if it was needed |
universe@623 | 140 | if (tmp != sbo_mem) { |
universe@623 | 141 | free(tmp); |
universe@623 | 142 | } |
universe@623 | 143 | } |
universe@623 | 144 | |
universe@628 | 145 | // HIGH LEVEL ARRAY LIST FUNCTIONS |
universe@607 | 146 | |
universe@607 | 147 | typedef struct { |
universe@607 | 148 | struct cx_list_s base; |
universe@607 | 149 | void *data; |
universe@610 | 150 | struct cx_array_reallocator_s reallocator; |
universe@607 | 151 | } cx_array_list; |
universe@607 | 152 | |
universe@610 | 153 | static void *cx_arl_realloc( |
universe@610 | 154 | void *array, |
universe@610 | 155 | size_t capacity, |
universe@610 | 156 | size_t elem_size, |
universe@610 | 157 | struct cx_array_reallocator_s *alloc |
universe@610 | 158 | ) { |
universe@628 | 159 | // retrieve the pointer to the list allocator |
universe@610 | 160 | CxAllocator const *al = alloc->ptr1; |
universe@610 | 161 | |
universe@628 | 162 | // use the list allocator to reallocate the memory |
universe@610 | 163 | return cxRealloc(al, array, capacity * elem_size); |
universe@610 | 164 | } |
universe@610 | 165 | |
universe@607 | 166 | static void cx_arl_destructor(struct cx_list_s *list) { |
universe@610 | 167 | cx_array_list *arl = (cx_array_list *) list; |
universe@607 | 168 | cxFree(list->allocator, arl->data); |
universe@607 | 169 | } |
universe@607 | 170 | |
universe@607 | 171 | static int cx_arl_add( |
universe@607 | 172 | struct cx_list_s *list, |
universe@607 | 173 | void const *elem |
universe@607 | 174 | ) { |
universe@610 | 175 | cx_array_list *arl = (cx_array_list *) list; |
universe@610 | 176 | return cx_array_copy( |
universe@610 | 177 | &arl->data, |
universe@610 | 178 | &list->size, |
universe@610 | 179 | &list->capacity, |
universe@610 | 180 | list->size, |
universe@610 | 181 | elem, |
universe@610 | 182 | list->itemsize, |
universe@610 | 183 | 1, |
universe@610 | 184 | &arl->reallocator |
universe@610 | 185 | ); |
universe@607 | 186 | } |
universe@607 | 187 | |
universe@638 | 188 | static size_t cx_arl_insert_array( |
universe@629 | 189 | struct cx_list_s *list, |
universe@638 | 190 | size_t index, |
universe@629 | 191 | void const *array, |
universe@629 | 192 | size_t n |
universe@629 | 193 | ) { |
universe@638 | 194 | // out of bounds and special case check |
universe@638 | 195 | if (index > list->size || n == 0) return 0; |
universe@638 | 196 | |
universe@638 | 197 | // get a correctly typed pointer to the list |
universe@629 | 198 | cx_array_list *arl = (cx_array_list *) list; |
universe@638 | 199 | |
universe@638 | 200 | // do we need to move some elements? |
universe@638 | 201 | if (index < list->size) { |
universe@638 | 202 | char const *first_to_move = (char const *) arl->data; |
universe@638 | 203 | first_to_move += index * list->itemsize; |
universe@638 | 204 | size_t elems_to_move = list->size - index; |
universe@638 | 205 | size_t start_of_moved = index + n; |
universe@638 | 206 | |
universe@638 | 207 | if (CX_ARRAY_COPY_SUCCESS != cx_array_copy( |
universe@638 | 208 | &arl->data, |
universe@638 | 209 | &list->size, |
universe@638 | 210 | &list->capacity, |
universe@638 | 211 | start_of_moved, |
universe@638 | 212 | first_to_move, |
universe@638 | 213 | list->itemsize, |
universe@638 | 214 | elems_to_move, |
universe@638 | 215 | &arl->reallocator |
universe@638 | 216 | )) { |
universe@638 | 217 | // if moving existing elems is unsuccessful, abort |
universe@638 | 218 | return 0; |
universe@638 | 219 | } |
universe@638 | 220 | } |
universe@638 | 221 | |
universe@638 | 222 | // note that if we had to move the elements, the following operation |
universe@638 | 223 | // is guaranteed to succeed, because we have the memory already allocated |
universe@638 | 224 | // therefore, it is impossible to leave this function with an invalid array |
universe@638 | 225 | |
universe@638 | 226 | // place the new elements |
universe@629 | 227 | if (CX_ARRAY_COPY_SUCCESS == cx_array_copy( |
universe@629 | 228 | &arl->data, |
universe@629 | 229 | &list->size, |
universe@629 | 230 | &list->capacity, |
universe@638 | 231 | index, |
universe@629 | 232 | array, |
universe@629 | 233 | list->itemsize, |
universe@629 | 234 | n, |
universe@629 | 235 | &arl->reallocator |
universe@629 | 236 | )) { |
universe@629 | 237 | return n; |
universe@629 | 238 | } else { |
universe@629 | 239 | // array list implementation is "all or nothing" |
universe@629 | 240 | return 0; |
universe@629 | 241 | } |
universe@629 | 242 | } |
universe@629 | 243 | |
universe@638 | 244 | static size_t cx_arl_add_array( |
universe@638 | 245 | struct cx_list_s *list, |
universe@638 | 246 | void const *array, |
universe@638 | 247 | size_t n |
universe@638 | 248 | ) { |
universe@638 | 249 | return cx_arl_insert_array(list, list->size, array, n); |
universe@638 | 250 | } |
universe@638 | 251 | |
universe@607 | 252 | static int cx_arl_insert( |
universe@607 | 253 | struct cx_list_s *list, |
universe@607 | 254 | size_t index, |
universe@607 | 255 | void const *elem |
universe@607 | 256 | ) { |
universe@611 | 257 | if (index > list->size) { |
universe@611 | 258 | return 1; |
universe@611 | 259 | } else if (index == list->size) { |
universe@611 | 260 | return cx_arl_add(list, elem); |
universe@611 | 261 | } else { |
universe@611 | 262 | cx_array_list *arl = (cx_array_list *) list; |
universe@611 | 263 | |
universe@628 | 264 | // move elements starting at index to the right |
universe@611 | 265 | if (cx_array_copy( |
universe@611 | 266 | &arl->data, |
universe@611 | 267 | &list->size, |
universe@611 | 268 | &list->capacity, |
universe@611 | 269 | index + 1, |
universe@611 | 270 | ((char *) arl->data) + index * list->itemsize, |
universe@611 | 271 | list->itemsize, |
universe@611 | 272 | list->size - index, |
universe@611 | 273 | &arl->reallocator |
universe@611 | 274 | )) { |
universe@611 | 275 | return 1; |
universe@611 | 276 | } |
universe@611 | 277 | |
universe@628 | 278 | // place the element |
universe@611 | 279 | memcpy(((char *) arl->data) + index * list->itemsize, |
universe@611 | 280 | elem, list->itemsize); |
universe@611 | 281 | |
universe@611 | 282 | return 0; |
universe@611 | 283 | } |
universe@607 | 284 | } |
universe@607 | 285 | |
universe@607 | 286 | static int cx_arl_insert_iter( |
universe@630 | 287 | struct cx_mut_iterator_s *iter, |
universe@607 | 288 | void const *elem, |
universe@607 | 289 | int prepend |
universe@607 | 290 | ) { |
universe@619 | 291 | struct cx_list_s *list = iter->src_handle; |
universe@619 | 292 | if (iter->index < list->size) { |
universe@619 | 293 | int result = cx_arl_insert( |
universe@619 | 294 | list, |
universe@619 | 295 | iter->index + 1 - prepend, |
universe@619 | 296 | elem |
universe@619 | 297 | ); |
universe@619 | 298 | if (result == 0 && prepend != 0) { |
universe@619 | 299 | iter->index++; |
universe@619 | 300 | iter->elem_handle = ((char *) iter->elem_handle) + list->itemsize; |
universe@619 | 301 | } |
universe@619 | 302 | return result; |
universe@619 | 303 | } else { |
universe@619 | 304 | int result = cx_arl_add(list, elem); |
universe@619 | 305 | iter->index = list->size; |
universe@619 | 306 | return result; |
universe@619 | 307 | } |
universe@607 | 308 | } |
universe@607 | 309 | |
universe@607 | 310 | static int cx_arl_remove( |
universe@607 | 311 | struct cx_list_s *list, |
universe@607 | 312 | size_t index |
universe@607 | 313 | ) { |
universe@628 | 314 | // out-of-bounds check |
universe@613 | 315 | if (index >= list->size) { |
universe@613 | 316 | return 1; |
universe@613 | 317 | } |
universe@613 | 318 | |
universe@628 | 319 | // short-circuit removal of last element |
universe@624 | 320 | if (index == list->size - 1) { |
universe@624 | 321 | list->size--; |
universe@624 | 322 | return 0; |
universe@624 | 323 | } |
universe@613 | 324 | |
universe@628 | 325 | // just move the elements starting at index to the left |
universe@624 | 326 | cx_array_list *arl = (cx_array_list *) list; |
universe@613 | 327 | int result = cx_array_copy( |
universe@613 | 328 | &arl->data, |
universe@613 | 329 | &list->size, |
universe@613 | 330 | &list->capacity, |
universe@613 | 331 | index, |
universe@613 | 332 | ((char *) arl->data) + (index + 1) * list->itemsize, |
universe@613 | 333 | list->itemsize, |
universe@626 | 334 | list->size - index - 1, |
universe@613 | 335 | &arl->reallocator |
universe@613 | 336 | ); |
universe@613 | 337 | if (result == 0) { |
universe@628 | 338 | // decrease the size |
universe@613 | 339 | list->size--; |
universe@613 | 340 | } |
universe@613 | 341 | return result; |
universe@607 | 342 | } |
universe@607 | 343 | |
universe@610 | 344 | static void *cx_arl_at( |
universe@607 | 345 | struct cx_list_s const *list, |
universe@607 | 346 | size_t index |
universe@607 | 347 | ) { |
universe@610 | 348 | if (index < list->size) { |
universe@610 | 349 | cx_array_list const *arl = (cx_array_list const *) list; |
universe@610 | 350 | char *space = arl->data; |
universe@610 | 351 | return space + index * list->itemsize; |
universe@610 | 352 | } else { |
universe@610 | 353 | return NULL; |
universe@610 | 354 | } |
universe@607 | 355 | } |
universe@607 | 356 | |
universe@607 | 357 | static size_t cx_arl_find( |
universe@607 | 358 | struct cx_list_s const *list, |
universe@607 | 359 | void const *elem |
universe@607 | 360 | ) { |
universe@614 | 361 | char *cur = ((cx_array_list const *) list)->data; |
universe@614 | 362 | |
universe@614 | 363 | for (size_t i = 0; i < list->size; i++) { |
universe@614 | 364 | if (0 == list->cmpfunc(elem, cur)) { |
universe@614 | 365 | return i; |
universe@614 | 366 | } |
universe@614 | 367 | cur += list->itemsize; |
universe@614 | 368 | } |
universe@614 | 369 | |
universe@614 | 370 | return list->size; |
universe@607 | 371 | } |
universe@607 | 372 | |
universe@607 | 373 | static void cx_arl_sort(struct cx_list_s *list) { |
universe@615 | 374 | qsort(((cx_array_list *) list)->data, |
universe@615 | 375 | list->size, |
universe@615 | 376 | list->itemsize, |
universe@615 | 377 | list->cmpfunc |
universe@615 | 378 | ); |
universe@607 | 379 | } |
universe@607 | 380 | |
universe@607 | 381 | static int cx_arl_compare( |
universe@607 | 382 | struct cx_list_s const *list, |
universe@607 | 383 | struct cx_list_s const *other |
universe@607 | 384 | ) { |
universe@622 | 385 | if (list->size == other->size) { |
universe@622 | 386 | char const *left = ((cx_array_list const *) list)->data; |
universe@622 | 387 | char const *right = ((cx_array_list const *) other)->data; |
universe@622 | 388 | for (size_t i = 0; i < list->size; i++) { |
universe@622 | 389 | int d = list->cmpfunc(left, right); |
universe@622 | 390 | if (d != 0) { |
universe@622 | 391 | return d; |
universe@622 | 392 | } |
universe@622 | 393 | left += list->itemsize; |
universe@622 | 394 | right += other->itemsize; |
universe@622 | 395 | } |
universe@622 | 396 | return 0; |
universe@622 | 397 | } else { |
universe@622 | 398 | return list->size < other->size ? -1 : 1; |
universe@622 | 399 | } |
universe@607 | 400 | } |
universe@607 | 401 | |
universe@607 | 402 | static void cx_arl_reverse(struct cx_list_s *list) { |
universe@623 | 403 | if (list->size < 2) return; |
universe@623 | 404 | void *data = ((cx_array_list const *) list)->data; |
universe@623 | 405 | size_t half = list->size / 2; |
universe@623 | 406 | for (size_t i = 0; i < half; i++) { |
universe@623 | 407 | cx_array_swap(data, list->itemsize, i, list->size - 1 - i); |
universe@623 | 408 | } |
universe@607 | 409 | } |
universe@607 | 410 | |
universe@630 | 411 | static bool cx_arl_iter_valid(void const *it) { |
universe@630 | 412 | struct cx_iterator_s const *iter = it; |
universe@616 | 413 | struct cx_list_s const *list = iter->src_handle; |
universe@616 | 414 | return iter->index < list->size; |
universe@616 | 415 | } |
universe@616 | 416 | |
universe@630 | 417 | static void *cx_arl_iter_current(void const *it) { |
universe@630 | 418 | struct cx_iterator_s const *iter = it; |
universe@616 | 419 | return iter->elem_handle; |
universe@616 | 420 | } |
universe@616 | 421 | |
universe@630 | 422 | static void cx_arl_iter_next(void *it) { |
universe@630 | 423 | struct cx_iterator_base_s *itbase = it; |
universe@630 | 424 | if (itbase->remove) { |
universe@630 | 425 | struct cx_mut_iterator_s *iter = it; |
universe@630 | 426 | itbase->remove = false; |
universe@616 | 427 | cx_arl_remove(iter->src_handle, iter->index); |
universe@616 | 428 | } else { |
universe@630 | 429 | struct cx_iterator_s *iter = it; |
universe@616 | 430 | iter->index++; |
universe@620 | 431 | iter->elem_handle = |
universe@620 | 432 | ((char *) iter->elem_handle) |
universe@620 | 433 | + ((struct cx_list_s const *) iter->src_handle)->itemsize; |
universe@616 | 434 | } |
universe@616 | 435 | } |
universe@616 | 436 | |
universe@630 | 437 | static bool cx_arl_iter_flag_rm(void *it) { |
universe@630 | 438 | struct cx_iterator_base_s *iter = it; |
universe@630 | 439 | if (iter->mutating) { |
universe@630 | 440 | iter->remove = true; |
universe@630 | 441 | return true; |
universe@630 | 442 | } else { |
universe@630 | 443 | return false; |
universe@630 | 444 | } |
universe@630 | 445 | } |
universe@630 | 446 | |
universe@607 | 447 | static struct cx_iterator_s cx_arl_iterator( |
universe@630 | 448 | struct cx_list_s const *list, |
universe@607 | 449 | size_t index |
universe@607 | 450 | ) { |
universe@607 | 451 | struct cx_iterator_s iter; |
universe@607 | 452 | |
universe@616 | 453 | iter.index = index; |
universe@616 | 454 | iter.src_handle = list; |
universe@616 | 455 | iter.elem_handle = cx_arl_at(list, index); |
universe@630 | 456 | iter.base.valid = cx_arl_iter_valid; |
universe@630 | 457 | iter.base.current = cx_arl_iter_current; |
universe@630 | 458 | iter.base.next = cx_arl_iter_next; |
universe@630 | 459 | iter.base.flag_removal = cx_arl_iter_flag_rm; |
universe@630 | 460 | iter.base.remove = false; |
universe@630 | 461 | iter.base.mutating = false; |
universe@616 | 462 | |
universe@607 | 463 | return iter; |
universe@607 | 464 | } |
universe@607 | 465 | |
universe@630 | 466 | static struct cx_mut_iterator_s cx_arl_mut_iterator( |
universe@630 | 467 | struct cx_list_s *list, |
universe@630 | 468 | size_t index |
universe@630 | 469 | ) { |
universe@630 | 470 | CxIterator it = cx_arl_iterator(list, index); |
universe@630 | 471 | it.base.mutating = true; |
universe@630 | 472 | |
universe@630 | 473 | // we know the iterators share the same memory layout |
universe@630 | 474 | CxMutIterator iter; |
universe@630 | 475 | memcpy(&iter, &it, sizeof(CxMutIterator)); |
universe@630 | 476 | return iter; |
universe@630 | 477 | } |
universe@630 | 478 | |
universe@607 | 479 | static cx_list_class cx_array_list_class = { |
universe@607 | 480 | cx_arl_destructor, |
universe@607 | 481 | cx_arl_add, |
universe@629 | 482 | cx_arl_add_array, |
universe@607 | 483 | cx_arl_insert, |
universe@638 | 484 | cx_arl_insert_array, |
universe@607 | 485 | cx_arl_insert_iter, |
universe@607 | 486 | cx_arl_remove, |
universe@607 | 487 | cx_arl_at, |
universe@607 | 488 | cx_arl_find, |
universe@607 | 489 | cx_arl_sort, |
universe@607 | 490 | cx_arl_compare, |
universe@607 | 491 | cx_arl_reverse, |
universe@607 | 492 | cx_arl_iterator, |
universe@630 | 493 | cx_arl_mut_iterator, |
universe@607 | 494 | }; |
universe@607 | 495 | |
universe@606 | 496 | CxList *cxArrayListCreate( |
universe@606 | 497 | CxAllocator const *allocator, |
universe@606 | 498 | CxListComparator comparator, |
universe@606 | 499 | size_t item_size, |
universe@606 | 500 | size_t initial_capacity |
universe@606 | 501 | ) { |
universe@607 | 502 | cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list)); |
universe@607 | 503 | if (list == NULL) return NULL; |
universe@607 | 504 | |
universe@607 | 505 | list->data = cxCalloc(allocator, initial_capacity, item_size); |
universe@607 | 506 | if (list->data == NULL) { |
universe@607 | 507 | cxFree(allocator, list); |
universe@607 | 508 | return NULL; |
universe@607 | 509 | } |
universe@607 | 510 | |
universe@607 | 511 | list->base.cl = &cx_array_list_class; |
universe@607 | 512 | list->base.allocator = allocator; |
universe@607 | 513 | list->base.cmpfunc = comparator; |
universe@607 | 514 | list->base.itemsize = item_size; |
universe@607 | 515 | list->base.capacity = initial_capacity; |
universe@607 | 516 | |
universe@628 | 517 | // configure the reallocator |
universe@610 | 518 | list->reallocator.realloc = cx_arl_realloc; |
universe@610 | 519 | list->reallocator.ptr1 = (void *) allocator; |
universe@610 | 520 | |
universe@607 | 521 | return (CxList *) list; |
universe@606 | 522 | } |