Wed, 31 Dec 2025 16:41:16 +0100
update ucx to version 4.0
|
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 | ||
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
104 | // calculate required capacity |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
105 | size_t req_capacity = array->size + n; |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
106 | if (req_capacity <= array->size) { |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
107 | errno = EOVERFLOW; |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
108 | return -1; |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
109 | } |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
110 | |
| 891 | 111 | // guarantee enough capacity |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
112 | if (array->capacity < req_capacity) { |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
113 | const size_t new_capacity = cx_array_grow_capacity(array->capacity,req_capacity); |
| 891 | 114 | if (cxReallocateArray(allocator, &array->data, new_capacity, elem_size)) { |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
115 | return -1; |
| 891 | 116 | } |
| 117 | array->capacity = new_capacity; | |
| 852 | 118 | } |
| 119 | ||
| 891 | 120 | // determine insert position |
| 121 | char *dst = array->data; | |
| 122 | dst += index * elem_size; | |
| 123 | ||
| 124 | // do we need to move some elements? | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
125 | size_t elems_to_move = array->size - index; |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
126 | if (elems_to_move > 0) { |
| 891 | 127 | char *target = dst + n * elem_size; |
| 128 | memmove(target, dst, elems_to_move * elem_size); | |
| 852 | 129 | } |
| 130 | ||
| 891 | 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); | |
| 852 | 135 | } |
| 891 | 136 | array->size += n; |
| 852 | 137 | |
| 138 | return 0; | |
| 139 | } | |
| 140 | ||
| 891 | 141 | int cx_array_insert_sorted_c_( |
| 142 | const CxAllocator *allocator, | |
| 143 | CxArray *array, | |
| 852 | 144 | size_t elem_size, |
| 891 | 145 | const void *sorted_data, |
| 146 | size_t n, | |
| 147 | cx_compare_func2 cmp_func, | |
| 148 | void *context, | |
| 149 | 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
|
150 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
151 | // assert pointers |
| 891 | 152 | assert(allocator != NULL); |
| 153 | assert(array != NULL); | |
| 154 | assert(cmp_func != NULL); | |
| 155 | assert(sorted_data != NULL); | |
| 852 | 156 | |
| 891 | 157 | // corner case |
| 158 | if (n == 0) return 0; | |
| 852 | 159 | |
| 891 | 160 | // overflow check |
| 161 | // LCOV_EXCL_START | |
| 162 | if (n > SIZE_MAX - array->size) { | |
| 852 | 163 | errno = EOVERFLOW; |
| 164 | return 1; | |
| 165 | } | |
| 891 | 166 | // LCOV_EXCL_STOP |
| 852 | 167 | |
| 168 | // store some counts | |
| 891 | 169 | const size_t old_size = array->size; |
| 170 | const size_t old_capacity = array->capacity; | |
| 889 | 171 | // the necessary capacity is the worst case assumption, including duplicates |
| 891 | 172 | const size_t needed_capacity = cx_array_grow_capacity(old_capacity, old_size + n); |
| 852 | 173 | |
| 174 | // if we need more than we have, try a reallocation | |
| 886 | 175 | if (needed_capacity > old_capacity) { |
| 891 | 176 | if (cxReallocateArray(allocator, &array->data, needed_capacity, elem_size)) { |
| 177 | return -1; // LCOV_EXCL_LINE | |
| 852 | 178 | } |
| 891 | 179 | array->capacity = needed_capacity; |
| 852 | 180 | } |
| 181 | ||
| 182 | // now we have guaranteed that we can insert everything | |
| 891 | 183 | size_t new_size = old_size + n; |
| 184 | array->size = new_size; | |
| 852 | 185 | |
| 186 | // declare the source and destination indices/pointers | |
| 187 | size_t si = 0, di = 0; | |
| 188 | const char *src = sorted_data; | |
| 891 | 189 | char *dest = array->data; |
| 852 | 190 | |
| 191 | // find the first insertion point | |
| 891 | 192 | di = cx_array_binary_search_sup_c(dest, old_size, elem_size, src, cmp_func, context); |
| 852 | 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; | |
| 891 | 199 | char *bptr = ((char *) array->data) + bi * elem_size; |
| 852 | 200 | memmove(bptr, dest, buf_size * elem_size); |
| 201 | ||
| 202 | // while there are both source and buffered elements left, | |
| 203 | // copy them interleaving | |
| 891 | 204 | while (si < n && bi < new_size) { |
| 205 | // determine how many source elements can be inserted. | |
| 206 | // the first element that shall not be inserted is the smallest element | |
| 207 | // that is strictly larger than the first buffered element | |
| 208 | // (located at the index of the infimum plus one). | |
| 209 | // the infimum is guaranteed to exist: | |
| 210 | // - if all src elements are larger, | |
| 211 | // there is no buffer, and this loop is skipped | |
| 212 | // - if any src element is smaller or equal, the infimum exists | |
| 213 | // - when all src elements that are smaller are copied, the second part | |
| 214 | // of this loop body will copy the remaining buffer (emptying it) | |
| 215 | // Therefore, the buffer can never contain an element that is smaller | |
| 216 | // than any element in the source and the infimum exists. | |
| 852 | 217 | size_t copy_len, bytes_copied; |
| 891 | 218 | copy_len = cx_array_binary_search_inf_c( |
| 219 | src, n - si, elem_size, bptr, cmp_func, context | |
| 852 | 220 | ); |
| 891 | 221 | copy_len++; |
| 852 | 222 | |
| 223 | // copy the source elements | |
| 889 | 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; | |
| 891 | 238 | while (copy_len > 0 && cmp_func(bptr, end_of_src, context) == 0) { |
| 889 | 239 | end_of_src -= elem_size; |
| 240 | skip_len++; | |
| 241 | copy_len--; | |
| 242 | } | |
| 891 | 243 | char *last = dest == array->data ? NULL : dest - elem_size; |
| 889 | 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++) { | |
| 891 | 248 | if (last != NULL && cmp_func(last, src, context) == 0) { |
| 889 | 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 | |
| 891 | 267 | array->size -= skip_len; |
| 889 | 268 | } |
| 269 | } | |
| 852 | 270 | |
| 271 | // when all source elements are in place, we are done | |
| 891 | 272 | if (si >= n) break; |
| 852 | 273 | |
| 274 | // determine how many buffered elements need to be restored | |
| 891 | 275 | copy_len = cx_array_binary_search_sup_c( |
| 852 | 276 | bptr, |
| 277 | new_size - bi, | |
| 278 | elem_size, | |
| 279 | src, | |
| 891 | 280 | cmp_func, |
| 281 | context | |
| 852 | 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; | |
| 889 | 289 | di += copy_len; |
| 852 | 290 | bi += copy_len; |
| 291 | } | |
| 292 | ||
| 889 | 293 | // still source elements left? |
| 891 | 294 | if (si < n) { |
| 889 | 295 | if (allow_duplicates) { |
| 296 | // duplicates allowed or nothing inserted yet: simply copy everything | |
| 891 | 297 | memcpy(dest, src, elem_size * (n - si)); |
| 889 | 298 | } else { |
| 891 | 299 | // we must check the remaining source elements one by one |
| 300 | // to skip the duplicates. | |
| 301 | // Note that no source element can equal the last element in the | |
| 302 | // destination, because that would have created an insertion point | |
| 303 | // and a buffer, s.t. the above loop already handled the duplicates | |
| 304 | while (si < n) { | |
| 889 | 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; | |
| 891 | 309 | while (si + copy_len + skip_len < n) { |
| 889 | 310 | const char *right_src = left_src + elem_size; |
| 891 | 311 | int d = cmp_func(left_src, right_src, context); |
| 889 | 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 { | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
324 | // should be unreachable because the requirement is |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
325 | // that the source array is sorted |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
326 | break; // LCOV_EXCL_LINE |
| 889 | 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; | |
| 891 | 336 | array->size -= skip_len; |
| 889 | 337 | } |
| 338 | } | |
| 852 | 339 | } |
| 340 | ||
| 889 | 341 | // buffered elements need to be moved when we skipped duplicates |
| 891 | 342 | size_t total_skipped = new_size - array->size; |
| 889 | 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 | } | |
| 852 | 347 | |
| 348 | return 0; | |
| 349 | } | |
| 350 | ||
| 891 | 351 | int cx_array_insert_sorted_( |
| 352 | const CxAllocator *allocator, | |
| 353 | CxArray *array, | |
| 354 | size_t elem_size, | |
| 889 | 355 | const void *sorted_data, |
| 891 | 356 | size_t n, |
| 357 | cx_compare_func cmp_func, | |
| 358 | bool allow_duplicates | |
| 889 | 359 | ) { |
| 891 | 360 | cx_compare_func_wrapper wrapper = {cmp_func}; |
| 361 | return cx_array_insert_sorted_c_(allocator, array, elem_size, sorted_data, | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
362 | n, cx_cmp_wrap, &wrapper, allow_duplicates); |
| 891 | 363 | } |
| 364 | ||
| 365 | #ifndef WITH_QSORT_R | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
366 | static cx_thread_local cx_compare_func2 cx_array_fn_for_qsort; |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
367 | static cx_thread_local void *cx_array_context_for_qsort; |
| 891 | 368 | static int cx_array_qsort_wrapper(const void *l, const void *r) { |
| 369 | return cx_array_fn_for_qsort(l, r, cx_array_context_for_qsort); | |
| 370 | } | |
| 371 | #endif | |
| 372 | ||
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
373 | #if defined(WITH_QSORT_R) && defined(__APPLE__) |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
374 | // macOS uses a different comparefunc signature for qsort_r |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
375 | typedef struct QsortCmpFuncWrapper { |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
376 | cx_compare_func2 fn; |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
377 | void *context; |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
378 | } QsortCmpFuncWrapper; |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
379 | |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
380 | static int sort_comparefunc(void *context, const void *left, const void *right){ |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
381 | QsortCmpFuncWrapper *w = context; |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
382 | return w->fn(left, right, w->context); |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
383 | } |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
384 | #endif |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
385 | |
| 891 | 386 | void cx_array_qsort_c(void *array, size_t nmemb, size_t size, |
| 387 | cx_compare_func2 fn, void *context) { | |
| 388 | #ifdef WITH_QSORT_R | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
389 | #ifndef __APPLE__ |
| 891 | 390 | qsort_r(array, nmemb, size, fn, context); |
| 391 | #else | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
392 | QsortCmpFuncWrapper wrapper; |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
393 | wrapper.fn = fn; |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
394 | wrapper.context = context; |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
395 | qsort_r(array, nmemb, size, &wrapper, sort_comparefunc); |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
396 | #endif |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
397 | #else |
| 891 | 398 | cx_array_fn_for_qsort = fn; |
| 399 | cx_array_context_for_qsort = context; | |
| 400 | qsort(array, nmemb, size, cx_array_qsort_wrapper); | |
| 401 | #endif | |
| 402 | } | |
| 403 | ||
| 404 | void cx_array_sort_(CxArray *array, size_t elem_size, | |
| 405 | cx_compare_func fn) { | |
| 406 | qsort(array->data, array->size, elem_size, fn); | |
| 407 | } | |
| 408 | ||
| 409 | void cx_array_sort_c_(CxArray *array, size_t elem_size, | |
| 410 | cx_compare_func2 fn, void *context) { | |
| 411 | cx_array_qsort_c(array->data, array->size, elem_size, fn, context); | |
| 412 | } | |
| 413 | ||
| 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); | |
| 889 | 420 | } |
| 421 | ||
| 891 | 422 | void cx_array_remove_(CxArray *array, size_t elem_size, size_t index, size_t n, bool fast) { |
| 423 | if (n == 0) return; | |
| 424 | if (index >= array->size) return; | |
| 425 | if (index + n >= array->size) { | |
| 426 | // only tail elements are removed | |
| 427 | array->size = index; | |
| 428 | return; | |
| 429 | } | |
| 430 | array->size -= n; | |
| 431 | size_t remaining = array->size - index; | |
| 432 | char *dest = ((char*)array->data) + index * elem_size; | |
| 433 | if (fast) { | |
| 434 | char *src = dest + remaining * elem_size; | |
| 435 | if (n == 1 && elem_size <= CX_WORDSIZE/8) { | |
| 436 | // try to optimize int-sized values | |
| 437 | // (from likely to unlikely) | |
| 438 | if (elem_size == sizeof(int32_t)) { | |
| 439 | *(int32_t*)dest = *(int32_t*)src; | |
| 440 | return; | |
| 441 | } | |
| 442 | #if CX_WORDSIZE == 64 | |
| 443 | if (elem_size == sizeof(int64_t)) { | |
| 444 | *(int64_t*)dest = *(int64_t*)src; | |
| 445 | return; | |
| 446 | } | |
| 447 | #endif | |
| 448 | if (elem_size == sizeof(int8_t)) { | |
| 449 | *(int8_t*)dest = *(int8_t*)src; | |
| 450 | return; | |
| 451 | } | |
| 452 | if (elem_size == sizeof(int16_t)) { | |
| 453 | *(int16_t*)dest = *(int16_t*)src; | |
| 454 | return; | |
| 455 | } | |
| 456 | // note we cannot optimize the last branch, because | |
| 457 | // the elem_size could be crazily misaligned | |
| 458 | } | |
| 459 | memcpy(dest, src, n * elem_size); | |
| 460 | } else { | |
| 461 | char *src = dest + n * elem_size; | |
| 462 | memmove(dest, src, remaining * elem_size); | |
| 463 | } | |
| 889 | 464 | } |
| 465 | ||
| 891 | 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 | ||
| 473 | // implementation that finds ANY index | |
| 474 | static size_t cx_array_binary_search_inf_impl( | |
| 852 | 475 | const void *arr, |
| 476 | size_t size, | |
| 477 | size_t elem_size, | |
| 478 | const void *elem, | |
| 891 | 479 | cx_compare_func2 cmp_func, |
| 480 | void *context | |
| 852 | 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 | |
| 891 | 492 | result = cmp_func(elem, array, context); |
| 852 | 493 | if (result < 0) { |
| 494 | return size; | |
| 495 | } else if (result == 0) { | |
| 496 | return 0; | |
| 497 | } | |
| 498 | ||
| 499 | // special case: there is only one element and that is smaller | |
| 500 | if (size == 1) return 0; | |
| 501 | ||
| 502 | // check the last array element | |
| 891 | 503 | result = cmp_func(elem, array + elem_size * (size - 1), context); |
| 852 | 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; | |
| 891 | 512 | size_t pivot_index = 0; |
| 852 | 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; | |
| 891 | 517 | result = cmp_func(elem, arr_elem, context); |
| 852 | 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 | ||
| 891 | 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 | ||
| 602 | size_t cx_array_binary_search_inf( | |
| 603 | const void *arr, | |
| 604 | size_t size, | |
| 605 | size_t elem_size, | |
| 606 | const void *elem, | |
| 607 | cx_compare_func cmp_func | |
| 608 | ) { | |
| 609 | cx_compare_func_wrapper wrapper = {cmp_func}; | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
610 | return cx_array_binary_search_inf_c(arr, size, elem_size, elem, cx_cmp_wrap, &wrapper); |
| 891 | 611 | } |
| 612 | ||
| 852 | 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 | ) { | |
| 891 | 620 | cx_compare_func_wrapper wrapper = {cmp_func}; |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
621 | return cx_array_binary_search_c(arr, size, elem_size, elem, cx_cmp_wrap, &wrapper); |
| 852 | 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 | ) { | |
| 891 | 631 | cx_compare_func_wrapper wrapper = {cmp_func}; |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
632 | return cx_array_binary_search_sup_c(arr, size, elem_size, elem, cx_cmp_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
|
633 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
634 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
635 | #ifndef CX_ARRAY_SWAP_SBO_SIZE |
| 776 | 636 | #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
|
637 | #endif |
| 852 | 638 | 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
|
639 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
640 | 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
|
641 | void *arr, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
642 | 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
|
643 | 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
|
644 | 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
|
645 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
646 | 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
|
647 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
648 | // short circuit |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
649 | 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
|
650 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
651 | 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
|
652 | void *tmp; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
653 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
654 | // 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
|
655 | if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) { |
| 886 | 656 | 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
|
657 | // 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
|
658 | 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
|
659 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
660 | 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
|
661 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
662 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
663 | // 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
|
664 | 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
|
665 | 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
|
666 | 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
|
667 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
668 | // 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
|
669 | 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
|
670 | 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
|
671 | 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
|
672 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
673 | // 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
|
674 | if (tmp != sbo_mem) { |
| 886 | 675 | 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
|
676 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
677 | } |
|
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 | // 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
|
680 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
681 | typedef struct { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
682 | 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
|
683 | void *data; |
|
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 capacity; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
685 | } 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
|
686 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
687 | 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
|
688 | cx_array_list *arl = (cx_array_list *) list; |
| 750 | 689 | |
| 690 | char *ptr = arl->data; | |
| 691 | ||
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
692 | 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
|
693 | for (size_t i = 0; i < list->collection.size; i++) { |
| 750 | 694 | 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
|
695 | ptr += list->collection.elem_size; |
| 750 | 696 | } |
| 697 | } | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
698 | 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
|
699 | for (size_t i = 0; i < list->collection.size; i++) { |
| 750 | 700 | 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
|
701 | ptr += list->collection.elem_size; |
| 750 | 702 | } |
| 703 | } | |
| 704 | ||
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
705 | 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
|
706 | 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
|
707 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
708 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
709 | 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
|
710 | 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
|
711 | size_t index, |
| 852 | 712 | 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
|
713 | 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
|
714 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
715 | cx_array_list *arl = (cx_array_list *) list; |
| 891 | 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; | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
722 | } |
| 891 | 723 | arl->data = wrap.data; |
| 724 | arl->capacity = wrap.capacity; | |
| 725 | list->collection.size = wrap.size; | |
| 726 | return n; | |
| 727 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
728 | |
| 891 | 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 | }; | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
739 | |
| 891 | 740 | if (cx_array_insert_sorted_c_( |
| 741 | list->collection.allocator, | |
| 742 | &wrap, | |
| 743 | list->collection.elem_size, | |
| 744 | sorted_data, | |
| 745 | n, | |
| 746 | cx_list_compare_wrapper, | |
| 747 | list, | |
| 748 | allow_duplicates | |
| 749 | )) { | |
| 750 | // array list implementation is "all or nothing" | |
| 751 | return 0; // LCOV_EXCL_LINE | |
| 852 | 752 | } |
| 891 | 753 | arl->data = wrap.data; |
| 754 | arl->capacity = wrap.capacity; | |
| 755 | list->collection.size = wrap.size; | |
| 886 | 756 | return n; |
| 852 | 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 | ) { | |
| 891 | 764 | 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
|
765 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
766 | |
| 889 | 767 | static size_t cx_arl_insert_unique( |
| 768 | struct cx_list_s *list, | |
| 769 | const void *sorted_data, | |
| 770 | size_t n | |
| 771 | ) { | |
| 891 | 772 | return cx_arl_insert_sorted_impl(list, sorted_data, n, false); |
| 889 | 773 | } |
| 774 | ||
| 886 | 775 | 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
|
776 | 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
|
777 | size_t index, |
| 852 | 778 | 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
|
779 | ) { |
| 886 | 780 | if (cx_arl_insert_array(list, index, element, 1) == 1) { |
| 781 | return ((char*)((cx_array_list *) list)->data) + index * list->collection.elem_size; | |
| 782 | } else { | |
| 783 | return NULL; | |
| 784 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
785 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
786 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
787 | 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
|
788 | struct cx_iterator_s *iter, |
| 852 | 789 | 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
|
790 | int prepend |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
791 | ) { |
| 889 | 792 | 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
|
793 | if (iter->index < list->collection.size) { |
| 886 | 794 | if (cx_arl_insert_element(list, |
| 795 | iter->index + 1 - prepend, elem) == NULL) { | |
| 891 | 796 | return 1; // LCOV_EXCL_LINE |
| 886 | 797 | } |
| 798 | iter->elem_count++; | |
| 799 | if (prepend != 0) { | |
| 800 | iter->index++; | |
| 801 | 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
|
802 | } |
| 886 | 803 | 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
|
804 | } else { |
| 886 | 805 | if (cx_arl_insert_element(list, list->collection.size, elem) == NULL) { |
| 891 | 806 | return 1; // LCOV_EXCL_LINE |
| 852 | 807 | } |
| 886 | 808 | iter->elem_count++; |
| 809 | iter->index = list->collection.size; | |
| 810 | 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
|
811 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
812 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
813 | |
| 852 | 814 | 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
|
815 | struct cx_list_s *list, |
| 852 | 816 | size_t index, |
| 817 | size_t num, | |
| 818 | 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
|
819 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
820 | 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
|
821 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
822 | // out-of-bounds check |
| 852 | 823 | 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
|
824 | if (index >= list->collection.size) { |
| 852 | 825 | remove = 0; |
| 826 | } else if (index + num > list->collection.size) { | |
| 827 | remove = list->collection.size - index; | |
| 828 | } else { | |
| 829 | 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
|
830 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
831 | |
| 852 | 832 | // easy exit |
| 833 | 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
|
834 | |
| 852 | 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 | ); | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
849 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
850 | |
| 891 | 851 | // calculate how many elements would need to be moved |
| 852 | size_t remaining = list->collection.size - index - remove; | |
| 853 | ||
| 852 | 854 | // short-circuit removal of last elements |
| 891 | 855 | if (remaining == 0) { |
| 852 | 856 | list->collection.size -= remove; |
| 857 | return remove; | |
| 858 | } | |
| 859 | ||
| 860 | // just move the elements to the left | |
| 891 | 861 | char *dst_move = arl->data; |
| 862 | dst_move += index * list->collection.elem_size; | |
| 863 | char *first_remaining = dst_move + remove * list->collection.elem_size; | |
| 864 | 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
|
865 | |
| 852 | 866 | // decrease the size |
| 867 | 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
|
868 | |
| 852 | 869 | 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
|
870 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
871 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
872 | 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
|
873 | 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
|
874 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
875 | 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
|
876 | 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
|
877 | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
878 | 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
|
879 | 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
|
880 | 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
|
881 | 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
|
882 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
883 | } |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
884 | 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
|
885 | 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
|
886 | 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
|
887 | 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
|
888 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
889 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
890 | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
891 | 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
|
892 | 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
|
893 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
894 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
895 | 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
|
896 | 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
|
897 | 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
|
898 | 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
|
899 | ) { |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
900 | 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
|
901 | 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
|
902 | 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
|
903 | return 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
904 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
905 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
906 | static void *cx_arl_at( |
| 852 | 907 | 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
|
908 | 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
|
909 | ) { |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
910 | if (index < list->collection.size) { |
| 852 | 911 | 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
|
912 | 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
|
913 | 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
|
914 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
915 | return NULL; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
916 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
917 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
918 | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
919 | 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
|
920 | struct cx_list_s *list, |
| 852 | 921 | 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
|
922 | 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
|
923 | ) { |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
924 | assert(list != NULL); |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
925 | if (list->collection.size == 0) return 0; |
| 852 | 926 | 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
|
927 | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
928 | // optimize with binary search, when sorted |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
929 | if (list->collection.sorted) { |
| 891 | 930 | 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
|
931 | cur, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
932 | list->collection.size, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
933 | list->collection.elem_size, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
934 | elem, |
| 891 | 935 | cx_list_compare_wrapper, |
| 936 | list | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
937 | ); |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
938 | if (remove && i < list->collection.size) { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
939 | cx_arl_remove(list, i, 1, NULL); |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
940 | } |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
941 | return i; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
942 | } |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
943 | |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
944 | // fallback: linear search |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
945 | for (size_t i = 0; i < list->collection.size; i++) { |
| 891 | 946 | 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
|
947 | if (remove) { |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
948 | 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
|
949 | } |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
950 | 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
|
951 | } |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
952 | 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
|
953 | } |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
954 | 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
|
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 | static void cx_arl_sort(struct cx_list_s *list) { |
| 891 | 958 | 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
|
959 | list->collection.size, |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
960 | list->collection.elem_size, |
| 891 | 961 | cx_list_compare_wrapper, |
| 962 | list | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
963 | ); |
|
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 | static int cx_arl_compare( |
| 852 | 967 | const struct cx_list_s *list, |
| 968 | 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
|
969 | ) { |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
970 | if (list->collection.size == other->collection.size) { |
| 852 | 971 | const char *left = ((const cx_array_list *) list)->data; |
| 972 | 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
|
973 | for (size_t i = 0; i < list->collection.size; i++) { |
| 891 | 974 | 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
|
975 | 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
|
976 | return d; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
977 | } |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
978 | 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
|
979 | 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
|
980 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
981 | return 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
982 | } else { |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
983 | 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
|
984 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
985 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
986 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
987 | 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
|
988 | if (list->collection.size < 2) return; |
| 852 | 989 | 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
|
990 | 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
|
991 | 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
|
992 | 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
|
993 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
994 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
995 | |
| 852 | 996 | static bool cx_arl_iter_valid(const void *it) { |
| 997 | const struct cx_iterator_s *iter = it; | |
| 889 | 998 | 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
|
999 | 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
|
1000 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1001 | |
| 852 | 1002 | static void *cx_arl_iter_current(const void *it) { |
| 1003 | 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
|
1004 | 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
|
1005 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1006 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1007 | 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
|
1008 | 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
|
1009 | 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
|
1010 | iter->base.remove = false; |
| 889 | 1011 | cx_arl_remove(iter->src_handle, iter->index, 1, NULL); |
| 1012 | 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
|
1013 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1014 | iter->index++; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1015 | 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
|
1016 | ((char *) iter->elem_handle) |
| 889 | 1017 | + ((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
|
1018 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1019 | } |
|
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 | 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
|
1022 | 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
|
1023 | 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
|
1024 | iter->base.remove = false; |
| 889 | 1025 | cx_arl_remove(iter->src_handle, iter->index, 1, NULL); |
| 1026 | 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
|
1027 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1028 | iter->index--; |
| 889 | 1029 | 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
|
1030 | 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
|
1031 | 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
|
1032 | + 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
|
1033 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1034 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1035 | |
| 891 | 1036 | static int cx_arl_change_capacity( |
| 1037 | struct cx_list_s *list, | |
| 1038 | size_t new_capacity | |
| 1039 | ) { | |
| 1040 | cx_array_list *arl = (cx_array_list *)list; | |
| 1041 | return cxReallocateArray(list->collection.allocator, | |
| 1042 | &arl->data, new_capacity, list->collection.elem_size); | |
| 1043 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1044 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1045 | static struct cx_iterator_s cx_arl_iterator( |
| 852 | 1046 | 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
|
1047 | 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
|
1048 | bool backwards |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1049 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1050 | 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
|
1051 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1052 | iter.index = index; |
| 889 | 1053 | 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
|
1054 | 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
|
1055 | 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
|
1056 | 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
|
1057 | 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
|
1058 | 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
|
1059 | 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
|
1060 | iter.base.remove = false; |
| 889 | 1061 | 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
|
1062 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1063 | return iter; |
|
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 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1066 | 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
|
1067 | 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
|
1068 | 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
|
1069 | cx_arl_insert_array, |
| 852 | 1070 | cx_arl_insert_sorted, |
| 889 | 1071 | 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
|
1072 | 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
|
1073 | 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
|
1074 | 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
|
1075 | 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
|
1076 | 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
|
1077 | 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
|
1078 | 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
|
1079 | 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
|
1080 | cx_arl_reverse, |
| 891 | 1081 | 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
|
1082 | 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
|
1083 | }; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1084 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1085 | CxList *cxArrayListCreate( |
| 852 | 1086 | 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
|
1087 | 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
|
1088 | 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
|
1089 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1090 | 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
|
1091 | allocator = cxDefaultAllocator; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1092 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1093 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1094 | 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
|
1095 | if (list == NULL) return NULL; |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
1096 | cx_list_init((CxList*)list, &cx_array_list_class, 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
|
1097 | 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
|
1098 | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
1099 | // 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
|
1100 | list->data = cxCalloc(allocator, initial_capacity, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1101 | list->base.collection.elem_size); |
| 852 | 1102 | 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
|
1103 | 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
|
1104 | return NULL; |
| 852 | 1105 | } // 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
|
1106 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1107 | 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
|
1108 | } |