Fri, 19 Dec 2025 17:22:03 +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 | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
66 | static void cx_emptyl_noop(cx_attr_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( |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
71 | cx_attr_unused const 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
|
72 | cx_attr_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( |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
78 | cx_attr_unused 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
|
79 | cx_attr_unused const void *elem, |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
80 | cx_attr_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 | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
85 | static bool cx_emptyl_iter_valid(cx_attr_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, |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
92 | cx_attr_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++) { |
| 31 | 150 | if (NULL == list->cl->insert_element(list, index + i, src) |
| 22 | 151 | ) { |
| 152 | return i; // LCOV_EXCL_LINE | |
| 153 | } | |
| 154 | if (src != NULL) { | |
| 155 | src += list->collection.elem_size; | |
| 156 | } | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
157 | } |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
158 | return i; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
159 | } |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
160 | |
| 22 | 161 | 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
|
162 | 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
|
163 | const void *sorted_data, |
| 22 | 164 | size_t n, |
| 165 | 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
|
166 | ) { |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
167 | // corner case |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
168 | 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
|
169 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
170 | 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
|
171 | 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
|
172 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
173 | // track indices and number of inserted items |
| 22 | 174 | 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
|
175 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
176 | // search the list for insertion points |
| 22 | 177 | while (di < list->collection.size) { |
| 31 | 178 | 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
|
179 | |
| 22 | 180 | // compare the current list element with the first source element |
| 181 | // if less, skip the list elements | |
| 182 | // if equal, skip the list elements and optionally the source elements | |
| 183 | { | |
| 31 | 184 | int d = cx_list_compare_wrapper(list_elm, src, list); |
| 22 | 185 | if (d <= 0) { |
| 186 | if (!allow_duplicates && d == 0) { | |
| 187 | src += elem_size; | |
| 188 | si++; | |
| 189 | processed++; // we also count duplicates for the return value | |
| 31 | 190 | while (si < n && cx_list_compare_wrapper(list_elm, src, list) == 0) { |
| 22 | 191 | src += elem_size; |
| 192 | si++; | |
| 193 | processed++; | |
| 194 | } | |
| 195 | if (processed == n) { | |
| 196 | return processed; | |
| 197 | } | |
| 198 | } | |
| 199 | di++; | |
| 200 | continue; | |
| 201 | } | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
202 | } |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
203 | |
| 22 | 204 | // determine the number of consecutive elements that can be inserted |
| 205 | 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
|
206 | 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
|
207 | while (++si < n) { |
| 22 | 208 | if (!allow_duplicates) { |
| 209 | // skip duplicates within the source | |
| 31 | 210 | if (cx_list_compare_wrapper(next, next + elem_size, list) == 0) { |
| 22 | 211 | next += elem_size; |
| 212 | skip++; | |
| 213 | continue; | |
| 214 | } else { | |
| 215 | if (skip > 0) { | |
| 216 | // if we had to skip something, we must wait for the next run | |
| 217 | next += elem_size; | |
| 218 | break; | |
| 219 | } | |
| 220 | } | |
| 221 | } | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
222 | next += elem_size; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
223 | // once we become larger than the list elem, break |
| 31 | 224 | 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
|
225 | break; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
226 | } |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
227 | // 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
|
228 | ins++; |
|
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 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
231 | // 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
|
232 | if (ins == 1) { |
| 31 | 233 | if (NULL == list->cl->insert_element(list, di, src)) { |
| 22 | 234 | return processed; // LCOV_EXCL_LINE |
| 235 | } | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
236 | } else { |
| 31 | 237 | size_t r = list->cl->insert_array(list, di, src, ins); |
| 22 | 238 | if (r < ins) { |
| 239 | return processed + r; // LCOV_EXCL_LINE | |
| 240 | } | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
241 | } |
| 22 | 242 | 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
|
243 | di += ins; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
244 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
245 | // everything inserted? |
| 22 | 246 | if (processed == n) { |
| 247 | return processed; | |
| 248 | } | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
249 | src = next; |
|
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 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
252 | // insert remaining items |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
253 | if (si < n) { |
| 22 | 254 | if (allow_duplicates) { |
| 31 | 255 | processed += list->cl->insert_array(list, di, src, n - si); |
| 22 | 256 | } else { |
| 31 | 257 | const void *last = di == 0 ? NULL : list->cl->at(list, di - 1); |
| 22 | 258 | for (; si < n; si++) { |
| 259 | // skip duplicates within the source | |
| 31 | 260 | if (last == NULL || cx_list_compare_wrapper(last, src, list) != 0) { |
| 261 | if (NULL == list->cl->insert_element(list, di, src)) { | |
| 22 | 262 | return processed; // LCOV_EXCL_LINE |
| 263 | } | |
| 264 | last = src; | |
| 265 | di++; | |
| 266 | } | |
| 267 | processed++; | |
| 268 | src += elem_size; | |
| 269 | } | |
| 270 | } | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
271 | } |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
272 | |
| 22 | 273 | return processed; |
| 274 | } | |
| 275 | ||
| 276 | size_t cx_list_default_insert_sorted( | |
| 277 | struct cx_list_s *list, | |
| 278 | const void *sorted_data, | |
| 279 | size_t n | |
| 280 | ) { | |
| 281 | return cx_list_default_insert_sorted_impl(list, sorted_data, n, true); | |
| 282 | } | |
| 283 | ||
| 284 | size_t cx_list_default_insert_unique( | |
| 285 | struct cx_list_s *list, | |
| 286 | const void *sorted_data, | |
| 287 | size_t n | |
| 288 | ) { | |
| 289 | 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
|
290 | } |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
291 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
292 | 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
|
293 | 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
|
294 | size_t list_size = list->collection.size; |
| 21 | 295 | void *tmp = cxMallocDefault(elem_size * list_size); |
| 22 | 296 | 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
|
297 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
298 | // 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
|
299 | char *loc = tmp; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
300 | for (size_t i = 0; i < list_size; i++) { |
| 31 | 301 | 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
|
302 | 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
|
303 | loc += elem_size; |
|
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 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
306 | // qsort |
| 31 | 307 | 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
|
308 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
309 | // copy elements back |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
310 | loc = tmp; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
311 | for (size_t i = 0; i < list_size; i++) { |
| 31 | 312 | 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
|
313 | 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
|
314 | loc += elem_size; |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
315 | } |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
316 | |
| 21 | 317 | cxFreeDefault(tmp); |
|
11
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 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
320 | 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
|
321 | 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
|
322 | 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
|
323 | 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
|
324 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
325 | 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
|
326 | |
| 21 | 327 | void *tmp = cxMallocDefault(elem_size); |
| 22 | 328 | 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
|
329 | |
| 31 | 330 | void *ip = list->cl->at(list, i); |
| 331 | 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
|
332 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
333 | 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
|
334 | 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
|
335 | 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
|
336 | |
| 21 | 337 | cxFreeDefault(tmp); |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
338 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
339 | return 0; |
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
340 | } |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
341 | |
| 16 | 342 | void cx_list_init( |
| 343 | struct cx_list_s *list, | |
| 344 | struct cx_list_class_s *cl, | |
| 345 | const struct cx_allocator_s *allocator, | |
| 346 | size_t elem_size | |
| 347 | ) { | |
| 348 | list->cl = cl; | |
| 349 | list->collection.allocator = allocator; | |
| 31 | 350 | list->collection.size = 0; |
| 351 | list->collection.sorted = false; // should be set by the implementation | |
| 16 | 352 | if (elem_size > 0) { |
| 353 | list->collection.elem_size = elem_size; | |
| 31 | 354 | list->collection.simple_cmp = NULL; |
| 355 | list->collection.advanced_cmp = cx_ccmp_memcmp; | |
| 356 | list->collection.cmp_data = &list->collection.elem_size; | |
| 357 | list->collection.store_pointer = false; | |
| 16 | 358 | } else { |
| 359 | list->collection.elem_size = sizeof(void *); | |
| 31 | 360 | list->collection.simple_cmp = cx_cmp_ptr; |
| 361 | list->collection.advanced_cmp = NULL; | |
| 362 | list->collection.cmp_data = NULL; | |
| 16 | 363 | list->collection.store_pointer = true; |
| 364 | } | |
| 365 | } | |
| 366 | ||
|
0
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
367 | int cxListCompare( |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
368 | const CxList *list, |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
369 | 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
|
370 | ) { |
| 31 | 371 | // 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
|
372 | 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
|
373 | |
|
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
374 | // 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
|
375 | 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
|
376 | |
| 31 | 377 | // check if the lists are incompatible or this list does not implement compare |
| 378 | cx_compare_func list_cmp = (cx_compare_func) list->cl->compare; | |
| 379 | cx_compare_func other_cmp = (cx_compare_func) other->cl->compare; | |
| 380 | cannot_optimize |= list_cmp != other_cmp; | |
| 381 | 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
|
382 | |
|
11
0aa8cbd7912e
refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
383 | 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
|
384 | // 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
|
385 | if (list->collection.size == other->collection.size) { |
| 31 | 386 | CxIterator left = cxListIterator(list); |
| 387 | 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
|
388 | 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
|
389 | 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
|
390 | void *rightValue = cxIteratorCurrent(right); |
| 31 | 391 | // values are already unwrapped, invoke immediately |
| 392 | 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
|
393 | if (d != 0) { |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
394 | return d; |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
395 | } |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
396 | cxIteratorNext(left); |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
397 | cxIteratorNext(right); |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
398 | } |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
399 | return 0; |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
400 | } else { |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
401 | 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
|
402 | } |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
403 | } else { |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
404 | // lists are compatible |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
405 | 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
|
406 | } |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
407 | } |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
408 | |
| 22 | 409 | size_t cxListSize(const CxList *list) { |
| 410 | return list->collection.size; | |
| 411 | } | |
| 412 | ||
| 413 | int cxListAdd(CxList *list, const void *elem) { | |
| 414 | list->collection.sorted = false; | |
| 31 | 415 | return list->cl->insert_element(list, list->collection.size, cx_ref(list, elem)) == NULL; |
| 22 | 416 | } |
| 417 | ||
| 418 | size_t cxListAddArray(CxList *list, const void *array, size_t n) { | |
| 419 | list->collection.sorted = false; | |
| 420 | return list->cl->insert_array(list, list->collection.size, array, n); | |
| 421 | } | |
| 422 | ||
| 423 | int cxListInsert(CxList *list, size_t index, const void *elem) { | |
| 424 | list->collection.sorted = false; | |
| 31 | 425 | return list->cl->insert_element(list, index, cx_ref(list, elem)) == NULL; |
| 22 | 426 | } |
| 427 | ||
| 428 | void *cxListEmplaceAt(CxList *list, size_t index) { | |
| 429 | list->collection.sorted = false; | |
| 430 | return list->cl->insert_element(list, index, NULL); | |
| 431 | } | |
| 432 | ||
| 433 | void *cxListEmplace(CxList *list) { | |
| 434 | list->collection.sorted = false; | |
| 435 | return list->cl->insert_element(list, list->collection.size, NULL); | |
| 436 | } | |
| 437 | ||
| 438 | static bool cx_list_emplace_iterator_valid(const void *it) { | |
| 439 | const CxIterator *iter = it; | |
| 440 | return iter->index < iter->elem_count; | |
| 441 | } | |
| 442 | ||
| 443 | CxIterator cxListEmplaceArrayAt(CxList *list, size_t index, size_t n) { | |
| 444 | list->collection.sorted = false; | |
| 445 | size_t c = list->cl->insert_array(list, index, NULL, n); | |
| 446 | CxIterator iter = list->cl->iterator(list, index, false); | |
| 447 | // tweak the fields of this iterator | |
| 448 | iter.elem_count = c; | |
| 449 | iter.index = 0; | |
| 450 | // replace the valid function to abort iteration when c is reached | |
| 451 | iter.base.valid = cx_list_emplace_iterator_valid; | |
| 452 | return iter; | |
| 453 | } | |
| 454 | ||
| 455 | CxIterator cxListEmplaceArray(CxList *list, size_t n) { | |
| 456 | return cxListEmplaceArrayAt(list, list->collection.size, n); | |
| 457 | } | |
| 458 | ||
| 459 | int cxListInsertSorted(CxList *list, const void *elem) { | |
| 460 | assert(cxCollectionSorted(list)); | |
| 461 | list->collection.sorted = true; | |
| 31 | 462 | return list->cl->insert_sorted(list, cx_ref(list, elem), 1) == 0; |
| 22 | 463 | } |
| 464 | ||
| 465 | int cxListInsertUnique(CxList *list, const void *elem) { | |
| 466 | if (cxCollectionSorted(list)) { | |
| 467 | list->collection.sorted = true; | |
| 31 | 468 | return list->cl->insert_unique(list, cx_ref(list, elem), 1) == 0; |
| 22 | 469 | } else { |
| 470 | if (cxListContains(list, elem)) { | |
| 471 | return 0; | |
| 472 | } else { | |
| 473 | return cxListAdd(list, elem); | |
| 474 | } | |
| 475 | } | |
| 476 | } | |
| 477 | ||
| 478 | size_t cxListInsertArray(CxList *list, size_t index, const void *array, size_t n) { | |
| 479 | list->collection.sorted = false; | |
| 480 | 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
|
481 | } |
|
1a157da63d7c
add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
482 | |
| 22 | 483 | size_t cxListInsertSortedArray(CxList *list, const void *array, size_t n) { |
| 484 | assert(cxCollectionSorted(list)); | |
| 485 | list->collection.sorted = true; | |
| 486 | return list->cl->insert_sorted(list, array, n); | |
| 487 | } | |
| 488 | ||
| 489 | size_t cxListInsertUniqueArray(CxList *list, const void *array, size_t n) { | |
| 490 | if (cxCollectionSorted(list)) { | |
| 491 | list->collection.sorted = true; | |
| 492 | return list->cl->insert_unique(list, array, n); | |
| 493 | } else { | |
| 494 | const char *source = array; | |
| 495 | for (size_t i = 0 ; i < n; i++) { | |
| 496 | // note: this also checks elements added in a previous iteration | |
| 31 | 497 | const void *data = cx_deref(list, source); |
| 22 | 498 | if (!cxListContains(list, data)) { |
| 499 | if (cxListAdd(list, data)) { | |
| 500 | return i; // LCOV_EXCL_LINE | |
| 501 | } | |
| 502 | } | |
| 503 | source += list->collection.elem_size; | |
| 504 | } | |
| 505 | return n; | |
| 506 | } | |
| 507 | } | |
| 508 | ||
| 509 | int cxListInsertAfter(CxIterator *iter, const void *elem) { | |
| 31 | 510 | CxList* list = iter->src_handle; |
| 22 | 511 | list->collection.sorted = false; |
| 31 | 512 | return list->cl->insert_iter(iter, cx_ref(list, elem), 0); |
| 22 | 513 | } |
| 514 | ||
| 515 | int cxListInsertBefore(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), 1); |
| 22 | 519 | } |
| 520 | ||
| 521 | int cxListRemove(CxList *list, size_t index) { | |
| 522 | 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
|
523 | } |
| 16 | 524 | |
| 22 | 525 | int cxListRemoveAndGet(CxList *list, size_t index, void *targetbuf) { |
| 526 | return list->cl->remove(list, index, 1, targetbuf) == 0; | |
| 527 | } | |
| 528 | ||
| 529 | int cxListRemoveAndGetFirst(CxList *list, void *targetbuf) { | |
| 530 | return list->cl->remove(list, 0, 1, targetbuf) == 0; | |
| 531 | } | |
| 532 | ||
| 533 | int cxListRemoveAndGetLast(CxList *list, void *targetbuf) { | |
| 534 | // note: index may wrap - member function will catch that | |
| 535 | return list->cl->remove(list, list->collection.size - 1, 1, targetbuf) == 0; | |
| 536 | } | |
| 537 | ||
| 538 | size_t cxListRemoveArray(CxList *list, size_t index, size_t num) { | |
| 539 | return list->cl->remove(list, index, num, NULL); | |
| 540 | } | |
| 541 | ||
| 542 | size_t cxListRemoveArrayAndGet(CxList *list, size_t index, size_t num, void *targetbuf) { | |
| 543 | return list->cl->remove(list, index, num, targetbuf); | |
| 16 | 544 | } |
| 21 | 545 | |
| 22 | 546 | void cxListClear(CxList *list) { |
| 547 | list->cl->clear(list); | |
| 548 | list->collection.sorted = true; // empty lists are always sorted | |
| 549 | } | |
| 550 | ||
| 551 | int cxListSwap(CxList *list, size_t i, size_t j) { | |
| 552 | list->collection.sorted = false; | |
| 553 | return list->cl->swap(list, i, j); | |
| 554 | } | |
| 555 | ||
| 556 | void *cxListAt(const CxList *list, size_t index) { | |
| 31 | 557 | void *result = list->cl->at(list, index); |
| 558 | if (result == NULL) return NULL; | |
| 559 | return cx_deref(list, result); | |
| 22 | 560 | } |
| 561 | ||
| 562 | void *cxListFirst(const CxList *list) { | |
| 31 | 563 | return cxListAt(list, 0); |
| 22 | 564 | } |
| 565 | ||
| 566 | void *cxListLast(const CxList *list) { | |
| 31 | 567 | return cxListAt(list, list->collection.size - 1); |
| 22 | 568 | } |
| 569 | ||
| 570 | int cxListSet(CxList *list, size_t index, const void *elem) { | |
| 21 | 571 | if (index >= list->collection.size) { |
| 572 | return 1; | |
| 573 | } | |
| 574 | ||
| 575 | if (list->collection.store_pointer) { | |
| 31 | 576 | void **target = list->cl->at(list, index); |
| 21 | 577 | *target = (void *)elem; |
| 578 | } else { | |
| 579 | void *target = list->cl->at(list, index); | |
| 580 | memcpy(target, elem, list->collection.elem_size); | |
| 581 | } | |
| 582 | ||
| 583 | return 0; | |
| 584 | } | |
| 22 | 585 | |
| 31 | 586 | static void *cx_pl_iter_current(const void *it) { |
| 587 | const struct cx_iterator_s *iter = it; | |
| 588 | void **ptr = iter->base.current_impl(it); | |
| 589 | return ptr == NULL ? NULL : *ptr; | |
| 590 | } | |
| 591 | ||
| 592 | CX_INLINE CxIterator cx_pl_iter_wrap(const CxList *list, CxIterator iter) { | |
| 593 | if (cxCollectionStoresPointers(list)) { | |
| 594 | iter.base.current_impl = iter.base.current; | |
| 595 | iter.base.current = cx_pl_iter_current; | |
| 596 | return iter; | |
| 597 | } else { | |
| 598 | return iter; | |
| 599 | } | |
| 600 | } | |
| 601 | ||
| 22 | 602 | CxIterator cxListIteratorAt(const CxList *list, size_t index) { |
| 603 | if (list == NULL) list = cxEmptyList; | |
| 31 | 604 | return cx_pl_iter_wrap(list, list->cl->iterator(list, index, false)); |
| 22 | 605 | } |
| 606 | ||
| 607 | CxIterator cxListBackwardsIteratorAt(const CxList *list, size_t index) { | |
| 608 | if (list == NULL) list = cxEmptyList; | |
| 31 | 609 | return cx_pl_iter_wrap(list, list->cl->iterator(list, index, true)); |
| 22 | 610 | } |
| 611 | ||
| 612 | CxIterator cxListIterator(const CxList *list) { | |
| 613 | if (list == NULL) list = cxEmptyList; | |
| 31 | 614 | return cx_pl_iter_wrap(list, list->cl->iterator(list, 0, false)); |
| 22 | 615 | } |
| 616 | ||
| 617 | CxIterator cxListBackwardsIterator(const CxList *list) { | |
| 618 | if (list == NULL) list = cxEmptyList; | |
| 31 | 619 | return cx_pl_iter_wrap(list, list->cl->iterator(list, list->collection.size - 1, true)); |
| 22 | 620 | } |
| 621 | ||
| 622 | size_t cxListFind(const CxList *list, const void *elem) { | |
| 31 | 623 | return list->cl->find_remove((CxList*)list, cx_ref(list, elem), false); |
| 22 | 624 | } |
| 625 | ||
| 626 | bool cxListContains(const CxList* list, const void* elem) { | |
| 31 | 627 | return list->cl->find_remove((CxList*)list, cx_ref(list, elem), false) < list->collection.size; |
| 22 | 628 | } |
| 629 | ||
| 630 | bool cxListIndexValid(const CxList *list, size_t index) { | |
| 631 | return index < list->collection.size; | |
| 632 | } | |
| 633 | ||
| 634 | size_t cxListFindRemove(CxList *list, const void *elem) { | |
| 31 | 635 | return list->cl->find_remove(list, cx_ref(list, elem), true); |
| 22 | 636 | } |
| 637 | ||
| 638 | void cxListSort(CxList *list) { | |
| 639 | if (list->collection.sorted) return; | |
| 640 | list->cl->sort(list); | |
| 641 | list->collection.sorted = true; | |
| 642 | } | |
| 643 | ||
| 644 | void cxListReverse(CxList *list) { | |
| 645 | // still sorted, but not according to the cmp_func | |
| 646 | list->collection.sorted = false; | |
| 647 | list->cl->reverse(list); | |
| 648 | } | |
| 649 | ||
| 650 | void cxListFree(CxList *list) { | |
| 651 | if (list == NULL) return; | |
| 652 | list->cl->deallocate(list); | |
| 653 | } | |
| 654 | ||
| 655 | static void cx_list_pop_uninitialized_elements(CxList *list, size_t n) { | |
| 656 | cx_destructor_func destr_bak = list->collection.simple_destructor; | |
| 657 | cx_destructor_func2 destr2_bak = list->collection.advanced_destructor; | |
| 658 | list->collection.simple_destructor = NULL; | |
| 659 | list->collection.advanced_destructor = NULL; | |
| 660 | if (n == 1) { | |
| 661 | cxListRemove(list, list->collection.size - 1); | |
| 662 | } else { | |
| 663 | cxListRemoveArray(list,list->collection.size - n, n); | |
| 664 | } | |
| 665 | list->collection.simple_destructor = destr_bak; | |
| 666 | list->collection.advanced_destructor = destr2_bak; | |
| 667 | } | |
| 668 | ||
| 31 | 669 | static void* cx_list_shallow_clone_func(void *dst, const void *src, const CxAllocator *al, void *data) { |
| 22 | 670 | size_t elem_size = *(size_t*)data; |
| 671 | if (dst == NULL) dst = cxMalloc(al, elem_size); | |
| 672 | if (dst != NULL) memcpy(dst, src, elem_size); | |
| 673 | return dst; | |
| 674 | } | |
| 675 | ||
| 31 | 676 | #define use_shallow_clone_func(list) cx_list_shallow_clone_func, NULL, (void*)&((list)->collection.elem_size) |
| 22 | 677 | |
| 678 | int cxListClone(CxList *dst, const CxList *src, cx_clone_func clone_func, | |
| 679 | const CxAllocator *clone_allocator, void *data) { | |
| 680 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 681 | ||
| 682 | // remember the original size | |
| 683 | size_t orig_size = dst->collection.size; | |
| 684 | ||
| 685 | // first, try to allocate the memory in the new list | |
| 686 | CxIterator empl_iter = cxListEmplaceArray(dst, src->collection.size); | |
| 687 | ||
| 688 | // get an iterator over the source elements | |
| 689 | CxIterator src_iter = cxListIterator(src); | |
| 690 | ||
| 691 | // now clone the elements | |
| 692 | size_t cloned = empl_iter.elem_count; | |
| 693 | for (size_t i = 0 ; i < empl_iter.elem_count; i++) { | |
| 694 | void *src_elem = cxIteratorCurrent(src_iter); | |
| 695 | void **dest_memory = cxIteratorCurrent(empl_iter); | |
| 696 | void *target = cxCollectionStoresPointers(dst) ? NULL : dest_memory; | |
| 697 | void *dest_ptr = clone_func(target, src_elem, clone_allocator, data); | |
| 698 | if (dest_ptr == NULL) { | |
| 699 | cloned = i; | |
| 700 | break; | |
| 701 | } | |
| 702 | if (cxCollectionStoresPointers(dst)) { | |
| 703 | *dest_memory = dest_ptr; | |
| 704 | } | |
| 705 | cxIteratorNext(src_iter); | |
| 706 | cxIteratorNext(empl_iter); | |
| 707 | } | |
| 708 | ||
| 709 | // if we could not clone everything, free the allocated memory | |
| 710 | // (disable the destructors!) | |
| 711 | if (cloned < src->collection.size) { | |
| 712 | cx_list_pop_uninitialized_elements(dst, | |
| 713 | dst->collection.size - cloned - orig_size); | |
| 714 | return 1; | |
| 715 | } | |
| 716 | ||
| 717 | // set the sorted flag when we know it's sorted | |
| 718 | if (orig_size == 0 && src->collection.sorted) { | |
| 719 | dst->collection.sorted = true; | |
| 720 | } | |
| 721 | ||
| 722 | return 0; | |
| 723 | } | |
| 724 | ||
| 725 | int cxListDifference(CxList *dst, | |
| 726 | const CxList *minuend, const CxList *subtrahend, | |
| 727 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { | |
| 728 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 729 | ||
| 730 | // optimize for sorted collections | |
| 731 | if (cxCollectionSorted(minuend) && cxCollectionSorted(subtrahend)) { | |
| 732 | bool dst_was_empty = cxCollectionSize(dst) == 0; | |
| 733 | ||
| 734 | CxIterator min_iter = cxListIterator(minuend); | |
| 735 | CxIterator sub_iter = cxListIterator(subtrahend); | |
| 736 | while (cxIteratorValid(min_iter)) { | |
| 737 | void *min_elem = cxIteratorCurrent(min_iter); | |
| 738 | void *sub_elem; | |
| 739 | int d; | |
| 740 | if (cxIteratorValid(sub_iter)) { | |
| 741 | sub_elem = cxIteratorCurrent(sub_iter); | |
| 31 | 742 | d = cx_list_compare_wrapper(sub_elem, min_elem, subtrahend); |
| 22 | 743 | } else { |
| 744 | // no more elements in the subtrahend, | |
| 745 | // i.e., the min_elem is larger than any elem of the subtrahend | |
| 746 | d = 1; | |
| 747 | } | |
| 748 | if (d == 0) { | |
| 749 | // is contained, so skip it | |
| 750 | cxIteratorNext(min_iter); | |
| 751 | } else if (d < 0) { | |
| 752 | // subtrahend is smaller than minuend, | |
| 753 | // check the next element | |
| 754 | cxIteratorNext(sub_iter); | |
| 755 | } else { | |
| 756 | // subtrahend is larger than the dst element, | |
| 757 | // clone the minuend and advance | |
| 758 | void **dst_mem = cxListEmplace(dst); | |
| 759 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 760 | void* dst_ptr = clone_func(target, min_elem, clone_allocator, data); | |
| 761 | if (dst_ptr == NULL) { | |
| 762 | cx_list_pop_uninitialized_elements(dst, 1); | |
| 763 | return 1; | |
| 764 | } | |
| 765 | if (cxCollectionStoresPointers(dst)) { | |
| 766 | *dst_mem = dst_ptr; | |
| 767 | } | |
| 768 | cxIteratorNext(min_iter); | |
| 769 | } | |
| 770 | } | |
| 771 | ||
| 772 | // if dst was empty, it is now guaranteed to be sorted | |
| 773 | dst->collection.sorted = dst_was_empty; | |
| 774 | } else { | |
| 775 | CxIterator min_iter = cxListIterator(minuend); | |
| 776 | cx_foreach(void *, elem, min_iter) { | |
| 777 | if (cxListContains(subtrahend, elem)) { | |
| 778 | continue; | |
| 779 | } | |
| 780 | void **dst_mem = cxListEmplace(dst); | |
| 781 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 782 | void* dst_ptr = clone_func(target, elem, clone_allocator, data); | |
| 783 | if (dst_ptr == NULL) { | |
| 784 | cx_list_pop_uninitialized_elements(dst, 1); | |
| 785 | return 1; | |
| 786 | } | |
| 787 | if (cxCollectionStoresPointers(dst)) { | |
| 788 | *dst_mem = dst_ptr; | |
| 789 | } | |
| 790 | } | |
| 791 | } | |
| 792 | ||
| 793 | return 0; | |
| 794 | } | |
| 795 | ||
| 796 | int cxListIntersection(CxList *dst, | |
| 797 | const CxList *src, const CxList *other, | |
| 798 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { | |
| 799 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 800 | ||
| 801 | // optimize for sorted collections | |
| 802 | if (cxCollectionSorted(src) && cxCollectionSorted(other)) { | |
| 803 | bool dst_was_empty = cxCollectionSize(dst) == 0; | |
| 804 | ||
| 805 | CxIterator src_iter = cxListIterator(src); | |
| 806 | CxIterator other_iter = cxListIterator(other); | |
| 807 | while (cxIteratorValid(src_iter) && cxIteratorValid(other_iter)) { | |
| 808 | void *src_elem = cxIteratorCurrent(src_iter); | |
| 809 | void *other_elem = cxIteratorCurrent(other_iter); | |
| 31 | 810 | int d = cx_list_compare_wrapper(src_elem, other_elem, src); |
| 22 | 811 | if (d == 0) { |
| 812 | // is contained, clone it | |
| 813 | void **dst_mem = cxListEmplace(dst); | |
| 814 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 815 | void* dst_ptr = clone_func(target, src_elem, clone_allocator, data); | |
| 816 | if (dst_ptr == NULL) { | |
| 817 | cx_list_pop_uninitialized_elements(dst, 1); | |
| 818 | return 1; | |
| 819 | } | |
| 820 | if (cxCollectionStoresPointers(dst)) { | |
| 821 | *dst_mem = dst_ptr; | |
| 822 | } | |
| 823 | cxIteratorNext(src_iter); | |
| 824 | } else if (d < 0) { | |
| 825 | // the other element is larger, skip the source element | |
| 826 | cxIteratorNext(src_iter); | |
| 827 | } else { | |
| 828 | // the source element is larger, try to find it in the other list | |
| 829 | cxIteratorNext(other_iter); | |
| 830 | } | |
| 831 | } | |
| 832 | ||
| 833 | // if dst was empty, it is now guaranteed to be sorted | |
| 834 | dst->collection.sorted = dst_was_empty; | |
| 835 | } else { | |
| 836 | CxIterator src_iter = cxListIterator(src); | |
| 837 | cx_foreach(void *, elem, src_iter) { | |
| 838 | if (!cxListContains(other, elem)) { | |
| 839 | continue; | |
| 840 | } | |
| 841 | void **dst_mem = cxListEmplace(dst); | |
| 842 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 843 | void* dst_ptr = clone_func(target, elem, clone_allocator, data); | |
| 844 | if (dst_ptr == NULL) { | |
| 845 | cx_list_pop_uninitialized_elements(dst, 1); | |
| 846 | return 1; | |
| 847 | } | |
| 848 | if (cxCollectionStoresPointers(dst)) { | |
| 849 | *dst_mem = dst_ptr; | |
| 850 | } | |
| 851 | } | |
| 852 | } | |
| 853 | ||
| 854 | return 0; | |
| 855 | } | |
| 856 | ||
| 857 | int cxListUnion(CxList *dst, | |
| 858 | const CxList *src, const CxList *other, | |
| 859 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { | |
| 860 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; | |
| 861 | ||
| 862 | // optimize for sorted collections | |
| 863 | if (cxCollectionSorted(src) && cxCollectionSorted(other)) { | |
| 864 | bool dst_was_empty = cxCollectionSize(dst) == 0; | |
| 865 | ||
| 866 | CxIterator src_iter = cxListIterator(src); | |
| 867 | CxIterator other_iter = cxListIterator(other); | |
| 868 | while (cxIteratorValid(src_iter) || cxIteratorValid(other_iter)) { | |
| 23 | 869 | void *src_elem = NULL, *other_elem = NULL; |
| 22 | 870 | int d; |
| 871 | if (!cxIteratorValid(src_iter)) { | |
| 872 | other_elem = cxIteratorCurrent(other_iter); | |
| 873 | d = 1; | |
| 874 | } else if (!cxIteratorValid(other_iter)) { | |
| 875 | src_elem = cxIteratorCurrent(src_iter); | |
| 876 | d = -1; | |
| 877 | } else { | |
| 878 | src_elem = cxIteratorCurrent(src_iter); | |
| 879 | other_elem = cxIteratorCurrent(other_iter); | |
| 31 | 880 | d = cx_list_compare_wrapper(src_elem, other_elem, src); |
| 22 | 881 | } |
| 882 | void *clone_from; | |
| 883 | if (d < 0) { | |
| 884 | // source element is smaller clone it | |
| 885 | clone_from = src_elem; | |
| 886 | cxIteratorNext(src_iter); | |
| 887 | } else if (d == 0) { | |
| 888 | // both elements are equal, clone from the source, skip other | |
| 889 | clone_from = src_elem; | |
| 890 | cxIteratorNext(src_iter); | |
| 891 | cxIteratorNext(other_iter); | |
| 892 | } else { | |
| 893 | // the other element is smaller, clone it | |
| 894 | clone_from = other_elem; | |
| 895 | cxIteratorNext(other_iter); | |
| 896 | } | |
| 897 | void **dst_mem = cxListEmplace(dst); | |
| 898 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; | |
| 899 | void* dst_ptr = clone_func(target, clone_from, clone_allocator, data); | |
| 900 | if (dst_ptr == NULL) { | |
| 901 | cx_list_pop_uninitialized_elements(dst, 1); | |
| 902 | return 1; | |
| 903 | } | |
| 904 | if (cxCollectionStoresPointers(dst)) { | |
| 905 | *dst_mem = dst_ptr; | |
| 906 | } | |
| 907 | } | |
| 908 | ||
| 909 | // if dst was empty, it is now guaranteed to be sorted | |
| 910 | dst->collection.sorted = dst_was_empty; | |
| 911 | } else { | |
| 912 | if (cxListClone(dst, src, clone_func, clone_allocator, data)) { | |
| 913 | return 1; | |
| 914 | } | |
| 915 | CxIterator other_iter = cxListIterator(other); | |
| 916 | cx_foreach(void *, elem, other_iter) { | |
| 917 | if (cxListContains(src, 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 | ||
| 31 | 936 | int cxListCloneShallow(CxList *dst, const CxList *src) { |
| 937 | return cxListClone(dst, src, use_shallow_clone_func(src)); | |
| 22 | 938 | } |
| 939 | ||
| 31 | 940 | int cxListDifferenceShallow(CxList *dst, const CxList *minuend, const CxList *subtrahend) { |
| 941 | return cxListDifference(dst, minuend, subtrahend, use_shallow_clone_func(minuend)); | |
| 22 | 942 | } |
| 943 | ||
| 31 | 944 | int cxListIntersectionShallow(CxList *dst, const CxList *src, const CxList *other) { |
| 945 | return cxListIntersection(dst, src, other, use_shallow_clone_func(src)); | |
| 22 | 946 | } |
| 947 | ||
| 31 | 948 | int cxListUnionShallow(CxList *dst, const CxList *src, const CxList *other) { |
| 949 | return cxListUnion(dst, src, other, use_shallow_clone_func(src)); | |
| 22 | 950 | } |
| 951 | ||
| 952 | int cxListReserve(CxList *list, size_t capacity) { | |
| 953 | if (list->cl->change_capacity == NULL) { | |
| 954 | return 0; | |
| 955 | } | |
| 956 | if (capacity <= cxCollectionSize(list)) { | |
| 957 | return 0; | |
| 958 | } | |
| 959 | return list->cl->change_capacity(list, capacity); | |
| 960 | } | |
| 961 | ||
| 962 | int cxListShrink(CxList *list) { | |
| 963 | if (list->cl->change_capacity == NULL) { | |
| 964 | return 0; | |
| 965 | } | |
| 966 | return list->cl->change_capacity(list, cxCollectionSize(list)); | |
| 967 | } |