Sat, 08 Nov 2025 23:06:11 +0100
update ucx
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29 | #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
|
30 | #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
|
31 | #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
|
32 | #include <string.h> |
| 852 | 33 | #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
|
34 | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
35 | // Default array reallocator |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
36 | |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
37 | static void *cx_array_default_realloc( |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
38 | void *array, |
| 888 | 39 | cx_attr_unused size_t old_capacity, |
| 40 | size_t new_capacity, | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
41 | size_t elem_size, |
| 852 | 42 | cx_attr_unused CxArrayReallocator *alloc |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
43 | ) { |
| 852 | 44 | size_t n; |
| 888 | 45 | if (cx_szmul(new_capacity, elem_size, &n)) { |
| 852 | 46 | errno = EOVERFLOW; |
| 47 | return NULL; | |
| 48 | } | |
| 888 | 49 | return cxReallocDefault(array, n); |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
50 | } |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
51 | |
| 852 | 52 | CxArrayReallocator cx_array_default_reallocator_impl = { |
| 888 | 53 | cx_array_default_realloc, NULL, NULL |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
54 | }; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
55 | |
| 852 | 56 | CxArrayReallocator *cx_array_default_reallocator = &cx_array_default_reallocator_impl; |
| 57 | ||
| 58 | // Stack-aware array reallocator | |
| 59 | ||
| 60 | static void *cx_array_advanced_realloc( | |
| 61 | void *array, | |
| 888 | 62 | size_t old_capacity, |
| 63 | size_t new_capacity, | |
| 852 | 64 | size_t elem_size, |
| 65 | cx_attr_unused CxArrayReallocator *alloc | |
| 66 | ) { | |
| 67 | // check for overflow | |
| 68 | size_t n; | |
| 888 | 69 | if (cx_szmul(new_capacity, elem_size, &n)) { |
| 852 | 70 | errno = EOVERFLOW; |
| 71 | return NULL; | |
| 72 | } | |
| 73 | ||
| 74 | // retrieve the pointer to the actual allocator | |
| 888 | 75 | const CxAllocator *al = alloc->allocator; |
| 852 | 76 | |
| 77 | // check if the array is still located on the stack | |
| 78 | void *newmem; | |
| 888 | 79 | if (array == alloc->stack_ptr) { |
| 852 | 80 | newmem = cxMalloc(al, n); |
| 81 | if (newmem != NULL && array != NULL) { | |
| 888 | 82 | memcpy(newmem, array, old_capacity*elem_size); |
| 852 | 83 | } |
| 84 | } else { | |
| 85 | newmem = cxRealloc(al, array, n); | |
| 86 | } | |
| 87 | return newmem; | |
| 88 | } | |
| 89 | ||
| 90 | struct cx_array_reallocator_s cx_array_reallocator( | |
| 91 | const struct cx_allocator_s *allocator, | |
| 888 | 92 | const void *stack_ptr |
| 852 | 93 | ) { |
| 94 | if (allocator == NULL) { | |
| 95 | allocator = cxDefaultAllocator; | |
| 96 | } | |
| 97 | return (struct cx_array_reallocator_s) { | |
| 98 | cx_array_advanced_realloc, | |
| 888 | 99 | allocator, stack_ptr, |
| 852 | 100 | }; |
| 101 | } | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
102 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
103 | // 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
|
104 | |
| 888 | 105 | /** |
| 106 | * Increases the capacity until it is a multiple of a some alignment or reaches the maximum. | |
| 107 | * | |
| 108 | * @param cap the required capacity (must not be larger than @p max) | |
| 109 | * @param alignment the alignment | |
| 110 | * @param max the maximum capacity | |
| 111 | * @return the aligned capacity | |
| 112 | */ | |
| 852 | 113 | static size_t cx_array_align_capacity( |
| 114 | size_t cap, | |
| 115 | size_t alignment, | |
| 116 | size_t max | |
| 117 | ) { | |
| 118 | if (cap > max - alignment) { | |
| 119 | return cap; | |
| 120 | } else { | |
| 121 | return cap - (cap % alignment) + alignment; | |
| 122 | } | |
| 123 | } | |
| 124 | ||
| 125 | int cx_array_reserve( | |
| 126 | void **array, | |
| 127 | void *size, | |
| 128 | void *capacity, | |
| 129 | unsigned width, | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
130 | 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
|
131 | size_t elem_count, |
| 852 | 132 | CxArrayReallocator *reallocator |
| 133 | ) { | |
| 134 | // assert pointers | |
| 135 | assert(array != NULL); | |
| 136 | assert(size != NULL); | |
| 137 | assert(capacity != NULL); | |
| 138 | ||
| 139 | // default reallocator | |
| 140 | if (reallocator == NULL) { | |
| 141 | reallocator = cx_array_default_reallocator; | |
| 142 | } | |
| 143 | ||
| 144 | // determine size and capacity | |
| 145 | size_t oldcap; | |
| 146 | size_t oldsize; | |
| 147 | size_t max_size; | |
| 148 | if (width == 0 || width == sizeof(size_t)) { | |
| 149 | oldcap = *(size_t*) capacity; | |
| 150 | oldsize = *(size_t*) size; | |
| 151 | max_size = SIZE_MAX; | |
| 152 | } else if (width == sizeof(uint16_t)) { | |
| 153 | oldcap = *(uint16_t*) capacity; | |
| 154 | oldsize = *(uint16_t*) size; | |
| 155 | max_size = UINT16_MAX; | |
| 156 | } else if (width == sizeof(uint8_t)) { | |
| 157 | oldcap = *(uint8_t*) capacity; | |
| 158 | oldsize = *(uint8_t*) size; | |
| 159 | max_size = UINT8_MAX; | |
| 160 | } | |
| 161 | #if CX_WORDSIZE == 64 | |
| 162 | else if (width == sizeof(uint32_t)) { | |
| 163 | oldcap = *(uint32_t*) capacity; | |
| 164 | oldsize = *(uint32_t*) size; | |
| 165 | max_size = UINT32_MAX; | |
| 166 | } | |
| 167 | #endif | |
| 168 | else { | |
| 169 | errno = EINVAL; | |
| 170 | return 1; | |
| 171 | } | |
| 172 | ||
| 173 | // assert that the array is allocated when it has capacity | |
| 174 | assert(*array != NULL || oldcap == 0); | |
| 175 | ||
| 176 | // check for overflow | |
| 177 | if (elem_count > max_size - oldsize) { | |
| 178 | errno = EOVERFLOW; | |
| 179 | return 1; | |
| 180 | } | |
| 181 | ||
| 182 | // determine new capacity | |
| 183 | size_t newcap = oldsize + elem_count; | |
| 184 | ||
| 185 | // reallocate if possible | |
| 186 | if (newcap > oldcap) { | |
| 187 | // calculate new capacity (next number divisible by 16) | |
| 188 | newcap = cx_array_align_capacity(newcap, 16, max_size); | |
| 189 | ||
| 190 | // perform reallocation | |
| 191 | void *newmem = reallocator->realloc( | |
| 888 | 192 | *array, oldcap, newcap, elem_size, reallocator |
| 852 | 193 | ); |
| 194 | if (newmem == NULL) { | |
| 195 | return 1; // LCOV_EXCL_LINE | |
| 196 | } | |
| 197 | ||
| 198 | // store new pointer | |
| 199 | *array = newmem; | |
| 200 | ||
| 201 | // store new capacity | |
| 202 | if (width == 0 || width == sizeof(size_t)) { | |
| 203 | *(size_t*) capacity = newcap; | |
| 204 | } else if (width == sizeof(uint16_t)) { | |
| 205 | *(uint16_t*) capacity = (uint16_t) newcap; | |
| 206 | } else if (width == sizeof(uint8_t)) { | |
| 207 | *(uint8_t*) capacity = (uint8_t) newcap; | |
| 208 | } | |
| 209 | #if CX_WORDSIZE == 64 | |
| 210 | else if (width == sizeof(uint32_t)) { | |
| 211 | *(uint32_t*) capacity = (uint32_t) newcap; | |
| 212 | } | |
| 213 | #endif | |
| 214 | } | |
| 215 | ||
| 216 | return 0; | |
| 217 | } | |
| 218 | ||
| 219 | int cx_array_copy( | |
| 220 | void **target, | |
| 221 | void *size, | |
| 222 | void *capacity, | |
| 223 | unsigned width, | |
| 224 | size_t index, | |
| 225 | const void *src, | |
| 226 | size_t elem_size, | |
| 227 | size_t elem_count, | |
| 228 | CxArrayReallocator *reallocator | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
229 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
230 | // assert pointers |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
231 | assert(target != NULL); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
232 | assert(size != NULL); |
| 852 | 233 | assert(capacity != NULL); |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
234 | assert(src != NULL); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
235 | |
| 852 | 236 | // default reallocator |
| 237 | if (reallocator == NULL) { | |
| 238 | reallocator = cx_array_default_reallocator; | |
| 239 | } | |
| 240 | ||
| 241 | // determine size and capacity | |
| 242 | size_t oldcap; | |
| 243 | size_t oldsize; | |
| 244 | size_t max_size; | |
| 245 | if (width == 0 || width == sizeof(size_t)) { | |
| 246 | oldcap = *(size_t*) capacity; | |
| 247 | oldsize = *(size_t*) size; | |
| 248 | max_size = SIZE_MAX; | |
| 249 | } else if (width == sizeof(uint16_t)) { | |
| 250 | oldcap = *(uint16_t*) capacity; | |
| 251 | oldsize = *(uint16_t*) size; | |
| 252 | max_size = UINT16_MAX; | |
| 253 | } else if (width == sizeof(uint8_t)) { | |
| 254 | oldcap = *(uint8_t*) capacity; | |
| 255 | oldsize = *(uint8_t*) size; | |
| 256 | max_size = UINT8_MAX; | |
| 257 | } | |
| 258 | #if CX_WORDSIZE == 64 | |
| 259 | else if (width == sizeof(uint32_t)) { | |
| 260 | oldcap = *(uint32_t*) capacity; | |
| 261 | oldsize = *(uint32_t*) size; | |
| 262 | max_size = UINT32_MAX; | |
| 263 | } | |
| 264 | #endif | |
| 265 | else { | |
| 266 | errno = EINVAL; | |
| 267 | return 1; | |
| 268 | } | |
| 269 | ||
| 270 | // assert that the array is allocated when it has capacity | |
| 271 | assert(*target != NULL || oldcap == 0); | |
| 272 | ||
| 273 | // check for overflow | |
| 274 | if (index > max_size || elem_count > max_size - index) { | |
| 275 | errno = EOVERFLOW; | |
| 276 | return 1; | |
| 277 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
278 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
279 | // check if resize is required |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
280 | size_t minsize = index + elem_count; |
| 852 | 281 | size_t newsize = oldsize < minsize ? minsize : oldsize; |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
282 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
283 | // reallocate if possible |
| 852 | 284 | size_t newcap = oldcap; |
| 285 | if (newsize > oldcap) { | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
286 | // check, if we need to repair the src pointer |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
287 | uintptr_t targetaddr = (uintptr_t) *target; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
288 | uintptr_t srcaddr = (uintptr_t) src; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
289 | bool repairsrc = targetaddr <= srcaddr |
| 852 | 290 | && srcaddr < targetaddr + oldcap * 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
|
291 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
292 | // calculate new capacity (next number divisible by 16) |
| 852 | 293 | newcap = cx_array_align_capacity(newsize, 16, max_size); |
| 294 | assert(newcap > newsize); | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
295 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
296 | // perform reallocation |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
297 | void *newmem = reallocator->realloc( |
| 888 | 298 | *target, oldcap, newcap, elem_size, reallocator |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
299 | ); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
300 | if (newmem == NULL) { |
| 888 | 301 | return 1; // LCOV_EXCL_LINE |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
302 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
303 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
304 | // repair src pointer, if necessary |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
305 | if (repairsrc) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
306 | src = ((char *) newmem) + (srcaddr - targetaddr); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
307 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
308 | |
| 852 | 309 | // store new pointer |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
310 | *target = newmem; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
311 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
312 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
313 | // determine target pointer |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
314 | char *start = *target; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
315 | start += index * elem_size; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
316 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
317 | // copy elements and set new size |
| 852 | 318 | // note: no overflow check here, b/c we cannot get here w/o allocation |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
319 | memmove(start, src, elem_count * elem_size); |
| 852 | 320 | |
| 321 | // if any of size or capacity changed, store them back | |
| 322 | if (newsize != oldsize || newcap != oldcap) { | |
| 323 | if (width == 0 || width == sizeof(size_t)) { | |
| 324 | *(size_t*) capacity = newcap; | |
| 325 | *(size_t*) size = newsize; | |
| 326 | } else if (width == sizeof(uint16_t)) { | |
| 327 | *(uint16_t*) capacity = (uint16_t) newcap; | |
| 328 | *(uint16_t*) size = (uint16_t) newsize; | |
| 329 | } else if (width == sizeof(uint8_t)) { | |
| 330 | *(uint8_t*) capacity = (uint8_t) newcap; | |
| 331 | *(uint8_t*) size = (uint8_t) newsize; | |
| 332 | } | |
| 333 | #if CX_WORDSIZE == 64 | |
| 334 | else if (width == sizeof(uint32_t)) { | |
| 335 | *(uint32_t*) capacity = (uint32_t) newcap; | |
| 336 | *(uint32_t*) size = (uint32_t) newsize; | |
| 337 | } | |
| 338 | #endif | |
| 339 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
340 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
341 | // return successfully |
| 852 | 342 | return 0; |
| 343 | } | |
| 344 | ||
| 888 | 345 | static int cx_array_insert_sorted_impl( |
| 852 | 346 | void **target, |
| 347 | size_t *size, | |
| 348 | size_t *capacity, | |
| 349 | cx_compare_func cmp_func, | |
| 350 | const void *sorted_data, | |
| 351 | size_t elem_size, | |
| 352 | size_t elem_count, | |
| 888 | 353 | CxArrayReallocator *reallocator, |
| 354 | bool allow_duplicates | |
| 852 | 355 | ) { |
| 356 | // assert pointers | |
| 357 | assert(target != NULL); | |
| 358 | assert(size != NULL); | |
| 359 | assert(capacity != NULL); | |
| 360 | assert(cmp_func != NULL); | |
| 361 | assert(sorted_data != NULL); | |
| 362 | ||
| 363 | // default reallocator | |
| 364 | if (reallocator == NULL) { | |
| 365 | reallocator = cx_array_default_reallocator; | |
| 366 | } | |
| 367 | ||
| 368 | // corner case | |
| 369 | if (elem_count == 0) return 0; | |
| 370 | ||
| 371 | // overflow check | |
| 372 | if (elem_count > SIZE_MAX - *size) { | |
| 373 | errno = EOVERFLOW; | |
| 374 | return 1; | |
| 375 | } | |
| 376 | ||
| 377 | // store some counts | |
| 378 | size_t old_size = *size; | |
| 888 | 379 | size_t old_capacity = *capacity; |
| 380 | // the necessary capacity is the worst case assumption, including duplicates | |
| 852 | 381 | size_t needed_capacity = old_size + elem_count; |
| 382 | ||
| 383 | // if we need more than we have, try a reallocation | |
| 888 | 384 | if (needed_capacity > old_capacity) { |
| 852 | 385 | size_t new_capacity = cx_array_align_capacity(needed_capacity, 16, SIZE_MAX); |
| 386 | void *new_mem = reallocator->realloc( | |
| 888 | 387 | *target, old_capacity, new_capacity, elem_size, reallocator |
| 852 | 388 | ); |
| 389 | if (new_mem == NULL) { | |
| 390 | // give it up right away, there is no contract | |
| 391 | // that requires us to insert as much as we can | |
| 392 | return 1; // LCOV_EXCL_LINE | |
| 393 | } | |
| 394 | *target = new_mem; | |
| 395 | *capacity = new_capacity; | |
| 396 | } | |
| 397 | ||
| 398 | // now we have guaranteed that we can insert everything | |
| 399 | size_t new_size = old_size + elem_count; | |
| 400 | *size = new_size; | |
| 401 | ||
| 402 | // declare the source and destination indices/pointers | |
| 403 | size_t si = 0, di = 0; | |
| 404 | const char *src = sorted_data; | |
| 405 | char *dest = *target; | |
| 406 | ||
| 407 | // find the first insertion point | |
| 408 | di = cx_array_binary_search_sup(dest, old_size, elem_size, src, cmp_func); | |
| 409 | dest += di * elem_size; | |
| 410 | ||
| 411 | // move the remaining elements in the array completely to the right | |
| 412 | // we will call it the "buffer" for parked elements | |
| 413 | size_t buf_size = old_size - di; | |
| 414 | size_t bi = new_size - buf_size; | |
| 415 | char *bptr = ((char *) *target) + bi * elem_size; | |
| 416 | memmove(bptr, dest, buf_size * elem_size); | |
| 417 | ||
| 418 | // while there are both source and buffered elements left, | |
| 419 | // copy them interleaving | |
| 420 | while (si < elem_count && bi < new_size) { | |
| 421 | // determine how many source elements can be inserted | |
| 422 | size_t copy_len, bytes_copied; | |
| 423 | copy_len = cx_array_binary_search_sup( | |
| 424 | src, | |
| 425 | elem_count - si, | |
| 426 | elem_size, | |
| 427 | bptr, | |
| 428 | cmp_func | |
| 429 | ); | |
| 888 | 430 | // binary search gives us the smallest index; |
| 431 | // we also want to include equal elements here | |
| 432 | while (si + copy_len < elem_count | |
| 433 | && cmp_func(bptr, src+copy_len*elem_size) == 0) { | |
| 434 | copy_len++; | |
| 435 | } | |
| 852 | 436 | |
| 437 | // copy the source elements | |
| 888 | 438 | if (copy_len > 0) { |
| 439 | if (allow_duplicates) { | |
| 440 | // we can copy the entire chunk | |
| 441 | bytes_copied = copy_len * elem_size; | |
| 442 | memcpy(dest, src, bytes_copied); | |
| 443 | dest += bytes_copied; | |
| 444 | src += bytes_copied; | |
| 445 | si += copy_len; | |
| 446 | di += copy_len; | |
| 447 | } else { | |
| 448 | // first, check the end of the source chunk | |
| 449 | // for being a duplicate of the bptr | |
| 450 | const char *end_of_src = src + (copy_len - 1) * elem_size; | |
| 451 | size_t skip_len = 0; | |
| 452 | while (copy_len > 0 && cmp_func(bptr, end_of_src) == 0) { | |
| 453 | end_of_src -= elem_size; | |
| 454 | skip_len++; | |
| 455 | copy_len--; | |
| 456 | } | |
| 457 | char *last = dest == *target ? NULL : dest - elem_size; | |
| 458 | // then iterate through the source chunk | |
| 459 | // and skip all duplicates with the last element in the array | |
| 460 | size_t more_skipped = 0; | |
| 461 | for (unsigned j = 0; j < copy_len; j++) { | |
| 462 | if (last != NULL && cmp_func(last, src) == 0) { | |
| 463 | // duplicate - skip | |
| 464 | src += elem_size; | |
| 465 | si++; | |
| 466 | more_skipped++; | |
| 467 | } else { | |
| 468 | memcpy(dest, src, elem_size); | |
| 469 | src += elem_size; | |
| 470 | last = dest; | |
| 471 | dest += elem_size; | |
| 472 | si++; | |
| 473 | di++; | |
| 474 | } | |
| 475 | } | |
| 476 | // skip the previously identified elements as well | |
| 477 | src += skip_len * elem_size; | |
| 478 | si += skip_len; | |
| 479 | skip_len += more_skipped; | |
| 480 | // reduce the actual size by the number of skipped elements | |
| 481 | *size -= skip_len; | |
| 482 | } | |
| 483 | } | |
| 852 | 484 | |
| 485 | // when all source elements are in place, we are done | |
| 486 | if (si >= elem_count) break; | |
| 487 | ||
| 488 | // determine how many buffered elements need to be restored | |
| 489 | copy_len = cx_array_binary_search_sup( | |
| 490 | bptr, | |
| 491 | new_size - bi, | |
| 492 | elem_size, | |
| 493 | src, | |
| 494 | cmp_func | |
| 495 | ); | |
| 496 | ||
| 497 | // restore the buffered elements | |
| 498 | bytes_copied = copy_len * elem_size; | |
| 499 | memmove(dest, bptr, bytes_copied); | |
| 500 | dest += bytes_copied; | |
| 501 | bptr += bytes_copied; | |
| 888 | 502 | di += copy_len; |
| 852 | 503 | bi += copy_len; |
| 504 | } | |
| 505 | ||
| 888 | 506 | // still source elements left? |
| 852 | 507 | if (si < elem_count) { |
| 888 | 508 | if (allow_duplicates) { |
| 509 | // duplicates allowed or nothing inserted yet: simply copy everything | |
| 510 | memcpy(dest, src, elem_size * (elem_count - si)); | |
| 511 | } else { | |
| 512 | if (dest != *target) { | |
| 513 | // skip all source elements that equal the last element | |
| 514 | char *last = dest - elem_size; | |
| 515 | while (si < elem_count) { | |
| 516 | if (last != NULL && cmp_func(last, src) == 0) { | |
| 517 | src += elem_size; | |
| 518 | si++; | |
| 519 | (*size)--; | |
| 520 | } else { | |
| 521 | break; | |
| 522 | } | |
| 523 | } | |
| 524 | } | |
| 525 | // we must check the elements in the chunk one by one | |
| 526 | while (si < elem_count) { | |
| 527 | // find a chain of elements that can be copied | |
| 528 | size_t copy_len = 1, skip_len = 0; | |
| 529 | { | |
| 530 | const char *left_src = src; | |
| 531 | while (si + copy_len < elem_count) { | |
| 532 | const char *right_src = left_src + elem_size; | |
| 533 | int d = cmp_func(left_src, right_src); | |
| 534 | if (d < 0) { | |
| 535 | if (skip_len > 0) { | |
| 536 | // new larger element found; | |
| 537 | // handle it in the next cycle | |
| 538 | break; | |
| 539 | } | |
| 540 | left_src += elem_size; | |
| 541 | copy_len++; | |
| 542 | } else if (d == 0) { | |
| 543 | left_src += elem_size; | |
| 544 | skip_len++; | |
| 545 | } else { | |
| 546 | break; | |
| 547 | } | |
| 548 | } | |
| 549 | } | |
| 550 | size_t bytes_copied = copy_len * elem_size; | |
| 551 | memcpy(dest, src, bytes_copied); | |
| 552 | dest += bytes_copied; | |
| 553 | src += bytes_copied + skip_len * elem_size; | |
| 554 | si += copy_len + skip_len; | |
| 555 | di += copy_len; | |
| 556 | *size -= skip_len; | |
| 557 | } | |
| 558 | } | |
| 852 | 559 | } |
| 560 | ||
| 888 | 561 | // buffered elements need to be moved when we skipped duplicates |
| 562 | size_t total_skipped = new_size - *size; | |
| 563 | if (bi < new_size && total_skipped > 0) { | |
| 564 | // move the remaining buffer to the end of the array | |
| 565 | memmove(dest, bptr, elem_size * (new_size - bi)); | |
| 566 | } | |
| 852 | 567 | |
| 568 | return 0; | |
| 569 | } | |
| 570 | ||
| 888 | 571 | int cx_array_insert_sorted( |
| 572 | void **target, | |
| 573 | size_t *size, | |
| 574 | size_t *capacity, | |
| 575 | cx_compare_func cmp_func, | |
| 576 | const void *sorted_data, | |
| 577 | size_t elem_size, | |
| 578 | size_t elem_count, | |
| 579 | CxArrayReallocator *reallocator | |
| 580 | ) { | |
| 581 | return cx_array_insert_sorted_impl(target, size, capacity, | |
| 582 | cmp_func, sorted_data, elem_size, elem_count, reallocator, true); | |
| 583 | } | |
| 584 | ||
| 585 | int cx_array_insert_unique( | |
| 586 | void **target, | |
| 587 | size_t *size, | |
| 588 | size_t *capacity, | |
| 589 | cx_compare_func cmp_func, | |
| 590 | const void *sorted_data, | |
| 591 | size_t elem_size, | |
| 592 | size_t elem_count, | |
| 593 | CxArrayReallocator *reallocator | |
| 594 | ) { | |
| 595 | return cx_array_insert_sorted_impl(target, size, capacity, | |
| 596 | cmp_func, sorted_data, elem_size, elem_count, reallocator, false); | |
| 597 | } | |
| 598 | ||
| 852 | 599 | size_t cx_array_binary_search_inf( |
| 600 | const void *arr, | |
| 601 | size_t size, | |
| 602 | size_t elem_size, | |
| 603 | const void *elem, | |
| 604 | cx_compare_func cmp_func | |
| 605 | ) { | |
| 606 | // special case: empty array | |
| 607 | if (size == 0) return 0; | |
| 608 | ||
| 609 | // declare a variable that will contain the compare results | |
| 610 | int result; | |
| 611 | ||
| 612 | // cast the array pointer to something we can use offsets with | |
| 613 | const char *array = arr; | |
| 614 | ||
| 615 | // check the first array element | |
| 616 | result = cmp_func(elem, array); | |
| 617 | if (result < 0) { | |
| 618 | return size; | |
| 619 | } else if (result == 0) { | |
| 620 | return 0; | |
| 621 | } | |
| 622 | ||
| 623 | // special case: there is only one element and that is smaller | |
| 624 | if (size == 1) return 0; | |
| 625 | ||
| 626 | // check the last array element | |
| 627 | result = cmp_func(elem, array + elem_size * (size - 1)); | |
| 628 | if (result >= 0) { | |
| 629 | return size - 1; | |
| 630 | } | |
| 631 | ||
| 632 | // the element is now guaranteed to be somewhere in the list | |
| 633 | // so start the binary search | |
| 634 | size_t left_index = 1; | |
| 635 | size_t right_index = size - 1; | |
| 636 | size_t pivot_index; | |
| 637 | ||
| 638 | while (left_index <= right_index) { | |
| 639 | pivot_index = left_index + (right_index - left_index) / 2; | |
| 640 | const char *arr_elem = array + pivot_index * elem_size; | |
| 641 | result = cmp_func(elem, arr_elem); | |
| 642 | if (result == 0) { | |
| 643 | // found it! | |
| 888 | 644 | // check previous elements; |
| 645 | // when they are equal, report the smallest index | |
| 646 | arr_elem -= elem_size; | |
| 647 | while (pivot_index > 0 && cmp_func(elem, arr_elem) == 0) { | |
| 648 | pivot_index--; | |
| 649 | arr_elem -= elem_size; | |
| 650 | } | |
| 852 | 651 | return pivot_index; |
| 652 | } else if (result < 0) { | |
| 653 | // element is smaller than pivot, continue search left | |
| 654 | right_index = pivot_index - 1; | |
| 655 | } else { | |
| 656 | // element is larger than pivot, continue search right | |
| 657 | left_index = pivot_index + 1; | |
| 658 | } | |
| 659 | } | |
| 660 | ||
| 661 | // report the largest upper bound | |
| 662 | return result < 0 ? (pivot_index - 1) : pivot_index; | |
| 663 | } | |
| 664 | ||
| 665 | size_t cx_array_binary_search( | |
| 666 | const void *arr, | |
| 667 | size_t size, | |
| 668 | size_t elem_size, | |
| 669 | const void *elem, | |
| 670 | cx_compare_func cmp_func | |
| 671 | ) { | |
| 672 | size_t index = cx_array_binary_search_inf( | |
| 673 | arr, size, elem_size, elem, cmp_func | |
| 674 | ); | |
| 675 | if (index < size && | |
| 676 | cmp_func(((const char *) arr) + index * elem_size, elem) == 0) { | |
| 677 | return index; | |
| 678 | } else { | |
| 679 | return size; | |
| 680 | } | |
| 681 | } | |
| 682 | ||
| 683 | size_t cx_array_binary_search_sup( | |
| 684 | const void *arr, | |
| 685 | size_t size, | |
| 686 | size_t elem_size, | |
| 687 | const void *elem, | |
| 688 | cx_compare_func cmp_func | |
| 689 | ) { | |
| 690 | size_t inf = cx_array_binary_search_inf( | |
| 691 | arr, size, elem_size, elem, cmp_func | |
| 692 | ); | |
| 693 | if (inf == size) { | |
| 694 | // no infimum means, first element is supremum | |
| 695 | return 0; | |
| 696 | } else if (cmp_func(((const char *) arr) + inf * elem_size, elem) == 0) { | |
| 697 | return inf; | |
| 698 | } else { | |
| 699 | return inf + 1; | |
| 700 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
701 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
702 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
703 | #ifndef CX_ARRAY_SWAP_SBO_SIZE |
| 776 | 704 | #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
|
705 | #endif |
| 852 | 706 | 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
|
707 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
708 | 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
|
709 | void *arr, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
710 | 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
|
711 | 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
|
712 | 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
|
713 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
714 | 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
|
715 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
716 | // short circuit |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
717 | 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
|
718 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
719 | 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
|
720 | void *tmp; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
721 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
722 | // 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
|
723 | if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) { |
| 888 | 724 | 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
|
725 | // 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
|
726 | 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
|
727 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
728 | 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
|
729 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
730 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
731 | // 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
|
732 | 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
|
733 | 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
|
734 | 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
|
735 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
736 | // 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
|
737 | 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
|
738 | 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
|
739 | 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
|
740 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
741 | // 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
|
742 | if (tmp != sbo_mem) { |
| 888 | 743 | 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
|
744 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
745 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
746 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
747 | // 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
|
748 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
749 | typedef struct { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
750 | 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
|
751 | void *data; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
752 | size_t capacity; |
| 852 | 753 | CxArrayReallocator reallocator; |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
754 | } 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
|
755 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
756 | 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
|
757 | cx_array_list *arl = (cx_array_list *) list; |
| 750 | 758 | |
| 759 | char *ptr = arl->data; | |
| 760 | ||
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
761 | 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
|
762 | for (size_t i = 0; i < list->collection.size; i++) { |
| 750 | 763 | 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
|
764 | ptr += list->collection.elem_size; |
| 750 | 765 | } |
| 766 | } | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
767 | 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
|
768 | for (size_t i = 0; i < list->collection.size; i++) { |
| 750 | 769 | 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
|
770 | ptr += list->collection.elem_size; |
| 750 | 771 | } |
| 772 | } | |
| 773 | ||
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
774 | 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
|
775 | 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
|
776 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
777 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
778 | 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
|
779 | 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
|
780 | size_t index, |
| 852 | 781 | 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
|
782 | 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
|
783 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
784 | // out of bounds and special case check |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
785 | if (index > list->collection.size || n == 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
|
786 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
787 | // get a correctly typed pointer to the list |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
788 | 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
|
789 | |
| 888 | 790 | // guarantee enough capacity |
| 791 | if (arl->capacity < list->collection.size + n) { | |
| 792 | size_t new_capacity = list->collection.size + n; | |
| 793 | new_capacity = cx_array_align_capacity(new_capacity, 16, SIZE_MAX); | |
| 794 | if (cxReallocateArray( | |
| 795 | list->collection.allocator, | |
| 796 | &arl->data, new_capacity, | |
| 797 | list->collection.elem_size) | |
| 798 | ) { | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
799 | return 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
800 | } |
| 888 | 801 | arl->capacity = new_capacity; |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
802 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
803 | |
| 888 | 804 | // determine insert position |
| 805 | char *arl_data = arl->data; | |
| 806 | char *insert_pos = arl_data + 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
|
807 | |
| 888 | 808 | // do we need to move some elements? |
| 809 | if (index < list->collection.size) { | |
| 810 | size_t elems_to_move = list->collection.size - index; | |
| 811 | char *target = insert_pos + n * list->collection.elem_size; | |
| 812 | memmove(target, insert_pos, elems_to_move * list->collection.elem_size); | |
| 852 | 813 | } |
| 888 | 814 | |
| 815 | // place the new elements, if any | |
| 816 | if (array != NULL) { | |
| 817 | memcpy(insert_pos, array, n * list->collection.elem_size); | |
| 818 | } | |
| 819 | list->collection.size += n; | |
| 820 | ||
| 821 | return n; | |
| 852 | 822 | } |
| 823 | ||
| 824 | static size_t cx_arl_insert_sorted( | |
| 825 | struct cx_list_s *list, | |
| 826 | const void *sorted_data, | |
| 827 | size_t n | |
| 828 | ) { | |
| 829 | // get a correctly typed pointer to the list | |
| 830 | cx_array_list *arl = (cx_array_list *) list; | |
| 831 | ||
| 832 | if (cx_array_insert_sorted( | |
| 833 | &arl->data, | |
| 834 | &list->collection.size, | |
| 835 | &arl->capacity, | |
| 836 | list->collection.cmpfunc, | |
| 837 | sorted_data, | |
| 838 | list->collection.elem_size, | |
| 839 | n, | |
| 840 | &arl->reallocator | |
| 841 | )) { | |
| 842 | // array list implementation is "all or nothing" | |
| 843 | return 0; | |
| 844 | } else { | |
| 845 | return n; | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
846 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
847 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
848 | |
| 888 | 849 | static size_t cx_arl_insert_unique( |
| 850 | struct cx_list_s *list, | |
| 851 | const void *sorted_data, | |
| 852 | size_t n | |
| 853 | ) { | |
| 854 | // get a correctly typed pointer to the list | |
| 855 | cx_array_list *arl = (cx_array_list *) list; | |
| 856 | ||
| 857 | if (cx_array_insert_unique( | |
| 858 | &arl->data, | |
| 859 | &list->collection.size, | |
| 860 | &arl->capacity, | |
| 861 | list->collection.cmpfunc, | |
| 862 | sorted_data, | |
| 863 | list->collection.elem_size, | |
| 864 | n, | |
| 865 | &arl->reallocator | |
| 866 | )) { | |
| 867 | // array list implementation is "all or nothing" | |
| 868 | return 0; | |
| 869 | } else { | |
| 870 | return n; | |
| 871 | } | |
| 872 | } | |
| 873 | ||
| 874 | 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
|
875 | 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
|
876 | size_t index, |
| 852 | 877 | 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
|
878 | ) { |
| 888 | 879 | if (cx_arl_insert_array(list, index, element, 1) == 1) { |
| 880 | return ((char*)((cx_array_list *) list)->data) + index * list->collection.elem_size; | |
| 881 | } else { | |
| 882 | return NULL; | |
| 883 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
884 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
885 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
886 | 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
|
887 | struct cx_iterator_s *iter, |
| 852 | 888 | 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
|
889 | int prepend |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
890 | ) { |
| 888 | 891 | 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
|
892 | if (iter->index < list->collection.size) { |
| 888 | 893 | if (cx_arl_insert_element(list, |
| 894 | iter->index + 1 - prepend, elem) == NULL) { | |
| 895 | return 1; | |
| 896 | } | |
| 897 | iter->elem_count++; | |
| 898 | if (prepend != 0) { | |
| 899 | iter->index++; | |
| 900 | 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
|
901 | } |
| 888 | 902 | 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
|
903 | } else { |
| 888 | 904 | if (cx_arl_insert_element(list, list->collection.size, elem) == NULL) { |
| 905 | return 1; | |
| 852 | 906 | } |
| 888 | 907 | iter->elem_count++; |
| 908 | iter->index = list->collection.size; | |
| 909 | 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
|
910 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
911 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
912 | |
| 852 | 913 | 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
|
914 | struct cx_list_s *list, |
| 852 | 915 | size_t index, |
| 916 | size_t num, | |
| 917 | 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
|
918 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
919 | 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
|
920 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
921 | // out-of-bounds check |
| 852 | 922 | 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
|
923 | if (index >= list->collection.size) { |
| 852 | 924 | remove = 0; |
| 925 | } else if (index + num > list->collection.size) { | |
| 926 | remove = list->collection.size - index; | |
| 927 | } else { | |
| 928 | 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
|
929 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
930 | |
| 852 | 931 | // easy exit |
| 932 | 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
|
933 | |
| 852 | 934 | // destroy or copy contents |
| 935 | if (targetbuf == NULL) { | |
| 936 | for (size_t idx = index; idx < index + remove; idx++) { | |
| 937 | cx_invoke_destructor( | |
| 938 | list, | |
| 939 | ((char *) arl->data) + idx * list->collection.elem_size | |
| 940 | ); | |
| 941 | } | |
| 942 | } else { | |
| 943 | memcpy( | |
| 944 | targetbuf, | |
| 945 | ((char *) arl->data) + index * list->collection.elem_size, | |
| 946 | remove * list->collection.elem_size | |
| 947 | ); | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
948 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
949 | |
| 852 | 950 | // short-circuit removal of last elements |
| 951 | if (index + remove == list->collection.size) { | |
| 952 | list->collection.size -= remove; | |
| 953 | return remove; | |
| 954 | } | |
| 955 | ||
| 956 | // just move the elements to the left | |
| 957 | cx_array_copy( | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
958 | &arl->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, |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
960 | &arl->capacity, |
| 852 | 961 | 0, |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
962 | index, |
| 852 | 963 | ((char *) arl->data) + (index + remove) * 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
|
964 | list->collection.elem_size, |
| 852 | 965 | list->collection.size - index - remove, |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
966 | &arl->reallocator |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
967 | ); |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
968 | |
| 852 | 969 | // decrease the size |
| 970 | 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
|
971 | |
| 852 | 972 | 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
|
973 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
974 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
975 | 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
|
976 | 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
|
977 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
978 | 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
|
979 | 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
|
980 | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
981 | 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
|
982 | 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
|
983 | 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
|
984 | 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
|
985 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
986 | } |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
987 | 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
|
988 | 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
|
989 | 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
|
990 | 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
|
991 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
992 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
993 | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
994 | 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
|
995 | 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
|
996 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
997 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
998 | 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
|
999 | 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
|
1000 | 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
|
1001 | 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
|
1002 | ) { |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
1003 | 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
|
1004 | 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
|
1005 | 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
|
1006 | return 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1007 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1008 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1009 | static void *cx_arl_at( |
| 852 | 1010 | 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
|
1011 | 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
|
1012 | ) { |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
1013 | if (index < list->collection.size) { |
| 852 | 1014 | 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
|
1015 | 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
|
1016 | 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
|
1017 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1018 | return NULL; |
|
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 | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1022 | 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
|
1023 | struct cx_list_s *list, |
| 852 | 1024 | 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
|
1025 | 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
|
1026 | ) { |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1027 | assert(list != NULL); |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
1028 | assert(list->collection.cmpfunc != NULL); |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1029 | if (list->collection.size == 0) return 0; |
| 852 | 1030 | 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
|
1031 | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1032 | // optimize with binary search, when sorted |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1033 | if (list->collection.sorted) { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1034 | size_t i = cx_array_binary_search( |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1035 | cur, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1036 | list->collection.size, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1037 | list->collection.elem_size, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1038 | elem, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1039 | list->collection.cmpfunc |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1040 | ); |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1041 | if (remove && i < list->collection.size) { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1042 | cx_arl_remove(list, i, 1, NULL); |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1043 | } |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1044 | return i; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1045 | } |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1046 | |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1047 | // fallback: linear search |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1048 | for (size_t i = 0; i < list->collection.size; i++) { |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
1049 | if (0 == list->collection.cmpfunc(elem, cur)) { |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
1050 | if (remove) { |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1051 | 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
|
1052 | } |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1053 | 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
|
1054 | } |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
1055 | 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
|
1056 | } |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1057 | 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
|
1058 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1059 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1060 | static void cx_arl_sort(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
|
1061 | assert(list->collection.cmpfunc != NULL); |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1062 | qsort(((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
|
1063 | list->collection.size, |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
1064 | 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
|
1065 | list->collection.cmpfunc |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1066 | ); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1067 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1068 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1069 | static int cx_arl_compare( |
| 852 | 1070 | const struct cx_list_s *list, |
| 1071 | 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
|
1072 | ) { |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
1073 | assert(list->collection.cmpfunc != NULL); |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
1074 | if (list->collection.size == other->collection.size) { |
| 852 | 1075 | const char *left = ((const cx_array_list *) list)->data; |
| 1076 | 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
|
1077 | for (size_t i = 0; i < list->collection.size; i++) { |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
1078 | int d = list->collection.cmpfunc(left, right); |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1079 | 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
|
1080 | return d; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1081 | } |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
1082 | 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
|
1083 | 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
|
1084 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1085 | return 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1086 | } else { |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
1087 | 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
|
1088 | } |
|
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 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1091 | 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
|
1092 | if (list->collection.size < 2) return; |
| 852 | 1093 | 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
|
1094 | 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
|
1095 | 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
|
1096 | 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
|
1097 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1098 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1099 | |
| 852 | 1100 | static bool cx_arl_iter_valid(const void *it) { |
| 1101 | const struct cx_iterator_s *iter = it; | |
| 888 | 1102 | 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
|
1103 | 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
|
1104 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1105 | |
| 852 | 1106 | static void *cx_arl_iter_current(const void *it) { |
| 1107 | 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
|
1108 | 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
|
1109 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1110 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1111 | 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
|
1112 | 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
|
1113 | 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
|
1114 | iter->base.remove = false; |
| 888 | 1115 | cx_arl_remove(iter->src_handle, iter->index, 1, NULL); |
| 1116 | 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
|
1117 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1118 | iter->index++; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1119 | 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
|
1120 | ((char *) iter->elem_handle) |
| 888 | 1121 | + ((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
|
1122 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1123 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1124 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1125 | 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
|
1126 | 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
|
1127 | 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
|
1128 | iter->base.remove = false; |
| 888 | 1129 | cx_arl_remove(iter->src_handle, iter->index, 1, NULL); |
| 1130 | 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
|
1131 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1132 | iter->index--; |
| 888 | 1133 | 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
|
1134 | 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
|
1135 | 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
|
1136 | + 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
|
1137 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1138 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1139 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1140 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1141 | static struct cx_iterator_s cx_arl_iterator( |
| 852 | 1142 | 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
|
1143 | 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
|
1144 | bool backwards |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1145 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1146 | 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
|
1147 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1148 | iter.index = index; |
| 888 | 1149 | 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
|
1150 | 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
|
1151 | 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
|
1152 | 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
|
1153 | 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
|
1154 | 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
|
1155 | 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
|
1156 | iter.base.remove = false; |
| 888 | 1157 | 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
|
1158 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1159 | return iter; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1160 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1161 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1162 | 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
|
1163 | 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
|
1164 | 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
|
1165 | cx_arl_insert_array, |
| 852 | 1166 | cx_arl_insert_sorted, |
| 888 | 1167 | 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
|
1168 | 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
|
1169 | 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
|
1170 | 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
|
1171 | 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
|
1172 | 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
|
1173 | 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
|
1174 | 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
|
1175 | 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
|
1176 | cx_arl_reverse, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1177 | 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
|
1178 | }; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1179 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1180 | CxList *cxArrayListCreate( |
| 852 | 1181 | const CxAllocator *allocator, |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1182 | cx_compare_func comparator, |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
1183 | 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
|
1184 | 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
|
1185 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1186 | 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
|
1187 | allocator = cxDefaultAllocator; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1188 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1189 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1190 | 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
|
1191 | if (list == NULL) return NULL; |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1192 | cx_list_init((CxList*)list, &cx_array_list_class, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1193 | allocator, comparator, 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
|
1194 | 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
|
1195 | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
1196 | // 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
|
1197 | list->data = cxCalloc(allocator, initial_capacity, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1198 | list->base.collection.elem_size); |
| 852 | 1199 | 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
|
1200 | 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
|
1201 | return NULL; |
| 852 | 1202 | } // 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
|
1203 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1204 | // configure the reallocator |
| 852 | 1205 | list->reallocator = cx_array_reallocator(allocator, NULL); |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1206 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1207 | 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
|
1208 | } |