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