Tue, 09 Sep 2025 16:01:30 +0200
move resource_get_remote_change to a separate source file
|
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, |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
39 | size_t capacity, |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
40 | size_t elem_size, |
| 852 | 41 | 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
|
42 | ) { |
| 852 | 43 | size_t n; |
| 44 | if (cx_szmul(capacity, elem_size, &n)) { | |
| 45 | errno = EOVERFLOW; | |
| 46 | return NULL; | |
| 47 | } | |
| 48 | return realloc(array, n); | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
49 | } |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
50 | |
| 852 | 51 | CxArrayReallocator cx_array_default_reallocator_impl = { |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
52 | cx_array_default_realloc, NULL, NULL, 0, 0 |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
53 | }; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
54 | |
| 852 | 55 | CxArrayReallocator *cx_array_default_reallocator = &cx_array_default_reallocator_impl; |
| 56 | ||
| 57 | // Stack-aware array reallocator | |
| 58 | ||
| 59 | static void *cx_array_advanced_realloc( | |
| 60 | void *array, | |
| 61 | size_t capacity, | |
| 62 | size_t elem_size, | |
| 63 | cx_attr_unused CxArrayReallocator *alloc | |
| 64 | ) { | |
| 65 | // check for overflow | |
| 66 | size_t n; | |
| 67 | if (cx_szmul(capacity, elem_size, &n)) { | |
| 68 | errno = EOVERFLOW; | |
| 69 | return NULL; | |
| 70 | } | |
| 71 | ||
| 72 | // retrieve the pointer to the actual allocator | |
| 73 | const CxAllocator *al = alloc->ptr1; | |
| 74 | ||
| 75 | // check if the array is still located on the stack | |
| 76 | void *newmem; | |
| 77 | if (array == alloc->ptr2) { | |
| 78 | newmem = cxMalloc(al, n); | |
| 79 | if (newmem != NULL && array != NULL) { | |
| 80 | memcpy(newmem, array, n); | |
| 81 | } | |
| 82 | } else { | |
| 83 | newmem = cxRealloc(al, array, n); | |
| 84 | } | |
| 85 | return newmem; | |
| 86 | } | |
| 87 | ||
| 88 | struct cx_array_reallocator_s cx_array_reallocator( | |
| 89 | const struct cx_allocator_s *allocator, | |
| 90 | const void *stackmem | |
| 91 | ) { | |
| 92 | if (allocator == NULL) { | |
| 93 | allocator = cxDefaultAllocator; | |
| 94 | } | |
| 95 | return (struct cx_array_reallocator_s) { | |
| 96 | cx_array_advanced_realloc, | |
| 97 | (void*) allocator, (void*) stackmem, | |
| 98 | 0, 0 | |
| 99 | }; | |
| 100 | } | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
101 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
102 | // 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
|
103 | |
| 852 | 104 | static size_t cx_array_align_capacity( |
| 105 | size_t cap, | |
| 106 | size_t alignment, | |
| 107 | size_t max | |
| 108 | ) { | |
| 109 | if (cap > max - alignment) { | |
| 110 | return cap; | |
| 111 | } else { | |
| 112 | return cap - (cap % alignment) + alignment; | |
| 113 | } | |
| 114 | } | |
| 115 | ||
| 116 | int cx_array_reserve( | |
| 117 | void **array, | |
| 118 | void *size, | |
| 119 | void *capacity, | |
| 120 | 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
|
121 | 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
|
122 | size_t elem_count, |
| 852 | 123 | CxArrayReallocator *reallocator |
| 124 | ) { | |
| 125 | // assert pointers | |
| 126 | assert(array != NULL); | |
| 127 | assert(size != NULL); | |
| 128 | assert(capacity != NULL); | |
| 129 | ||
| 130 | // default reallocator | |
| 131 | if (reallocator == NULL) { | |
| 132 | reallocator = cx_array_default_reallocator; | |
| 133 | } | |
| 134 | ||
| 135 | // determine size and capacity | |
| 136 | size_t oldcap; | |
| 137 | size_t oldsize; | |
| 138 | size_t max_size; | |
| 139 | if (width == 0 || width == sizeof(size_t)) { | |
| 140 | oldcap = *(size_t*) capacity; | |
| 141 | oldsize = *(size_t*) size; | |
| 142 | max_size = SIZE_MAX; | |
| 143 | } else if (width == sizeof(uint16_t)) { | |
| 144 | oldcap = *(uint16_t*) capacity; | |
| 145 | oldsize = *(uint16_t*) size; | |
| 146 | max_size = UINT16_MAX; | |
| 147 | } else if (width == sizeof(uint8_t)) { | |
| 148 | oldcap = *(uint8_t*) capacity; | |
| 149 | oldsize = *(uint8_t*) size; | |
| 150 | max_size = UINT8_MAX; | |
| 151 | } | |
| 152 | #if CX_WORDSIZE == 64 | |
| 153 | else if (width == sizeof(uint32_t)) { | |
| 154 | oldcap = *(uint32_t*) capacity; | |
| 155 | oldsize = *(uint32_t*) size; | |
| 156 | max_size = UINT32_MAX; | |
| 157 | } | |
| 158 | #endif | |
| 159 | else { | |
| 160 | errno = EINVAL; | |
| 161 | return 1; | |
| 162 | } | |
| 163 | ||
| 164 | // assert that the array is allocated when it has capacity | |
| 165 | assert(*array != NULL || oldcap == 0); | |
| 166 | ||
| 167 | // check for overflow | |
| 168 | if (elem_count > max_size - oldsize) { | |
| 169 | errno = EOVERFLOW; | |
| 170 | return 1; | |
| 171 | } | |
| 172 | ||
| 173 | // determine new capacity | |
| 174 | size_t newcap = oldsize + elem_count; | |
| 175 | ||
| 176 | // reallocate if possible | |
| 177 | if (newcap > oldcap) { | |
| 178 | // calculate new capacity (next number divisible by 16) | |
| 179 | newcap = cx_array_align_capacity(newcap, 16, max_size); | |
| 180 | ||
| 181 | // perform reallocation | |
| 182 | void *newmem = reallocator->realloc( | |
| 183 | *array, newcap, elem_size, reallocator | |
| 184 | ); | |
| 185 | if (newmem == NULL) { | |
| 186 | return 1; // LCOV_EXCL_LINE | |
| 187 | } | |
| 188 | ||
| 189 | // store new pointer | |
| 190 | *array = newmem; | |
| 191 | ||
| 192 | // store new capacity | |
| 193 | if (width == 0 || width == sizeof(size_t)) { | |
| 194 | *(size_t*) capacity = newcap; | |
| 195 | } else if (width == sizeof(uint16_t)) { | |
| 196 | *(uint16_t*) capacity = (uint16_t) newcap; | |
| 197 | } else if (width == sizeof(uint8_t)) { | |
| 198 | *(uint8_t*) capacity = (uint8_t) newcap; | |
| 199 | } | |
| 200 | #if CX_WORDSIZE == 64 | |
| 201 | else if (width == sizeof(uint32_t)) { | |
| 202 | *(uint32_t*) capacity = (uint32_t) newcap; | |
| 203 | } | |
| 204 | #endif | |
| 205 | } | |
| 206 | ||
| 207 | return 0; | |
| 208 | } | |
| 209 | ||
| 210 | int cx_array_copy( | |
| 211 | void **target, | |
| 212 | void *size, | |
| 213 | void *capacity, | |
| 214 | unsigned width, | |
| 215 | size_t index, | |
| 216 | const void *src, | |
| 217 | size_t elem_size, | |
| 218 | size_t elem_count, | |
| 219 | 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
|
220 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
221 | // assert pointers |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
222 | 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
|
223 | assert(size != NULL); |
| 852 | 224 | 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
|
225 | 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
|
226 | |
| 852 | 227 | // default reallocator |
| 228 | if (reallocator == NULL) { | |
| 229 | reallocator = cx_array_default_reallocator; | |
| 230 | } | |
| 231 | ||
| 232 | // determine size and capacity | |
| 233 | size_t oldcap; | |
| 234 | size_t oldsize; | |
| 235 | size_t max_size; | |
| 236 | if (width == 0 || width == sizeof(size_t)) { | |
| 237 | oldcap = *(size_t*) capacity; | |
| 238 | oldsize = *(size_t*) size; | |
| 239 | max_size = SIZE_MAX; | |
| 240 | } else if (width == sizeof(uint16_t)) { | |
| 241 | oldcap = *(uint16_t*) capacity; | |
| 242 | oldsize = *(uint16_t*) size; | |
| 243 | max_size = UINT16_MAX; | |
| 244 | } else if (width == sizeof(uint8_t)) { | |
| 245 | oldcap = *(uint8_t*) capacity; | |
| 246 | oldsize = *(uint8_t*) size; | |
| 247 | max_size = UINT8_MAX; | |
| 248 | } | |
| 249 | #if CX_WORDSIZE == 64 | |
| 250 | else if (width == sizeof(uint32_t)) { | |
| 251 | oldcap = *(uint32_t*) capacity; | |
| 252 | oldsize = *(uint32_t*) size; | |
| 253 | max_size = UINT32_MAX; | |
| 254 | } | |
| 255 | #endif | |
| 256 | else { | |
| 257 | errno = EINVAL; | |
| 258 | return 1; | |
| 259 | } | |
| 260 | ||
| 261 | // assert that the array is allocated when it has capacity | |
| 262 | assert(*target != NULL || oldcap == 0); | |
| 263 | ||
| 264 | // check for overflow | |
| 265 | if (index > max_size || elem_count > max_size - index) { | |
| 266 | errno = EOVERFLOW; | |
| 267 | return 1; | |
| 268 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
269 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
270 | // 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
|
271 | size_t minsize = index + elem_count; |
| 852 | 272 | 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
|
273 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
274 | // reallocate if possible |
| 852 | 275 | size_t newcap = oldcap; |
| 276 | 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
|
277 | // 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
|
278 | 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
|
279 | 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
|
280 | bool repairsrc = targetaddr <= srcaddr |
| 852 | 281 | && 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
|
282 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
283 | // calculate new capacity (next number divisible by 16) |
| 852 | 284 | newcap = cx_array_align_capacity(newsize, 16, max_size); |
| 285 | 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
|
286 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
287 | // perform reallocation |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
288 | void *newmem = reallocator->realloc( |
| 852 | 289 | *target, 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
|
290 | ); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
291 | if (newmem == NULL) { |
| 852 | 292 | 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
|
293 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
294 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
295 | // 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
|
296 | if (repairsrc) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
297 | 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
|
298 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
299 | |
| 852 | 300 | // 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
|
301 | *target = newmem; |
|
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 | // 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
|
305 | 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
|
306 | 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
|
307 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
308 | // copy elements and set new size |
| 852 | 309 | // 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
|
310 | memmove(start, src, elem_count * elem_size); |
| 852 | 311 | |
| 312 | // if any of size or capacity changed, store them back | |
| 313 | if (newsize != oldsize || newcap != oldcap) { | |
| 314 | if (width == 0 || width == sizeof(size_t)) { | |
| 315 | *(size_t*) capacity = newcap; | |
| 316 | *(size_t*) size = newsize; | |
| 317 | } else if (width == sizeof(uint16_t)) { | |
| 318 | *(uint16_t*) capacity = (uint16_t) newcap; | |
| 319 | *(uint16_t*) size = (uint16_t) newsize; | |
| 320 | } else if (width == sizeof(uint8_t)) { | |
| 321 | *(uint8_t*) capacity = (uint8_t) newcap; | |
| 322 | *(uint8_t*) size = (uint8_t) newsize; | |
| 323 | } | |
| 324 | #if CX_WORDSIZE == 64 | |
| 325 | else if (width == sizeof(uint32_t)) { | |
| 326 | *(uint32_t*) capacity = (uint32_t) newcap; | |
| 327 | *(uint32_t*) size = (uint32_t) newsize; | |
| 328 | } | |
| 329 | #endif | |
| 330 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
331 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
332 | // return successfully |
| 852 | 333 | return 0; |
| 334 | } | |
| 335 | ||
| 336 | int cx_array_insert_sorted( | |
| 337 | void **target, | |
| 338 | size_t *size, | |
| 339 | size_t *capacity, | |
| 340 | cx_compare_func cmp_func, | |
| 341 | const void *sorted_data, | |
| 342 | size_t elem_size, | |
| 343 | size_t elem_count, | |
| 344 | CxArrayReallocator *reallocator | |
| 345 | ) { | |
| 346 | // assert pointers | |
| 347 | assert(target != NULL); | |
| 348 | assert(size != NULL); | |
| 349 | assert(capacity != NULL); | |
| 350 | assert(cmp_func != NULL); | |
| 351 | assert(sorted_data != NULL); | |
| 352 | ||
| 353 | // default reallocator | |
| 354 | if (reallocator == NULL) { | |
| 355 | reallocator = cx_array_default_reallocator; | |
| 356 | } | |
| 357 | ||
| 358 | // corner case | |
| 359 | if (elem_count == 0) return 0; | |
| 360 | ||
| 361 | // overflow check | |
| 362 | if (elem_count > SIZE_MAX - *size) { | |
| 363 | errno = EOVERFLOW; | |
| 364 | return 1; | |
| 365 | } | |
| 366 | ||
| 367 | // store some counts | |
| 368 | size_t old_size = *size; | |
| 369 | size_t needed_capacity = old_size + elem_count; | |
| 370 | ||
| 371 | // if we need more than we have, try a reallocation | |
| 372 | if (needed_capacity > *capacity) { | |
| 373 | size_t new_capacity = cx_array_align_capacity(needed_capacity, 16, SIZE_MAX); | |
| 374 | void *new_mem = reallocator->realloc( | |
| 375 | *target, new_capacity, elem_size, reallocator | |
| 376 | ); | |
| 377 | if (new_mem == NULL) { | |
| 378 | // give it up right away, there is no contract | |
| 379 | // that requires us to insert as much as we can | |
| 380 | return 1; // LCOV_EXCL_LINE | |
| 381 | } | |
| 382 | *target = new_mem; | |
| 383 | *capacity = new_capacity; | |
| 384 | } | |
| 385 | ||
| 386 | // now we have guaranteed that we can insert everything | |
| 387 | size_t new_size = old_size + elem_count; | |
| 388 | *size = new_size; | |
| 389 | ||
| 390 | // declare the source and destination indices/pointers | |
| 391 | size_t si = 0, di = 0; | |
| 392 | const char *src = sorted_data; | |
| 393 | char *dest = *target; | |
| 394 | ||
| 395 | // find the first insertion point | |
| 396 | di = cx_array_binary_search_sup(dest, old_size, elem_size, src, cmp_func); | |
| 397 | dest += di * elem_size; | |
| 398 | ||
| 399 | // move the remaining elements in the array completely to the right | |
| 400 | // we will call it the "buffer" for parked elements | |
| 401 | size_t buf_size = old_size - di; | |
| 402 | size_t bi = new_size - buf_size; | |
| 403 | char *bptr = ((char *) *target) + bi * elem_size; | |
| 404 | memmove(bptr, dest, buf_size * elem_size); | |
| 405 | ||
| 406 | // while there are both source and buffered elements left, | |
| 407 | // copy them interleaving | |
| 408 | while (si < elem_count && bi < new_size) { | |
| 409 | // determine how many source elements can be inserted | |
| 410 | size_t copy_len, bytes_copied; | |
| 411 | copy_len = cx_array_binary_search_sup( | |
| 412 | src, | |
| 413 | elem_count - si, | |
| 414 | elem_size, | |
| 415 | bptr, | |
| 416 | cmp_func | |
| 417 | ); | |
| 418 | ||
| 419 | // copy the source elements | |
| 420 | bytes_copied = copy_len * elem_size; | |
| 421 | memcpy(dest, src, bytes_copied); | |
| 422 | dest += bytes_copied; | |
| 423 | src += bytes_copied; | |
| 424 | si += copy_len; | |
| 425 | ||
| 426 | // when all source elements are in place, we are done | |
| 427 | if (si >= elem_count) break; | |
| 428 | ||
| 429 | // determine how many buffered elements need to be restored | |
| 430 | copy_len = cx_array_binary_search_sup( | |
| 431 | bptr, | |
| 432 | new_size - bi, | |
| 433 | elem_size, | |
| 434 | src, | |
| 435 | cmp_func | |
| 436 | ); | |
| 437 | ||
| 438 | // restore the buffered elements | |
| 439 | bytes_copied = copy_len * elem_size; | |
| 440 | memmove(dest, bptr, bytes_copied); | |
| 441 | dest += bytes_copied; | |
| 442 | bptr += bytes_copied; | |
| 443 | bi += copy_len; | |
| 444 | } | |
| 445 | ||
| 446 | // still source elements left? simply append them | |
| 447 | if (si < elem_count) { | |
| 448 | memcpy(dest, src, elem_size * (elem_count - si)); | |
| 449 | } | |
| 450 | ||
| 451 | // still buffer elements left? | |
| 452 | // don't worry, we already moved them to the correct place | |
| 453 | ||
| 454 | return 0; | |
| 455 | } | |
| 456 | ||
| 457 | size_t cx_array_binary_search_inf( | |
| 458 | const void *arr, | |
| 459 | size_t size, | |
| 460 | size_t elem_size, | |
| 461 | const void *elem, | |
| 462 | cx_compare_func cmp_func | |
| 463 | ) { | |
| 464 | // special case: empty array | |
| 465 | if (size == 0) return 0; | |
| 466 | ||
| 467 | // declare a variable that will contain the compare results | |
| 468 | int result; | |
| 469 | ||
| 470 | // cast the array pointer to something we can use offsets with | |
| 471 | const char *array = arr; | |
| 472 | ||
| 473 | // check the first array element | |
| 474 | result = cmp_func(elem, array); | |
| 475 | if (result < 0) { | |
| 476 | return size; | |
| 477 | } else if (result == 0) { | |
| 478 | return 0; | |
| 479 | } | |
| 480 | ||
| 481 | // special case: there is only one element and that is smaller | |
| 482 | if (size == 1) return 0; | |
| 483 | ||
| 484 | // check the last array element | |
| 485 | result = cmp_func(elem, array + elem_size * (size - 1)); | |
| 486 | if (result >= 0) { | |
| 487 | return size - 1; | |
| 488 | } | |
| 489 | ||
| 490 | // the element is now guaranteed to be somewhere in the list | |
| 491 | // so start the binary search | |
| 492 | size_t left_index = 1; | |
| 493 | size_t right_index = size - 1; | |
| 494 | size_t pivot_index; | |
| 495 | ||
| 496 | while (left_index <= right_index) { | |
| 497 | pivot_index = left_index + (right_index - left_index) / 2; | |
| 498 | const char *arr_elem = array + pivot_index * elem_size; | |
| 499 | result = cmp_func(elem, arr_elem); | |
| 500 | if (result == 0) { | |
| 501 | // found it! | |
| 502 | return pivot_index; | |
| 503 | } else if (result < 0) { | |
| 504 | // element is smaller than pivot, continue search left | |
| 505 | right_index = pivot_index - 1; | |
| 506 | } else { | |
| 507 | // element is larger than pivot, continue search right | |
| 508 | left_index = pivot_index + 1; | |
| 509 | } | |
| 510 | } | |
| 511 | ||
| 512 | // report the largest upper bound | |
| 513 | return result < 0 ? (pivot_index - 1) : pivot_index; | |
| 514 | } | |
| 515 | ||
| 516 | size_t cx_array_binary_search( | |
| 517 | const void *arr, | |
| 518 | size_t size, | |
| 519 | size_t elem_size, | |
| 520 | const void *elem, | |
| 521 | cx_compare_func cmp_func | |
| 522 | ) { | |
| 523 | size_t index = cx_array_binary_search_inf( | |
| 524 | arr, size, elem_size, elem, cmp_func | |
| 525 | ); | |
| 526 | if (index < size && | |
| 527 | cmp_func(((const char *) arr) + index * elem_size, elem) == 0) { | |
| 528 | return index; | |
| 529 | } else { | |
| 530 | return size; | |
| 531 | } | |
| 532 | } | |
| 533 | ||
| 534 | size_t cx_array_binary_search_sup( | |
| 535 | const void *arr, | |
| 536 | size_t size, | |
| 537 | size_t elem_size, | |
| 538 | const void *elem, | |
| 539 | cx_compare_func cmp_func | |
| 540 | ) { | |
| 541 | size_t inf = cx_array_binary_search_inf( | |
| 542 | arr, size, elem_size, elem, cmp_func | |
| 543 | ); | |
| 544 | if (inf == size) { | |
| 545 | // no infimum means, first element is supremum | |
| 546 | return 0; | |
| 547 | } else if (cmp_func(((const char *) arr) + inf * elem_size, elem) == 0) { | |
| 548 | return inf; | |
| 549 | } else { | |
| 550 | return inf + 1; | |
| 551 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
552 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
553 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
554 | #ifndef CX_ARRAY_SWAP_SBO_SIZE |
| 776 | 555 | #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
|
556 | #endif |
| 852 | 557 | 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
|
558 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
559 | 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
|
560 | void *arr, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
561 | 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
|
562 | 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
|
563 | 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
|
564 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
565 | 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
|
566 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
567 | // short circuit |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
568 | 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
|
569 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
570 | 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
|
571 | void *tmp; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
572 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
573 | // 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
|
574 | if (elem_size > 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
|
575 | tmp = malloc(elem_size); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
576 | // 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
|
577 | 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
|
578 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
579 | 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
|
580 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
581 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
582 | // 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
|
583 | 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
|
584 | 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
|
585 | 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
|
586 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
587 | // 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
|
588 | 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
|
589 | 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
|
590 | 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
|
591 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
592 | // 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
|
593 | if (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
|
594 | free(tmp); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
595 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
596 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
597 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
598 | // 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
|
599 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
600 | typedef struct { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
601 | 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
|
602 | void *data; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
603 | size_t capacity; |
| 852 | 604 | 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
|
605 | } 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
|
606 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
607 | 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
|
608 | cx_array_list *arl = (cx_array_list *) list; |
| 750 | 609 | |
| 610 | char *ptr = arl->data; | |
| 611 | ||
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
612 | 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
|
613 | for (size_t i = 0; i < list->collection.size; i++) { |
| 750 | 614 | 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
|
615 | ptr += list->collection.elem_size; |
| 750 | 616 | } |
| 617 | } | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
618 | 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
|
619 | for (size_t i = 0; i < list->collection.size; i++) { |
| 750 | 620 | 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
|
621 | ptr += list->collection.elem_size; |
| 750 | 622 | } |
| 623 | } | |
| 624 | ||
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
625 | 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
|
626 | 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
|
627 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
628 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
629 | 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
|
630 | 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
|
631 | size_t index, |
| 852 | 632 | 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
|
633 | 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
|
634 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
635 | // 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
|
636 | 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
|
637 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
638 | // 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
|
639 | 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
|
640 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
641 | // do we need to move some elements? |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
642 | if (index < list->collection.size) { |
| 852 | 643 | const char *first_to_move = (const char *) arl->data; |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
644 | first_to_move += index * 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
|
645 | size_t elems_to_move = list->collection.size - index; |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
646 | size_t start_of_moved = index + n; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
647 | |
| 852 | 648 | if (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
|
649 | &arl->data, |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
650 | &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
|
651 | &arl->capacity, |
| 852 | 652 | 0, |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
653 | start_of_moved, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
654 | first_to_move, |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
655 | 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
|
656 | elems_to_move, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
657 | &arl->reallocator |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
658 | )) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
659 | // if moving existing elems is unsuccessful, abort |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
660 | return 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
661 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
662 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
663 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
664 | // note that if we had to move the elements, the following operation |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
665 | // is guaranteed to succeed, because we have the memory already allocated |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
666 | // therefore, it is impossible to leave this function with an invalid array |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
667 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
668 | // place the new elements |
| 852 | 669 | if (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
|
670 | &arl->data, |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
671 | &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
|
672 | &arl->capacity, |
| 852 | 673 | 0, |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
674 | index, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
675 | array, |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
676 | 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
|
677 | n, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
678 | &arl->reallocator |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
679 | )) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
680 | // array list implementation is "all or nothing" |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
681 | return 0; |
| 852 | 682 | } else { |
| 683 | return n; | |
| 684 | } | |
| 685 | } | |
| 686 | ||
| 687 | static size_t cx_arl_insert_sorted( | |
| 688 | struct cx_list_s *list, | |
| 689 | const void *sorted_data, | |
| 690 | size_t n | |
| 691 | ) { | |
| 692 | // get a correctly typed pointer to the list | |
| 693 | cx_array_list *arl = (cx_array_list *) list; | |
| 694 | ||
| 695 | if (cx_array_insert_sorted( | |
| 696 | &arl->data, | |
| 697 | &list->collection.size, | |
| 698 | &arl->capacity, | |
| 699 | list->collection.cmpfunc, | |
| 700 | sorted_data, | |
| 701 | list->collection.elem_size, | |
| 702 | n, | |
| 703 | &arl->reallocator | |
| 704 | )) { | |
| 705 | // array list implementation is "all or nothing" | |
| 706 | return 0; | |
| 707 | } else { | |
| 708 | 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
|
709 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
710 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
711 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
712 | static int 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
|
713 | 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
|
714 | size_t index, |
| 852 | 715 | 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
|
716 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
717 | return 1 != cx_arl_insert_array(list, index, element, 1); |
|
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 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
720 | 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
|
721 | struct cx_iterator_s *iter, |
| 852 | 722 | 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
|
723 | int prepend |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
724 | ) { |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
725 | struct cx_list_s *list = iter->src_handle.m; |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
726 | if (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
|
727 | int result = 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
|
728 | list, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
729 | iter->index + 1 - prepend, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
730 | elem |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
731 | ); |
| 852 | 732 | if (result == 0) { |
| 733 | iter->elem_count++; | |
| 734 | if (prepend != 0) { | |
| 735 | iter->index++; | |
| 736 | iter->elem_handle = ((char *) iter->elem_handle) + list->collection.elem_size; | |
| 737 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
738 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
739 | return result; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
740 | } else { |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
741 | int result = cx_arl_insert_element(list, list->collection.size, elem); |
| 852 | 742 | if (result == 0) { |
| 743 | iter->elem_count++; | |
| 744 | iter->index = list->collection.size; | |
| 745 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
746 | return result; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
747 | } |
|
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 | |
| 852 | 750 | 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
|
751 | struct cx_list_s *list, |
| 852 | 752 | size_t index, |
| 753 | size_t num, | |
| 754 | 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
|
755 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
756 | 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
|
757 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
758 | // out-of-bounds check |
| 852 | 759 | 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
|
760 | if (index >= list->collection.size) { |
| 852 | 761 | remove = 0; |
| 762 | } else if (index + num > list->collection.size) { | |
| 763 | remove = list->collection.size - index; | |
| 764 | } else { | |
| 765 | 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
|
766 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
767 | |
| 852 | 768 | // easy exit |
| 769 | 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
|
770 | |
| 852 | 771 | // destroy or copy contents |
| 772 | if (targetbuf == NULL) { | |
| 773 | for (size_t idx = index; idx < index + remove; idx++) { | |
| 774 | cx_invoke_destructor( | |
| 775 | list, | |
| 776 | ((char *) arl->data) + idx * list->collection.elem_size | |
| 777 | ); | |
| 778 | } | |
| 779 | } else { | |
| 780 | memcpy( | |
| 781 | targetbuf, | |
| 782 | ((char *) arl->data) + index * list->collection.elem_size, | |
| 783 | remove * list->collection.elem_size | |
| 784 | ); | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
785 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
786 | |
| 852 | 787 | // short-circuit removal of last elements |
| 788 | if (index + remove == list->collection.size) { | |
| 789 | list->collection.size -= remove; | |
| 790 | return remove; | |
| 791 | } | |
| 792 | ||
| 793 | // just move the elements to the left | |
| 794 | 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
|
795 | &arl->data, |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
796 | &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
|
797 | &arl->capacity, |
| 852 | 798 | 0, |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
799 | index, |
| 852 | 800 | ((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
|
801 | list->collection.elem_size, |
| 852 | 802 | 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
|
803 | &arl->reallocator |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
804 | ); |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
805 | |
| 852 | 806 | // decrease the size |
| 807 | 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
|
808 | |
| 852 | 809 | 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
|
810 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
811 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
812 | 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
|
813 | 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
|
814 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
815 | 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
|
816 | 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
|
817 | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
818 | 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
|
819 | 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
|
820 | 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
|
821 | 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
|
822 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
823 | } |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
824 | 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
|
825 | 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
|
826 | 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
|
827 | 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
|
828 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
829 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
830 | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
831 | 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
|
832 | 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
|
833 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
834 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
835 | 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
|
836 | 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
|
837 | 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
|
838 | 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
|
839 | ) { |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
840 | 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
|
841 | 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
|
842 | 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
|
843 | return 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
844 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
845 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
846 | static void *cx_arl_at( |
| 852 | 847 | 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
|
848 | 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
|
849 | ) { |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
850 | if (index < list->collection.size) { |
| 852 | 851 | 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
|
852 | 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
|
853 | 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
|
854 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
855 | return NULL; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
856 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
857 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
858 | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
859 | 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
|
860 | struct cx_list_s *list, |
| 852 | 861 | 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
|
862 | 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
|
863 | ) { |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
864 | 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
|
865 | assert(list->collection.cmpfunc != NULL); |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
866 | if (list->collection.size == 0) return 0; |
| 852 | 867 | 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
|
868 | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
869 | // optimize with binary search, when sorted |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
870 | if (list->collection.sorted) { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
871 | size_t i = cx_array_binary_search( |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
872 | cur, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
873 | list->collection.size, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
874 | list->collection.elem_size, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
875 | elem, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
876 | list->collection.cmpfunc |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
877 | ); |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
878 | if (remove && i < list->collection.size) { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
879 | cx_arl_remove(list, i, 1, NULL); |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
880 | } |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
881 | return i; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
882 | } |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
883 | |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
884 | // fallback: linear search |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
885 | 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
|
886 | 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
|
887 | if (remove) { |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
888 | 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
|
889 | } |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
890 | 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
|
891 | } |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
892 | 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
|
893 | } |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
894 | 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
|
895 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
896 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
897 | 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
|
898 | 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
|
899 | 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
|
900 | list->collection.size, |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
901 | 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
|
902 | 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
|
903 | ); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
904 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
905 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
906 | static int cx_arl_compare( |
| 852 | 907 | const struct cx_list_s *list, |
| 908 | 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
|
909 | ) { |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
910 | 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
|
911 | if (list->collection.size == other->collection.size) { |
| 852 | 912 | const char *left = ((const cx_array_list *) list)->data; |
| 913 | 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
|
914 | 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
|
915 | 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
|
916 | 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
|
917 | return d; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
918 | } |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
919 | 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
|
920 | 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
|
921 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
922 | return 0; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
923 | } else { |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
924 | 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
|
925 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
926 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
927 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
928 | static void cx_arl_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
|
929 | if (list->collection.size < 2) return; |
| 852 | 930 | 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
|
931 | 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
|
932 | 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
|
933 | 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
|
934 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
935 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
936 | |
| 852 | 937 | static bool cx_arl_iter_valid(const void *it) { |
| 938 | const struct cx_iterator_s *iter = it; | |
| 939 | const struct cx_list_s *list = iter->src_handle.c; | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
940 | 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
|
941 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
942 | |
| 852 | 943 | static void *cx_arl_iter_current(const void *it) { |
| 944 | 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
|
945 | 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
|
946 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
947 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
948 | 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
|
949 | 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
|
950 | 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
|
951 | iter->base.remove = false; |
| 852 | 952 | cx_arl_remove(iter->src_handle.m, iter->index, 1, NULL); |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
953 | } else { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
954 | iter->index++; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
955 | 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
|
956 | ((char *) iter->elem_handle) |
| 852 | 957 | + ((const struct cx_list_s *) iter->src_handle.c)->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
|
958 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
959 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
960 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
961 | 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
|
962 | struct cx_iterator_s *iter = it; |
| 852 | 963 | const cx_array_list *list = iter->src_handle.c; |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
964 | 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
|
965 | iter->base.remove = false; |
| 852 | 966 | cx_arl_remove(iter->src_handle.m, iter->index, 1, NULL); |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
967 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
968 | iter->index--; |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
969 | 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
|
970 | 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
|
971 | + 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
|
972 | } |
|
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 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
976 | static struct cx_iterator_s cx_arl_iterator( |
| 852 | 977 | 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
|
978 | 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
|
979 | bool backwards |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
980 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
981 | 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
|
982 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
983 | iter.index = index; |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
984 | iter.src_handle.c = list; |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
985 | 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
|
986 | 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
|
987 | 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
|
988 | 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
|
989 | 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
|
990 | 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
|
991 | iter.base.remove = false; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
992 | iter.base.mutating = false; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
993 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
994 | return iter; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
995 | } |
|
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 | 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
|
998 | 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
|
999 | 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
|
1000 | cx_arl_insert_array, |
| 852 | 1001 | cx_arl_insert_sorted, |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1002 | 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
|
1003 | 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
|
1004 | 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
|
1005 | 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
|
1006 | 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
|
1007 | 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
|
1008 | 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
|
1009 | 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
|
1010 | 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
|
1011 | 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
|
1012 | }; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1013 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1014 | CxList *cxArrayListCreate( |
| 852 | 1015 | 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
|
1016 | 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
|
1017 | 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
|
1018 | 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
|
1019 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1020 | 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
|
1021 | allocator = cxDefaultAllocator; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1022 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1023 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1024 | 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
|
1025 | if (list == NULL) return NULL; |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1026 | 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
|
1027 | 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
|
1028 | 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
|
1029 | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
776
diff
changeset
|
1030 | // 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
|
1031 | list->data = cxCalloc(allocator, initial_capacity, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
1032 | list->base.collection.elem_size); |
| 852 | 1033 | 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
|
1034 | 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
|
1035 | return NULL; |
| 852 | 1036 | } // 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
|
1037 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1038 | // configure the reallocator |
| 852 | 1039 | 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
|
1040 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1041 | 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
|
1042 | } |