ucx/list.c

Sat, 08 Nov 2025 23:06:11 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 08 Nov 2025 23:06:11 +0100
changeset 888
af685cc9d623
parent 854
1c8401ece69e
permissions
-rw-r--r--

update ucx

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

mercurial