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