Wed, 31 Dec 2025 16:41:16 +0100
update ucx to version 4.0
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
1 | /* |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
3 | * |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
5 | * |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
8 | * |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
11 | * |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
15 | * |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
27 | */ |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
28 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
29 | #include "cx/list.h" |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
30 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
31 | #include <string.h> |
| 889 | 32 | #include <assert.h> |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
33 | |
| 891 | 34 | // we don't want to include the full array_list.h. |
| 35 | // therefore, we only forward declare the one function we want to use | |
| 36 | CX_EXPORT void cx_array_qsort_c(void *array, size_t nmemb, size_t size, | |
| 37 | cx_compare_func2 fn, void *context); | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
38 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
39 | |
| 891 | 40 | int cx_list_compare_wrapper(const void *l, const void *r, void *c) { |
| 41 | CxList *list = c; | |
| 42 | const void *left; | |
| 43 | const void *right; | |
| 44 | if (cxCollectionStoresPointers(list)) { | |
| 45 | left = *(void**)l; | |
| 46 | right = *(void**)r; | |
| 47 | // for historic reasons, we are handling the NULL case here | |
| 48 | // because every UCX compare function does not support NULL arguments | |
| 49 | if (left == NULL) { | |
| 50 | if (right == NULL) return 0; | |
| 51 | return -1; | |
| 52 | } else if (right == NULL) { | |
| 53 | return 1; | |
| 54 | } | |
| 55 | } else { | |
| 56 | left = l; | |
| 57 | right = r; | |
| 58 | } | |
| 59 | return cx_invoke_compare_func(list, left, right); | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
60 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
61 | |
| 891 | 62 | #define cx_list_compare_wrapper(l, r, c) cx_list_compare_wrapper(l, r, (void*)c) |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
63 | |
| 750 | 64 | // <editor-fold desc="empty list implementation"> |
| 65 | ||
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
66 | static void cx_emptyl_noop(CX_UNUSED CxList *list) { |
| 750 | 67 | // this is a noop, but MUST be implemented |
| 68 | } | |
| 69 | ||
| 70 | static void *cx_emptyl_at( | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
71 | CX_UNUSED const struct cx_list_s *list, |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
72 | CX_UNUSED size_t index |
| 750 | 73 | ) { |
| 74 | return NULL; | |
| 75 | } | |
| 76 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
77 | static size_t cx_emptyl_find_remove( |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
78 | CX_UNUSED struct cx_list_s *list, |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
79 | CX_UNUSED const void *elem, |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
80 | CX_UNUSED bool remove |
| 750 | 81 | ) { |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
82 | return 0; |
| 750 | 83 | } |
| 84 | ||
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
85 | static bool cx_emptyl_iter_valid(CX_UNUSED const void *iter) { |
| 750 | 86 | return false; |
| 87 | } | |
| 88 | ||
| 89 | static CxIterator cx_emptyl_iterator( | |
| 852 | 90 | const struct cx_list_s *list, |
| 750 | 91 | size_t index, |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
92 | CX_UNUSED bool backwards |
| 750 | 93 | ) { |
| 94 | CxIterator iter = {0}; | |
| 889 | 95 | iter.src_handle = (void*) list; |
| 750 | 96 | iter.index = index; |
| 97 | iter.base.valid = cx_emptyl_iter_valid; | |
| 98 | return iter; | |
| 99 | } | |
| 100 | ||
| 101 | static cx_list_class cx_empty_list_class = { | |
| 102 | cx_emptyl_noop, | |
| 103 | NULL, | |
| 104 | NULL, | |
| 105 | NULL, | |
| 106 | NULL, | |
| 852 | 107 | NULL, |
| 889 | 108 | NULL, |
| 750 | 109 | cx_emptyl_noop, |
| 110 | NULL, | |
| 111 | cx_emptyl_at, | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
750
diff
changeset
|
112 | cx_emptyl_find_remove, |
| 750 | 113 | cx_emptyl_noop, |
| 852 | 114 | NULL, |
| 750 | 115 | cx_emptyl_noop, |
| 891 | 116 | NULL, |
| 750 | 117 | cx_emptyl_iterator, |
| 118 | }; | |
| 119 | ||
| 120 | CxList cx_empty_list = { | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
121 | { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
122 | NULL, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
123 | 0, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
124 | 0, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
125 | NULL, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
126 | NULL, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
127 | NULL, |
| 891 | 128 | NULL, |
| 129 | NULL, | |
| 130 | NULL, | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
131 | false, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
132 | true, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
133 | }, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
134 | &cx_empty_list_class, |
| 750 | 135 | }; |
| 136 | ||
| 137 | CxList *const cxEmptyList = &cx_empty_list; | |
| 138 | ||
| 139 | // </editor-fold> | |
| 140 | ||
| 852 | 141 | size_t cx_list_default_insert_array( |
| 142 | struct cx_list_s *list, | |
| 143 | size_t index, | |
| 144 | const void *data, | |
| 145 | size_t n | |
| 146 | ) { | |
| 147 | const char *src = data; | |
| 148 | size_t i = 0; | |
| 149 | for (; i < n; i++) { | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
150 | if (NULL == list->cl->insert_element(list, index + i, src)) { |
| 889 | 151 | return i; // LCOV_EXCL_LINE |
| 152 | } | |
| 153 | if (src != NULL) { | |
| 154 | src += list->collection.elem_size; | |
| 155 | } | |
| 852 | 156 | } |
| 157 | return i; | |
| 158 | } | |
| 159 | ||
| 889 | 160 | static size_t cx_list_default_insert_sorted_impl( |
| 852 | 161 | struct cx_list_s *list, |
| 162 | const void *sorted_data, | |
| 889 | 163 | size_t n, |
| 164 | bool allow_duplicates | |
| 852 | 165 | ) { |
| 166 | // corner case | |
| 167 | if (n == 0) return 0; | |
| 168 | ||
| 169 | size_t elem_size = list->collection.elem_size; | |
| 170 | const char *src = sorted_data; | |
| 171 | ||
| 172 | // track indices and number of inserted items | |
| 889 | 173 | size_t di = 0, si = 0, processed = 0; |
| 852 | 174 | |
| 175 | // search the list for insertion points | |
| 889 | 176 | while (di < list->collection.size) { |
| 891 | 177 | const void *list_elm = list->cl->at(list, di); |
| 852 | 178 | |
| 889 | 179 | // compare the current list element with the first source element |
| 180 | // if less, skip the list elements | |
| 181 | // if equal, skip the list elements and optionally the source elements | |
| 182 | { | |
| 891 | 183 | int d = cx_list_compare_wrapper(list_elm, src, list); |
| 889 | 184 | if (d <= 0) { |
| 185 | if (!allow_duplicates && d == 0) { | |
| 186 | src += elem_size; | |
| 187 | si++; | |
| 188 | processed++; // we also count duplicates for the return value | |
| 891 | 189 | while (si < n && cx_list_compare_wrapper(list_elm, src, list) == 0) { |
| 889 | 190 | src += elem_size; |
| 191 | si++; | |
| 192 | processed++; | |
| 193 | } | |
| 194 | if (processed == n) { | |
| 195 | return processed; | |
| 196 | } | |
| 197 | } | |
| 198 | di++; | |
| 199 | continue; | |
| 200 | } | |
| 852 | 201 | } |
| 202 | ||
| 889 | 203 | // determine the number of consecutive elements that can be inserted |
| 204 | size_t ins = 1, skip = 0; | |
| 852 | 205 | const char *next = src; |
| 206 | while (++si < n) { | |
| 889 | 207 | if (!allow_duplicates) { |
| 208 | // skip duplicates within the source | |
| 891 | 209 | if (cx_list_compare_wrapper(next, next + elem_size, list) == 0) { |
| 889 | 210 | next += elem_size; |
| 211 | skip++; | |
| 212 | continue; | |
| 213 | } else { | |
| 214 | if (skip > 0) { | |
| 215 | // if we had to skip something, we must wait for the next run | |
| 216 | next += elem_size; | |
| 217 | break; | |
| 218 | } | |
| 219 | } | |
| 220 | } | |
| 852 | 221 | next += elem_size; |
| 222 | // once we become larger than the list elem, break | |
| 891 | 223 | if (cx_list_compare_wrapper(list_elm, next, list) <= 0) { |
| 852 | 224 | break; |
| 225 | } | |
| 226 | // otherwise, we can insert one more | |
| 227 | ins++; | |
| 228 | } | |
| 229 | ||
| 230 | // insert the elements at location si | |
| 231 | if (ins == 1) { | |
| 891 | 232 | if (NULL == list->cl->insert_element(list, di, src)) { |
| 889 | 233 | return processed; // LCOV_EXCL_LINE |
| 234 | } | |
| 852 | 235 | } else { |
| 891 | 236 | size_t r = list->cl->insert_array(list, di, src, ins); |
| 889 | 237 | if (r < ins) { |
| 238 | return processed + r; // LCOV_EXCL_LINE | |
| 239 | } | |
| 852 | 240 | } |
| 889 | 241 | processed += ins + skip; |
| 852 | 242 | di += ins; |
| 243 | ||
| 244 | // everything inserted? | |
| 889 | 245 | if (processed == n) { |
| 246 | return processed; | |
| 247 | } | |
| 852 | 248 | src = next; |
| 249 | } | |
| 250 | ||
| 251 | // insert remaining items | |
| 252 | if (si < n) { | |
| 889 | 253 | if (allow_duplicates) { |
| 891 | 254 | processed += list->cl->insert_array(list, di, src, n - si); |
| 889 | 255 | } else { |
| 891 | 256 | const void *last = di == 0 ? NULL : list->cl->at(list, di - 1); |
| 889 | 257 | for (; si < n; si++) { |
| 258 | // skip duplicates within the source | |
| 891 | 259 | if (last == NULL || cx_list_compare_wrapper(last, src, list) != 0) { |
| 260 | if (NULL == list->cl->insert_element(list, di, src)) { | |
| 889 | 261 | return processed; // LCOV_EXCL_LINE |
| 262 | } | |
| 263 | last = src; | |
| 264 | di++; | |
| 265 | } | |
| 266 | processed++; | |
| 267 | src += elem_size; | |
| 268 | } | |
| 269 | } | |
| 852 | 270 | } |
| 271 | ||
| 889 | 272 | return processed; |
| 273 | } | |
| 274 | ||
| 275 | size_t cx_list_default_insert_sorted( | |
| 276 | struct cx_list_s *list, | |
| 277 | const void *sorted_data, | |
| 278 | size_t n | |
| 279 | ) { | |
| 280 | return cx_list_default_insert_sorted_impl(list, sorted_data, n, true); | |
| 281 | } | |
| 282 | ||
| 283 | size_t cx_list_default_insert_unique( | |
| 284 | struct cx_list_s *list, | |
| 285 | const void *sorted_data, | |
| 286 | size_t n | |
| 287 | ) { | |
| 288 | return cx_list_default_insert_sorted_impl(list, sorted_data, n, false); | |
| 852 | 289 | } |
| 290 | ||
| 291 | void cx_list_default_sort(struct cx_list_s *list) { | |
| 292 | size_t elem_size = list->collection.elem_size; | |
| 293 | size_t list_size = list->collection.size; | |
| 886 | 294 | void *tmp = cxMallocDefault(elem_size * list_size); |
| 889 | 295 | if (tmp == NULL) abort(); // LCOV_EXCL_LINE |
| 852 | 296 | |
| 297 | // copy elements from source array | |
| 298 | char *loc = tmp; | |
| 299 | for (size_t i = 0; i < list_size; i++) { | |
| 891 | 300 | void *src = list->cl->at(list, i); |
| 852 | 301 | memcpy(loc, src, elem_size); |
| 302 | loc += elem_size; | |
| 303 | } | |
| 304 | ||
| 305 | // qsort | |
| 891 | 306 | cx_array_qsort_c(tmp, list_size, elem_size, cx_list_compare_wrapper, list); |
| 852 | 307 | |
| 308 | // copy elements back | |
| 309 | loc = tmp; | |
| 310 | for (size_t i = 0; i < list_size; i++) { | |
| 891 | 311 | void *dest = list->cl->at(list, i); |
| 852 | 312 | memcpy(dest, loc, elem_size); |
| 313 | loc += elem_size; | |
| 314 | } | |
| 315 | ||
| 886 | 316 | cxFreeDefault(tmp); |
| 852 | 317 | } |
| 318 | ||
| 319 | int cx_list_default_swap(struct cx_list_s *list, size_t i, size_t j) { | |
| 320 | if (i == j) return 0; | |
| 321 | if (i >= list->collection.size) return 1; | |
| 322 | if (j >= list->collection.size) return 1; | |
| 323 | ||
| 324 | size_t elem_size = list->collection.elem_size; | |
| 325 | ||
| 886 | 326 | void *tmp = cxMallocDefault(elem_size); |
| 889 | 327 | if (tmp == NULL) return 1; // LCOV_EXCL_LINE |
| 852 | 328 | |
| 891 | 329 | void *ip = list->cl->at(list, i); |
| 330 | void *jp = list->cl->at(list, j); | |
| 852 | 331 | |
| 332 | memcpy(tmp, ip, elem_size); | |
| 333 | memcpy(ip, jp, elem_size); | |
| 334 | memcpy(jp, tmp, elem_size); | |
| 335 | ||
| 886 | 336 | cxFreeDefault(tmp); |
| 852 | 337 | |
| 338 | return 0; | |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
339 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
340 | |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
341 | static int cx_list_cmpfunc2_safe_memcmp(const void *a, const void *b, void *c) { |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
342 | // it is not safe to store a pointer to the size in the list |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
343 | // because the entire list structure might get reallocated |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
344 | size_t elem_size = (size_t)(uintptr_t)c; |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
345 | return memcmp(a, b, elem_size); |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
346 | } |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
347 | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
348 | void cx_list_init( |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
349 | struct cx_list_s *list, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
350 | struct cx_list_class_s *cl, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
351 | const struct cx_allocator_s *allocator, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
352 | size_t elem_size |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
353 | ) { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
354 | list->cl = cl; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
355 | list->collection.allocator = allocator; |
| 891 | 356 | list->collection.size = 0; |
| 357 | list->collection.sorted = false; // should be set by the implementation | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
358 | if (elem_size > 0) { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
359 | list->collection.elem_size = elem_size; |
| 891 | 360 | list->collection.simple_cmp = NULL; |
|
894
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
361 | list->collection.advanced_cmp = cx_list_cmpfunc2_safe_memcmp; |
|
e86049631677
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
891
diff
changeset
|
362 | list->collection.cmp_data = (void*)(uintptr_t)list->collection.elem_size; |
| 891 | 363 | list->collection.store_pointer = false; |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
364 | } else { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
365 | list->collection.elem_size = sizeof(void *); |
| 891 | 366 | list->collection.simple_cmp = cx_cmp_ptr; |
| 367 | list->collection.advanced_cmp = NULL; | |
| 368 | list->collection.cmp_data = NULL; | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
369 | list->collection.store_pointer = true; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
370 | } |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
371 | } |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
372 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
373 | int cxListCompare( |
| 852 | 374 | const CxList *list, |
| 375 | const CxList *other | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
376 | ) { |
| 891 | 377 | // check if we cannot use the list internal function |
| 852 | 378 | bool cannot_optimize = false; |
| 379 | ||
| 380 | // if one is storing pointers but the other is not | |
| 381 | cannot_optimize |= list->collection.store_pointer ^ other->collection.store_pointer; | |
| 382 | ||
| 891 | 383 | // check if the lists are incompatible or this list does not implement compare |
| 384 | cx_compare_func list_cmp = (cx_compare_func) list->cl->compare; | |
| 385 | cx_compare_func other_cmp = (cx_compare_func) other->cl->compare; | |
| 386 | cannot_optimize |= list_cmp != other_cmp; | |
| 387 | cannot_optimize |= list_cmp == NULL; | |
| 750 | 388 | |
| 852 | 389 | if (cannot_optimize) { |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
390 | // lists are definitely different - cannot use internal compare function |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
750
diff
changeset
|
391 | if (list->collection.size == other->collection.size) { |
| 891 | 392 | CxIterator left = cxListIterator(list); |
| 393 | CxIterator right = cxListIterator(other); | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
750
diff
changeset
|
394 | 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:
505
diff
changeset
|
395 | void *leftValue = cxIteratorCurrent(left); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
396 | void *rightValue = cxIteratorCurrent(right); |
| 891 | 397 | // values are already unwrapped, invoke immediately |
| 398 | int d = cx_invoke_compare_func(list, leftValue, rightValue); | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
399 | if (d != 0) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
400 | return d; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
401 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
402 | cxIteratorNext(left); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
403 | cxIteratorNext(right); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
404 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
405 | return 0; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
406 | } else { |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
750
diff
changeset
|
407 | return list->collection.size < other->collection.size ? -1 : 1; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
408 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
409 | } else { |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
410 | // lists are compatible |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
411 | return list->cl->compare(list, other); |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
412 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
413 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
414 | |
| 889 | 415 | size_t cxListSize(const CxList *list) { |
| 416 | return list->collection.size; | |
| 417 | } | |
| 418 | ||
| 419 | int cxListAdd(CxList *list, const void *elem) { | |
| 420 | list->collection.sorted = false; | |
| 891 | 421 | return list->cl->insert_element(list, list->collection.size, cx_ref(list, elem)) == NULL; |
| 889 | 422 | } |
| 423 | ||
| 424 | size_t cxListAddArray(CxList *list, const void *array, size_t n) { | |
| 425 | list->collection.sorted = false; | |
| 426 | return list->cl->insert_array(list, list->collection.size, array, n); | |
| 427 | } | |
| 428 | ||
| 429 | int cxListInsert(CxList *list, size_t index, const void *elem) { | |
| 430 | list->collection.sorted = false; | |
| 891 | 431 | return list->cl->insert_element(list, index, cx_ref(list, elem)) == NULL; |
| 889 | 432 | } |
| 433 | ||
| 434 | void *cxListEmplaceAt(CxList *list, size_t index) { | |
| 435 | list->collection.sorted = false; | |
| 436 | return list->cl->insert_element(list, index, NULL); | |
| 437 | } | |
| 438 | ||
| 439 | void *cxListEmplace(CxList *list) { | |
| 440 | list->collection.sorted = false; | |
| 441 | return list->cl->insert_element(list, list->collection.size, NULL); | |
| 442 | } | |
| 443 | ||
| 444 | static bool cx_list_emplace_iterator_valid(const void *it) { | |
| 445 | const CxIterator *iter = it; | |
| 446 | return iter->index < iter->elem_count; | |
| 447 | } | |
| 448 | ||
| 449 | CxIterator cxListEmplaceArrayAt(CxList *list, size_t index, size_t n) { | |
| 450 | list->collection.sorted = false; | |
| 451 | size_t c = list->cl->insert_array(list, index, NULL, n); | |
| 452 | CxIterator iter = list->cl->iterator(list, index, false); | |
| 453 | // tweak the fields of this iterator | |
| 454 | iter.elem_count = c; | |
| 455 | iter.index = 0; | |
| 456 | // replace the valid function to abort iteration when c is reached | |
| 457 | iter.base.valid = cx_list_emplace_iterator_valid; | |
| 458 | return iter; | |
| 459 | } | |
| 460 | ||
| 461 | CxIterator cxListEmplaceArray(CxList *list, size_t n) { | |
| 462 | return cxListEmplaceArrayAt(list, list->collection.size, n); | |
| 463 | } | |
| 464 | ||
| 465 | int cxListInsertSorted(CxList *list, const void *elem) { | |
| 466 | assert(cxCollectionSorted(list)); | |
| 467 | list->collection.sorted = true; | |
| 891 | 468 | return list->cl->insert_sorted(list, cx_ref(list, elem), 1) == 0; |
| 889 | 469 | } |
| 470 | ||
| 471 | int cxListInsertUnique(CxList *list, const void *elem) { | |
| 472 | if (cxCollectionSorted(list)) { | |
| 473 | list->collection.sorted = true; | |
| 891 | 474 | return list->cl->insert_unique(list, cx_ref(list, elem), 1) == 0; |
| 889 | 475 | } else { |
| 476 | if (cxListContains(list, elem)) { | |
| 477 | return 0; | |
| 478 | } else { | |
| 479 | return cxListAdd(list, elem); | |
| 480 | } | |
| 481 | } | |
| 482 | } | |
| 483 | ||
| 484 | size_t cxListInsertArray(CxList *list, size_t index, const void *array, size_t n) { | |
| 485 | list->collection.sorted = false; | |
| 486 | return list->cl->insert_array(list, index, array, n); | |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
487 | } |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
488 | |
| 889 | 489 | size_t cxListInsertSortedArray(CxList *list, const void *array, size_t n) { |
| 490 | assert(cxCollectionSorted(list)); | |
| 491 | list->collection.sorted = true; | |
| 492 | return list->cl->insert_sorted(list, array, n); | |
| 493 | } | |
| 494 | ||
| 495 | size_t cxListInsertUniqueArray(CxList *list, const void *array, size_t n) { | |
| 496 | if (cxCollectionSorted(list)) { | |
| 497 | list->collection.sorted = true; | |
| 498 | return list->cl->insert_unique(list, array, n); | |
| 499 | } else { | |
| 500 | const char *source = array; | |
| 501 | for (size_t i = 0 ; i < n; i++) { | |
| 502 | // note: this also checks elements added in a previous iteration | |
| 891 | 503 | const void *data = cx_deref(list, source); |
| 889 | 504 | if (!cxListContains(list, data)) { |
| 505 | if (cxListAdd(list, data)) { | |
| 506 | return i; // LCOV_EXCL_LINE | |
| 507 | } | |
| 508 | } | |
| 509 | source += list->collection.elem_size; | |
| 510 | } | |
| 511 | return n; | |
| 512 | } | |
| 513 | } | |
| 514 | ||
| 515 | int cxListInsertAfter(CxIterator *iter, const void *elem) { | |
| 891 | 516 | CxList* list = iter->src_handle; |
| 889 | 517 | list->collection.sorted = false; |
| 891 | 518 | return list->cl->insert_iter(iter, cx_ref(list, elem), 0); |
| 889 | 519 | } |
| 520 | ||
| 521 | int cxListInsertBefore(CxIterator *iter, const void *elem) { | |
| 891 | 522 | CxList* list = iter->src_handle; |
| 889 | 523 | list->collection.sorted = false; |
| 891 | 524 | return list->cl->insert_iter(iter, cx_ref(list, elem), 1); |
| 889 | 525 | } |
| 526 | ||
| 527 | int cxListRemove(CxList *list, size_t index) { | |
| 528 | return list->cl->remove(list, index, 1, NULL) == 0; | |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
529 | } |
| 852 | 530 | |
| 889 | 531 | int cxListRemoveAndGet(CxList *list, size_t index, void *targetbuf) { |
| 532 | return list->cl->remove(list, index, 1, targetbuf) == 0; | |
| 533 | } | |
| 534 | ||
| 535 | int cxListRemoveAndGetFirst(CxList *list, void *targetbuf) { | |
| 536 | return list->cl->remove(list, 0, 1, targetbuf) == 0; | |
| 537 | } | |
| 538 | ||
| 539 | int cxListRemoveAndGetLast(CxList *list, void *targetbuf) { | |
| 540 | // note: index may wrap - member function will catch that | |
| 541 | return list->cl->remove(list, list->collection.size - 1, 1, targetbuf) == 0; | |
| 542 | } | |
| 543 | ||
| 544 | size_t cxListRemoveArray(CxList *list, size_t index, size_t num) { | |
| 545 | return list->cl->remove(list, index, num, NULL); | |
| 546 | } | |
| 547 | ||
| 548 | size_t cxListRemoveArrayAndGet(CxList *list, size_t index, size_t num, void *targetbuf) { | |
| 549 | return list->cl->remove(list, index, num, targetbuf); | |
| 852 | 550 | } |
| 886 | 551 | |
| 889 | 552 | void cxListClear(CxList *list) { |
| 553 | list->cl->clear(list); | |
| 554 | list->collection.sorted = true; // empty lists are always sorted | |
| 555 | } | |
| 556 | ||
| 557 | int cxListSwap(CxList *list, size_t i, size_t j) { | |
| 558 | list->collection.sorted = false; | |
| 559 | return list->cl->swap(list, i, j); | |
| 560 | } | |
| 561 | ||
| 562 | void *cxListAt(const CxList *list, size_t index) { | |
| 891 | 563 | void *result = list->cl->at(list, index); |
| 564 | if (result == NULL) return NULL; | |
| 565 | return cx_deref(list, result); | |
| 889 | 566 | } |
| 567 | ||
| 568 | void *cxListFirst(const CxList *list) { | |
| 891 | 569 | return cxListAt(list, 0); |
| 889 | 570 | } |
| 571 | ||
| 572 | void *cxListLast(const CxList *list) { | |
| 891 | 573 | return cxListAt(list, list->collection.size - 1); |
| 889 | 574 | } |
| 575 | ||
| 576 | int cxListSet(CxList *list, size_t index, const void *elem) { | |
| 886 | 577 | if (index >= list->collection.size) { |
| 578 | return 1; | |
| 579 | } | |
| 580 | ||
| 581 | if (list->collection.store_pointer) { | |
| 891 | 582 | void **target = list->cl->at(list, index); |
| 886 | 583 | *target = (void *)elem; |
| 584 | } else { | |
| 585 | void *target = list->cl->at(list, index); | |
| 586 | memcpy(target, elem, list->collection.elem_size); | |
| 587 | } | |
| 588 | ||
| 589 | return 0; | |
| 590 | } | |
| 889 | 591 | |
| 891 | 592 | static void *cx_pl_iter_current(const void *it) { |
| 593 | const struct cx_iterator_s *iter = it; | |
| 594 | void **ptr = iter->base.current_impl(it); | |
| 595 | return ptr == NULL ? NULL : *ptr; | |
| 596 | } | |
| 597 | ||
| 598 | CX_INLINE CxIterator cx_pl_iter_wrap(const CxList *list, CxIterator iter) { | |
| 599 | if (cxCollectionStoresPointers(list)) { | |
| 600 | iter.base.current_impl = iter.base.current; | |
| 601 | iter.base.current = cx_pl_iter_current; | |
| 602 | return iter; | |
| 603 | } else { | |
| 604 | return iter; | |
| 605 | } | |
| 606 | } | |
| 607 | ||
| 889 | 608 | CxIterator cxListIteratorAt(const CxList *list, size_t index) { |
| 609 | if (list == NULL) list = cxEmptyList; | |
| 891 | 610 | return cx_pl_iter_wrap(list, list->cl->iterator(list, index, false)); |
| 889 | 611 | } |
| 612 | ||
| 613 | CxIterator cxListBackwardsIteratorAt(const CxList *list, size_t index) { | |
| 614 | if (list == NULL) list = cxEmptyList; | |
| 891 | 615 | return cx_pl_iter_wrap(list, list->cl->iterator(list, index, true)); |
| 889 | 616 | } |
| 617 | ||
| 618 | CxIterator cxListIterator(const CxList *list) { | |
| 619 | if (list == NULL) list = cxEmptyList; | |
| 891 | 620 | return cx_pl_iter_wrap(list, list->cl->iterator(list, 0, false)); |
| 889 | 621 | } |
| 622 | ||
| 623 | CxIterator cxListBackwardsIterator(const CxList *list) { | |
| 624 | if (list == NULL) list = cxEmptyList; | |
| 891 | 625 | return cx_pl_iter_wrap(list, list->cl->iterator(list, list->collection.size - 1, true)); |
| 889 | 626 | } |
| 627 | ||
| 628 | size_t cxListFind(const CxList *list, const void *elem) { | |
| 891 | 629 | return list->cl->find_remove((CxList*)list, cx_ref(list, elem), false); |
| 889 | 630 | } |
| 631 | ||
| 632 | bool cxListContains(const CxList* list, const void* elem) { | |
| 891 | 633 | return list->cl->find_remove((CxList*)list, cx_ref(list, elem), false) < list->collection.size; |
| 889 | 634 | } |
| 635 | ||
| 636 | bool cxListIndexValid(const CxList *list, size_t index) { | |
| 637 | return index < list->collection.size; | |
| 638 | } | |
| 639 | ||
| 640 | size_t cxListFindRemove(CxList *list, const void *elem) { | |
| 891 | 641 | return list->cl->find_remove(list, cx_ref(list, elem), true); |
| 889 | 642 | } |
| 643 | ||
| 644 | void cxListSort(CxList *list) { | |
| 645 | if (list->collection.sorted) return; | |
| 646 | list->cl->sort(list); | |
| 647 | list->collection.sorted = true; | |
| 648 | } | |
| 649 | ||
| 650 | void cxListReverse(CxList *list) { | |
| 651 | // still sorted, but not according to the cmp_func | |
| 652 | list->collection.sorted = false; | |
| 653 | list->cl->reverse(list); | |
| 654 | } | |
| 655 | ||
| 656 | void cxListFree(CxList *list) { | |
| 657 | if (list == NULL) return; | |
| 658 | list->cl->deallocate(list); | |
| 659 | } | |
| 660 | ||
| 661 | static void cx_list_pop_uninitialized_elements(CxList *list, size_t n) { | |
| 662 | cx_destructor_func destr_bak = list->collection.simple_destructor; | |
| 663 | cx_destructor_func2 destr2_bak = list->collection.advanced_destructor; | |
| 664 | list->collection.simple_destructor = NULL; | |
| 665 | list->collection.advanced_destructor = NULL; | |
| 666 | if (n == 1) { | |
| 667 | cxListRemove(list, list->collection.size - 1); | |
| 668 | } else { | |
| 669 | cxListRemoveArray(list,list->collection.size - n, n); | |
| 670 | } | |
| 671 | list->collection.simple_destructor = destr_bak; | |
| 672 | list->collection.advanced_destructor = destr2_bak; | |
| 673 | } | |
| 674 | ||
| 891 | 675 | static void* cx_list_shallow_clone_func(void *dst, const void *src, const CxAllocator *al, void *data) { |
| 676 | size_t elem_size = *(size_t*)data; | |
| 677 | if (dst == NULL) dst = cxMalloc(al, elem_size); | |
| 678 | if (dst != NULL) memcpy(dst, src, elem_size); | |
| 679 | return dst; | |
| 680 | } | |
| 681 | ||
| 682 | #define use_shallow_clone_func(list) cx_list_shallow_clone_func, NULL, (void*)&((list)->collection.elem_size) | |
| 683 | ||
| 889 | 684 | int cxListClone(CxList *dst, const CxList *src, cx_clone_func clone_func, |
| 685 | const CxAllocator *clone_allocator, void *data) { | |
| 686 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 687 | ||
| 688 | // remember the original size | |
| 689 | size_t orig_size = dst->collection.size; | |
| 690 | ||
| 691 | // first, try to allocate the memory in the new list | |
| 692 | CxIterator empl_iter = cxListEmplaceArray(dst, src->collection.size); | |
| 693 | ||
| 694 | // get an iterator over the source elements | |
| 695 | CxIterator src_iter = cxListIterator(src); | |
| 696 | ||
| 697 | // now clone the elements | |
| 698 | size_t cloned = empl_iter.elem_count; | |
| 699 | for (size_t i = 0 ; i < empl_iter.elem_count; i++) { | |
| 700 | void *src_elem = cxIteratorCurrent(src_iter); | |
| 701 | void **dest_memory = cxIteratorCurrent(empl_iter); | |
| 702 | void *target = cxCollectionStoresPointers(dst) ? NULL : dest_memory; | |
| 703 | void *dest_ptr = clone_func(target, src_elem, clone_allocator, data); | |
| 704 | if (dest_ptr == NULL) { | |
| 705 | cloned = i; | |
| 706 | break; | |
| 707 | } | |
| 708 | if (cxCollectionStoresPointers(dst)) { | |
| 709 | *dest_memory = dest_ptr; | |
| 710 | } | |
| 711 | cxIteratorNext(src_iter); | |
| 712 | cxIteratorNext(empl_iter); | |
| 713 | } | |
| 714 | ||
| 715 | // if we could not clone everything, free the allocated memory | |
| 716 | // (disable the destructors!) | |
| 717 | if (cloned < src->collection.size) { | |
| 718 | cx_list_pop_uninitialized_elements(dst, | |
| 719 | dst->collection.size - cloned - orig_size); | |
| 720 | return 1; | |
| 721 | } | |
| 722 | ||
| 891 | 723 | // set the sorted flag when we know it's sorted |
| 724 | if (orig_size == 0 && src->collection.sorted) { | |
| 725 | dst->collection.sorted = true; | |
| 726 | } | |
| 727 | ||
| 889 | 728 | return 0; |
| 729 | } | |
| 730 | ||
| 731 | int cxListDifference(CxList *dst, | |
| 732 | const CxList *minuend, const CxList *subtrahend, | |
| 733 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { | |
| 734 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 735 | ||
| 736 | // optimize for sorted collections | |
| 737 | if (cxCollectionSorted(minuend) && cxCollectionSorted(subtrahend)) { | |
| 738 | bool dst_was_empty = cxCollectionSize(dst) == 0; | |
| 739 | ||
| 740 | CxIterator min_iter = cxListIterator(minuend); | |
| 741 | CxIterator sub_iter = cxListIterator(subtrahend); | |
| 742 | while (cxIteratorValid(min_iter)) { | |
| 743 | void *min_elem = cxIteratorCurrent(min_iter); | |
| 744 | void *sub_elem; | |
| 745 | int d; | |
| 746 | if (cxIteratorValid(sub_iter)) { | |
| 747 | sub_elem = cxIteratorCurrent(sub_iter); | |
| 891 | 748 | d = cx_list_compare_wrapper(sub_elem, min_elem, subtrahend); |
| 889 | 749 | } else { |
| 750 | // no more elements in the subtrahend, | |
| 751 | // i.e., the min_elem is larger than any elem of the subtrahend | |
| 752 | d = 1; | |
| 753 | } | |
| 754 | if (d == 0) { | |
| 755 | // is contained, so skip it | |
| 756 | cxIteratorNext(min_iter); | |
| 757 | } else if (d < 0) { | |
| 758 | // subtrahend is smaller than minuend, | |
| 759 | // check the next element | |
| 760 | cxIteratorNext(sub_iter); | |
| 761 | } else { | |
| 762 | // subtrahend is larger than the dst element, | |
| 763 | // clone the minuend and advance | |
| 764 | void **dst_mem = cxListEmplace(dst); | |
| 765 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 766 | void* dst_ptr = clone_func(target, min_elem, clone_allocator, data); | |
| 767 | if (dst_ptr == NULL) { | |
| 768 | cx_list_pop_uninitialized_elements(dst, 1); | |
| 769 | return 1; | |
| 770 | } | |
| 771 | if (cxCollectionStoresPointers(dst)) { | |
| 772 | *dst_mem = dst_ptr; | |
| 773 | } | |
| 774 | cxIteratorNext(min_iter); | |
| 775 | } | |
| 776 | } | |
| 777 | ||
| 778 | // if dst was empty, it is now guaranteed to be sorted | |
| 779 | dst->collection.sorted = dst_was_empty; | |
| 780 | } else { | |
| 781 | CxIterator min_iter = cxListIterator(minuend); | |
| 782 | cx_foreach(void *, elem, min_iter) { | |
| 783 | if (cxListContains(subtrahend, elem)) { | |
| 784 | continue; | |
| 785 | } | |
| 786 | void **dst_mem = cxListEmplace(dst); | |
| 787 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 788 | void* dst_ptr = clone_func(target, elem, clone_allocator, data); | |
| 789 | if (dst_ptr == NULL) { | |
| 790 | cx_list_pop_uninitialized_elements(dst, 1); | |
| 791 | return 1; | |
| 792 | } | |
| 793 | if (cxCollectionStoresPointers(dst)) { | |
| 794 | *dst_mem = dst_ptr; | |
| 795 | } | |
| 796 | } | |
| 797 | } | |
| 798 | ||
| 799 | return 0; | |
| 800 | } | |
| 801 | ||
| 802 | int cxListIntersection(CxList *dst, | |
| 803 | const CxList *src, const CxList *other, | |
| 804 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { | |
| 805 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 806 | ||
| 807 | // optimize for sorted collections | |
| 808 | if (cxCollectionSorted(src) && cxCollectionSorted(other)) { | |
| 809 | bool dst_was_empty = cxCollectionSize(dst) == 0; | |
| 810 | ||
| 811 | CxIterator src_iter = cxListIterator(src); | |
| 812 | CxIterator other_iter = cxListIterator(other); | |
| 813 | while (cxIteratorValid(src_iter) && cxIteratorValid(other_iter)) { | |
| 814 | void *src_elem = cxIteratorCurrent(src_iter); | |
| 815 | void *other_elem = cxIteratorCurrent(other_iter); | |
| 891 | 816 | int d = cx_list_compare_wrapper(src_elem, other_elem, src); |
| 889 | 817 | if (d == 0) { |
| 818 | // is contained, clone it | |
| 819 | void **dst_mem = cxListEmplace(dst); | |
| 820 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 821 | void* dst_ptr = clone_func(target, src_elem, clone_allocator, data); | |
| 822 | if (dst_ptr == NULL) { | |
| 823 | cx_list_pop_uninitialized_elements(dst, 1); | |
| 824 | return 1; | |
| 825 | } | |
| 826 | if (cxCollectionStoresPointers(dst)) { | |
| 827 | *dst_mem = dst_ptr; | |
| 828 | } | |
| 829 | cxIteratorNext(src_iter); | |
| 830 | } else if (d < 0) { | |
| 831 | // the other element is larger, skip the source element | |
| 832 | cxIteratorNext(src_iter); | |
| 833 | } else { | |
| 834 | // the source element is larger, try to find it in the other list | |
| 835 | cxIteratorNext(other_iter); | |
| 836 | } | |
| 837 | } | |
| 838 | ||
| 839 | // if dst was empty, it is now guaranteed to be sorted | |
| 840 | dst->collection.sorted = dst_was_empty; | |
| 841 | } else { | |
| 842 | CxIterator src_iter = cxListIterator(src); | |
| 843 | cx_foreach(void *, elem, src_iter) { | |
| 844 | if (!cxListContains(other, elem)) { | |
| 845 | continue; | |
| 846 | } | |
| 847 | void **dst_mem = cxListEmplace(dst); | |
| 848 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 849 | void* dst_ptr = clone_func(target, elem, clone_allocator, data); | |
| 850 | if (dst_ptr == NULL) { | |
| 851 | cx_list_pop_uninitialized_elements(dst, 1); | |
| 852 | return 1; | |
| 853 | } | |
| 854 | if (cxCollectionStoresPointers(dst)) { | |
| 855 | *dst_mem = dst_ptr; | |
| 856 | } | |
| 857 | } | |
| 858 | } | |
| 859 | ||
| 860 | return 0; | |
| 861 | } | |
| 862 | ||
| 863 | int cxListUnion(CxList *dst, | |
| 864 | const CxList *src, const CxList *other, | |
| 865 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { | |
| 866 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 867 | ||
| 868 | // optimize for sorted collections | |
| 869 | if (cxCollectionSorted(src) && cxCollectionSorted(other)) { | |
| 870 | bool dst_was_empty = cxCollectionSize(dst) == 0; | |
| 871 | ||
| 872 | CxIterator src_iter = cxListIterator(src); | |
| 873 | CxIterator other_iter = cxListIterator(other); | |
| 874 | while (cxIteratorValid(src_iter) || cxIteratorValid(other_iter)) { | |
| 891 | 875 | void *src_elem = NULL, *other_elem = NULL; |
| 889 | 876 | int d; |
| 877 | if (!cxIteratorValid(src_iter)) { | |
| 878 | other_elem = cxIteratorCurrent(other_iter); | |
| 879 | d = 1; | |
| 880 | } else if (!cxIteratorValid(other_iter)) { | |
| 881 | src_elem = cxIteratorCurrent(src_iter); | |
| 882 | d = -1; | |
| 883 | } else { | |
| 884 | src_elem = cxIteratorCurrent(src_iter); | |
| 885 | other_elem = cxIteratorCurrent(other_iter); | |
| 891 | 886 | d = cx_list_compare_wrapper(src_elem, other_elem, src); |
| 889 | 887 | } |
| 891 | 888 | void *clone_from; |
| 889 | if (d < 0) { | |
| 890 | // source element is smaller clone it | |
| 891 | clone_from = src_elem; | |
| 889 | 892 | cxIteratorNext(src_iter); |
| 891 | 893 | } else if (d == 0) { |
| 894 | // both elements are equal, clone from the source, skip other | |
| 895 | clone_from = src_elem; | |
| 896 | cxIteratorNext(src_iter); | |
| 897 | cxIteratorNext(other_iter); | |
| 889 | 898 | } else { |
| 899 | // the other element is smaller, clone it | |
| 891 | 900 | clone_from = other_elem; |
| 889 | 901 | cxIteratorNext(other_iter); |
| 902 | } | |
| 891 | 903 | void **dst_mem = cxListEmplace(dst); |
| 904 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 905 | void* dst_ptr = clone_func(target, clone_from, clone_allocator, data); | |
| 906 | if (dst_ptr == NULL) { | |
| 907 | cx_list_pop_uninitialized_elements(dst, 1); | |
| 908 | return 1; | |
| 909 | } | |
| 910 | if (cxCollectionStoresPointers(dst)) { | |
| 911 | *dst_mem = dst_ptr; | |
| 912 | } | |
| 889 | 913 | } |
| 914 | ||
| 915 | // if dst was empty, it is now guaranteed to be sorted | |
| 916 | dst->collection.sorted = dst_was_empty; | |
| 917 | } else { | |
| 918 | if (cxListClone(dst, src, clone_func, clone_allocator, data)) { | |
| 919 | return 1; | |
| 920 | } | |
| 921 | CxIterator other_iter = cxListIterator(other); | |
| 922 | cx_foreach(void *, elem, other_iter) { | |
| 923 | if (cxListContains(src, elem)) { | |
| 924 | continue; | |
| 925 | } | |
| 926 | void **dst_mem = cxListEmplace(dst); | |
| 927 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 928 | void* dst_ptr = clone_func(target, elem, clone_allocator, data); | |
| 929 | if (dst_ptr == NULL) { | |
| 930 | cx_list_pop_uninitialized_elements(dst, 1); | |
| 931 | return 1; | |
| 932 | } | |
| 933 | if (cxCollectionStoresPointers(dst)) { | |
| 934 | *dst_mem = dst_ptr; | |
| 935 | } | |
| 936 | } | |
| 937 | } | |
| 938 | ||
| 939 | return 0; | |
| 940 | } | |
| 891 | 941 | |
| 942 | int cxListCloneShallow(CxList *dst, const CxList *src) { | |
| 943 | return cxListClone(dst, src, use_shallow_clone_func(src)); | |
| 944 | } | |
| 945 | ||
| 946 | int cxListDifferenceShallow(CxList *dst, const CxList *minuend, const CxList *subtrahend) { | |
| 947 | return cxListDifference(dst, minuend, subtrahend, use_shallow_clone_func(minuend)); | |
| 948 | } | |
| 949 | ||
| 950 | int cxListIntersectionShallow(CxList *dst, const CxList *src, const CxList *other) { | |
| 951 | return cxListIntersection(dst, src, other, use_shallow_clone_func(src)); | |
| 952 | } | |
| 953 | ||
| 954 | int cxListUnionShallow(CxList *dst, const CxList *src, const CxList *other) { | |
| 955 | return cxListUnion(dst, src, other, use_shallow_clone_func(src)); | |
| 956 | } | |
| 957 | ||
| 958 | int cxListReserve(CxList *list, size_t capacity) { | |
| 959 | if (list->cl->change_capacity == NULL) { | |
| 960 | return 0; | |
| 961 | } | |
| 962 | if (capacity <= cxCollectionSize(list)) { | |
| 963 | return 0; | |
| 964 | } | |
| 965 | return list->cl->change_capacity(list, capacity); | |
| 966 | } | |
| 967 | ||
| 968 | int cxListShrink(CxList *list) { | |
| 969 | if (list->cl->change_capacity == NULL) { | |
| 970 | return 0; | |
| 971 | } | |
| 972 | return list->cl->change_capacity(list, cxCollectionSize(list)); | |
| 973 | } |