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