Thu, 01 Jan 2026 17:59:32 +0100
update ucx
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29 | #include "cx/list.h" |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
30 | |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
31 | #include <string.h> |
| 22 | 32 | #include <assert.h> |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
33 | |
| 31 | 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); | |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38 | |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | |
| 31 | 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); | |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
60 | } |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
61 | |
| 31 | 62 | #define cx_list_compare_wrapper(l, r, c) cx_list_compare_wrapper(l, r, (void*)c) |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
63 | |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
64 | // <editor-fold desc="empty list implementation"> |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
65 | |
| 38 | 66 | static void cx_emptyl_noop(CX_UNUSED CxList *list) { |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67 | // this is a noop, but MUST be implemented |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68 | } |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69 | |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70 | static void *cx_emptyl_at( |
| 38 | 71 | CX_UNUSED const struct cx_list_s *list, |
| 72 | CX_UNUSED size_t index | |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
73 | ) { |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
74 | return NULL; |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
75 | } |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
76 | |
| 16 | 77 | static size_t cx_emptyl_find_remove( |
| 38 | 78 | CX_UNUSED struct cx_list_s *list, |
| 79 | CX_UNUSED const void *elem, | |
| 80 | CX_UNUSED bool remove | |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
81 | ) { |
| 16 | 82 | return 0; |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
83 | } |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
84 | |
| 38 | 85 | static bool cx_emptyl_iter_valid(CX_UNUSED const void *iter) { |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
86 | return false; |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
87 | } |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
88 | |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
89 | static CxIterator cx_emptyl_iterator( |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
90 | const struct cx_list_s *list, |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
91 | size_t index, |
| 38 | 92 | CX_UNUSED bool backwards |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
93 | ) { |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
94 | CxIterator iter = {0}; |
| 22 | 95 | iter.src_handle = (void*) list; |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
96 | iter.index = index; |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
97 | iter.base.valid = cx_emptyl_iter_valid; |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
98 | return iter; |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
99 | } |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
100 | |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
101 | static cx_list_class cx_empty_list_class = { |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
102 | cx_emptyl_noop, |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
103 | NULL, |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
104 | NULL, |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
105 | NULL, |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
106 | NULL, |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
107 | NULL, |
| 22 | 108 | NULL, |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
109 | cx_emptyl_noop, |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
110 | NULL, |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
111 | cx_emptyl_at, |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
112 | cx_emptyl_find_remove, |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
113 | cx_emptyl_noop, |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
114 | NULL, |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
115 | cx_emptyl_noop, |
| 22 | 116 | NULL, |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
117 | cx_emptyl_iterator, |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
118 | }; |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
119 | |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
120 | CxList cx_empty_list = { |
| 16 | 121 | { |
| 122 | NULL, | |
| 123 | 0, | |
| 124 | 0, | |
| 125 | NULL, | |
| 126 | NULL, | |
| 127 | NULL, | |
| 31 | 128 | NULL, |
| 129 | NULL, | |
| 130 | NULL, | |
| 16 | 131 | false, |
| 132 | true, | |
| 133 | }, | |
| 134 | &cx_empty_list_class, | |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
135 | }; |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
136 | |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
137 | CxList *const cxEmptyList = &cx_empty_list; |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
138 | |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
139 | // </editor-fold> |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
140 | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
141 | size_t cx_list_default_insert_array( |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
142 | struct cx_list_s *list, |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
143 | size_t index, |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
144 | const void *data, |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
145 | size_t n |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
146 | ) { |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
147 | const char *src = data; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
148 | size_t i = 0; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
149 | for (; i < n; i++) { |
| 38 | 150 | if (NULL == list->cl->insert_element(list, index + i, src)) { |
| 22 | 151 | return i; // LCOV_EXCL_LINE |
| 152 | } | |
| 153 | if (src != NULL) { | |
| 154 | src += list->collection.elem_size; | |
| 155 | } | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
156 | } |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
157 | return i; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
158 | } |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
159 | |
| 22 | 160 | static size_t cx_list_default_insert_sorted_impl( |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
161 | struct cx_list_s *list, |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
162 | const void *sorted_data, |
| 22 | 163 | size_t n, |
| 164 | bool allow_duplicates | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
165 | ) { |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
166 | // corner case |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
167 | if (n == 0) return 0; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
168 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
169 | size_t elem_size = list->collection.elem_size; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
170 | const char *src = sorted_data; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
171 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
172 | // track indices and number of inserted items |
| 22 | 173 | size_t di = 0, si = 0, processed = 0; |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
174 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
175 | // search the list for insertion points |
| 22 | 176 | while (di < list->collection.size) { |
| 31 | 177 | const void *list_elm = list->cl->at(list, di); |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
178 | |
| 22 | 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 | { | |
| 31 | 183 | int d = cx_list_compare_wrapper(list_elm, src, list); |
| 22 | 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 | |
| 31 | 189 | while (si < n && cx_list_compare_wrapper(list_elm, src, list) == 0) { |
| 22 | 190 | src += elem_size; |
| 191 | si++; | |
| 192 | processed++; | |
| 193 | } | |
| 194 | if (processed == n) { | |
| 195 | return processed; | |
| 196 | } | |
| 197 | } | |
| 198 | di++; | |
| 199 | continue; | |
| 200 | } | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
201 | } |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
202 | |
| 22 | 203 | // determine the number of consecutive elements that can be inserted |
| 204 | size_t ins = 1, skip = 0; | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
205 | const char *next = src; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
206 | while (++si < n) { |
| 22 | 207 | if (!allow_duplicates) { |
| 208 | // skip duplicates within the source | |
| 31 | 209 | if (cx_list_compare_wrapper(next, next + elem_size, list) == 0) { |
| 22 | 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 | } | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
221 | next += elem_size; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
222 | // once we become larger than the list elem, break |
| 31 | 223 | if (cx_list_compare_wrapper(list_elm, next, list) <= 0) { |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
224 | break; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
225 | } |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
226 | // otherwise, we can insert one more |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
227 | ins++; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
228 | } |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
229 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
230 | // insert the elements at location si |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
231 | if (ins == 1) { |
| 31 | 232 | if (NULL == list->cl->insert_element(list, di, src)) { |
| 22 | 233 | return processed; // LCOV_EXCL_LINE |
| 234 | } | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
235 | } else { |
| 31 | 236 | size_t r = list->cl->insert_array(list, di, src, ins); |
| 22 | 237 | if (r < ins) { |
| 238 | return processed + r; // LCOV_EXCL_LINE | |
| 239 | } | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
240 | } |
| 22 | 241 | processed += ins + skip; |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
242 | di += ins; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
243 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
244 | // everything inserted? |
| 22 | 245 | if (processed == n) { |
| 246 | return processed; | |
| 247 | } | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
248 | src = next; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
249 | } |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
250 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
251 | // insert remaining items |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
252 | if (si < n) { |
| 22 | 253 | if (allow_duplicates) { |
| 31 | 254 | processed += list->cl->insert_array(list, di, src, n - si); |
| 22 | 255 | } else { |
| 31 | 256 | const void *last = di == 0 ? NULL : list->cl->at(list, di - 1); |
| 22 | 257 | for (; si < n; si++) { |
| 258 | // skip duplicates within the source | |
| 31 | 259 | if (last == NULL || cx_list_compare_wrapper(last, src, list) != 0) { |
| 260 | if (NULL == list->cl->insert_element(list, di, src)) { | |
| 22 | 261 | return processed; // LCOV_EXCL_LINE |
| 262 | } | |
| 263 | last = src; | |
| 264 | di++; | |
| 265 | } | |
| 266 | processed++; | |
| 267 | src += elem_size; | |
| 268 | } | |
| 269 | } | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
270 | } |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
271 | |
| 22 | 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); | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
289 | } |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
290 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
291 | void cx_list_default_sort(struct cx_list_s *list) { |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
292 | size_t elem_size = list->collection.elem_size; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
293 | size_t list_size = list->collection.size; |
| 21 | 294 | void *tmp = cxMallocDefault(elem_size * list_size); |
| 22 | 295 | if (tmp == NULL) abort(); // LCOV_EXCL_LINE |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
296 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
297 | // copy elements from source array |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
298 | char *loc = tmp; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
299 | for (size_t i = 0; i < list_size; i++) { |
| 31 | 300 | void *src = list->cl->at(list, i); |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
301 | memcpy(loc, src, elem_size); |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
302 | loc += elem_size; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
303 | } |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
304 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
305 | // qsort |
| 31 | 306 | cx_array_qsort_c(tmp, list_size, elem_size, cx_list_compare_wrapper, list); |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
307 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
308 | // copy elements back |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
309 | loc = tmp; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
310 | for (size_t i = 0; i < list_size; i++) { |
| 31 | 311 | void *dest = list->cl->at(list, i); |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
312 | memcpy(dest, loc, elem_size); |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
313 | loc += elem_size; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
314 | } |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
315 | |
| 21 | 316 | cxFreeDefault(tmp); |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
317 | } |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
318 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
319 | int cx_list_default_swap(struct cx_list_s *list, size_t i, size_t j) { |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
320 | if (i == j) return 0; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
321 | if (i >= list->collection.size) return 1; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
322 | if (j >= list->collection.size) return 1; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
323 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
324 | size_t elem_size = list->collection.elem_size; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
325 | |
| 21 | 326 | void *tmp = cxMallocDefault(elem_size); |
| 22 | 327 | if (tmp == NULL) return 1; // LCOV_EXCL_LINE |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
328 | |
| 31 | 329 | void *ip = list->cl->at(list, i); |
| 330 | void *jp = list->cl->at(list, j); | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
331 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
332 | memcpy(tmp, ip, elem_size); |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
333 | memcpy(ip, jp, elem_size); |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
334 | memcpy(jp, tmp, elem_size); |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
335 | |
| 21 | 336 | cxFreeDefault(tmp); |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
337 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
338 | return 0; |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
339 | } |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
340 | |
| 38 | 341 | static int cx_list_cmpfunc2_safe_memcmp(const void *a, const void *b, void *c) { |
| 342 | // it is not safe to store a pointer to the size in the list | |
| 343 | // because the entire list structure might get reallocated | |
| 344 | size_t elem_size = (size_t)(uintptr_t)c; | |
| 345 | return memcmp(a, b, elem_size); | |
| 346 | } | |
| 347 | ||
| 16 | 348 | void cx_list_init( |
| 349 | struct cx_list_s *list, | |
| 350 | struct cx_list_class_s *cl, | |
| 351 | const struct cx_allocator_s *allocator, | |
| 352 | size_t elem_size | |
| 353 | ) { | |
| 354 | list->cl = cl; | |
| 355 | list->collection.allocator = allocator; | |
| 31 | 356 | list->collection.size = 0; |
| 357 | list->collection.sorted = false; // should be set by the implementation | |
| 16 | 358 | if (elem_size > 0) { |
| 359 | list->collection.elem_size = elem_size; | |
| 31 | 360 | list->collection.simple_cmp = NULL; |
| 38 | 361 | list->collection.advanced_cmp = cx_list_cmpfunc2_safe_memcmp; |
| 362 | list->collection.cmp_data = (void*)(uintptr_t)list->collection.elem_size; | |
| 31 | 363 | list->collection.store_pointer = false; |
| 16 | 364 | } else { |
| 365 | list->collection.elem_size = sizeof(void *); | |
| 31 | 366 | list->collection.simple_cmp = cx_cmp_ptr; |
| 367 | list->collection.advanced_cmp = NULL; | |
| 368 | list->collection.cmp_data = NULL; | |
| 16 | 369 | list->collection.store_pointer = true; |
| 370 | } | |
| 371 | } | |
| 372 | ||
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
373 | int cxListCompare( |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
374 | const CxList *list, |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
375 | const CxList *other |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
376 | ) { |
| 31 | 377 | // check if we cannot use the list internal function |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
378 | bool cannot_optimize = false; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
379 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
380 | // if one is storing pointers but the other is not |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
381 | cannot_optimize |= list->collection.store_pointer ^ other->collection.store_pointer; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
382 | |
| 31 | 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; | |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
388 | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
389 | if (cannot_optimize) { |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
390 | // lists are definitely different - cannot use internal compare function |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
391 | if (list->collection.size == other->collection.size) { |
| 31 | 392 | CxIterator left = cxListIterator(list); |
| 393 | CxIterator right = cxListIterator(other); | |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
394 | for (size_t i = 0; i < list->collection.size; i++) { |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
395 | void *leftValue = cxIteratorCurrent(left); |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
396 | void *rightValue = cxIteratorCurrent(right); |
| 31 | 397 | // values are already unwrapped, invoke immediately |
| 398 | int d = cx_invoke_compare_func(list, leftValue, rightValue); | |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
399 | if (d != 0) { |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
400 | return d; |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
401 | } |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
402 | cxIteratorNext(left); |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
403 | cxIteratorNext(right); |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
404 | } |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
405 | return 0; |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
406 | } else { |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
407 | return list->collection.size < other->collection.size ? -1 : 1; |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
408 | } |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
409 | } else { |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
410 | // lists are compatible |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
411 | return list->cl->compare(list, other); |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
412 | } |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
413 | } |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
414 | |
| 22 | 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; | |
| 31 | 421 | return list->cl->insert_element(list, list->collection.size, cx_ref(list, elem)) == NULL; |
| 22 | 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; | |
| 31 | 431 | return list->cl->insert_element(list, index, cx_ref(list, elem)) == NULL; |
| 22 | 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; | |
| 31 | 468 | return list->cl->insert_sorted(list, cx_ref(list, elem), 1) == 0; |
| 22 | 469 | } |
| 470 | ||
| 471 | int cxListInsertUnique(CxList *list, const void *elem) { | |
| 472 | if (cxCollectionSorted(list)) { | |
| 473 | list->collection.sorted = true; | |
| 31 | 474 | return list->cl->insert_unique(list, cx_ref(list, elem), 1) == 0; |
| 22 | 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); | |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
487 | } |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
488 | |
| 22 | 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 | |
| 31 | 503 | const void *data = cx_deref(list, source); |
| 22 | 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) { | |
| 31 | 516 | CxList* list = iter->src_handle; |
| 22 | 517 | list->collection.sorted = false; |
| 31 | 518 | return list->cl->insert_iter(iter, cx_ref(list, elem), 0); |
| 22 | 519 | } |
| 520 | ||
| 521 | int cxListInsertBefore(CxIterator *iter, const void *elem) { | |
| 31 | 522 | CxList* list = iter->src_handle; |
| 22 | 523 | list->collection.sorted = false; |
| 31 | 524 | return list->cl->insert_iter(iter, cx_ref(list, elem), 1); |
| 22 | 525 | } |
| 526 | ||
| 527 | int cxListRemove(CxList *list, size_t index) { | |
| 528 | return list->cl->remove(list, index, 1, NULL) == 0; | |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
529 | } |
| 16 | 530 | |
| 22 | 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); | |
| 16 | 550 | } |
| 21 | 551 | |
| 22 | 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) { | |
| 31 | 563 | void *result = list->cl->at(list, index); |
| 564 | if (result == NULL) return NULL; | |
| 565 | return cx_deref(list, result); | |
| 22 | 566 | } |
| 567 | ||
| 568 | void *cxListFirst(const CxList *list) { | |
| 31 | 569 | return cxListAt(list, 0); |
| 22 | 570 | } |
| 571 | ||
| 572 | void *cxListLast(const CxList *list) { | |
| 31 | 573 | return cxListAt(list, list->collection.size - 1); |
| 22 | 574 | } |
| 575 | ||
| 576 | int cxListSet(CxList *list, size_t index, const void *elem) { | |
| 21 | 577 | if (index >= list->collection.size) { |
| 578 | return 1; | |
| 579 | } | |
| 580 | ||
| 581 | if (list->collection.store_pointer) { | |
| 31 | 582 | void **target = list->cl->at(list, index); |
| 21 | 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 | } | |
| 22 | 591 | |
| 31 | 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 | ||
| 22 | 608 | CxIterator cxListIteratorAt(const CxList *list, size_t index) { |
| 609 | if (list == NULL) list = cxEmptyList; | |
| 31 | 610 | return cx_pl_iter_wrap(list, list->cl->iterator(list, index, false)); |
| 22 | 611 | } |
| 612 | ||
| 613 | CxIterator cxListBackwardsIteratorAt(const CxList *list, size_t index) { | |
| 614 | if (list == NULL) list = cxEmptyList; | |
| 31 | 615 | return cx_pl_iter_wrap(list, list->cl->iterator(list, index, true)); |
| 22 | 616 | } |
| 617 | ||
| 618 | CxIterator cxListIterator(const CxList *list) { | |
| 619 | if (list == NULL) list = cxEmptyList; | |
| 31 | 620 | return cx_pl_iter_wrap(list, list->cl->iterator(list, 0, false)); |
| 22 | 621 | } |
| 622 | ||
| 623 | CxIterator cxListBackwardsIterator(const CxList *list) { | |
| 624 | if (list == NULL) list = cxEmptyList; | |
| 31 | 625 | return cx_pl_iter_wrap(list, list->cl->iterator(list, list->collection.size - 1, true)); |
| 22 | 626 | } |
| 627 | ||
| 628 | size_t cxListFind(const CxList *list, const void *elem) { | |
| 31 | 629 | return list->cl->find_remove((CxList*)list, cx_ref(list, elem), false); |
| 22 | 630 | } |
| 631 | ||
| 632 | bool cxListContains(const CxList* list, const void* elem) { | |
| 31 | 633 | return list->cl->find_remove((CxList*)list, cx_ref(list, elem), false) < list->collection.size; |
| 22 | 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) { | |
| 31 | 641 | return list->cl->find_remove(list, cx_ref(list, elem), true); |
| 22 | 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 | ||
| 31 | 675 | static void* cx_list_shallow_clone_func(void *dst, const void *src, const CxAllocator *al, void *data) { |
| 22 | 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 | ||
| 31 | 682 | #define use_shallow_clone_func(list) cx_list_shallow_clone_func, NULL, (void*)&((list)->collection.elem_size) |
| 22 | 683 | |
| 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 | ||
| 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 | ||
| 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); | |
| 31 | 748 | d = cx_list_compare_wrapper(sub_elem, min_elem, subtrahend); |
| 22 | 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); | |
| 31 | 816 | int d = cx_list_compare_wrapper(src_elem, other_elem, src); |
| 22 | 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)) { | |
| 23 | 875 | void *src_elem = NULL, *other_elem = NULL; |
| 22 | 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); | |
| 31 | 886 | d = cx_list_compare_wrapper(src_elem, other_elem, src); |
| 22 | 887 | } |
| 888 | void *clone_from; | |
| 889 | if (d < 0) { | |
| 890 | // source element is smaller clone it | |
| 891 | clone_from = src_elem; | |
| 892 | cxIteratorNext(src_iter); | |
| 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); | |
| 898 | } else { | |
| 899 | // the other element is smaller, clone it | |
| 900 | clone_from = other_elem; | |
| 901 | cxIteratorNext(other_iter); | |
| 902 | } | |
| 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 | } | |
| 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 | } | |
| 941 | ||
| 31 | 942 | int cxListCloneShallow(CxList *dst, const CxList *src) { |
| 943 | return cxListClone(dst, src, use_shallow_clone_func(src)); | |
| 22 | 944 | } |
| 945 | ||
| 31 | 946 | int cxListDifferenceShallow(CxList *dst, const CxList *minuend, const CxList *subtrahend) { |
| 947 | return cxListDifference(dst, minuend, subtrahend, use_shallow_clone_func(minuend)); | |
| 22 | 948 | } |
| 949 | ||
| 31 | 950 | int cxListIntersectionShallow(CxList *dst, const CxList *src, const CxList *other) { |
| 951 | return cxListIntersection(dst, src, other, use_shallow_clone_func(src)); | |
| 22 | 952 | } |
| 953 | ||
| 31 | 954 | int cxListUnionShallow(CxList *dst, const CxList *src, const CxList *other) { |
| 955 | return cxListUnion(dst, src, other, use_shallow_clone_func(src)); | |
| 22 | 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 | } |