Thu, 18 Dec 2025 17:50:15 +0100
update ucx
| 174 | 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 | */ | |
| 28 | ||
| 29 | #include "cx/array_list.h" | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
30 | #include "cx/compare.h" |
| 174 | 31 | #include <assert.h> |
| 32 | #include <string.h> | |
| 440 | 33 | #include <errno.h> |
| 174 | 34 | |
| 35 | // LOW LEVEL ARRAY LIST FUNCTIONS | |
| 36 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
37 | /** |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
38 | * Intelligently calculates a new capacity, reserving some more |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
39 | * elements than required to prevent too many allocations. |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
40 | * |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
41 | * @param current_capacity the current capacity of the array |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
42 | * @param needed_capacity the required capacity of the array |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
43 | * @return the new capacity |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
44 | */ |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
45 | static size_t cx_array_grow_capacity( |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
46 | size_t current_capacity, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
47 | size_t needed_capacity |
| 440 | 48 | ) { |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
49 | if (current_capacity >= needed_capacity) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
50 | return current_capacity; |
| 440 | 51 | } |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
52 | size_t cap = needed_capacity; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
53 | size_t alignment; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
54 | if (cap < 128) alignment = 16; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
55 | else if (cap < 1024) alignment = 64; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
56 | else if (cap < 8192) alignment = 512; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
57 | else alignment = 1024; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
58 | return cap - (cap % alignment) + alignment; |
| 440 | 59 | } |
| 60 | ||
| 1016 | 61 | int cx_array_init_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity) { |
| 62 | memset(array, 0, sizeof(CxArray)); | |
| 63 | return cx_array_reserve_(allocator, array, elem_size, capacity); | |
| 64 | } | |
| 65 | ||
| 66 | void cx_array_init_fixed_(CxArray *array, const void *data, size_t capacity, size_t size) { | |
| 67 | array->data = (void*) data; | |
| 68 | array->capacity = capacity; | |
| 69 | array->size = size; | |
| 70 | } | |
| 71 | ||
| 72 | int cx_array_reserve_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity) { | |
| 73 | if (cxReallocateArray(allocator, &array->data, capacity, elem_size)) { | |
| 74 | return -1; // LCOV_EXCL_LINE | |
| 75 | } | |
| 76 | array->capacity = capacity; | |
| 77 | if (array->size > capacity) { | |
| 78 | array->size = capacity; | |
| 79 | } | |
| 80 | return 0; | |
| 81 | } | |
| 440 | 82 | |
| 1016 | 83 | int cx_array_copy_to_new_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity) { |
| 84 | CxArray heap_array; | |
| 85 | if (cx_array_init_(allocator, &heap_array, elem_size, capacity)) { | |
| 86 | return -1; // LCOV_EXCL_LINE | |
| 87 | } | |
| 88 | heap_array.size = array->size; | |
| 89 | memcpy(heap_array.data, array->data, elem_size * array->size); | |
| 90 | *array = heap_array; | |
| 91 | return 0; | |
| 92 | } | |
| 93 | ||
| 94 | int cx_array_insert_(const CxAllocator *allocator, CxArray *array, | |
| 95 | size_t elem_size, size_t index, const void *other, size_t n) { | |
| 96 | // out of bounds and special case check | |
| 97 | if (index > array->size) return -1; | |
| 98 | if (n == 0) return 0; | |
| 99 | ||
| 100 | // guarantee enough capacity | |
| 101 | if (array->capacity < array->size + n) { | |
| 102 | const size_t new_capacity = cx_array_grow_capacity(array->capacity,array->size + n); | |
| 103 | if (cxReallocateArray(allocator, &array->data, new_capacity, elem_size)) { | |
| 104 | return -1; // LCOV_EXCL_LINE | |
| 105 | } | |
| 106 | array->capacity = new_capacity; | |
| 440 | 107 | } |
| 108 | ||
| 1016 | 109 | // determine insert position |
| 110 | char *dst = array->data; | |
| 111 | dst += index * elem_size; | |
| 112 | ||
| 113 | // do we need to move some elements? | |
| 114 | if (index < array->size) { | |
| 115 | size_t elems_to_move = array->size - index; | |
| 116 | char *target = dst + n * elem_size; | |
| 117 | memmove(target, dst, elems_to_move * elem_size); | |
| 440 | 118 | } |
| 119 | ||
| 1016 | 120 | // place the new elements, if any |
| 121 | // otherwise, this function just reserved the memory (a.k.a emplace) | |
| 122 | if (other != NULL) { | |
| 123 | memcpy(dst, other, n * elem_size); | |
| 440 | 124 | } |
| 1016 | 125 | array->size += n; |
| 440 | 126 | |
| 127 | return 0; | |
| 128 | } | |
| 129 | ||
| 1016 | 130 | int cx_array_insert_sorted_s_( |
| 131 | const CxAllocator *allocator, | |
| 132 | CxArray *array, | |
| 174 | 133 | size_t elem_size, |
| 1016 | 134 | const void *sorted_data, |
| 135 | size_t n, | |
| 136 | bool allow_duplicates, | |
| 137 | cx_compare_func2 cmp_func, | |
| 138 | void *context | |
| 174 | 139 | ) { |
| 140 | // assert pointers | |
| 1016 | 141 | assert(allocator != NULL); |
| 142 | assert(array != NULL); | |
| 324 | 143 | assert(cmp_func != NULL); |
| 144 | assert(sorted_data != NULL); | |
| 440 | 145 | |
| 324 | 146 | // corner case |
| 1016 | 147 | if (n == 0) return 0; |
| 324 | 148 | |
| 440 | 149 | // overflow check |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
150 | // LCOV_EXCL_START |
| 1016 | 151 | if (n > SIZE_MAX - array->size) { |
| 440 | 152 | errno = EOVERFLOW; |
| 153 | return 1; | |
| 154 | } | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
155 | // LCOV_EXCL_STOP |
| 440 | 156 | |
| 324 | 157 | // store some counts |
| 1016 | 158 | const size_t old_size = array->size; |
| 159 | const size_t old_capacity = array->capacity; | |
| 845 | 160 | // the necessary capacity is the worst case assumption, including duplicates |
| 1016 | 161 | const size_t needed_capacity = cx_array_grow_capacity(old_capacity, old_size + n); |
| 324 | 162 | |
| 163 | // if we need more than we have, try a reallocation | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
164 | if (needed_capacity > old_capacity) { |
| 1016 | 165 | if (cxReallocateArray(allocator, &array->data, needed_capacity, elem_size)) { |
| 166 | return -1; // LCOV_EXCL_LINE | |
| 324 | 167 | } |
| 1016 | 168 | array->capacity = needed_capacity; |
| 324 | 169 | } |
| 170 | ||
| 171 | // now we have guaranteed that we can insert everything | |
| 1016 | 172 | size_t new_size = old_size + n; |
| 173 | array->size = new_size; | |
| 324 | 174 | |
| 175 | // declare the source and destination indices/pointers | |
| 176 | size_t si = 0, di = 0; | |
| 177 | const char *src = sorted_data; | |
| 1016 | 178 | char *dest = array->data; |
| 324 | 179 | |
| 180 | // find the first insertion point | |
| 1016 | 181 | di = cx_array_binary_search_sup_c(dest, old_size, elem_size, src, cmp_func, context); |
| 324 | 182 | dest += di * elem_size; |
| 183 | ||
| 184 | // move the remaining elements in the array completely to the right | |
| 185 | // we will call it the "buffer" for parked elements | |
| 186 | size_t buf_size = old_size - di; | |
| 187 | size_t bi = new_size - buf_size; | |
| 1016 | 188 | char *bptr = ((char *) array->data) + bi * elem_size; |
| 324 | 189 | memmove(bptr, dest, buf_size * elem_size); |
| 190 | ||
| 191 | // while there are both source and buffered elements left, | |
| 192 | // copy them interleaving | |
| 1016 | 193 | while (si < n && bi < new_size) { |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
194 | // determine how many source elements can be inserted. |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
195 | // the first element that shall not be inserted is the smallest element |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
196 | // that is strictly larger than the first buffered element |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
197 | // (located at the index of the infimum plus one). |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
198 | // the infimum is guaranteed to exist: |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
199 | // - if all src elements are larger, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
200 | // there is no buffer, and this loop is skipped |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
201 | // - if any src element is smaller or equal, the infimum exists |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
202 | // - when all src elements that are smaller are copied, the second part |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
203 | // of this loop body will copy the remaining buffer (emptying it) |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
204 | // Therefore, the buffer can never contain an element that is smaller |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
205 | // than any element in the source and the infimum exists. |
| 324 | 206 | size_t copy_len, bytes_copied; |
| 1016 | 207 | copy_len = cx_array_binary_search_inf_c( |
| 208 | src, n - si, elem_size, bptr, cmp_func, context | |
| 324 | 209 | ); |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
210 | copy_len++; |
| 324 | 211 | |
| 212 | // copy the source elements | |
| 845 | 213 | if (copy_len > 0) { |
| 214 | if (allow_duplicates) { | |
| 215 | // we can copy the entire chunk | |
| 216 | bytes_copied = copy_len * elem_size; | |
| 217 | memcpy(dest, src, bytes_copied); | |
| 218 | dest += bytes_copied; | |
| 219 | src += bytes_copied; | |
| 220 | si += copy_len; | |
| 221 | di += copy_len; | |
| 222 | } else { | |
| 223 | // first, check the end of the source chunk | |
| 224 | // for being a duplicate of the bptr | |
| 225 | const char *end_of_src = src + (copy_len - 1) * elem_size; | |
| 226 | size_t skip_len = 0; | |
| 1016 | 227 | while (copy_len > 0 && cmp_func(bptr, end_of_src, context) == 0) { |
| 845 | 228 | end_of_src -= elem_size; |
| 229 | skip_len++; | |
| 230 | copy_len--; | |
| 231 | } | |
| 1016 | 232 | char *last = dest == array->data ? NULL : dest - elem_size; |
| 845 | 233 | // then iterate through the source chunk |
| 234 | // and skip all duplicates with the last element in the array | |
| 235 | size_t more_skipped = 0; | |
| 236 | for (unsigned j = 0; j < copy_len; j++) { | |
| 1016 | 237 | if (last != NULL && cmp_func(last, src, context) == 0) { |
| 845 | 238 | // duplicate - skip |
| 239 | src += elem_size; | |
| 240 | si++; | |
| 241 | more_skipped++; | |
| 242 | } else { | |
| 243 | memcpy(dest, src, elem_size); | |
| 244 | src += elem_size; | |
| 245 | last = dest; | |
| 246 | dest += elem_size; | |
| 247 | si++; | |
| 248 | di++; | |
| 249 | } | |
| 250 | } | |
| 251 | // skip the previously identified elements as well | |
| 252 | src += skip_len * elem_size; | |
| 253 | si += skip_len; | |
| 254 | skip_len += more_skipped; | |
| 255 | // reduce the actual size by the number of skipped elements | |
| 1016 | 256 | array->size -= skip_len; |
| 845 | 257 | } |
| 258 | } | |
| 324 | 259 | |
| 260 | // when all source elements are in place, we are done | |
| 1016 | 261 | if (si >= n) break; |
| 324 | 262 | |
| 263 | // determine how many buffered elements need to be restored | |
| 1016 | 264 | copy_len = cx_array_binary_search_sup_c( |
| 324 | 265 | bptr, |
| 266 | new_size - bi, | |
| 267 | elem_size, | |
| 268 | src, | |
| 1016 | 269 | cmp_func, |
| 270 | context | |
| 324 | 271 | ); |
| 272 | ||
| 273 | // restore the buffered elements | |
| 274 | bytes_copied = copy_len * elem_size; | |
| 275 | memmove(dest, bptr, bytes_copied); | |
| 276 | dest += bytes_copied; | |
| 277 | bptr += bytes_copied; | |
| 845 | 278 | di += copy_len; |
| 324 | 279 | bi += copy_len; |
| 280 | } | |
| 281 | ||
| 845 | 282 | // still source elements left? |
| 1016 | 283 | if (si < n) { |
| 845 | 284 | if (allow_duplicates) { |
| 285 | // duplicates allowed or nothing inserted yet: simply copy everything | |
| 1016 | 286 | memcpy(dest, src, elem_size * (n - si)); |
| 845 | 287 | } else { |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
288 | // we must check the remaining source elements one by one |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
289 | // to skip the duplicates. |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
290 | // Note that no source element can equal the last element in the |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
291 | // destination, because that would have created an insertion point |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
292 | // and a buffer, s.t. the above loop already handled the duplicates |
| 1016 | 293 | while (si < n) { |
| 845 | 294 | // find a chain of elements that can be copied |
| 295 | size_t copy_len = 1, skip_len = 0; | |
| 296 | { | |
| 297 | const char *left_src = src; | |
| 1016 | 298 | while (si + copy_len + skip_len < n) { |
| 845 | 299 | const char *right_src = left_src + elem_size; |
| 1016 | 300 | int d = cmp_func(left_src, right_src, context); |
| 845 | 301 | if (d < 0) { |
| 302 | if (skip_len > 0) { | |
| 303 | // new larger element found; | |
| 304 | // handle it in the next cycle | |
| 305 | break; | |
| 306 | } | |
| 307 | left_src += elem_size; | |
| 308 | copy_len++; | |
| 309 | } else if (d == 0) { | |
| 310 | left_src += elem_size; | |
| 311 | skip_len++; | |
| 312 | } else { | |
| 313 | break; | |
| 314 | } | |
| 315 | } | |
| 316 | } | |
| 317 | size_t bytes_copied = copy_len * elem_size; | |
| 318 | memcpy(dest, src, bytes_copied); | |
| 319 | dest += bytes_copied; | |
| 320 | src += bytes_copied + skip_len * elem_size; | |
| 321 | si += copy_len + skip_len; | |
| 322 | di += copy_len; | |
| 1016 | 323 | array->size -= skip_len; |
| 845 | 324 | } |
| 325 | } | |
| 324 | 326 | } |
| 327 | ||
| 845 | 328 | // buffered elements need to be moved when we skipped duplicates |
| 1016 | 329 | size_t total_skipped = new_size - array->size; |
| 845 | 330 | if (bi < new_size && total_skipped > 0) { |
| 331 | // move the remaining buffer to the end of the array | |
| 332 | memmove(dest, bptr, elem_size * (new_size - bi)); | |
| 333 | } | |
| 324 | 334 | |
| 440 | 335 | return 0; |
| 324 | 336 | } |
| 337 | ||
| 1016 | 338 | int cx_array_insert_sorted_( |
| 339 | const CxAllocator *allocator, | |
| 340 | CxArray *array, | |
| 341 | size_t elem_size, | |
| 845 | 342 | cx_compare_func cmp_func, |
| 343 | const void *sorted_data, | |
| 1016 | 344 | size_t n, |
| 345 | bool allow_duplicates | |
| 845 | 346 | ) { |
| 1016 | 347 | cx_compare_func_wrapper wrapper = {cmp_func}; |
| 348 | return cx_array_insert_sorted_s_(allocator, array, elem_size, sorted_data, | |
| 349 | n, allow_duplicates, cx_acmp_wrap, &wrapper); | |
| 845 | 350 | } |
| 351 | ||
| 1016 | 352 | CxIterator cx_array_iterator_(CxArray *array, size_t elem_size) { |
| 353 | return cxIterator(array->data, elem_size, array->size); | |
| 354 | } | |
| 355 | ||
| 356 | CxIterator cx_array_iterator_ptr_(CxArray *array) { | |
| 357 | return cxIteratorPtr(array->data, array->size); | |
| 845 | 358 | } |
| 359 | ||
| 1016 | 360 | void cx_array_free_(const CxAllocator *allocator, CxArray *array) { |
| 361 | cxFree(allocator, array->data); | |
| 362 | array->data = NULL; | |
| 363 | array->size = array->capacity = 0; | |
| 364 | } | |
| 365 | ||
| 366 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
367 | // implementation that finds ANY index |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
368 | static size_t cx_array_binary_search_inf_impl( |
| 324 | 369 | const void *arr, |
| 370 | size_t size, | |
| 371 | size_t elem_size, | |
| 372 | const void *elem, | |
| 1016 | 373 | cx_compare_func2 cmp_func, |
| 374 | void *context | |
| 324 | 375 | ) { |
| 376 | // special case: empty array | |
| 377 | if (size == 0) return 0; | |
| 378 | ||
| 379 | // declare a variable that will contain the compare results | |
| 380 | int result; | |
| 381 | ||
| 382 | // cast the array pointer to something we can use offsets with | |
| 383 | const char *array = arr; | |
| 384 | ||
| 385 | // check the first array element | |
| 1016 | 386 | result = cmp_func(elem, array, context); |
| 324 | 387 | if (result < 0) { |
| 388 | return size; | |
| 389 | } else if (result == 0) { | |
| 390 | return 0; | |
| 391 | } | |
| 392 | ||
| 440 | 393 | // special case: there is only one element and that is smaller |
| 394 | if (size == 1) return 0; | |
| 395 | ||
| 324 | 396 | // check the last array element |
| 1016 | 397 | result = cmp_func(elem, array + elem_size * (size - 1), context); |
| 324 | 398 | if (result >= 0) { |
| 399 | return size - 1; | |
| 400 | } | |
| 401 | ||
| 402 | // the element is now guaranteed to be somewhere in the list | |
| 403 | // so start the binary search | |
| 404 | size_t left_index = 1; | |
| 405 | size_t right_index = size - 1; | |
| 1016 | 406 | size_t pivot_index = 0; |
| 324 | 407 | |
| 408 | while (left_index <= right_index) { | |
| 409 | pivot_index = left_index + (right_index - left_index) / 2; | |
| 410 | const char *arr_elem = array + pivot_index * elem_size; | |
| 1016 | 411 | result = cmp_func(elem, arr_elem, context); |
| 324 | 412 | if (result == 0) { |
| 413 | // found it! | |
| 414 | return pivot_index; | |
| 415 | } else if (result < 0) { | |
| 416 | // element is smaller than pivot, continue search left | |
| 417 | right_index = pivot_index - 1; | |
| 418 | } else { | |
| 419 | // element is larger than pivot, continue search right | |
| 420 | left_index = pivot_index + 1; | |
| 421 | } | |
| 422 | } | |
| 423 | ||
| 424 | // report the largest upper bound | |
| 425 | return result < 0 ? (pivot_index - 1) : pivot_index; | |
| 426 | } | |
| 427 | ||
| 1016 | 428 | size_t cx_array_binary_search_inf_c( |
| 429 | const void *arr, | |
| 430 | size_t size, | |
| 431 | size_t elem_size, | |
| 432 | const void *elem, | |
| 433 | cx_compare_func2 cmp_func, | |
| 434 | void *context | |
| 435 | ) { | |
| 436 | size_t index = cx_array_binary_search_inf_impl( | |
| 437 | arr, size, elem_size, elem, cmp_func, context); | |
| 438 | // in case of equality, report the largest index | |
| 439 | const char *e = ((const char *) arr) + (index + 1) * elem_size; | |
| 440 | while (index + 1 < size && cmp_func(e, elem, context) == 0) { | |
| 441 | e += elem_size; | |
| 442 | index++; | |
| 443 | } | |
| 444 | return index; | |
| 445 | } | |
| 446 | ||
| 447 | size_t cx_array_binary_search_c( | |
| 448 | const void *arr, | |
| 449 | size_t size, | |
| 450 | size_t elem_size, | |
| 451 | const void *elem, | |
| 452 | cx_compare_func2 cmp_func, | |
| 453 | void *context | |
| 454 | ) { | |
| 455 | size_t index = cx_array_binary_search_inf_c( | |
| 456 | arr, size, elem_size, elem, cmp_func, context | |
| 457 | ); | |
| 458 | if (index < size && cmp_func(((const char *) arr) + index * elem_size, | |
| 459 | elem, context) == 0) { | |
| 460 | return index; | |
| 461 | } else { | |
| 462 | return size; | |
| 463 | } | |
| 464 | } | |
| 465 | ||
| 466 | size_t cx_array_binary_search_sup_c( | |
| 467 | const void *arr, | |
| 468 | size_t size, | |
| 469 | size_t elem_size, | |
| 470 | const void *elem, | |
| 471 | cx_compare_func2 cmp_func, | |
| 472 | void *context | |
| 473 | ) { | |
| 474 | size_t index = cx_array_binary_search_inf_impl( | |
| 475 | arr, size, elem_size, elem, cmp_func, context | |
| 476 | ); | |
| 477 | const char *e = ((const char *) arr) + index * elem_size; | |
| 478 | if (index == size) { | |
| 479 | // no infimum means the first element is supremum | |
| 480 | return 0; | |
| 481 | } else if (cmp_func(e, elem, context) == 0) { | |
| 482 | // found an equal element, search the smallest index | |
| 483 | e -= elem_size; // e now contains the element at index-1 | |
| 484 | while (index > 0 && cmp_func(e, elem, context) == 0) { | |
| 485 | e -= elem_size; | |
| 486 | index--; | |
| 487 | } | |
| 488 | return index; | |
| 489 | } else { | |
| 490 | // we already have the largest index of the infimum (by design) | |
| 491 | // the next element is the supremum (or there is no supremum) | |
| 492 | return index + 1; | |
| 493 | } | |
| 494 | } | |
| 495 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
496 | size_t cx_array_binary_search_inf( |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
497 | const void *arr, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
498 | size_t size, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
499 | size_t elem_size, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
500 | const void *elem, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
501 | cx_compare_func cmp_func |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
502 | ) { |
| 1016 | 503 | cx_compare_func_wrapper wrapper = {cmp_func}; |
| 504 | return cx_array_binary_search_inf_c(arr, size, elem_size, elem, cx_acmp_wrap, &wrapper); | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
505 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
506 | |
| 440 | 507 | size_t cx_array_binary_search( |
| 508 | const void *arr, | |
| 509 | size_t size, | |
| 510 | size_t elem_size, | |
| 511 | const void *elem, | |
| 512 | cx_compare_func cmp_func | |
| 513 | ) { | |
| 1016 | 514 | cx_compare_func_wrapper wrapper = {cmp_func}; |
| 515 | return cx_array_binary_search_c(arr, size, elem_size, elem, cx_acmp_wrap, &wrapper); | |
| 440 | 516 | } |
| 517 | ||
| 518 | size_t cx_array_binary_search_sup( | |
| 519 | const void *arr, | |
| 520 | size_t size, | |
| 521 | size_t elem_size, | |
| 522 | const void *elem, | |
| 523 | cx_compare_func cmp_func | |
| 524 | ) { | |
| 1016 | 525 | cx_compare_func_wrapper wrapper = {cmp_func}; |
| 526 | return cx_array_binary_search_sup_c(arr, size, elem_size, elem, cx_acmp_wrap, &wrapper); | |
| 440 | 527 | } |
| 528 | ||
| 174 | 529 | #ifndef CX_ARRAY_SWAP_SBO_SIZE |
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
530 | #define CX_ARRAY_SWAP_SBO_SIZE 128 |
| 174 | 531 | #endif |
| 440 | 532 | const unsigned cx_array_swap_sbo_size = CX_ARRAY_SWAP_SBO_SIZE; |
| 174 | 533 | |
| 534 | void cx_array_swap( | |
| 535 | void *arr, | |
| 536 | size_t elem_size, | |
| 537 | size_t idx1, | |
| 538 | size_t idx2 | |
| 539 | ) { | |
| 540 | assert(arr != NULL); | |
| 541 | ||
| 542 | // short circuit | |
| 543 | if (idx1 == idx2) return; | |
| 544 | ||
| 545 | char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE]; | |
| 546 | void *tmp; | |
| 547 | ||
| 548 | // decide if we can use the local buffer | |
| 549 | if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) { | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
550 | tmp = cxMallocDefault(elem_size); |
| 174 | 551 | // we don't want to enforce error handling |
| 552 | if (tmp == NULL) abort(); | |
| 553 | } else { | |
| 554 | tmp = sbo_mem; | |
| 555 | } | |
| 556 | ||
| 557 | // calculate memory locations | |
| 558 | char *left = arr, *right = arr; | |
| 559 | left += idx1 * elem_size; | |
| 560 | right += idx2 * elem_size; | |
| 561 | ||
| 562 | // three-way swap | |
| 563 | memcpy(tmp, left, elem_size); | |
| 564 | memcpy(left, right, elem_size); | |
| 565 | memcpy(right, tmp, elem_size); | |
| 566 | ||
| 567 | // free dynamic memory, if it was needed | |
| 568 | if (tmp != sbo_mem) { | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
569 | cxFreeDefault(tmp); |
| 174 | 570 | } |
| 571 | } | |
| 572 | ||
| 573 | // HIGH LEVEL ARRAY LIST FUNCTIONS | |
| 574 | ||
| 575 | typedef struct { | |
| 576 | struct cx_list_s base; | |
| 577 | void *data; | |
| 578 | size_t capacity; | |
| 579 | } cx_array_list; | |
| 580 | ||
| 581 | static void cx_arl_destructor(struct cx_list_s *list) { | |
| 582 | cx_array_list *arl = (cx_array_list *) list; | |
| 583 | ||
| 584 | char *ptr = arl->data; | |
| 585 | ||
| 324 | 586 | if (list->collection.simple_destructor) { |
| 587 | for (size_t i = 0; i < list->collection.size; i++) { | |
| 174 | 588 | cx_invoke_simple_destructor(list, ptr); |
| 324 | 589 | ptr += list->collection.elem_size; |
| 174 | 590 | } |
| 591 | } | |
| 324 | 592 | if (list->collection.advanced_destructor) { |
| 593 | for (size_t i = 0; i < list->collection.size; i++) { | |
| 174 | 594 | cx_invoke_advanced_destructor(list, ptr); |
| 324 | 595 | ptr += list->collection.elem_size; |
| 174 | 596 | } |
| 597 | } | |
| 598 | ||
| 324 | 599 | cxFree(list->collection.allocator, arl->data); |
| 600 | cxFree(list->collection.allocator, list); | |
| 174 | 601 | } |
| 602 | ||
| 603 | static size_t cx_arl_insert_array( | |
| 604 | struct cx_list_s *list, | |
| 605 | size_t index, | |
| 324 | 606 | const void *array, |
| 174 | 607 | size_t n |
| 608 | ) { | |
| 609 | cx_array_list *arl = (cx_array_list *) list; | |
| 1016 | 610 | CxArray wrap = { |
| 611 | arl->data, list->collection.size, arl->capacity | |
| 612 | }; | |
| 613 | if (cx_array_insert_(list->collection.allocator, &wrap, | |
| 614 | list->collection.elem_size, index, array, n)) { | |
| 615 | return 0; | |
| 174 | 616 | } |
| 1016 | 617 | arl->data = wrap.data; |
| 618 | arl->capacity = wrap.capacity; | |
| 619 | list->collection.size = wrap.size; | |
| 620 | return n; | |
| 621 | } | |
| 174 | 622 | |
| 1016 | 623 | static size_t cx_arl_insert_sorted_impl( |
| 624 | struct cx_list_s *list, | |
| 625 | const void *sorted_data, | |
| 626 | size_t n, | |
| 627 | bool allow_duplicates | |
| 628 | ) { | |
| 629 | cx_array_list *arl = (cx_array_list *) list; | |
| 630 | CxArray wrap = { | |
| 631 | arl->data, list->collection.size, arl->capacity | |
| 632 | }; | |
| 174 | 633 | |
| 1016 | 634 | if (cx_array_insert_sorted_s_( |
| 635 | list->collection.allocator, | |
| 636 | &wrap, | |
| 637 | list->collection.elem_size, | |
| 638 | sorted_data, | |
| 639 | n, | |
| 640 | allow_duplicates, | |
| 641 | cx_list_compare_wrapper, | |
| 642 | list | |
| 643 | )) { | |
| 644 | // array list implementation is "all or nothing" | |
| 645 | return 0; // LCOV_EXCL_LINE | |
| 324 | 646 | } |
| 1016 | 647 | arl->data = wrap.data; |
| 648 | arl->capacity = wrap.capacity; | |
| 649 | list->collection.size = wrap.size; | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
650 | return n; |
| 324 | 651 | } |
| 652 | ||
| 653 | static size_t cx_arl_insert_sorted( | |
| 654 | struct cx_list_s *list, | |
| 655 | const void *sorted_data, | |
| 656 | size_t n | |
| 657 | ) { | |
| 1016 | 658 | return cx_arl_insert_sorted_impl(list, sorted_data, n, true); |
| 174 | 659 | } |
| 660 | ||
| 845 | 661 | static size_t cx_arl_insert_unique( |
| 662 | struct cx_list_s *list, | |
| 663 | const void *sorted_data, | |
| 664 | size_t n | |
| 665 | ) { | |
| 1016 | 666 | return cx_arl_insert_sorted_impl(list, sorted_data, n, false); |
| 845 | 667 | } |
| 668 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
669 | static void *cx_arl_insert_element( |
| 174 | 670 | struct cx_list_s *list, |
| 671 | size_t index, | |
| 324 | 672 | const void *element |
| 174 | 673 | ) { |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
674 | if (cx_arl_insert_array(list, index, element, 1) == 1) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
675 | return ((char*)((cx_array_list *) list)->data) + index * list->collection.elem_size; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
676 | } else { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
677 | return NULL; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
678 | } |
| 174 | 679 | } |
| 680 | ||
| 681 | static int cx_arl_insert_iter( | |
| 324 | 682 | struct cx_iterator_s *iter, |
| 683 | const void *elem, | |
| 174 | 684 | int prepend |
| 685 | ) { | |
| 870 | 686 | struct cx_list_s *list = iter->src_handle; |
| 324 | 687 | if (iter->index < list->collection.size) { |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
688 | if (cx_arl_insert_element(list, |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
689 | iter->index + 1 - prepend, elem) == NULL) { |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
690 | return 1; // LCOV_EXCL_LINE |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
691 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
692 | iter->elem_count++; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
693 | if (prepend != 0) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
694 | iter->index++; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
695 | iter->elem_handle = ((char *) iter->elem_handle) + list->collection.elem_size; |
| 174 | 696 | } |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
697 | return 0; |
| 174 | 698 | } else { |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
699 | if (cx_arl_insert_element(list, list->collection.size, elem) == NULL) { |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
700 | return 1; // LCOV_EXCL_LINE |
| 324 | 701 | } |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
702 | iter->elem_count++; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
703 | iter->index = list->collection.size; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
704 | return 0; |
| 174 | 705 | } |
| 706 | } | |
| 707 | ||
| 440 | 708 | static size_t cx_arl_remove( |
| 174 | 709 | struct cx_list_s *list, |
| 440 | 710 | size_t index, |
| 711 | size_t num, | |
| 712 | void *targetbuf | |
| 174 | 713 | ) { |
| 714 | cx_array_list *arl = (cx_array_list *) list; | |
| 715 | ||
| 716 | // out-of-bounds check | |
| 440 | 717 | size_t remove; |
| 324 | 718 | if (index >= list->collection.size) { |
| 440 | 719 | remove = 0; |
| 720 | } else if (index + num > list->collection.size) { | |
| 721 | remove = list->collection.size - index; | |
| 722 | } else { | |
| 723 | remove = num; | |
| 174 | 724 | } |
| 725 | ||
| 440 | 726 | // easy exit |
| 727 | if (remove == 0) return 0; | |
| 174 | 728 | |
| 440 | 729 | // destroy or copy contents |
| 730 | if (targetbuf == NULL) { | |
| 731 | for (size_t idx = index; idx < index + remove; idx++) { | |
| 732 | cx_invoke_destructor( | |
| 733 | list, | |
| 734 | ((char *) arl->data) + idx * list->collection.elem_size | |
| 735 | ); | |
| 736 | } | |
| 737 | } else { | |
| 738 | memcpy( | |
| 739 | targetbuf, | |
| 740 | ((char *) arl->data) + index * list->collection.elem_size, | |
| 741 | remove * list->collection.elem_size | |
| 742 | ); | |
| 174 | 743 | } |
| 744 | ||
| 1016 | 745 | // calculate how many elements would need to be moved |
| 746 | size_t remaining = list->collection.size - index - remove; | |
| 747 | ||
| 440 | 748 | // short-circuit removal of last elements |
| 1016 | 749 | if (remaining == 0) { |
| 440 | 750 | list->collection.size -= remove; |
| 751 | return remove; | |
| 752 | } | |
| 753 | ||
| 754 | // just move the elements to the left | |
| 1016 | 755 | char *first_remaining = arl->data; |
| 756 | first_remaining += (index + remove) * list->collection.elem_size; | |
| 757 | char *dst_move = arl->data; | |
| 758 | dst_move += index * list->collection.elem_size; | |
| 759 | memmove(dst_move, first_remaining, remaining * list->collection.elem_size); | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
760 | |
| 440 | 761 | // decrease the size |
| 762 | list->collection.size -= remove; | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
763 | |
| 440 | 764 | return remove; |
| 174 | 765 | } |
| 766 | ||
| 767 | static void cx_arl_clear(struct cx_list_s *list) { | |
| 324 | 768 | if (list->collection.size == 0) return; |
| 174 | 769 | |
| 770 | cx_array_list *arl = (cx_array_list *) list; | |
| 771 | char *ptr = arl->data; | |
| 772 | ||
| 324 | 773 | if (list->collection.simple_destructor) { |
| 774 | for (size_t i = 0; i < list->collection.size; i++) { | |
| 174 | 775 | cx_invoke_simple_destructor(list, ptr); |
| 324 | 776 | ptr += list->collection.elem_size; |
| 174 | 777 | } |
| 778 | } | |
| 324 | 779 | if (list->collection.advanced_destructor) { |
| 780 | for (size_t i = 0; i < list->collection.size; i++) { | |
| 174 | 781 | cx_invoke_advanced_destructor(list, ptr); |
| 324 | 782 | ptr += list->collection.elem_size; |
| 174 | 783 | } |
| 784 | } | |
| 785 | ||
| 324 | 786 | memset(arl->data, 0, list->collection.size * list->collection.elem_size); |
| 787 | list->collection.size = 0; | |
| 174 | 788 | } |
| 789 | ||
| 790 | static int cx_arl_swap( | |
| 791 | struct cx_list_s *list, | |
| 792 | size_t i, | |
| 793 | size_t j | |
| 794 | ) { | |
| 324 | 795 | if (i >= list->collection.size || j >= list->collection.size) return 1; |
| 174 | 796 | cx_array_list *arl = (cx_array_list *) list; |
| 324 | 797 | cx_array_swap(arl->data, list->collection.elem_size, i, j); |
| 174 | 798 | return 0; |
| 799 | } | |
| 800 | ||
| 801 | static void *cx_arl_at( | |
| 324 | 802 | const struct cx_list_s *list, |
| 174 | 803 | size_t index |
| 804 | ) { | |
| 324 | 805 | if (index < list->collection.size) { |
| 806 | const cx_array_list *arl = (const cx_array_list *) list; | |
| 174 | 807 | char *space = arl->data; |
| 324 | 808 | return space + index * list->collection.elem_size; |
| 174 | 809 | } else { |
| 810 | return NULL; | |
| 811 | } | |
| 812 | } | |
| 813 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
814 | static size_t cx_arl_find_remove( |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
815 | struct cx_list_s *list, |
| 324 | 816 | const void *elem, |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
817 | bool remove |
| 174 | 818 | ) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
819 | assert(list != NULL); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
820 | if (list->collection.size == 0) return 0; |
| 324 | 821 | char *cur = ((const cx_array_list *) list)->data; |
| 174 | 822 | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
823 | // optimize with binary search, when sorted |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
824 | if (list->collection.sorted) { |
| 1016 | 825 | size_t i = cx_array_binary_search_c( |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
826 | cur, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
827 | list->collection.size, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
828 | list->collection.elem_size, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
829 | elem, |
| 1016 | 830 | cx_list_compare_wrapper, |
| 831 | list | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
832 | ); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
833 | if (remove && i < list->collection.size) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
834 | cx_arl_remove(list, i, 1, NULL); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
835 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
836 | return i; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
837 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
838 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
839 | // fallback: linear search |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
840 | for (size_t i = 0; i < list->collection.size; i++) { |
| 1016 | 841 | if (0 == cx_list_compare_wrapper(elem, cur, list)) { |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
842 | if (remove) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
843 | cx_arl_remove(list, i, 1, NULL); |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
844 | } |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
845 | return i; |
| 174 | 846 | } |
| 324 | 847 | cur += list->collection.elem_size; |
| 174 | 848 | } |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
849 | return list->collection.size; |
| 174 | 850 | } |
| 851 | ||
| 1016 | 852 | // TODO: remove this hack once we have a solution for qsort() / qsort_s() |
| 853 | static _Thread_local CxList *cx_hack_for_qsort_list; | |
| 854 | static int cx_hack_cmp_for_qsort(const void *l, const void *r) { | |
| 855 | return cx_list_compare_wrapper(l, r, cx_hack_for_qsort_list); | |
| 856 | } | |
| 857 | ||
| 174 | 858 | static void cx_arl_sort(struct cx_list_s *list) { |
| 1016 | 859 | // TODO: think about if we can somehow use qsort()_s |
| 860 | cx_hack_for_qsort_list = list; | |
| 174 | 861 | qsort(((cx_array_list *) list)->data, |
| 324 | 862 | list->collection.size, |
| 863 | list->collection.elem_size, | |
| 1016 | 864 | cx_hack_cmp_for_qsort |
| 174 | 865 | ); |
| 866 | } | |
| 867 | ||
| 868 | static int cx_arl_compare( | |
| 324 | 869 | const struct cx_list_s *list, |
| 870 | const struct cx_list_s *other | |
| 174 | 871 | ) { |
| 324 | 872 | if (list->collection.size == other->collection.size) { |
| 873 | const char *left = ((const cx_array_list *) list)->data; | |
| 874 | const char *right = ((const cx_array_list *) other)->data; | |
| 875 | for (size_t i = 0; i < list->collection.size; i++) { | |
| 1016 | 876 | int d = cx_list_compare_wrapper(left, right, (void*)list); |
| 174 | 877 | if (d != 0) { |
| 878 | return d; | |
| 879 | } | |
| 324 | 880 | left += list->collection.elem_size; |
| 881 | right += other->collection.elem_size; | |
| 174 | 882 | } |
| 883 | return 0; | |
| 884 | } else { | |
| 324 | 885 | return list->collection.size < other->collection.size ? -1 : 1; |
| 174 | 886 | } |
| 887 | } | |
| 888 | ||
| 889 | static void cx_arl_reverse(struct cx_list_s *list) { | |
| 324 | 890 | if (list->collection.size < 2) return; |
| 891 | void *data = ((const cx_array_list *) list)->data; | |
| 892 | size_t half = list->collection.size / 2; | |
| 174 | 893 | for (size_t i = 0; i < half; i++) { |
| 324 | 894 | cx_array_swap(data, list->collection.elem_size, i, list->collection.size - 1 - i); |
| 174 | 895 | } |
| 896 | } | |
| 897 | ||
| 324 | 898 | static bool cx_arl_iter_valid(const void *it) { |
| 899 | const struct cx_iterator_s *iter = it; | |
| 870 | 900 | const struct cx_list_s *list = iter->src_handle; |
| 324 | 901 | return iter->index < list->collection.size; |
| 174 | 902 | } |
| 903 | ||
| 324 | 904 | static void *cx_arl_iter_current(const void *it) { |
| 905 | const struct cx_iterator_s *iter = it; | |
| 174 | 906 | return iter->elem_handle; |
| 907 | } | |
| 908 | ||
| 909 | static void cx_arl_iter_next(void *it) { | |
| 324 | 910 | struct cx_iterator_s *iter = it; |
| 911 | if (iter->base.remove) { | |
| 912 | iter->base.remove = false; | |
| 870 | 913 | cx_arl_remove(iter->src_handle, iter->index, 1, NULL); |
| 845 | 914 | iter->elem_count--; |
| 174 | 915 | } else { |
| 916 | iter->index++; | |
| 917 | iter->elem_handle = | |
| 918 | ((char *) iter->elem_handle) | |
| 870 | 919 | + ((const struct cx_list_s *) iter->src_handle)->collection.elem_size; |
| 174 | 920 | } |
| 921 | } | |
| 922 | ||
| 923 | static void cx_arl_iter_prev(void *it) { | |
| 324 | 924 | struct cx_iterator_s *iter = it; |
| 925 | if (iter->base.remove) { | |
| 926 | iter->base.remove = false; | |
| 870 | 927 | cx_arl_remove(iter->src_handle, iter->index, 1, NULL); |
| 845 | 928 | iter->elem_count--; |
| 174 | 929 | } |
| 930 | iter->index--; | |
| 870 | 931 | cx_array_list *list = iter->src_handle; |
| 324 | 932 | if (iter->index < list->base.collection.size) { |
| 174 | 933 | iter->elem_handle = ((char *) list->data) |
| 324 | 934 | + iter->index * list->base.collection.elem_size; |
| 174 | 935 | } |
| 936 | } | |
| 937 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
938 | static int cx_arl_change_capacity( |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
939 | struct cx_list_s *list, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
940 | size_t new_capacity |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
941 | ) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
942 | cx_array_list *arl = (cx_array_list *)list; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
943 | return cxReallocateArray(list->collection.allocator, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
944 | &arl->data, new_capacity, list->collection.elem_size); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
945 | } |
| 174 | 946 | |
| 947 | static struct cx_iterator_s cx_arl_iterator( | |
| 324 | 948 | const struct cx_list_s *list, |
| 174 | 949 | size_t index, |
| 950 | bool backwards | |
| 951 | ) { | |
| 952 | struct cx_iterator_s iter; | |
| 953 | ||
| 954 | iter.index = index; | |
| 870 | 955 | iter.src_handle = (void*)list; |
| 174 | 956 | iter.elem_handle = cx_arl_at(list, index); |
| 324 | 957 | iter.elem_size = list->collection.elem_size; |
| 958 | iter.elem_count = list->collection.size; | |
| 174 | 959 | iter.base.valid = cx_arl_iter_valid; |
| 960 | iter.base.current = cx_arl_iter_current; | |
| 961 | iter.base.next = backwards ? cx_arl_iter_prev : cx_arl_iter_next; | |
| 962 | iter.base.remove = false; | |
| 870 | 963 | iter.base.allow_remove = true; |
| 174 | 964 | |
| 965 | return iter; | |
| 966 | } | |
| 967 | ||
| 968 | static cx_list_class cx_array_list_class = { | |
| 969 | cx_arl_destructor, | |
| 970 | cx_arl_insert_element, | |
| 971 | cx_arl_insert_array, | |
| 324 | 972 | cx_arl_insert_sorted, |
| 845 | 973 | cx_arl_insert_unique, |
| 174 | 974 | cx_arl_insert_iter, |
| 975 | cx_arl_remove, | |
| 976 | cx_arl_clear, | |
| 977 | cx_arl_swap, | |
| 978 | cx_arl_at, | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
979 | cx_arl_find_remove, |
| 174 | 980 | cx_arl_sort, |
| 981 | cx_arl_compare, | |
| 982 | cx_arl_reverse, | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
983 | cx_arl_change_capacity, |
| 174 | 984 | cx_arl_iterator, |
| 985 | }; | |
| 986 | ||
| 987 | CxList *cxArrayListCreate( | |
| 324 | 988 | const CxAllocator *allocator, |
| 989 | size_t elem_size, | |
| 174 | 990 | size_t initial_capacity |
| 991 | ) { | |
| 992 | if (allocator == NULL) { | |
| 993 | allocator = cxDefaultAllocator; | |
| 994 | } | |
| 995 | ||
| 996 | cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list)); | |
| 997 | if (list == NULL) return NULL; | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
998 | cx_list_init((CxList*)list, &cx_array_list_class, |
| 1016 | 999 | allocator, elem_size); |
| 174 | 1000 | list->capacity = initial_capacity; |
| 1001 | ||
| 324 | 1002 | // allocate the array after the real elem_size is known |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1003 | list->data = cxCalloc(allocator, initial_capacity, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1004 | list->base.collection.elem_size); |
| 440 | 1005 | if (list->data == NULL) { // LCOV_EXCL_START |
| 174 | 1006 | cxFree(allocator, list); |
| 1007 | return NULL; | |
| 440 | 1008 | } // LCOV_EXCL_STOP |
| 174 | 1009 | |
| 1010 | return (CxList *) list; | |
| 1011 | } |