Sat, 08 Nov 2025 23:06:11 +0100
update ucx
|
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> |
| 888 | 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 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
34 | // <editor-fold desc="Store Pointers Functionality"> |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
35 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
36 | static _Thread_local cx_compare_func cx_pl_cmpfunc_impl; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
37 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
38 | static int cx_pl_cmpfunc( |
| 852 | 39 | const void *l, |
| 40 | const void *r | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
41 | ) { |
| 888 | 42 | // l and r are guaranteed to be non-NULL pointing to the list's memory |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
43 | void *const *lptr = l; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
44 | void *const *rptr = r; |
| 888 | 45 | const void *left = *lptr; |
| 46 | const void *right = *rptr; | |
| 47 | if (left == NULL) { | |
| 48 | // NULL is smaller than any value except NULL | |
| 49 | return right == NULL ? 0 : -1; | |
| 50 | } else if (right == NULL) { | |
| 51 | // any value is larger than NULL | |
| 52 | return 1; | |
| 53 | } | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
54 | return cx_pl_cmpfunc_impl(left, right); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
55 | } |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
56 | |
| 852 | 57 | static void cx_pl_hack_cmpfunc(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:
505
diff
changeset
|
58 | // cast away const - this is the hacky thing |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
750
diff
changeset
|
59 | struct cx_collection_s *l = (struct cx_collection_s*) &list->collection; |
|
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 | cx_pl_cmpfunc_impl = l->cmpfunc; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
61 | l->cmpfunc = cx_pl_cmpfunc; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
62 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
63 | |
| 852 | 64 | static void cx_pl_unhack_cmpfunc(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:
505
diff
changeset
|
65 | // cast away const - this is the hacky thing |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
750
diff
changeset
|
66 | struct cx_collection_s *l = (struct cx_collection_s*) &list->collection; |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
67 | l->cmpfunc = cx_pl_cmpfunc_impl; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
68 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
69 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
70 | static void cx_pl_destructor(struct cx_list_s *list) { |
| 852 | 71 | list->climpl->deallocate(list); |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
72 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
73 | |
| 888 | 74 | static void *cx_pl_insert_element( |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
75 | 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:
505
diff
changeset
|
76 | size_t index, |
| 852 | 77 | const void *element |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
78 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
79 | return list->climpl->insert_element(list, index, &element); |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
80 | } |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
81 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
82 | static size_t cx_pl_insert_array( |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
83 | 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:
505
diff
changeset
|
84 | size_t index, |
| 852 | 85 | const void *array, |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
86 | size_t n |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
87 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
88 | return list->climpl->insert_array(list, index, array, n); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
89 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
90 | |
| 852 | 91 | static size_t cx_pl_insert_sorted( |
| 92 | struct cx_list_s *list, | |
| 93 | const void *array, | |
| 94 | size_t n | |
| 95 | ) { | |
| 96 | cx_pl_hack_cmpfunc(list); | |
| 97 | size_t result = list->climpl->insert_sorted(list, array, n); | |
| 98 | cx_pl_unhack_cmpfunc(list); | |
| 99 | return result; | |
| 100 | } | |
| 101 | ||
| 888 | 102 | static size_t cx_pl_insert_unique( |
| 103 | struct cx_list_s *list, | |
| 104 | const void *array, | |
| 105 | size_t n | |
| 106 | ) { | |
| 107 | cx_pl_hack_cmpfunc(list); | |
| 108 | size_t result = list->climpl->insert_unique(list, array, n); | |
| 109 | cx_pl_unhack_cmpfunc(list); | |
| 110 | return result; | |
| 111 | } | |
| 112 | ||
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
113 | static int cx_pl_insert_iter( |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
750
diff
changeset
|
114 | struct cx_iterator_s *iter, |
| 852 | 115 | const void *elem, |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
116 | int prepend |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
117 | ) { |
| 888 | 118 | struct cx_list_s *list = iter->src_handle; |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
119 | return list->climpl->insert_iter(iter, &elem, prepend); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
120 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
121 | |
| 852 | 122 | static size_t cx_pl_remove( |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
123 | struct cx_list_s *list, |
| 852 | 124 | size_t index, |
| 125 | size_t num, | |
| 126 | void *targetbuf | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
127 | ) { |
| 852 | 128 | return list->climpl->remove(list, index, num, targetbuf); |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
129 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
130 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
131 | static void cx_pl_clear(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:
505
diff
changeset
|
132 | list->climpl->clear(list); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
133 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
134 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
135 | static int cx_pl_swap( |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
136 | 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:
505
diff
changeset
|
137 | size_t i, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
138 | size_t j |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
139 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
140 | return list->climpl->swap(list, i, j); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
141 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
142 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
143 | static void *cx_pl_at( |
| 852 | 144 | 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:
505
diff
changeset
|
145 | size_t index |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
146 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
147 | void **ptr = list->climpl->at(list, index); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
148 | return ptr == NULL ? NULL : *ptr; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
149 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
150 | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
151 | static size_t cx_pl_find_remove( |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
750
diff
changeset
|
152 | struct cx_list_s *list, |
| 852 | 153 | const void *elem, |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
750
diff
changeset
|
154 | bool remove |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
155 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
156 | cx_pl_hack_cmpfunc(list); |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
157 | size_t ret = list->climpl->find_remove(list, &elem, remove); |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
158 | cx_pl_unhack_cmpfunc(list); |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
159 | return ret; |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
160 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
161 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
162 | static void cx_pl_sort(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:
505
diff
changeset
|
163 | cx_pl_hack_cmpfunc(list); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
164 | list->climpl->sort(list); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
165 | cx_pl_unhack_cmpfunc(list); |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
166 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
167 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
168 | static int cx_pl_compare( |
| 852 | 169 | const struct cx_list_s *list, |
| 170 | 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:
505
diff
changeset
|
171 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
172 | cx_pl_hack_cmpfunc(list); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
173 | int ret = list->climpl->compare(list, other); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
174 | cx_pl_unhack_cmpfunc(list); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
175 | return ret; |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
176 | } |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
177 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
178 | static void cx_pl_reverse(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:
505
diff
changeset
|
179 | list->climpl->reverse(list); |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
180 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
181 | |
| 852 | 182 | static void *cx_pl_iter_current(const void *it) { |
| 183 | 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:
505
diff
changeset
|
184 | void **ptr = iter->base.current_impl(it); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
185 | return ptr == NULL ? NULL : *ptr; |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
186 | } |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
187 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
188 | static struct cx_iterator_s cx_pl_iterator( |
| 852 | 189 | 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:
505
diff
changeset
|
190 | size_t index, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
191 | bool backwards |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
192 | ) { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
193 | struct cx_iterator_s iter = list->climpl->iterator(list, index, backwards); |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
194 | iter.base.current_impl = iter.base.current; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
195 | iter.base.current = cx_pl_iter_current; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
196 | return iter; |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
197 | } |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
198 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
199 | static cx_list_class cx_pointer_list_class = { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
200 | cx_pl_destructor, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
201 | cx_pl_insert_element, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
202 | cx_pl_insert_array, |
| 852 | 203 | cx_pl_insert_sorted, |
| 888 | 204 | cx_pl_insert_unique, |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
205 | cx_pl_insert_iter, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
206 | cx_pl_remove, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
207 | cx_pl_clear, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
208 | cx_pl_swap, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
209 | cx_pl_at, |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
750
diff
changeset
|
210 | cx_pl_find_remove, |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
211 | cx_pl_sort, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
212 | cx_pl_compare, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
213 | cx_pl_reverse, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
214 | cx_pl_iterator, |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
215 | }; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
216 | // </editor-fold> |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
217 | |
| 750 | 218 | // <editor-fold desc="empty list implementation"> |
| 219 | ||
| 852 | 220 | static void cx_emptyl_noop(cx_attr_unused CxList *list) { |
| 750 | 221 | // this is a noop, but MUST be implemented |
| 222 | } | |
| 223 | ||
| 224 | static void *cx_emptyl_at( | |
| 852 | 225 | cx_attr_unused const struct cx_list_s *list, |
| 226 | cx_attr_unused size_t index | |
| 750 | 227 | ) { |
| 228 | return NULL; | |
| 229 | } | |
| 230 | ||
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
231 | static size_t cx_emptyl_find_remove( |
| 852 | 232 | cx_attr_unused struct cx_list_s *list, |
| 233 | cx_attr_unused const void *elem, | |
| 234 | cx_attr_unused bool remove | |
| 750 | 235 | ) { |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
236 | return 0; |
| 750 | 237 | } |
| 238 | ||
| 852 | 239 | static bool cx_emptyl_iter_valid(cx_attr_unused const void *iter) { |
| 750 | 240 | return false; |
| 241 | } | |
| 242 | ||
| 243 | static CxIterator cx_emptyl_iterator( | |
| 852 | 244 | const struct cx_list_s *list, |
| 750 | 245 | size_t index, |
| 852 | 246 | cx_attr_unused bool backwards |
| 750 | 247 | ) { |
| 248 | CxIterator iter = {0}; | |
| 888 | 249 | iter.src_handle = (void*) list; |
| 750 | 250 | iter.index = index; |
| 251 | iter.base.valid = cx_emptyl_iter_valid; | |
| 252 | return iter; | |
| 253 | } | |
| 254 | ||
| 255 | static cx_list_class cx_empty_list_class = { | |
| 256 | cx_emptyl_noop, | |
| 257 | NULL, | |
| 258 | NULL, | |
| 259 | NULL, | |
| 260 | NULL, | |
| 852 | 261 | NULL, |
| 888 | 262 | NULL, |
| 750 | 263 | cx_emptyl_noop, |
| 264 | NULL, | |
| 265 | 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
|
266 | cx_emptyl_find_remove, |
| 750 | 267 | cx_emptyl_noop, |
| 852 | 268 | NULL, |
| 750 | 269 | cx_emptyl_noop, |
| 270 | cx_emptyl_iterator, | |
| 271 | }; | |
| 272 | ||
| 273 | CxList cx_empty_list = { | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
274 | { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
275 | NULL, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
276 | NULL, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
277 | 0, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
278 | 0, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
279 | NULL, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
280 | NULL, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
281 | NULL, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
282 | false, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
283 | true, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
284 | }, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
285 | &cx_empty_list_class, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
286 | NULL |
| 750 | 287 | }; |
| 288 | ||
| 289 | CxList *const cxEmptyList = &cx_empty_list; | |
| 290 | ||
| 291 | // </editor-fold> | |
| 292 | ||
| 852 | 293 | #define invoke_list_func(name, list, ...) \ |
| 294 | ((list)->climpl == NULL ? (list)->cl->name : (list)->climpl->name) \ | |
| 295 | (list, __VA_ARGS__) | |
| 296 | ||
| 297 | size_t cx_list_default_insert_array( | |
| 298 | struct cx_list_s *list, | |
| 299 | size_t index, | |
| 300 | const void *data, | |
| 301 | size_t n | |
| 302 | ) { | |
| 303 | const char *src = data; | |
| 304 | size_t i = 0; | |
| 305 | for (; i < n; i++) { | |
| 888 | 306 | if (NULL == invoke_list_func( |
| 307 | insert_element, list, index + i, src) | |
| 308 | ) { | |
| 309 | return i; // LCOV_EXCL_LINE | |
| 310 | } | |
| 311 | if (src != NULL) { | |
| 312 | src += list->collection.elem_size; | |
| 313 | } | |
| 852 | 314 | } |
| 315 | return i; | |
| 316 | } | |
| 317 | ||
| 888 | 318 | static size_t cx_list_default_insert_sorted_impl( |
| 852 | 319 | struct cx_list_s *list, |
| 320 | const void *sorted_data, | |
| 888 | 321 | size_t n, |
| 322 | bool allow_duplicates | |
| 852 | 323 | ) { |
| 324 | // corner case | |
| 325 | if (n == 0) return 0; | |
| 326 | ||
| 327 | size_t elem_size = list->collection.elem_size; | |
| 328 | cx_compare_func cmp = list->collection.cmpfunc; | |
| 329 | const char *src = sorted_data; | |
| 330 | ||
| 331 | // track indices and number of inserted items | |
| 888 | 332 | size_t di = 0, si = 0, processed = 0; |
| 852 | 333 | |
| 334 | // search the list for insertion points | |
| 888 | 335 | while (di < list->collection.size) { |
| 852 | 336 | const void *list_elm = invoke_list_func(at, list, di); |
| 337 | ||
| 888 | 338 | // compare the current list element with the first source element |
| 339 | // if less, skip the list elements | |
| 340 | // if equal, skip the list elements and optionally the source elements | |
| 341 | { | |
| 342 | int d = cmp(list_elm, src); | |
| 343 | if (d <= 0) { | |
| 344 | if (!allow_duplicates && d == 0) { | |
| 345 | src += elem_size; | |
| 346 | si++; | |
| 347 | processed++; // we also count duplicates for the return value | |
| 348 | while (si < n && cmp(list_elm, src) == 0) { | |
| 349 | src += elem_size; | |
| 350 | si++; | |
| 351 | processed++; | |
| 352 | } | |
| 353 | if (processed == n) { | |
| 354 | return processed; | |
| 355 | } | |
| 356 | } | |
| 357 | di++; | |
| 358 | continue; | |
| 359 | } | |
| 852 | 360 | } |
| 361 | ||
| 888 | 362 | // determine the number of consecutive elements that can be inserted |
| 363 | size_t ins = 1, skip = 0; | |
| 852 | 364 | const char *next = src; |
| 365 | while (++si < n) { | |
| 888 | 366 | if (!allow_duplicates) { |
| 367 | // skip duplicates within the source | |
| 368 | if (cmp(next, next + elem_size) == 0) { | |
| 369 | next += elem_size; | |
| 370 | skip++; | |
| 371 | continue; | |
| 372 | } else { | |
| 373 | if (skip > 0) { | |
| 374 | // if we had to skip something, we must wait for the next run | |
| 375 | next += elem_size; | |
| 376 | break; | |
| 377 | } | |
| 378 | } | |
| 379 | } | |
| 852 | 380 | next += elem_size; |
| 381 | // once we become larger than the list elem, break | |
| 382 | if (cmp(list_elm, next) <= 0) { | |
| 383 | break; | |
| 384 | } | |
| 385 | // otherwise, we can insert one more | |
| 386 | ins++; | |
| 387 | } | |
| 388 | ||
| 389 | // insert the elements at location si | |
| 390 | if (ins == 1) { | |
| 888 | 391 | if (NULL == invoke_list_func(insert_element, list, di, src)) { |
| 392 | return processed; // LCOV_EXCL_LINE | |
| 393 | } | |
| 852 | 394 | } else { |
| 395 | size_t r = invoke_list_func(insert_array, list, di, src, ins); | |
| 888 | 396 | if (r < ins) { |
| 397 | return processed + r; // LCOV_EXCL_LINE | |
| 398 | } | |
| 852 | 399 | } |
| 888 | 400 | processed += ins + skip; |
| 852 | 401 | di += ins; |
| 402 | ||
| 403 | // everything inserted? | |
| 888 | 404 | if (processed == n) { |
| 405 | return processed; | |
| 406 | } | |
| 852 | 407 | src = next; |
| 408 | } | |
| 409 | ||
| 410 | // insert remaining items | |
| 411 | if (si < n) { | |
| 888 | 412 | if (allow_duplicates) { |
| 413 | processed += invoke_list_func(insert_array, list, di, src, n - si); | |
| 414 | } else { | |
| 415 | const void *last = di == 0 ? NULL : invoke_list_func(at, list, di - 1); | |
| 416 | for (; si < n; si++) { | |
| 417 | // skip duplicates within the source | |
| 418 | if (last == NULL || cmp(last, src) != 0) { | |
| 419 | if (NULL == invoke_list_func(insert_element, list, di, src)) { | |
| 420 | return processed; // LCOV_EXCL_LINE | |
| 421 | } | |
| 422 | last = src; | |
| 423 | di++; | |
| 424 | } | |
| 425 | processed++; | |
| 426 | src += elem_size; | |
| 427 | } | |
| 428 | } | |
| 852 | 429 | } |
| 430 | ||
| 888 | 431 | return processed; |
| 432 | } | |
| 433 | ||
| 434 | size_t cx_list_default_insert_sorted( | |
| 435 | struct cx_list_s *list, | |
| 436 | const void *sorted_data, | |
| 437 | size_t n | |
| 438 | ) { | |
| 439 | return cx_list_default_insert_sorted_impl(list, sorted_data, n, true); | |
| 440 | } | |
| 441 | ||
| 442 | size_t cx_list_default_insert_unique( | |
| 443 | struct cx_list_s *list, | |
| 444 | const void *sorted_data, | |
| 445 | size_t n | |
| 446 | ) { | |
| 447 | return cx_list_default_insert_sorted_impl(list, sorted_data, n, false); | |
| 852 | 448 | } |
| 449 | ||
| 450 | void cx_list_default_sort(struct cx_list_s *list) { | |
| 451 | size_t elem_size = list->collection.elem_size; | |
| 452 | size_t list_size = list->collection.size; | |
| 888 | 453 | void *tmp = cxMallocDefault(elem_size * list_size); |
| 454 | if (tmp == NULL) abort(); // LCOV_EXCL_LINE | |
| 852 | 455 | |
| 456 | // copy elements from source array | |
| 457 | char *loc = tmp; | |
| 458 | for (size_t i = 0; i < list_size; i++) { | |
| 459 | void *src = invoke_list_func(at, list, i); | |
| 460 | memcpy(loc, src, elem_size); | |
| 461 | loc += elem_size; | |
| 462 | } | |
| 463 | ||
| 464 | // qsort | |
| 465 | qsort(tmp, list_size, elem_size, | |
| 466 | list->collection.cmpfunc); | |
| 467 | ||
| 468 | // copy elements back | |
| 469 | loc = tmp; | |
| 470 | for (size_t i = 0; i < list_size; i++) { | |
| 471 | void *dest = invoke_list_func(at, list, i); | |
| 472 | memcpy(dest, loc, elem_size); | |
| 473 | loc += elem_size; | |
| 474 | } | |
| 475 | ||
| 888 | 476 | cxFreeDefault(tmp); |
| 852 | 477 | } |
| 478 | ||
| 479 | int cx_list_default_swap(struct cx_list_s *list, size_t i, size_t j) { | |
| 480 | if (i == j) return 0; | |
| 481 | if (i >= list->collection.size) return 1; | |
| 482 | if (j >= list->collection.size) return 1; | |
| 483 | ||
| 484 | size_t elem_size = list->collection.elem_size; | |
| 485 | ||
| 888 | 486 | void *tmp = cxMallocDefault(elem_size); |
| 487 | if (tmp == NULL) return 1; // LCOV_EXCL_LINE | |
| 852 | 488 | |
| 489 | void *ip = invoke_list_func(at, list, i); | |
| 490 | void *jp = invoke_list_func(at, list, j); | |
| 491 | ||
| 492 | memcpy(tmp, ip, elem_size); | |
| 493 | memcpy(ip, jp, elem_size); | |
| 494 | memcpy(jp, tmp, elem_size); | |
| 495 | ||
| 888 | 496 | cxFreeDefault(tmp); |
| 852 | 497 | |
| 498 | return 0; | |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
499 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
500 | |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
501 | void cx_list_init( |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
502 | struct cx_list_s *list, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
503 | struct cx_list_class_s *cl, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
504 | const struct cx_allocator_s *allocator, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
505 | cx_compare_func comparator, |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
506 | size_t elem_size |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
507 | ) { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
508 | list->cl = cl; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
509 | list->collection.allocator = allocator; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
510 | list->collection.cmpfunc = comparator; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
511 | if (elem_size > 0) { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
512 | list->collection.elem_size = elem_size; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
513 | } else { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
514 | list->collection.elem_size = sizeof(void *); |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
515 | if (list->collection.cmpfunc == NULL) { |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
516 | list->collection.cmpfunc = cx_cmp_ptr; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
517 | } |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
518 | list->collection.store_pointer = true; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
519 | list->climpl = list->cl; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
520 | list->cl = &cx_pointer_list_class; |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
521 | } |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
522 | } |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
523 | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
524 | int cxListCompare( |
| 852 | 525 | const CxList *list, |
| 526 | 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
|
527 | ) { |
| 852 | 528 | bool cannot_optimize = false; |
| 529 | ||
| 530 | // if one is storing pointers but the other is not | |
| 531 | cannot_optimize |= list->collection.store_pointer ^ other->collection.store_pointer; | |
| 532 | ||
| 533 | // if one class is wrapped but the other is not | |
| 534 | cannot_optimize |= (list->climpl == NULL) ^ (other->climpl == NULL); | |
| 750 | 535 | |
| 852 | 536 | // if the compare functions do not match or both are NULL |
| 537 | if (!cannot_optimize) { | |
| 538 | cx_compare_func list_cmp = (cx_compare_func) (list->climpl != NULL ? | |
| 539 | list->climpl->compare : list->cl->compare); | |
| 540 | cx_compare_func other_cmp = (cx_compare_func) (other->climpl != NULL ? | |
| 541 | other->climpl->compare : other->cl->compare); | |
| 542 | cannot_optimize |= list_cmp != other_cmp; | |
| 543 | cannot_optimize |= list_cmp == NULL; | |
| 544 | } | |
| 750 | 545 | |
| 852 | 546 | 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
|
547 | // 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
|
548 | if (list->collection.size == other->collection.size) { |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
750
diff
changeset
|
549 | CxIterator left = list->cl->iterator(list, 0, false); |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
750
diff
changeset
|
550 | CxIterator right = other->cl->iterator(other, 0, false); |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
750
diff
changeset
|
551 | 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
|
552 | 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
|
553 | void *rightValue = cxIteratorCurrent(right); |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
750
diff
changeset
|
554 | int d = list->collection.cmpfunc(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
|
555 | 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
|
556 | 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
|
557 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
558 | 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
|
559 | 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
|
560 | } |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
505
diff
changeset
|
561 | return 0; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
562 | } else { |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
750
diff
changeset
|
563 | return list->collection.size < other->collection.size ? -1 : 1; |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
564 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
565 | } 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
|
566 | // 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
|
567 | return list->cl->compare(list, other); |
|
1
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
568 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
569 | } |
|
1bcaac272cdf
added existing source code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
570 | |
| 888 | 571 | size_t cxListSize(const CxList *list) { |
| 572 | return list->collection.size; | |
| 573 | } | |
| 574 | ||
| 575 | int cxListAdd(CxList *list, const void *elem) { | |
| 576 | list->collection.sorted = false; | |
| 577 | return list->cl->insert_element(list, list->collection.size, elem) == NULL; | |
| 578 | } | |
| 579 | ||
| 580 | size_t cxListAddArray(CxList *list, const void *array, size_t n) { | |
| 581 | list->collection.sorted = false; | |
| 582 | return list->cl->insert_array(list, list->collection.size, array, n); | |
| 583 | } | |
| 584 | ||
| 585 | int cxListInsert(CxList *list, size_t index, const void *elem) { | |
| 586 | list->collection.sorted = false; | |
| 587 | return list->cl->insert_element(list, index, elem) == NULL; | |
| 588 | } | |
| 589 | ||
| 590 | void *cxListEmplaceAt(CxList *list, size_t index) { | |
| 591 | list->collection.sorted = false; | |
| 592 | return list->cl->insert_element(list, index, NULL); | |
| 593 | } | |
| 594 | ||
| 595 | void *cxListEmplace(CxList *list) { | |
| 596 | list->collection.sorted = false; | |
| 597 | return list->cl->insert_element(list, list->collection.size, NULL); | |
| 598 | } | |
| 599 | ||
| 600 | static bool cx_list_emplace_iterator_valid(const void *it) { | |
| 601 | const CxIterator *iter = it; | |
| 602 | return iter->index < iter->elem_count; | |
| 603 | } | |
| 604 | ||
| 605 | CxIterator cxListEmplaceArrayAt(CxList *list, size_t index, size_t n) { | |
| 606 | list->collection.sorted = false; | |
| 607 | size_t c = list->cl->insert_array(list, index, NULL, n); | |
| 608 | CxIterator iter = list->cl->iterator(list, index, false); | |
| 609 | // tweak the fields of this iterator | |
| 610 | iter.elem_count = c; | |
| 611 | iter.index = 0; | |
| 612 | // replace the valid function to abort iteration when c is reached | |
| 613 | iter.base.valid = cx_list_emplace_iterator_valid; | |
| 614 | // if we are storing pointers, we want to return the pure pointers. | |
| 615 | // therefore, we must unwrap the "current" method | |
| 616 | if (list->collection.store_pointer) { | |
| 617 | iter.base.current = iter.base.current_impl; | |
| 618 | } | |
| 619 | return iter; | |
| 620 | } | |
| 621 | ||
| 622 | CxIterator cxListEmplaceArray(CxList *list, size_t n) { | |
| 623 | return cxListEmplaceArrayAt(list, list->collection.size, n); | |
| 624 | } | |
| 625 | ||
| 626 | int cxListInsertSorted(CxList *list, const void *elem) { | |
| 627 | assert(cxCollectionSorted(list)); | |
| 628 | list->collection.sorted = true; | |
| 629 | const void *data = list->collection.store_pointer ? &elem : elem; | |
| 630 | return list->cl->insert_sorted(list, data, 1) == 0; | |
| 631 | } | |
| 632 | ||
| 633 | int cxListInsertUnique(CxList *list, const void *elem) { | |
| 634 | if (cxCollectionSorted(list)) { | |
| 635 | list->collection.sorted = true; | |
| 636 | const void *data = list->collection.store_pointer ? &elem : elem; | |
| 637 | return list->cl->insert_unique(list, data, 1) == 0; | |
| 638 | } else { | |
| 639 | if (cxListContains(list, elem)) { | |
| 640 | return 0; | |
| 641 | } else { | |
| 642 | return cxListAdd(list, elem); | |
| 643 | } | |
| 644 | } | |
| 645 | } | |
| 646 | ||
| 647 | size_t cxListInsertArray(CxList *list, size_t index, const void *array, size_t n) { | |
| 648 | list->collection.sorted = false; | |
| 649 | return list->cl->insert_array(list, index, array, n); | |
| 650 | } | |
| 651 | ||
| 652 | size_t cxListInsertSortedArray(CxList *list, const void *array, size_t n) { | |
| 653 | assert(cxCollectionSorted(list)); | |
| 654 | list->collection.sorted = true; | |
| 655 | return list->cl->insert_sorted(list, array, n); | |
| 656 | } | |
| 657 | ||
| 658 | size_t cxListInsertUniqueArray(CxList *list, const void *array, size_t n) { | |
| 659 | if (cxCollectionSorted(list)) { | |
| 660 | list->collection.sorted = true; | |
| 661 | return list->cl->insert_unique(list, array, n); | |
| 662 | } else { | |
| 663 | const char *source = array; | |
| 664 | for (size_t i = 0 ; i < n; i++) { | |
| 665 | // note: this also checks elements added in a previous iteration | |
| 666 | const void *data = list->collection.store_pointer ? | |
| 667 | *((const void**)source) : source; | |
| 668 | if (!cxListContains(list, data)) { | |
| 669 | if (cxListAdd(list, data)) { | |
| 670 | return i; // LCOV_EXCL_LINE | |
| 671 | } | |
| 672 | } | |
| 673 | source += list->collection.elem_size; | |
| 674 | } | |
| 675 | return n; | |
| 676 | } | |
| 677 | } | |
| 678 | ||
| 679 | int cxListInsertAfter(CxIterator *iter, const void *elem) { | |
| 680 | CxList* list = (CxList*)iter->src_handle; | |
| 681 | list->collection.sorted = false; | |
| 682 | return list->cl->insert_iter(iter, elem, 0); | |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
683 | } |
|
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
684 | |
| 888 | 685 | int cxListInsertBefore(CxIterator *iter, const void *elem) { |
| 686 | CxList* list = (CxList*)iter->src_handle; | |
| 687 | list->collection.sorted = false; | |
| 688 | return list->cl->insert_iter(iter, elem, 1); | |
| 689 | } | |
| 690 | ||
| 691 | int cxListRemove(CxList *list, size_t index) { | |
| 692 | return list->cl->remove(list, index, 1, NULL) == 0; | |
| 693 | } | |
| 694 | ||
| 695 | int cxListRemoveAndGet(CxList *list, size_t index, void *targetbuf) { | |
| 696 | return list->cl->remove(list, index, 1, targetbuf) == 0; | |
| 697 | } | |
| 698 | ||
| 699 | int cxListRemoveAndGetFirst(CxList *list, void *targetbuf) { | |
| 700 | return list->cl->remove(list, 0, 1, targetbuf) == 0; | |
| 701 | } | |
| 702 | ||
| 703 | int cxListRemoveAndGetLast(CxList *list, void *targetbuf) { | |
| 704 | // note: index may wrap - member function will catch that | |
| 705 | return list->cl->remove(list, list->collection.size - 1, 1, targetbuf) == 0; | |
| 706 | } | |
| 707 | ||
| 708 | size_t cxListRemoveArray(CxList *list, size_t index, size_t num) { | |
| 709 | return list->cl->remove(list, index, num, NULL); | |
| 710 | } | |
| 711 | ||
| 712 | size_t cxListRemoveArrayAndGet(CxList *list, size_t index, size_t num, void *targetbuf) { | |
| 713 | return list->cl->remove(list, index, num, targetbuf); | |
| 714 | } | |
| 715 | ||
| 716 | void cxListClear(CxList *list) { | |
| 717 | list->cl->clear(list); | |
| 718 | list->collection.sorted = true; // empty lists are always sorted | |
| 719 | } | |
| 720 | ||
| 721 | int cxListSwap(CxList *list, size_t i, size_t j) { | |
| 722 | list->collection.sorted = false; | |
| 723 | return list->cl->swap(list, i, j); | |
| 724 | } | |
| 725 | ||
| 726 | void *cxListAt(const CxList *list, size_t index) { | |
| 727 | return list->cl->at(list, index); | |
| 728 | } | |
| 729 | ||
| 730 | void *cxListFirst(const CxList *list) { | |
| 731 | return list->cl->at(list, 0); | |
| 732 | } | |
| 733 | ||
| 734 | void *cxListLast(const CxList *list) { | |
| 735 | return list->cl->at(list, list->collection.size - 1); | |
| 736 | } | |
| 737 | ||
| 738 | int cxListSet(CxList *list, size_t index, const void *elem) { | |
| 739 | if (index >= list->collection.size) { | |
| 740 | return 1; | |
| 741 | } | |
| 742 | ||
| 743 | if (list->collection.store_pointer) { | |
| 744 | // For pointer collections, always use climpl | |
| 745 | void **target = list->climpl->at(list, index); | |
| 746 | *target = (void *)elem; | |
| 747 | } else { | |
| 748 | void *target = list->cl->at(list, index); | |
| 749 | memcpy(target, elem, list->collection.elem_size); | |
| 750 | } | |
| 751 | ||
| 752 | return 0; | |
| 753 | } | |
| 754 | ||
| 755 | CxIterator cxListIteratorAt(const CxList *list, size_t index) { | |
| 756 | if (list == NULL) list = cxEmptyList; | |
| 757 | return list->cl->iterator(list, index, false); | |
| 758 | } | |
| 759 | ||
| 760 | CxIterator cxListBackwardsIteratorAt(const CxList *list, size_t index) { | |
| 761 | if (list == NULL) list = cxEmptyList; | |
| 762 | return list->cl->iterator(list, index, true); | |
| 763 | } | |
| 764 | ||
| 765 | CxIterator cxListIterator(const CxList *list) { | |
| 766 | if (list == NULL) list = cxEmptyList; | |
| 767 | return list->cl->iterator(list, 0, false); | |
| 768 | } | |
| 769 | ||
| 770 | CxIterator cxListBackwardsIterator(const CxList *list) { | |
| 771 | if (list == NULL) list = cxEmptyList; | |
| 772 | return list->cl->iterator(list, list->collection.size - 1, true); | |
| 773 | } | |
| 774 | ||
| 775 | size_t cxListFind(const CxList *list, const void *elem) { | |
| 776 | return list->cl->find_remove((CxList*)list, elem, false); | |
| 777 | } | |
| 778 | ||
| 779 | bool cxListContains(const CxList* list, const void* elem) { | |
| 780 | return list->cl->find_remove((CxList*)list, elem, false) < list->collection.size; | |
| 781 | } | |
| 782 | ||
| 783 | bool cxListIndexValid(const CxList *list, size_t index) { | |
| 784 | return index < list->collection.size; | |
| 785 | } | |
| 786 | ||
| 787 | size_t cxListFindRemove(CxList *list, const void *elem) { | |
| 788 | return list->cl->find_remove(list, elem, true); | |
| 789 | } | |
| 790 | ||
| 791 | void cxListSort(CxList *list) { | |
| 792 | if (list->collection.sorted) return; | |
| 793 | list->cl->sort(list); | |
| 794 | list->collection.sorted = true; | |
| 795 | } | |
| 796 | ||
| 797 | void cxListReverse(CxList *list) { | |
| 798 | // still sorted, but not according to the cmp_func | |
| 799 | list->collection.sorted = false; | |
| 800 | list->cl->reverse(list); | |
|
5
88625853ae74
new webdav api + repository and key configuration + aes encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
801 | } |
| 852 | 802 | |
| 803 | void cxListFree(CxList *list) { | |
| 804 | if (list == NULL) return; | |
| 805 | list->cl->deallocate(list); | |
| 806 | } | |
| 888 | 807 | |
| 808 | static void cx_list_pop_uninitialized_elements(CxList *list, size_t n) { | |
| 809 | cx_destructor_func destr_bak = list->collection.simple_destructor; | |
| 810 | cx_destructor_func2 destr2_bak = list->collection.advanced_destructor; | |
| 811 | list->collection.simple_destructor = NULL; | |
| 812 | list->collection.advanced_destructor = NULL; | |
| 813 | if (n == 1) { | |
| 814 | cxListRemove(list, list->collection.size - 1); | |
| 815 | } else { | |
| 816 | cxListRemoveArray(list,list->collection.size - n, n); | |
| 817 | } | |
| 818 | list->collection.simple_destructor = destr_bak; | |
| 819 | list->collection.advanced_destructor = destr2_bak; | |
| 820 | } | |
| 821 | ||
| 822 | int cxListClone(CxList *dst, const CxList *src, cx_clone_func clone_func, | |
| 823 | const CxAllocator *clone_allocator, void *data) { | |
| 824 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 825 | ||
| 826 | // remember the original size | |
| 827 | size_t orig_size = dst->collection.size; | |
| 828 | ||
| 829 | // first, try to allocate the memory in the new list | |
| 830 | CxIterator empl_iter = cxListEmplaceArray(dst, src->collection.size); | |
| 831 | ||
| 832 | // get an iterator over the source elements | |
| 833 | CxIterator src_iter = cxListIterator(src); | |
| 834 | ||
| 835 | // now clone the elements | |
| 836 | size_t cloned = empl_iter.elem_count; | |
| 837 | for (size_t i = 0 ; i < empl_iter.elem_count; i++) { | |
| 838 | void *src_elem = cxIteratorCurrent(src_iter); | |
| 839 | void **dest_memory = cxIteratorCurrent(empl_iter); | |
| 840 | void *target = cxCollectionStoresPointers(dst) ? NULL : dest_memory; | |
| 841 | void *dest_ptr = clone_func(target, src_elem, clone_allocator, data); | |
| 842 | if (dest_ptr == NULL) { | |
| 843 | cloned = i; | |
| 844 | break; | |
| 845 | } | |
| 846 | if (cxCollectionStoresPointers(dst)) { | |
| 847 | *dest_memory = dest_ptr; | |
| 848 | } | |
| 849 | cxIteratorNext(src_iter); | |
| 850 | cxIteratorNext(empl_iter); | |
| 851 | } | |
| 852 | ||
| 853 | // if we could not clone everything, free the allocated memory | |
| 854 | // (disable the destructors!) | |
| 855 | if (cloned < src->collection.size) { | |
| 856 | cx_list_pop_uninitialized_elements(dst, | |
| 857 | dst->collection.size - cloned - orig_size); | |
| 858 | return 1; | |
| 859 | } | |
| 860 | ||
| 861 | return 0; | |
| 862 | } | |
| 863 | ||
| 864 | int cxListDifference(CxList *dst, | |
| 865 | const CxList *minuend, const CxList *subtrahend, | |
| 866 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { | |
| 867 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 868 | ||
| 869 | // optimize for sorted collections | |
| 870 | if (cxCollectionSorted(minuend) && cxCollectionSorted(subtrahend)) { | |
| 871 | bool dst_was_empty = cxCollectionSize(dst) == 0; | |
| 872 | ||
| 873 | CxIterator min_iter = cxListIterator(minuend); | |
| 874 | CxIterator sub_iter = cxListIterator(subtrahend); | |
| 875 | while (cxIteratorValid(min_iter)) { | |
| 876 | void *min_elem = cxIteratorCurrent(min_iter); | |
| 877 | void *sub_elem; | |
| 878 | int d; | |
| 879 | if (cxIteratorValid(sub_iter)) { | |
| 880 | sub_elem = cxIteratorCurrent(sub_iter); | |
| 881 | cx_compare_func cmp = subtrahend->collection.cmpfunc; | |
| 882 | d = cmp(sub_elem, min_elem); | |
| 883 | } else { | |
| 884 | // no more elements in the subtrahend, | |
| 885 | // i.e., the min_elem is larger than any elem of the subtrahend | |
| 886 | d = 1; | |
| 887 | } | |
| 888 | if (d == 0) { | |
| 889 | // is contained, so skip it | |
| 890 | cxIteratorNext(min_iter); | |
| 891 | } else if (d < 0) { | |
| 892 | // subtrahend is smaller than minuend, | |
| 893 | // check the next element | |
| 894 | cxIteratorNext(sub_iter); | |
| 895 | } else { | |
| 896 | // subtrahend is larger than the dst element, | |
| 897 | // clone the minuend and advance | |
| 898 | void **dst_mem = cxListEmplace(dst); | |
| 899 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 900 | void* dst_ptr = clone_func(target, min_elem, clone_allocator, data); | |
| 901 | if (dst_ptr == NULL) { | |
| 902 | cx_list_pop_uninitialized_elements(dst, 1); | |
| 903 | return 1; | |
| 904 | } | |
| 905 | if (cxCollectionStoresPointers(dst)) { | |
| 906 | *dst_mem = dst_ptr; | |
| 907 | } | |
| 908 | cxIteratorNext(min_iter); | |
| 909 | } | |
| 910 | } | |
| 911 | ||
| 912 | // if dst was empty, it is now guaranteed to be sorted | |
| 913 | dst->collection.sorted = dst_was_empty; | |
| 914 | } else { | |
| 915 | CxIterator min_iter = cxListIterator(minuend); | |
| 916 | cx_foreach(void *, elem, min_iter) { | |
| 917 | if (cxListContains(subtrahend, elem)) { | |
| 918 | continue; | |
| 919 | } | |
| 920 | void **dst_mem = cxListEmplace(dst); | |
| 921 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 922 | void* dst_ptr = clone_func(target, elem, clone_allocator, data); | |
| 923 | if (dst_ptr == NULL) { | |
| 924 | cx_list_pop_uninitialized_elements(dst, 1); | |
| 925 | return 1; | |
| 926 | } | |
| 927 | if (cxCollectionStoresPointers(dst)) { | |
| 928 | *dst_mem = dst_ptr; | |
| 929 | } | |
| 930 | } | |
| 931 | } | |
| 932 | ||
| 933 | return 0; | |
| 934 | } | |
| 935 | ||
| 936 | int cxListIntersection(CxList *dst, | |
| 937 | const CxList *src, const CxList *other, | |
| 938 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { | |
| 939 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 940 | ||
| 941 | // optimize for sorted collections | |
| 942 | if (cxCollectionSorted(src) && cxCollectionSorted(other)) { | |
| 943 | bool dst_was_empty = cxCollectionSize(dst) == 0; | |
| 944 | ||
| 945 | CxIterator src_iter = cxListIterator(src); | |
| 946 | CxIterator other_iter = cxListIterator(other); | |
| 947 | while (cxIteratorValid(src_iter) && cxIteratorValid(other_iter)) { | |
| 948 | void *src_elem = cxIteratorCurrent(src_iter); | |
| 949 | void *other_elem = cxIteratorCurrent(other_iter); | |
| 950 | int d = src->collection.cmpfunc(src_elem, other_elem); | |
| 951 | if (d == 0) { | |
| 952 | // is contained, clone it | |
| 953 | void **dst_mem = cxListEmplace(dst); | |
| 954 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 955 | void* dst_ptr = clone_func(target, src_elem, clone_allocator, data); | |
| 956 | if (dst_ptr == NULL) { | |
| 957 | cx_list_pop_uninitialized_elements(dst, 1); | |
| 958 | return 1; | |
| 959 | } | |
| 960 | if (cxCollectionStoresPointers(dst)) { | |
| 961 | *dst_mem = dst_ptr; | |
| 962 | } | |
| 963 | cxIteratorNext(src_iter); | |
| 964 | } else if (d < 0) { | |
| 965 | // the other element is larger, skip the source element | |
| 966 | cxIteratorNext(src_iter); | |
| 967 | } else { | |
| 968 | // the source element is larger, try to find it in the other list | |
| 969 | cxIteratorNext(other_iter); | |
| 970 | } | |
| 971 | } | |
| 972 | ||
| 973 | // if dst was empty, it is now guaranteed to be sorted | |
| 974 | dst->collection.sorted = dst_was_empty; | |
| 975 | } else { | |
| 976 | CxIterator src_iter = cxListIterator(src); | |
| 977 | cx_foreach(void *, elem, src_iter) { | |
| 978 | if (!cxListContains(other, elem)) { | |
| 979 | continue; | |
| 980 | } | |
| 981 | void **dst_mem = cxListEmplace(dst); | |
| 982 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 983 | void* dst_ptr = clone_func(target, elem, clone_allocator, data); | |
| 984 | if (dst_ptr == NULL) { | |
| 985 | cx_list_pop_uninitialized_elements(dst, 1); | |
| 986 | return 1; | |
| 987 | } | |
| 988 | if (cxCollectionStoresPointers(dst)) { | |
| 989 | *dst_mem = dst_ptr; | |
| 990 | } | |
| 991 | } | |
| 992 | } | |
| 993 | ||
| 994 | return 0; | |
| 995 | } | |
| 996 | ||
| 997 | int cxListUnion(CxList *dst, | |
| 998 | const CxList *src, const CxList *other, | |
| 999 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { | |
| 1000 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 1001 | ||
| 1002 | // optimize for sorted collections | |
| 1003 | if (cxCollectionSorted(src) && cxCollectionSorted(other)) { | |
| 1004 | bool dst_was_empty = cxCollectionSize(dst) == 0; | |
| 1005 | ||
| 1006 | CxIterator src_iter = cxListIterator(src); | |
| 1007 | CxIterator other_iter = cxListIterator(other); | |
| 1008 | while (cxIteratorValid(src_iter) || cxIteratorValid(other_iter)) { | |
| 1009 | void *src_elem, *other_elem; | |
| 1010 | int d; | |
| 1011 | if (!cxIteratorValid(src_iter)) { | |
| 1012 | other_elem = cxIteratorCurrent(other_iter); | |
| 1013 | d = 1; | |
| 1014 | } else if (!cxIteratorValid(other_iter)) { | |
| 1015 | src_elem = cxIteratorCurrent(src_iter); | |
| 1016 | d = -1; | |
| 1017 | } else { | |
| 1018 | src_elem = cxIteratorCurrent(src_iter); | |
| 1019 | other_elem = cxIteratorCurrent(other_iter); | |
| 1020 | d = src->collection.cmpfunc(src_elem, other_elem); | |
| 1021 | } | |
| 1022 | if (d <= 0) { | |
| 1023 | // source element is smaller or equal, clone it | |
| 1024 | void **dst_mem = cxListEmplace(dst); | |
| 1025 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 1026 | void* dst_ptr = clone_func(target, src_elem, clone_allocator, data); | |
| 1027 | if (dst_ptr == NULL) { | |
| 1028 | cx_list_pop_uninitialized_elements(dst, 1); | |
| 1029 | return 1; | |
| 1030 | } | |
| 1031 | if (cxCollectionStoresPointers(dst)) { | |
| 1032 | *dst_mem = dst_ptr; | |
| 1033 | } | |
| 1034 | cxIteratorNext(src_iter); | |
| 1035 | // if the other element was equal, skip it | |
| 1036 | if (d == 0) { | |
| 1037 | cxIteratorNext(other_iter); | |
| 1038 | } | |
| 1039 | } else { | |
| 1040 | // the other element is smaller, clone it | |
| 1041 | void **dst_mem = cxListEmplace(dst); | |
| 1042 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 1043 | void* dst_ptr = clone_func(target, other_elem, clone_allocator, data); | |
| 1044 | if (dst_ptr == NULL) { | |
| 1045 | cx_list_pop_uninitialized_elements(dst, 1); | |
| 1046 | return 1; | |
| 1047 | } | |
| 1048 | if (cxCollectionStoresPointers(dst)) { | |
| 1049 | *dst_mem = dst_ptr; | |
| 1050 | } | |
| 1051 | cxIteratorNext(other_iter); | |
| 1052 | } | |
| 1053 | } | |
| 1054 | ||
| 1055 | // if dst was empty, it is now guaranteed to be sorted | |
| 1056 | dst->collection.sorted = dst_was_empty; | |
| 1057 | } else { | |
| 1058 | if (cxListClone(dst, src, clone_func, clone_allocator, data)) { | |
| 1059 | return 1; | |
| 1060 | } | |
| 1061 | CxIterator other_iter = cxListIterator(other); | |
| 1062 | cx_foreach(void *, elem, other_iter) { | |
| 1063 | if (cxListContains(src, elem)) { | |
| 1064 | continue; | |
| 1065 | } | |
| 1066 | void **dst_mem = cxListEmplace(dst); | |
| 1067 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 1068 | void* dst_ptr = clone_func(target, elem, clone_allocator, data); | |
| 1069 | if (dst_ptr == NULL) { | |
| 1070 | cx_list_pop_uninitialized_elements(dst, 1); | |
| 1071 | return 1; | |
| 1072 | } | |
| 1073 | if (cxCollectionStoresPointers(dst)) { | |
| 1074 | *dst_mem = dst_ptr; | |
| 1075 | } | |
| 1076 | } | |
| 1077 | } | |
| 1078 | ||
| 1079 | return 0; | |
| 1080 | } |