ucx/list.c

Thu, 01 Jan 2026 17:59:32 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 01 Jan 2026 17:59:32 +0100
changeset 38
27fadadaae31
parent 31
287484519844
permissions
-rw-r--r--

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
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
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
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
34 // we don't want to include the full array_list.h.
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
35 // therefore, we only forward declare the one function we want to use
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
36 CX_EXPORT void cx_array_qsort_c(void *array, size_t nmemb, size_t size,
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
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
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
40 int cx_list_compare_wrapper(const void *l, const void *r, void *c) {
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
41 CxList *list = c;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
42 const void *left;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
43 const void *right;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
44 if (cxCollectionStoresPointers(list)) {
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
45 left = *(void**)l;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
46 right = *(void**)r;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
47 // for historic reasons, we are handling the NULL case here
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
48 // because every UCX compare function does not support NULL arguments
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
49 if (left == NULL) {
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
50 if (right == NULL) return 0;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
51 return -1;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
52 } else if (right == NULL) {
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
53 return 1;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
54 }
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
55 } else {
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
56 left = l;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
57 right = r;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
58 }
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
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
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
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
27fadadaae31 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
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
27fadadaae31 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
71 CX_UNUSED const struct cx_list_s *list,
27fadadaae31 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
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
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
77 static size_t cx_emptyl_find_remove(
38
27fadadaae31 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
78 CX_UNUSED struct cx_list_s *list,
27fadadaae31 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
79 CX_UNUSED const void *elem,
27fadadaae31 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
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
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
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
27fadadaae31 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
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
27fadadaae31 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
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
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
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
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
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
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
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
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
121 {
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
122 NULL,
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
123 0,
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
124 0,
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
125 NULL,
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
126 NULL,
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
127 NULL,
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
128 NULL,
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
129 NULL,
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
130 NULL,
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
131 false,
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
132 true,
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
133 },
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
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
27fadadaae31 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
150 if (NULL == list->cl->insert_element(list, index + i, src)) {
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
151 return i; // LCOV_EXCL_LINE
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
152 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
153 if (src != NULL) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
154 src += list->collection.elem_size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
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
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
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
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
163 size_t n,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
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
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
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
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
176 while (di < list->collection.size) {
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
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
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
179 // compare the current list element with the first source element
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
180 // if less, skip the list elements
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
181 // if equal, skip the list elements and optionally the source elements
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
182 {
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
183 int d = cx_list_compare_wrapper(list_elm, src, list);
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
184 if (d <= 0) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
185 if (!allow_duplicates && d == 0) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
186 src += elem_size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
187 si++;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
188 processed++; // we also count duplicates for the return value
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
189 while (si < n && cx_list_compare_wrapper(list_elm, src, list) == 0) {
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
190 src += elem_size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
191 si++;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
192 processed++;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
193 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
194 if (processed == n) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
195 return processed;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
196 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
197 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
198 di++;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
199 continue;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
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
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
203 // determine the number of consecutive elements that can be inserted
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
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
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
207 if (!allow_duplicates) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
208 // skip duplicates within the source
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
209 if (cx_list_compare_wrapper(next, next + elem_size, list) == 0) {
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
210 next += elem_size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
211 skip++;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
212 continue;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
213 } else {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
214 if (skip > 0) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
215 // if we had to skip something, we must wait for the next run
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
216 next += elem_size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
217 break;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
218 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
219 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
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
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
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
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
232 if (NULL == list->cl->insert_element(list, di, src)) {
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
233 return processed; // LCOV_EXCL_LINE
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
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
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
236 size_t r = list->cl->insert_array(list, di, src, ins);
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
237 if (r < ins) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
238 return processed + r; // LCOV_EXCL_LINE
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
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
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
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
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
245 if (processed == n) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
246 return processed;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
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
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
253 if (allow_duplicates) {
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
254 processed += list->cl->insert_array(list, di, src, n - si);
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
255 } else {
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
256 const void *last = di == 0 ? NULL : list->cl->at(list, di - 1);
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
257 for (; si < n; si++) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
258 // skip duplicates within the source
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
259 if (last == NULL || cx_list_compare_wrapper(last, src, list) != 0) {
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
260 if (NULL == list->cl->insert_element(list, di, src)) {
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
261 return processed; // LCOV_EXCL_LINE
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
262 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
263 last = src;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
264 di++;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
265 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
266 processed++;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
267 src += elem_size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
268 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
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
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
272 return processed;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
273 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
274
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
275 size_t cx_list_default_insert_sorted(
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
276 struct cx_list_s *list,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
277 const void *sorted_data,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
278 size_t n
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
279 ) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
280 return cx_list_default_insert_sorted_impl(list, sorted_data, n, true);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
281 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
282
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
283 size_t cx_list_default_insert_unique(
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
284 struct cx_list_s *list,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
285 const void *sorted_data,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
286 size_t n
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
287 ) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
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
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
294 void *tmp = cxMallocDefault(elem_size * list_size);
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
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
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
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
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
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
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
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
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
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
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
326 void *tmp = cxMallocDefault(elem_size);
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
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
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
329 void *ip = list->cl->at(list, i);
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
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
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
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
27fadadaae31 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
341 static int cx_list_cmpfunc2_safe_memcmp(const void *a, const void *b, void *c) {
27fadadaae31 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
342 // it is not safe to store a pointer to the size in the list
27fadadaae31 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
343 // because the entire list structure might get reallocated
27fadadaae31 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
344 size_t elem_size = (size_t)(uintptr_t)c;
27fadadaae31 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
345 return memcmp(a, b, elem_size);
27fadadaae31 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
346 }
27fadadaae31 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
347
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
348 void cx_list_init(
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
349 struct cx_list_s *list,
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
350 struct cx_list_class_s *cl,
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
351 const struct cx_allocator_s *allocator,
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
352 size_t elem_size
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
353 ) {
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
354 list->cl = cl;
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
355 list->collection.allocator = allocator;
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
356 list->collection.size = 0;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
357 list->collection.sorted = false; // should be set by the implementation
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
358 if (elem_size > 0) {
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
359 list->collection.elem_size = elem_size;
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
360 list->collection.simple_cmp = NULL;
38
27fadadaae31 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
361 list->collection.advanced_cmp = cx_list_cmpfunc2_safe_memcmp;
27fadadaae31 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
362 list->collection.cmp_data = (void*)(uintptr_t)list->collection.elem_size;
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
363 list->collection.store_pointer = false;
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
364 } else {
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
365 list->collection.elem_size = sizeof(void *);
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
366 list->collection.simple_cmp = cx_cmp_ptr;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
367 list->collection.advanced_cmp = NULL;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
368 list->collection.cmp_data = NULL;
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
369 list->collection.store_pointer = true;
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
370 }
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
371 }
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
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
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
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
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
383 // check if the lists are incompatible or this list does not implement compare
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
384 cx_compare_func list_cmp = (cx_compare_func) list->cl->compare;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
385 cx_compare_func other_cmp = (cx_compare_func) other->cl->compare;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
386 cannot_optimize |= list_cmp != other_cmp;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
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
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
392 CxIterator left = cxListIterator(list);
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
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
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
397 // values are already unwrapped, invoke immediately
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
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
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
415 size_t cxListSize(const CxList *list) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
416 return list->collection.size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
417 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
418
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
419 int cxListAdd(CxList *list, const void *elem) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
420 list->collection.sorted = false;
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
421 return list->cl->insert_element(list, list->collection.size, cx_ref(list, elem)) == NULL;
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
422 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
423
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
424 size_t cxListAddArray(CxList *list, const void *array, size_t n) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
425 list->collection.sorted = false;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
426 return list->cl->insert_array(list, list->collection.size, array, n);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
427 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
428
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
429 int cxListInsert(CxList *list, size_t index, const void *elem) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
430 list->collection.sorted = false;
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
431 return list->cl->insert_element(list, index, cx_ref(list, elem)) == NULL;
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
432 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
433
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
434 void *cxListEmplaceAt(CxList *list, size_t index) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
435 list->collection.sorted = false;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
436 return list->cl->insert_element(list, index, NULL);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
437 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
438
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
439 void *cxListEmplace(CxList *list) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
440 list->collection.sorted = false;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
441 return list->cl->insert_element(list, list->collection.size, NULL);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
442 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
443
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
444 static bool cx_list_emplace_iterator_valid(const void *it) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
445 const CxIterator *iter = it;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
446 return iter->index < iter->elem_count;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
447 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
448
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
449 CxIterator cxListEmplaceArrayAt(CxList *list, size_t index, size_t n) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
450 list->collection.sorted = false;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
451 size_t c = list->cl->insert_array(list, index, NULL, n);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
452 CxIterator iter = list->cl->iterator(list, index, false);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
453 // tweak the fields of this iterator
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
454 iter.elem_count = c;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
455 iter.index = 0;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
456 // replace the valid function to abort iteration when c is reached
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
457 iter.base.valid = cx_list_emplace_iterator_valid;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
458 return iter;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
459 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
460
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
461 CxIterator cxListEmplaceArray(CxList *list, size_t n) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
462 return cxListEmplaceArrayAt(list, list->collection.size, n);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
463 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
464
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
465 int cxListInsertSorted(CxList *list, const void *elem) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
466 assert(cxCollectionSorted(list));
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
467 list->collection.sorted = true;
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
468 return list->cl->insert_sorted(list, cx_ref(list, elem), 1) == 0;
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
469 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
470
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
471 int cxListInsertUnique(CxList *list, const void *elem) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
472 if (cxCollectionSorted(list)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
473 list->collection.sorted = true;
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
474 return list->cl->insert_unique(list, cx_ref(list, elem), 1) == 0;
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
475 } else {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
476 if (cxListContains(list, elem)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
477 return 0;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
478 } else {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
479 return cxListAdd(list, elem);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
480 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
481 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
482 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
483
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
484 size_t cxListInsertArray(CxList *list, size_t index, const void *array, size_t n) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
485 list->collection.sorted = false;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
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
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
489 size_t cxListInsertSortedArray(CxList *list, const void *array, size_t n) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
490 assert(cxCollectionSorted(list));
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
491 list->collection.sorted = true;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
492 return list->cl->insert_sorted(list, array, n);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
493 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
494
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
495 size_t cxListInsertUniqueArray(CxList *list, const void *array, size_t n) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
496 if (cxCollectionSorted(list)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
497 list->collection.sorted = true;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
498 return list->cl->insert_unique(list, array, n);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
499 } else {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
500 const char *source = array;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
501 for (size_t i = 0 ; i < n; i++) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
502 // note: this also checks elements added in a previous iteration
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
503 const void *data = cx_deref(list, source);
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
504 if (!cxListContains(list, data)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
505 if (cxListAdd(list, data)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
506 return i; // LCOV_EXCL_LINE
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
507 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
508 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
509 source += list->collection.elem_size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
510 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
511 return n;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
512 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
513 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
514
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
515 int cxListInsertAfter(CxIterator *iter, const void *elem) {
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
516 CxList* list = iter->src_handle;
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
517 list->collection.sorted = false;
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
518 return list->cl->insert_iter(iter, cx_ref(list, elem), 0);
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
519 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
520
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
521 int cxListInsertBefore(CxIterator *iter, const void *elem) {
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
522 CxList* list = iter->src_handle;
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
523 list->collection.sorted = false;
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
524 return list->cl->insert_iter(iter, cx_ref(list, elem), 1);
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
525 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
526
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
527 int cxListRemove(CxList *list, size_t index) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
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
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
530
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
531 int cxListRemoveAndGet(CxList *list, size_t index, void *targetbuf) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
532 return list->cl->remove(list, index, 1, targetbuf) == 0;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
533 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
534
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
535 int cxListRemoveAndGetFirst(CxList *list, void *targetbuf) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
536 return list->cl->remove(list, 0, 1, targetbuf) == 0;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
537 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
538
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
539 int cxListRemoveAndGetLast(CxList *list, void *targetbuf) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
540 // note: index may wrap - member function will catch that
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
541 return list->cl->remove(list, list->collection.size - 1, 1, targetbuf) == 0;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
542 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
543
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
544 size_t cxListRemoveArray(CxList *list, size_t index, size_t num) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
545 return list->cl->remove(list, index, num, NULL);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
546 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
547
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
548 size_t cxListRemoveArrayAndGet(CxList *list, size_t index, size_t num, void *targetbuf) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
549 return list->cl->remove(list, index, num, targetbuf);
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
550 }
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
551
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
552 void cxListClear(CxList *list) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
553 list->cl->clear(list);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
554 list->collection.sorted = true; // empty lists are always sorted
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
555 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
556
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
557 int cxListSwap(CxList *list, size_t i, size_t j) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
558 list->collection.sorted = false;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
559 return list->cl->swap(list, i, j);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
560 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
561
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
562 void *cxListAt(const CxList *list, size_t index) {
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
563 void *result = list->cl->at(list, index);
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
564 if (result == NULL) return NULL;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
565 return cx_deref(list, result);
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
566 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
567
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
568 void *cxListFirst(const CxList *list) {
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
569 return cxListAt(list, 0);
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
570 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
571
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
572 void *cxListLast(const CxList *list) {
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
573 return cxListAt(list, list->collection.size - 1);
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
574 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
575
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
576 int cxListSet(CxList *list, size_t index, const void *elem) {
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
577 if (index >= list->collection.size) {
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
578 return 1;
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
579 }
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
580
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
581 if (list->collection.store_pointer) {
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
582 void **target = list->cl->at(list, index);
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
583 *target = (void *)elem;
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
584 } else {
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
585 void *target = list->cl->at(list, index);
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
586 memcpy(target, elem, list->collection.elem_size);
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
587 }
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
588
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
589 return 0;
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
590 }
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
591
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
592 static void *cx_pl_iter_current(const void *it) {
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
593 const struct cx_iterator_s *iter = it;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
594 void **ptr = iter->base.current_impl(it);
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
595 return ptr == NULL ? NULL : *ptr;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
596 }
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
597
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
598 CX_INLINE CxIterator cx_pl_iter_wrap(const CxList *list, CxIterator iter) {
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
599 if (cxCollectionStoresPointers(list)) {
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
600 iter.base.current_impl = iter.base.current;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
601 iter.base.current = cx_pl_iter_current;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
602 return iter;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
603 } else {
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
604 return iter;
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
605 }
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
606 }
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
607
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
608 CxIterator cxListIteratorAt(const CxList *list, size_t index) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
609 if (list == NULL) list = cxEmptyList;
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
610 return cx_pl_iter_wrap(list, list->cl->iterator(list, index, false));
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
611 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
612
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
613 CxIterator cxListBackwardsIteratorAt(const CxList *list, size_t index) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
614 if (list == NULL) list = cxEmptyList;
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
615 return cx_pl_iter_wrap(list, list->cl->iterator(list, index, true));
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
616 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
617
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
618 CxIterator cxListIterator(const CxList *list) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
619 if (list == NULL) list = cxEmptyList;
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
620 return cx_pl_iter_wrap(list, list->cl->iterator(list, 0, false));
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
621 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
622
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
623 CxIterator cxListBackwardsIterator(const CxList *list) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
624 if (list == NULL) list = cxEmptyList;
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
625 return cx_pl_iter_wrap(list, list->cl->iterator(list, list->collection.size - 1, true));
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
626 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
627
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
628 size_t cxListFind(const CxList *list, const void *elem) {
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
629 return list->cl->find_remove((CxList*)list, cx_ref(list, elem), false);
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
630 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
631
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
632 bool cxListContains(const CxList* list, const void* elem) {
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
633 return list->cl->find_remove((CxList*)list, cx_ref(list, elem), false) < list->collection.size;
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
634 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
635
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
636 bool cxListIndexValid(const CxList *list, size_t index) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
637 return index < list->collection.size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
638 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
639
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
640 size_t cxListFindRemove(CxList *list, const void *elem) {
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
641 return list->cl->find_remove(list, cx_ref(list, elem), true);
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
642 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
643
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
644 void cxListSort(CxList *list) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
645 if (list->collection.sorted) return;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
646 list->cl->sort(list);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
647 list->collection.sorted = true;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
648 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
649
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
650 void cxListReverse(CxList *list) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
651 // still sorted, but not according to the cmp_func
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
652 list->collection.sorted = false;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
653 list->cl->reverse(list);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
654 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
655
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
656 void cxListFree(CxList *list) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
657 if (list == NULL) return;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
658 list->cl->deallocate(list);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
659 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
660
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
661 static void cx_list_pop_uninitialized_elements(CxList *list, size_t n) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
662 cx_destructor_func destr_bak = list->collection.simple_destructor;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
663 cx_destructor_func2 destr2_bak = list->collection.advanced_destructor;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
664 list->collection.simple_destructor = NULL;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
665 list->collection.advanced_destructor = NULL;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
666 if (n == 1) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
667 cxListRemove(list, list->collection.size - 1);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
668 } else {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
669 cxListRemoveArray(list,list->collection.size - n, n);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
670 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
671 list->collection.simple_destructor = destr_bak;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
672 list->collection.advanced_destructor = destr2_bak;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
673 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
674
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
675 static void* cx_list_shallow_clone_func(void *dst, const void *src, const CxAllocator *al, void *data) {
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
676 size_t elem_size = *(size_t*)data;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
677 if (dst == NULL) dst = cxMalloc(al, elem_size);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
678 if (dst != NULL) memcpy(dst, src, elem_size);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
679 return dst;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
680 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
681
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
682 #define use_shallow_clone_func(list) cx_list_shallow_clone_func, NULL, (void*)&((list)->collection.elem_size)
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
683
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
684 int cxListClone(CxList *dst, const CxList *src, cx_clone_func clone_func,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
685 const CxAllocator *clone_allocator, void *data) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
686 if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
687
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
688 // remember the original size
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
689 size_t orig_size = dst->collection.size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
690
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
691 // first, try to allocate the memory in the new list
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
692 CxIterator empl_iter = cxListEmplaceArray(dst, src->collection.size);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
693
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
694 // get an iterator over the source elements
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
695 CxIterator src_iter = cxListIterator(src);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
696
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
697 // now clone the elements
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
698 size_t cloned = empl_iter.elem_count;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
699 for (size_t i = 0 ; i < empl_iter.elem_count; i++) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
700 void *src_elem = cxIteratorCurrent(src_iter);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
701 void **dest_memory = cxIteratorCurrent(empl_iter);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
702 void *target = cxCollectionStoresPointers(dst) ? NULL : dest_memory;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
703 void *dest_ptr = clone_func(target, src_elem, clone_allocator, data);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
704 if (dest_ptr == NULL) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
705 cloned = i;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
706 break;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
707 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
708 if (cxCollectionStoresPointers(dst)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
709 *dest_memory = dest_ptr;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
710 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
711 cxIteratorNext(src_iter);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
712 cxIteratorNext(empl_iter);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
713 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
714
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
715 // if we could not clone everything, free the allocated memory
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
716 // (disable the destructors!)
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
717 if (cloned < src->collection.size) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
718 cx_list_pop_uninitialized_elements(dst,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
719 dst->collection.size - cloned - orig_size);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
720 return 1;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
721 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
722
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
723 // set the sorted flag when we know it's sorted
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
724 if (orig_size == 0 && src->collection.sorted) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
725 dst->collection.sorted = true;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
726 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
727
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
728 return 0;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
729 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
730
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
731 int cxListDifference(CxList *dst,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
732 const CxList *minuend, const CxList *subtrahend,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
733 cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
734 if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
735
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
736 // optimize for sorted collections
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
737 if (cxCollectionSorted(minuend) && cxCollectionSorted(subtrahend)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
738 bool dst_was_empty = cxCollectionSize(dst) == 0;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
739
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
740 CxIterator min_iter = cxListIterator(minuend);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
741 CxIterator sub_iter = cxListIterator(subtrahend);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
742 while (cxIteratorValid(min_iter)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
743 void *min_elem = cxIteratorCurrent(min_iter);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
744 void *sub_elem;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
745 int d;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
746 if (cxIteratorValid(sub_iter)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
747 sub_elem = cxIteratorCurrent(sub_iter);
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
748 d = cx_list_compare_wrapper(sub_elem, min_elem, subtrahend);
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
749 } else {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
750 // no more elements in the subtrahend,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
751 // i.e., the min_elem is larger than any elem of the subtrahend
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
752 d = 1;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
753 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
754 if (d == 0) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
755 // is contained, so skip it
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
756 cxIteratorNext(min_iter);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
757 } else if (d < 0) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
758 // subtrahend is smaller than minuend,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
759 // check the next element
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
760 cxIteratorNext(sub_iter);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
761 } else {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
762 // subtrahend is larger than the dst element,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
763 // clone the minuend and advance
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
764 void **dst_mem = cxListEmplace(dst);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
765 void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
766 void* dst_ptr = clone_func(target, min_elem, clone_allocator, data);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
767 if (dst_ptr == NULL) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
768 cx_list_pop_uninitialized_elements(dst, 1);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
769 return 1;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
770 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
771 if (cxCollectionStoresPointers(dst)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
772 *dst_mem = dst_ptr;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
773 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
774 cxIteratorNext(min_iter);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
775 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
776 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
777
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
778 // if dst was empty, it is now guaranteed to be sorted
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
779 dst->collection.sorted = dst_was_empty;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
780 } else {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
781 CxIterator min_iter = cxListIterator(minuend);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
782 cx_foreach(void *, elem, min_iter) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
783 if (cxListContains(subtrahend, elem)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
784 continue;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
785 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
786 void **dst_mem = cxListEmplace(dst);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
787 void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
788 void* dst_ptr = clone_func(target, elem, clone_allocator, data);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
789 if (dst_ptr == NULL) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
790 cx_list_pop_uninitialized_elements(dst, 1);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
791 return 1;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
792 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
793 if (cxCollectionStoresPointers(dst)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
794 *dst_mem = dst_ptr;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
795 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
796 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
797 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
798
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
799 return 0;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
800 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
801
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
802 int cxListIntersection(CxList *dst,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
803 const CxList *src, const CxList *other,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
804 cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
805 if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
806
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
807 // optimize for sorted collections
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
808 if (cxCollectionSorted(src) && cxCollectionSorted(other)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
809 bool dst_was_empty = cxCollectionSize(dst) == 0;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
810
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
811 CxIterator src_iter = cxListIterator(src);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
812 CxIterator other_iter = cxListIterator(other);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
813 while (cxIteratorValid(src_iter) && cxIteratorValid(other_iter)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
814 void *src_elem = cxIteratorCurrent(src_iter);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
815 void *other_elem = cxIteratorCurrent(other_iter);
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
816 int d = cx_list_compare_wrapper(src_elem, other_elem, src);
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
817 if (d == 0) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
818 // is contained, clone it
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
819 void **dst_mem = cxListEmplace(dst);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
820 void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
821 void* dst_ptr = clone_func(target, src_elem, clone_allocator, data);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
822 if (dst_ptr == NULL) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
823 cx_list_pop_uninitialized_elements(dst, 1);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
824 return 1;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
825 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
826 if (cxCollectionStoresPointers(dst)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
827 *dst_mem = dst_ptr;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
828 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
829 cxIteratorNext(src_iter);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
830 } else if (d < 0) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
831 // the other element is larger, skip the source element
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
832 cxIteratorNext(src_iter);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
833 } else {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
834 // the source element is larger, try to find it in the other list
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
835 cxIteratorNext(other_iter);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
836 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
837 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
838
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
839 // if dst was empty, it is now guaranteed to be sorted
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
840 dst->collection.sorted = dst_was_empty;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
841 } else {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
842 CxIterator src_iter = cxListIterator(src);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
843 cx_foreach(void *, elem, src_iter) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
844 if (!cxListContains(other, elem)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
845 continue;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
846 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
847 void **dst_mem = cxListEmplace(dst);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
848 void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
849 void* dst_ptr = clone_func(target, elem, clone_allocator, data);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
850 if (dst_ptr == NULL) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
851 cx_list_pop_uninitialized_elements(dst, 1);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
852 return 1;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
853 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
854 if (cxCollectionStoresPointers(dst)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
855 *dst_mem = dst_ptr;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
856 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
857 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
858 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
859
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
860 return 0;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
861 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
862
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
863 int cxListUnion(CxList *dst,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
864 const CxList *src, const CxList *other,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
865 cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
866 if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
867
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
868 // optimize for sorted collections
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
869 if (cxCollectionSorted(src) && cxCollectionSorted(other)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
870 bool dst_was_empty = cxCollectionSize(dst) == 0;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
871
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
872 CxIterator src_iter = cxListIterator(src);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
873 CxIterator other_iter = cxListIterator(other);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
874 while (cxIteratorValid(src_iter) || cxIteratorValid(other_iter)) {
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
875 void *src_elem = NULL, *other_elem = NULL;
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
876 int d;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
877 if (!cxIteratorValid(src_iter)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
878 other_elem = cxIteratorCurrent(other_iter);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
879 d = 1;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
880 } else if (!cxIteratorValid(other_iter)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
881 src_elem = cxIteratorCurrent(src_iter);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
882 d = -1;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
883 } else {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
884 src_elem = cxIteratorCurrent(src_iter);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
885 other_elem = cxIteratorCurrent(other_iter);
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
886 d = cx_list_compare_wrapper(src_elem, other_elem, src);
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
887 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
888 void *clone_from;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
889 if (d < 0) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
890 // source element is smaller clone it
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
891 clone_from = src_elem;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
892 cxIteratorNext(src_iter);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
893 } else if (d == 0) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
894 // both elements are equal, clone from the source, skip other
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
895 clone_from = src_elem;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
896 cxIteratorNext(src_iter);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
897 cxIteratorNext(other_iter);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
898 } else {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
899 // the other element is smaller, clone it
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
900 clone_from = other_elem;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
901 cxIteratorNext(other_iter);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
902 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
903 void **dst_mem = cxListEmplace(dst);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
904 void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
905 void* dst_ptr = clone_func(target, clone_from, clone_allocator, data);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
906 if (dst_ptr == NULL) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
907 cx_list_pop_uninitialized_elements(dst, 1);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
908 return 1;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
909 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
910 if (cxCollectionStoresPointers(dst)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
911 *dst_mem = dst_ptr;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
912 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
913 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
914
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
915 // if dst was empty, it is now guaranteed to be sorted
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
916 dst->collection.sorted = dst_was_empty;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
917 } else {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
918 if (cxListClone(dst, src, clone_func, clone_allocator, data)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
919 return 1;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
920 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
921 CxIterator other_iter = cxListIterator(other);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
922 cx_foreach(void *, elem, other_iter) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
923 if (cxListContains(src, elem)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
924 continue;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
925 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
926 void **dst_mem = cxListEmplace(dst);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
927 void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
928 void* dst_ptr = clone_func(target, elem, clone_allocator, data);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
929 if (dst_ptr == NULL) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
930 cx_list_pop_uninitialized_elements(dst, 1);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
931 return 1;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
932 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
933 if (cxCollectionStoresPointers(dst)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
934 *dst_mem = dst_ptr;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
935 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
936 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
937 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
938
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
939 return 0;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
940 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
941
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
942 int cxListCloneShallow(CxList *dst, const CxList *src) {
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
943 return cxListClone(dst, src, use_shallow_clone_func(src));
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
944 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
945
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
946 int cxListDifferenceShallow(CxList *dst, const CxList *minuend, const CxList *subtrahend) {
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
947 return cxListDifference(dst, minuend, subtrahend, use_shallow_clone_func(minuend));
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
948 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
949
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
950 int cxListIntersectionShallow(CxList *dst, const CxList *src, const CxList *other) {
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
951 return cxListIntersection(dst, src, other, use_shallow_clone_func(src));
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
952 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
953
31
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
954 int cxListUnionShallow(CxList *dst, const CxList *src, const CxList *other) {
287484519844 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
955 return cxListUnion(dst, src, other, use_shallow_clone_func(src));
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
956 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
957
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
958 int cxListReserve(CxList *list, size_t capacity) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
959 if (list->cl->change_capacity == NULL) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
960 return 0;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
961 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
962 if (capacity <= cxCollectionSize(list)) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
963 return 0;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
964 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
965 return list->cl->change_capacity(list, capacity);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
966 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
967
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
968 int cxListShrink(CxList *list) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
969 if (list->cl->change_capacity == NULL) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
970 return 0;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
971 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
972 return list->cl->change_capacity(list, cxCollectionSize(list));
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
973 }

mercurial