Wed, 28 May 2025 20:54:43 +0200
add wrapper for openssl hashing functions
| 91 | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 3 | * | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
| 91 | 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 | */ | |
| 28 | ||
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
29 | #include "cx/list.h" |
| 91 | 30 | |
| 490 | 31 | #include <string.h> |
| 32 | ||
| 33 | // <editor-fold desc="Store Pointers Functionality"> | |
| 34 | ||
| 35 | static _Thread_local cx_compare_func cx_pl_cmpfunc_impl; | |
| 36 | ||
| 37 | static int cx_pl_cmpfunc( | |
| 579 | 38 | const void *l, |
| 39 | const void *r | |
| 490 | 40 | ) { |
| 41 | void *const *lptr = l; | |
| 42 | void *const *rptr = r; | |
| 579 | 43 | const void *left = lptr == NULL ? NULL : *lptr; |
| 44 | const void *right = rptr == NULL ? NULL : *rptr; | |
| 490 | 45 | return cx_pl_cmpfunc_impl(left, right); |
| 46 | } | |
| 47 | ||
| 579 | 48 | static void cx_pl_hack_cmpfunc(const struct cx_list_s *list) { |
| 490 | 49 | // cast away const - this is the hacky thing |
| 579 | 50 | struct cx_collection_s *l = (struct cx_collection_s*) &list->collection; |
| 490 | 51 | cx_pl_cmpfunc_impl = l->cmpfunc; |
| 52 | l->cmpfunc = cx_pl_cmpfunc; | |
| 53 | } | |
| 54 | ||
| 579 | 55 | static void cx_pl_unhack_cmpfunc(const struct cx_list_s *list) { |
| 490 | 56 | // cast away const - this is the hacky thing |
| 579 | 57 | struct cx_collection_s *l = (struct cx_collection_s*) &list->collection; |
| 490 | 58 | l->cmpfunc = cx_pl_cmpfunc_impl; |
| 59 | } | |
| 60 | ||
| 61 | static void cx_pl_destructor(struct cx_list_s *list) { | |
| 579 | 62 | list->climpl->deallocate(list); |
| 490 | 63 | } |
| 64 | ||
| 582 | 65 | static void *cx_pl_insert_element( |
| 490 | 66 | struct cx_list_s *list, |
| 67 | size_t index, | |
| 579 | 68 | const void *element |
| 490 | 69 | ) { |
| 70 | return list->climpl->insert_element(list, index, &element); | |
| 71 | } | |
| 72 | ||
| 73 | static size_t cx_pl_insert_array( | |
| 74 | struct cx_list_s *list, | |
| 75 | size_t index, | |
| 579 | 76 | const void *array, |
| 490 | 77 | size_t n |
| 78 | ) { | |
| 79 | return list->climpl->insert_array(list, index, array, n); | |
| 80 | } | |
| 81 | ||
| 579 | 82 | static size_t cx_pl_insert_sorted( |
| 83 | struct cx_list_s *list, | |
| 84 | const void *array, | |
| 85 | size_t n | |
| 86 | ) { | |
| 87 | cx_pl_hack_cmpfunc(list); | |
| 88 | size_t result = list->climpl->insert_sorted(list, array, n); | |
| 89 | cx_pl_unhack_cmpfunc(list); | |
| 90 | return result; | |
| 91 | } | |
| 92 | ||
| 490 | 93 | static int cx_pl_insert_iter( |
| 579 | 94 | struct cx_iterator_s *iter, |
| 95 | const void *elem, | |
| 490 | 96 | int prepend |
| 97 | ) { | |
| 579 | 98 | struct cx_list_s *list = iter->src_handle.m; |
| 490 | 99 | return list->climpl->insert_iter(iter, &elem, prepend); |
| 100 | } | |
| 101 | ||
| 579 | 102 | static size_t cx_pl_remove( |
| 490 | 103 | struct cx_list_s *list, |
| 579 | 104 | size_t index, |
| 105 | size_t num, | |
| 106 | void *targetbuf | |
| 490 | 107 | ) { |
| 579 | 108 | return list->climpl->remove(list, index, num, targetbuf); |
| 490 | 109 | } |
| 110 | ||
| 111 | static void cx_pl_clear(struct cx_list_s *list) { | |
| 112 | list->climpl->clear(list); | |
| 113 | } | |
| 114 | ||
| 115 | static int cx_pl_swap( | |
| 116 | struct cx_list_s *list, | |
| 117 | size_t i, | |
| 118 | size_t j | |
| 119 | ) { | |
| 120 | return list->climpl->swap(list, i, j); | |
| 121 | } | |
| 122 | ||
| 123 | static void *cx_pl_at( | |
| 579 | 124 | const struct cx_list_s *list, |
| 490 | 125 | size_t index |
| 126 | ) { | |
| 127 | void **ptr = list->climpl->at(list, index); | |
| 128 | return ptr == NULL ? NULL : *ptr; | |
| 129 | } | |
| 130 | ||
| 579 | 131 | static size_t cx_pl_find_remove( |
| 132 | struct cx_list_s *list, | |
| 133 | const void *elem, | |
| 134 | bool remove | |
| 490 | 135 | ) { |
| 136 | cx_pl_hack_cmpfunc(list); | |
| 579 | 137 | size_t ret = list->climpl->find_remove(list, &elem, remove); |
| 490 | 138 | cx_pl_unhack_cmpfunc(list); |
| 139 | return ret; | |
| 140 | } | |
| 141 | ||
| 142 | static void cx_pl_sort(struct cx_list_s *list) { | |
| 143 | cx_pl_hack_cmpfunc(list); | |
| 144 | list->climpl->sort(list); | |
| 145 | cx_pl_unhack_cmpfunc(list); | |
| 146 | } | |
| 147 | ||
| 148 | static int cx_pl_compare( | |
| 579 | 149 | const struct cx_list_s *list, |
| 150 | const struct cx_list_s *other | |
| 490 | 151 | ) { |
| 152 | cx_pl_hack_cmpfunc(list); | |
| 153 | int ret = list->climpl->compare(list, other); | |
| 154 | cx_pl_unhack_cmpfunc(list); | |
| 155 | return ret; | |
| 156 | } | |
| 157 | ||
| 158 | static void cx_pl_reverse(struct cx_list_s *list) { | |
| 159 | list->climpl->reverse(list); | |
| 160 | } | |
| 161 | ||
| 579 | 162 | static void *cx_pl_iter_current(const void *it) { |
| 163 | const struct cx_iterator_s *iter = it; | |
| 490 | 164 | void **ptr = iter->base.current_impl(it); |
| 165 | return ptr == NULL ? NULL : *ptr; | |
| 166 | } | |
| 167 | ||
| 168 | static struct cx_iterator_s cx_pl_iterator( | |
| 579 | 169 | const struct cx_list_s *list, |
| 490 | 170 | size_t index, |
| 171 | bool backwards | |
| 172 | ) { | |
| 173 | struct cx_iterator_s iter = list->climpl->iterator(list, index, backwards); | |
| 174 | iter.base.current_impl = iter.base.current; | |
| 175 | iter.base.current = cx_pl_iter_current; | |
| 176 | return iter; | |
| 177 | } | |
| 178 | ||
| 179 | static cx_list_class cx_pointer_list_class = { | |
| 180 | cx_pl_destructor, | |
| 181 | cx_pl_insert_element, | |
| 182 | cx_pl_insert_array, | |
| 579 | 183 | cx_pl_insert_sorted, |
| 490 | 184 | cx_pl_insert_iter, |
| 185 | cx_pl_remove, | |
| 186 | cx_pl_clear, | |
| 187 | cx_pl_swap, | |
| 188 | cx_pl_at, | |
| 579 | 189 | cx_pl_find_remove, |
| 490 | 190 | cx_pl_sort, |
| 191 | cx_pl_compare, | |
| 192 | cx_pl_reverse, | |
| 193 | cx_pl_iterator, | |
| 194 | }; | |
| 195 | // </editor-fold> | |
| 196 | ||
| 504 | 197 | // <editor-fold desc="empty list implementation"> |
| 198 | ||
| 579 | 199 | static void cx_emptyl_noop(cx_attr_unused CxList *list) { |
| 504 | 200 | // this is a noop, but MUST be implemented |
| 201 | } | |
| 202 | ||
| 203 | static void *cx_emptyl_at( | |
| 579 | 204 | cx_attr_unused const struct cx_list_s *list, |
| 205 | cx_attr_unused size_t index | |
| 504 | 206 | ) { |
| 207 | return NULL; | |
| 208 | } | |
| 209 | ||
| 579 | 210 | static size_t cx_emptyl_find_remove( |
| 211 | cx_attr_unused struct cx_list_s *list, | |
| 212 | cx_attr_unused const void *elem, | |
| 213 | cx_attr_unused bool remove | |
| 504 | 214 | ) { |
| 579 | 215 | return 0; |
| 504 | 216 | } |
| 217 | ||
| 579 | 218 | static bool cx_emptyl_iter_valid(cx_attr_unused const void *iter) { |
| 504 | 219 | return false; |
| 220 | } | |
| 221 | ||
| 222 | static CxIterator cx_emptyl_iterator( | |
| 579 | 223 | const struct cx_list_s *list, |
| 504 | 224 | size_t index, |
| 579 | 225 | cx_attr_unused bool backwards |
| 504 | 226 | ) { |
| 227 | CxIterator iter = {0}; | |
| 579 | 228 | iter.src_handle.c = list; |
| 504 | 229 | iter.index = index; |
| 230 | iter.base.valid = cx_emptyl_iter_valid; | |
| 231 | return iter; | |
| 232 | } | |
| 233 | ||
| 234 | static cx_list_class cx_empty_list_class = { | |
| 235 | cx_emptyl_noop, | |
| 236 | NULL, | |
| 237 | NULL, | |
| 238 | NULL, | |
| 239 | NULL, | |
| 579 | 240 | NULL, |
| 504 | 241 | cx_emptyl_noop, |
| 242 | NULL, | |
| 243 | cx_emptyl_at, | |
| 579 | 244 | cx_emptyl_find_remove, |
| 504 | 245 | cx_emptyl_noop, |
| 579 | 246 | NULL, |
| 504 | 247 | cx_emptyl_noop, |
| 248 | cx_emptyl_iterator, | |
| 249 | }; | |
| 250 | ||
| 251 | CxList cx_empty_list = { | |
| 579 | 252 | { |
| 504 | 253 | NULL, |
| 254 | NULL, | |
| 255 | 0, | |
| 256 | 0, | |
| 257 | NULL, | |
| 258 | NULL, | |
| 259 | NULL, | |
| 260 | false, | |
| 579 | 261 | true, |
| 262 | }, | |
| 263 | &cx_empty_list_class, | |
| 264 | NULL | |
| 504 | 265 | }; |
| 266 | ||
| 267 | CxList *const cxEmptyList = &cx_empty_list; | |
| 268 | ||
| 269 | // </editor-fold> | |
| 270 | ||
| 579 | 271 | #define invoke_list_func(name, list, ...) \ |
| 272 | ((list)->climpl == NULL ? (list)->cl->name : (list)->climpl->name) \ | |
| 273 | (list, __VA_ARGS__) | |
| 274 | ||
| 275 | size_t cx_list_default_insert_array( | |
| 276 | struct cx_list_s *list, | |
| 277 | size_t index, | |
| 278 | const void *data, | |
| 279 | size_t n | |
| 280 | ) { | |
| 281 | size_t elem_size = list->collection.elem_size; | |
| 282 | const char *src = data; | |
| 283 | size_t i = 0; | |
| 284 | for (; i < n; i++) { | |
| 582 | 285 | if (NULL == invoke_list_func( |
| 579 | 286 | insert_element, list, index + i, |
| 287 | src + (i * elem_size))) return i; | |
| 288 | } | |
| 289 | return i; | |
| 290 | } | |
| 291 | ||
| 292 | size_t cx_list_default_insert_sorted( | |
| 293 | struct cx_list_s *list, | |
| 294 | const void *sorted_data, | |
| 295 | size_t n | |
| 296 | ) { | |
| 297 | // corner case | |
| 298 | if (n == 0) return 0; | |
| 299 | ||
| 300 | size_t elem_size = list->collection.elem_size; | |
| 301 | cx_compare_func cmp = list->collection.cmpfunc; | |
| 302 | const char *src = sorted_data; | |
| 303 | ||
| 304 | // track indices and number of inserted items | |
| 305 | size_t di = 0, si = 0, inserted = 0; | |
| 306 | ||
| 307 | // search the list for insertion points | |
| 308 | for (; di < list->collection.size; di++) { | |
| 309 | const void *list_elm = invoke_list_func(at, list, di); | |
| 310 | ||
| 311 | // compare current list element with first source element | |
| 312 | // if less or equal, skip | |
| 313 | if (cmp(list_elm, src) <= 0) { | |
| 314 | continue; | |
| 315 | } | |
| 316 | ||
| 317 | // determine number of consecutive elements that can be inserted | |
| 318 | size_t ins = 1; | |
| 319 | const char *next = src; | |
| 320 | while (++si < n) { | |
| 321 | next += elem_size; | |
| 322 | // once we become larger than the list elem, break | |
| 323 | if (cmp(list_elm, next) <= 0) { | |
| 324 | break; | |
| 325 | } | |
| 326 | // otherwise, we can insert one more | |
| 327 | ins++; | |
| 328 | } | |
| 329 | ||
| 330 | // insert the elements at location si | |
| 331 | if (ins == 1) { | |
| 582 | 332 | if (NULL == invoke_list_func( |
| 579 | 333 | insert_element, list, di, src)) return inserted; |
| 334 | } else { | |
| 335 | size_t r = invoke_list_func(insert_array, list, di, src, ins); | |
| 336 | if (r < ins) return inserted + r; | |
| 337 | } | |
| 338 | inserted += ins; | |
| 339 | di += ins; | |
| 340 | ||
| 341 | // everything inserted? | |
| 342 | if (inserted == n) return inserted; | |
| 343 | src = next; | |
| 344 | } | |
| 345 | ||
| 346 | // insert remaining items | |
| 347 | if (si < n) { | |
| 348 | inserted += invoke_list_func(insert_array, list, di, src, n - si); | |
| 349 | } | |
| 350 | ||
| 351 | return inserted; | |
| 352 | } | |
| 353 | ||
| 354 | void cx_list_default_sort(struct cx_list_s *list) { | |
| 355 | size_t elem_size = list->collection.elem_size; | |
| 356 | size_t list_size = list->collection.size; | |
| 582 | 357 | void *tmp = cxMallocDefault(elem_size * list_size); |
| 579 | 358 | if (tmp == NULL) abort(); |
| 359 | ||
| 360 | // copy elements from source array | |
| 361 | char *loc = tmp; | |
| 362 | for (size_t i = 0; i < list_size; i++) { | |
| 363 | void *src = invoke_list_func(at, list, i); | |
| 364 | memcpy(loc, src, elem_size); | |
| 365 | loc += elem_size; | |
| 366 | } | |
| 367 | ||
| 368 | // qsort | |
| 369 | qsort(tmp, list_size, elem_size, | |
| 370 | list->collection.cmpfunc); | |
| 371 | ||
| 372 | // copy elements back | |
| 373 | loc = tmp; | |
| 374 | for (size_t i = 0; i < list_size; i++) { | |
| 375 | void *dest = invoke_list_func(at, list, i); | |
| 376 | memcpy(dest, loc, elem_size); | |
| 377 | loc += elem_size; | |
| 378 | } | |
| 379 | ||
| 582 | 380 | cxFreeDefault(tmp); |
| 579 | 381 | } |
| 382 | ||
| 383 | int cx_list_default_swap(struct cx_list_s *list, size_t i, size_t j) { | |
| 384 | if (i == j) return 0; | |
| 385 | if (i >= list->collection.size) return 1; | |
| 386 | if (j >= list->collection.size) return 1; | |
| 387 | ||
| 388 | size_t elem_size = list->collection.elem_size; | |
| 389 | ||
| 582 | 390 | void *tmp = cxMallocDefault(elem_size); |
| 579 | 391 | if (tmp == NULL) return 1; |
| 392 | ||
| 393 | void *ip = invoke_list_func(at, list, i); | |
| 394 | void *jp = invoke_list_func(at, list, j); | |
| 395 | ||
| 396 | memcpy(tmp, ip, elem_size); | |
| 397 | memcpy(ip, jp, elem_size); | |
| 398 | memcpy(jp, tmp, elem_size); | |
| 399 | ||
| 582 | 400 | cxFreeDefault(tmp); |
| 579 | 401 | |
| 402 | return 0; | |
| 403 | } | |
| 404 | ||
| 405 | void cx_list_init( | |
| 406 | struct cx_list_s *list, | |
| 407 | struct cx_list_class_s *cl, | |
| 408 | const struct cx_allocator_s *allocator, | |
| 409 | cx_compare_func comparator, | |
| 410 | size_t elem_size | |
| 411 | ) { | |
| 412 | list->cl = cl; | |
| 413 | list->collection.allocator = allocator; | |
| 414 | list->collection.cmpfunc = comparator; | |
| 415 | if (elem_size > 0) { | |
| 416 | list->collection.elem_size = elem_size; | |
| 417 | } else { | |
| 418 | list->collection.elem_size = sizeof(void *); | |
| 419 | if (list->collection.cmpfunc == NULL) { | |
| 420 | list->collection.cmpfunc = cx_cmp_ptr; | |
| 421 | } | |
| 422 | list->collection.store_pointer = true; | |
| 423 | list->climpl = list->cl; | |
| 424 | list->cl = &cx_pointer_list_class; | |
| 425 | } | |
| 91 | 426 | } |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
427 | |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
428 | int cxListCompare( |
| 579 | 429 | const CxList *list, |
| 430 | const CxList *other | |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
431 | ) { |
| 579 | 432 | bool cannot_optimize = false; |
| 504 | 433 | |
| 579 | 434 | // if one is storing pointers but the other is not |
| 435 | cannot_optimize |= list->collection.store_pointer ^ other->collection.store_pointer; | |
| 436 | ||
| 437 | // if one class is wrapped but the other is not | |
| 438 | cannot_optimize |= (list->climpl == NULL) ^ (other->climpl == NULL); | |
| 504 | 439 | |
| 579 | 440 | // if the compare functions do not match or both are NULL |
| 441 | if (!cannot_optimize) { | |
| 442 | cx_compare_func list_cmp = (cx_compare_func) (list->climpl != NULL ? | |
| 443 | list->climpl->compare : list->cl->compare); | |
| 444 | cx_compare_func other_cmp = (cx_compare_func) (other->climpl != NULL ? | |
| 445 | other->climpl->compare : other->cl->compare); | |
| 446 | cannot_optimize |= list_cmp != other_cmp; | |
| 447 | cannot_optimize |= list_cmp == NULL; | |
| 448 | } | |
| 449 | ||
| 450 | if (cannot_optimize) { | |
| 490 | 451 | // lists are definitely different - cannot use internal compare function |
| 579 | 452 | if (list->collection.size == other->collection.size) { |
| 453 | CxIterator left = list->cl->iterator(list, 0, false); | |
| 454 | CxIterator right = other->cl->iterator(other, 0, false); | |
| 455 | for (size_t i = 0; i < list->collection.size; i++) { | |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
456 | void *leftValue = cxIteratorCurrent(left); |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
457 | void *rightValue = cxIteratorCurrent(right); |
| 579 | 458 | int d = list->collection.cmpfunc(leftValue, rightValue); |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
459 | if (d != 0) { |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
460 | return d; |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
461 | } |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
462 | cxIteratorNext(left); |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
463 | cxIteratorNext(right); |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
464 | } |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
465 | return 0; |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
466 | } else { |
| 579 | 467 | return list->collection.size < other->collection.size ? -1 : 1; |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
468 | } |
| 490 | 469 | } else { |
| 470 | // lists are compatible | |
| 471 | return list->cl->compare(list, other); | |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
472 | } |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
473 | } |
| 490 | 474 | |
| 579 | 475 | CxIterator cxListMutIteratorAt( |
| 490 | 476 | CxList *list, |
| 477 | size_t index | |
| 478 | ) { | |
| 479 | CxIterator it = list->cl->iterator(list, index, false); | |
| 480 | it.base.mutating = true; | |
| 579 | 481 | return it; |
| 490 | 482 | } |
| 483 | ||
| 579 | 484 | CxIterator cxListMutBackwardsIteratorAt( |
| 490 | 485 | CxList *list, |
| 486 | size_t index | |
| 487 | ) { | |
| 488 | CxIterator it = list->cl->iterator(list, index, true); | |
| 489 | it.base.mutating = true; | |
| 579 | 490 | return it; |
| 491 | } | |
| 490 | 492 | |
| 579 | 493 | void cxListFree(CxList *list) { |
| 494 | if (list == NULL) return; | |
| 495 | list->cl->deallocate(list); | |
| 490 | 496 | } |
| 582 | 497 | |
| 498 | int cxListSet( | |
| 499 | CxList *list, | |
| 500 | size_t index, | |
| 501 | const void *elem | |
| 502 | ) { | |
| 503 | if (index >= list->collection.size) { | |
| 504 | return 1; | |
| 505 | } | |
| 506 | ||
| 507 | if (list->collection.store_pointer) { | |
| 508 | // For pointer collections, always use climpl | |
| 509 | void **target = list->climpl->at(list, index); | |
| 510 | *target = (void *)elem; | |
| 511 | } else { | |
| 512 | void *target = list->cl->at(list, index); | |
| 513 | memcpy(target, elem, list->collection.elem_size); | |
| 514 | } | |
| 515 | ||
| 516 | return 0; | |
| 517 | } |