src/list.c

Fri, 23 May 2025 12:44:24 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 23 May 2025 12:44:24 +0200
changeset 1327
ed75dc1db503
parent 1319
aa1f580f8f59
permissions
-rw-r--r--

make test-compile depend on both static and shared

the shared lib is not needed for the tests,
but when run with coverage, gcov will be confused
when outdated line information is available from
a previous shared build

503
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "cx/list.h"
a89857072ace add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
640
55cc3b373c5e simplify list class - fixes #236
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
31 #include <string.h>
55cc3b373c5e simplify list class - fixes #236
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
32
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
33 // <editor-fold desc="Store Pointers Functionality">
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
34
677
b09aae58bba4 refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents: 666
diff changeset
35 static _Thread_local cx_compare_func cx_pl_cmpfunc_impl;
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
36
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
37 static int cx_pl_cmpfunc(
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
38 const void *l,
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
39 const void *r
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
40 ) {
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
41 void *const *lptr = l;
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
42 void *const *rptr = r;
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
43 const void *left = lptr == NULL ? NULL : *lptr;
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
44 const void *right = rptr == NULL ? NULL : *rptr;
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
45 return cx_pl_cmpfunc_impl(left, right);
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
46 }
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
47
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
48 static void cx_pl_hack_cmpfunc(const struct cx_list_s *list) {
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
49 // cast away const - this is the hacky thing
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
50 struct cx_collection_s *l = (struct cx_collection_s*) &list->collection;
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
51 cx_pl_cmpfunc_impl = l->cmpfunc;
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
52 l->cmpfunc = cx_pl_cmpfunc;
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
53 }
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
54
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
55 static void cx_pl_unhack_cmpfunc(const struct cx_list_s *list) {
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
56 // cast away const - this is the hacky thing
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
57 struct cx_collection_s *l = (struct cx_collection_s*) &list->collection;
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
58 l->cmpfunc = cx_pl_cmpfunc_impl;
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
59 }
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
60
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
61 static void cx_pl_destructor(struct cx_list_s *list) {
993
b642eca4b956 make names of destroy and free functions consistent - fixes #484
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
62 list->climpl->deallocate(list);
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
63 }
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
64
1316
c41538edfcef add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents: 1287
diff changeset
65 static void *cx_pl_insert_element(
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
66 struct cx_list_s *list,
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
67 size_t index,
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
68 const void *element
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
69 ) {
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
70 return list->climpl->insert_element(list, index, &element);
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
71 }
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
72
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
73 static size_t cx_pl_insert_array(
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
74 struct cx_list_s *list,
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
75 size_t index,
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
76 const void *array,
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
77 size_t n
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
78 ) {
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
79 return list->climpl->insert_array(list, index, array, n);
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
80 }
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
81
876
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
82 static size_t cx_pl_insert_sorted(
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
83 struct cx_list_s *list,
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
84 const void *array,
876
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
85 size_t n
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
86 ) {
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
87 cx_pl_hack_cmpfunc(list);
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
88 size_t result = list->climpl->insert_sorted(list, array, n);
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
89 cx_pl_unhack_cmpfunc(list);
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
90 return result;
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
91 }
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
92
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
93 static int cx_pl_insert_iter(
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 802
diff changeset
94 struct cx_iterator_s *iter,
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
95 const void *elem,
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
96 int prepend
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
97 ) {
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 802
diff changeset
98 struct cx_list_s *list = iter->src_handle.m;
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
99 return list->climpl->insert_iter(iter, &elem, prepend);
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
100 }
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
101
919
75da57d4634e add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents: 890
diff changeset
102 static size_t cx_pl_remove(
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
103 struct cx_list_s *list,
919
75da57d4634e add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents: 890
diff changeset
104 size_t index,
75da57d4634e add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents: 890
diff changeset
105 size_t num,
75da57d4634e add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents: 890
diff changeset
106 void *targetbuf
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
107 ) {
919
75da57d4634e add possibility to remove arrays of data and retrieve removed data
Mike Becker <universe@uap-core.de>
parents: 890
diff changeset
108 return list->climpl->remove(list, index, num, targetbuf);
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
109 }
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
110
664
af5bf4603a5d add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents: 655
diff changeset
111 static void cx_pl_clear(struct cx_list_s *list) {
af5bf4603a5d add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents: 655
diff changeset
112 list->climpl->clear(list);
af5bf4603a5d add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents: 655
diff changeset
113 }
af5bf4603a5d add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents: 655
diff changeset
114
647
2e6e9d9f2159 implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
115 static int cx_pl_swap(
2e6e9d9f2159 implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
116 struct cx_list_s *list,
2e6e9d9f2159 implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
117 size_t i,
2e6e9d9f2159 implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
118 size_t j
2e6e9d9f2159 implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
119 ) {
2e6e9d9f2159 implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
120 return list->climpl->swap(list, i, j);
2e6e9d9f2159 implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
121 }
2e6e9d9f2159 implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
122
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
123 static void *cx_pl_at(
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
124 const struct cx_list_s *list,
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
125 size_t index
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
126 ) {
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
127 void **ptr = list->climpl->at(list, index);
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
128 return ptr == NULL ? NULL : *ptr;
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
129 }
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
130
1162
e3bb67b72d33 remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents: 1111
diff changeset
131 static size_t cx_pl_find_remove(
764
ccbdbd088455 add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents: 708
diff changeset
132 struct cx_list_s *list,
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
133 const void *elem,
764
ccbdbd088455 add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents: 708
diff changeset
134 bool remove
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
135 ) {
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
136 cx_pl_hack_cmpfunc(list);
1162
e3bb67b72d33 remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents: 1111
diff changeset
137 size_t ret = list->climpl->find_remove(list, &elem, remove);
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
138 cx_pl_unhack_cmpfunc(list);
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
139 return ret;
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
140 }
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
141
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
142 static void cx_pl_sort(struct cx_list_s *list) {
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
143 cx_pl_hack_cmpfunc(list);
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
144 list->climpl->sort(list);
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
145 cx_pl_unhack_cmpfunc(list);
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
146 }
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
147
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
148 static int cx_pl_compare(
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
149 const struct cx_list_s *list,
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
150 const struct cx_list_s *other
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
151 ) {
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
152 cx_pl_hack_cmpfunc(list);
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
153 int ret = list->climpl->compare(list, other);
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
154 cx_pl_unhack_cmpfunc(list);
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
155 return ret;
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
156 }
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
157
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
158 static void cx_pl_reverse(struct cx_list_s *list) {
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
159 list->climpl->reverse(list);
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
160 }
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
161
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
162 static void *cx_pl_iter_current(const void *it) {
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
163 const struct cx_iterator_s *iter = it;
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
164 void **ptr = iter->base.current_impl(it);
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
165 return ptr == NULL ? NULL : *ptr;
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
166 }
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
167
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
168 static struct cx_iterator_s cx_pl_iterator(
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
169 const struct cx_list_s *list,
655
7340c4255f1f implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents: 647
diff changeset
170 size_t index,
7340c4255f1f implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents: 647
diff changeset
171 bool backwards
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
172 ) {
655
7340c4255f1f implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents: 647
diff changeset
173 struct cx_iterator_s iter = list->climpl->iterator(list, index, backwards);
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
174 iter.base.current_impl = iter.base.current;
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
175 iter.base.current = cx_pl_iter_current;
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
176 return iter;
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
177 }
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
178
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
179 static cx_list_class cx_pointer_list_class = {
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
180 cx_pl_destructor,
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
181 cx_pl_insert_element,
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
182 cx_pl_insert_array,
876
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
183 cx_pl_insert_sorted,
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
184 cx_pl_insert_iter,
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
185 cx_pl_remove,
664
af5bf4603a5d add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents: 655
diff changeset
186 cx_pl_clear,
647
2e6e9d9f2159 implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
187 cx_pl_swap,
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
188 cx_pl_at,
764
ccbdbd088455 add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents: 708
diff changeset
189 cx_pl_find_remove,
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
190 cx_pl_sort,
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
191 cx_pl_compare,
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
192 cx_pl_reverse,
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
193 cx_pl_iterator,
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
194 };
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
195 // </editor-fold>
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
196
704
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
197 // <editor-fold desc="empty list implementation">
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
198
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 919
diff changeset
199 static void cx_emptyl_noop(cx_attr_unused CxList *list) {
704
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
200 // this is a noop, but MUST be implemented
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
201 }
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
202
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
203 static void *cx_emptyl_at(
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 919
diff changeset
204 cx_attr_unused const struct cx_list_s *list,
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 919
diff changeset
205 cx_attr_unused size_t index
704
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
206 ) {
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
207 return NULL;
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
208 }
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
209
1162
e3bb67b72d33 remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents: 1111
diff changeset
210 static size_t cx_emptyl_find_remove(
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 919
diff changeset
211 cx_attr_unused struct cx_list_s *list,
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 919
diff changeset
212 cx_attr_unused const void *elem,
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 919
diff changeset
213 cx_attr_unused bool remove
704
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
214 ) {
1162
e3bb67b72d33 remove dependency to ssize_t - fixes #552
Mike Becker <universe@uap-core.de>
parents: 1111
diff changeset
215 return 0;
704
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
216 }
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
217
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 919
diff changeset
218 static bool cx_emptyl_iter_valid(cx_attr_unused const void *iter) {
704
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
219 return false;
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
220 }
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
221
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
222 static CxIterator cx_emptyl_iterator(
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
223 const struct cx_list_s *list,
704
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
224 size_t index,
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 919
diff changeset
225 cx_attr_unused bool backwards
704
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
226 ) {
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
227 CxIterator iter = {0};
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 802
diff changeset
228 iter.src_handle.c = list;
704
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
229 iter.index = index;
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
230 iter.base.valid = cx_emptyl_iter_valid;
704
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
231 return iter;
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
232 }
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
233
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
234 static cx_list_class cx_empty_list_class = {
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
235 cx_emptyl_noop,
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
236 NULL,
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
237 NULL,
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
238 NULL,
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
239 NULL,
876
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
240 NULL,
704
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
241 cx_emptyl_noop,
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
242 NULL,
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
243 cx_emptyl_at,
764
ccbdbd088455 add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents: 708
diff changeset
244 cx_emptyl_find_remove,
704
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
245 cx_emptyl_noop,
875
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
246 NULL,
704
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
247 cx_emptyl_noop,
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
248 cx_emptyl_iterator,
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
249 };
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
250
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
251 CxList cx_empty_list = {
1163
68ff0839bc6a optimize cx_arl_find_remove for sorted arrays - fixes #547
Mike Becker <universe@uap-core.de>
parents: 1162
diff changeset
252 {
68ff0839bc6a optimize cx_arl_find_remove for sorted arrays - fixes #547
Mike Becker <universe@uap-core.de>
parents: 1162
diff changeset
253 NULL,
68ff0839bc6a optimize cx_arl_find_remove for sorted arrays - fixes #547
Mike Becker <universe@uap-core.de>
parents: 1162
diff changeset
254 NULL,
68ff0839bc6a optimize cx_arl_find_remove for sorted arrays - fixes #547
Mike Becker <universe@uap-core.de>
parents: 1162
diff changeset
255 0,
68ff0839bc6a optimize cx_arl_find_remove for sorted arrays - fixes #547
Mike Becker <universe@uap-core.de>
parents: 1162
diff changeset
256 0,
68ff0839bc6a optimize cx_arl_find_remove for sorted arrays - fixes #547
Mike Becker <universe@uap-core.de>
parents: 1162
diff changeset
257 NULL,
68ff0839bc6a optimize cx_arl_find_remove for sorted arrays - fixes #547
Mike Becker <universe@uap-core.de>
parents: 1162
diff changeset
258 NULL,
68ff0839bc6a optimize cx_arl_find_remove for sorted arrays - fixes #547
Mike Becker <universe@uap-core.de>
parents: 1162
diff changeset
259 NULL,
68ff0839bc6a optimize cx_arl_find_remove for sorted arrays - fixes #547
Mike Becker <universe@uap-core.de>
parents: 1162
diff changeset
260 false,
68ff0839bc6a optimize cx_arl_find_remove for sorted arrays - fixes #547
Mike Becker <universe@uap-core.de>
parents: 1162
diff changeset
261 true,
68ff0839bc6a optimize cx_arl_find_remove for sorted arrays - fixes #547
Mike Becker <universe@uap-core.de>
parents: 1162
diff changeset
262 },
68ff0839bc6a optimize cx_arl_find_remove for sorted arrays - fixes #547
Mike Becker <universe@uap-core.de>
parents: 1162
diff changeset
263 &cx_empty_list_class,
68ff0839bc6a optimize cx_arl_find_remove for sorted arrays - fixes #547
Mike Becker <universe@uap-core.de>
parents: 1162
diff changeset
264 NULL
704
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
265 };
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
266
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
267 CxList *const cxEmptyList = &cx_empty_list;
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
268
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
269 // </editor-fold>
35f06c5eeb0e add empty list implementation - fixes #258
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
270
875
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
271 #define invoke_list_func(name, list, ...) \
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
272 ((list)->climpl == NULL ? (list)->cl->name : (list)->climpl->name) \
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
273 (list, __VA_ARGS__)
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
274
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
275 size_t cx_list_default_insert_array(
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
276 struct cx_list_s *list,
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
277 size_t index,
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
278 const void *data,
875
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
279 size_t n
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
280 ) {
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
281 size_t elem_size = list->collection.elem_size;
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
282 const char *src = data;
875
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
283 size_t i = 0;
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
284 for (; i < n; i++) {
1316
c41538edfcef add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents: 1287
diff changeset
285 if (NULL == invoke_list_func(
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
286 insert_element, list, index + i,
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
287 src + (i * elem_size))) return i;
875
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
288 }
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
289 return i;
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
290 }
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
291
876
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
292 size_t cx_list_default_insert_sorted(
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
293 struct cx_list_s *list,
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
294 const void *sorted_data,
876
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
295 size_t n
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
296 ) {
877
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
297 // corner case
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
298 if (n == 0) return 0;
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
299
876
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
300 size_t elem_size = list->collection.elem_size;
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
301 cx_compare_func cmp = list->collection.cmpfunc;
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
302 const char *src = sorted_data;
876
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
303
877
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
304 // track indices and number of inserted items
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
305 size_t di = 0, si = 0, inserted = 0;
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
306
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
307 // search the list for insertion points
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
308 for (; di < list->collection.size; di++) {
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
309 const void *list_elm = invoke_list_func(at, list, di);
877
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
310
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
311 // compare current list element with first source element
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
312 // if less or equal, skip
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
313 if (cmp(list_elm, src) <= 0) {
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
314 continue;
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
315 }
876
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
316
877
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
317 // determine number of consecutive elements that can be inserted
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
318 size_t ins = 1;
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
319 const char *next = src;
877
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
320 while (++si < n) {
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
321 next += elem_size;
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
322 // once we become larger than the list elem, break
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
323 if (cmp(list_elm, next) <= 0) {
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
324 break;
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
325 }
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
326 // otherwise, we can insert one more
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
327 ins++;
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
328 }
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
329
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
330 // insert the elements at location si
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
331 if (ins == 1) {
1316
c41538edfcef add cxListEmplace() and cxListEmplaceAt() plus some improvements to the array list implementation
Mike Becker <universe@uap-core.de>
parents: 1287
diff changeset
332 if (NULL == invoke_list_func(
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 993
diff changeset
333 insert_element, list, di, src)) return inserted;
877
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
334 } else {
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
335 size_t r = invoke_list_func(insert_array, list, di, src, ins);
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
336 if (r < ins) return inserted + r;
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
337 }
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
338 inserted += ins;
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
339 di += ins;
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
340
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
341 // everything inserted?
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
342 if (inserted == n) return inserted;
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
343 src = next;
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
344 }
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
345
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
346 // insert remaining items
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
347 if (si < n) {
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
348 inserted += invoke_list_func(insert_array, list, di, src, n - si);
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
349 }
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
350
608b14deea18 optimize default insert_sorted implementation
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
351 return inserted;
876
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
352 }
f4ce7df9cff0 add stupid default implementation for high level insertion sort
Mike Becker <universe@uap-core.de>
parents: 875
diff changeset
353
875
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
354 void cx_list_default_sort(struct cx_list_s *list) {
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
355 size_t elem_size = list->collection.elem_size;
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
356 size_t list_size = list->collection.size;
1319
aa1f580f8f59 add convenience macros for using the default allocator - relates to #669
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
357 void *tmp = cxMallocDefault(elem_size * list_size);
875
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
358 if (tmp == NULL) abort();
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
359
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
360 // copy elements from source array
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
361 char *loc = tmp;
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
362 for (size_t i = 0; i < list_size; i++) {
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
363 void *src = invoke_list_func(at, list, i);
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
364 memcpy(loc, src, elem_size);
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
365 loc += elem_size;
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
366 }
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
367
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
368 // qsort
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
369 qsort(tmp, list_size, elem_size,
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
370 list->collection.cmpfunc);
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
371
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
372 // copy elements back
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
373 loc = tmp;
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
374 for (size_t i = 0; i < list_size; i++) {
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
375 void *dest = invoke_list_func(at, list, i);
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
376 memcpy(dest, loc, elem_size);
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
377 loc += elem_size;
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
378 }
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
379
1319
aa1f580f8f59 add convenience macros for using the default allocator - relates to #669
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
380 cxFreeDefault(tmp);
875
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
381 }
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
382
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
383 int cx_list_default_swap(struct cx_list_s *list, size_t i, size_t j) {
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
384 if (i == j) return 0;
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
385 if (i >= list->collection.size) return 1;
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
386 if (j >= list->collection.size) return 1;
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
387
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
388 size_t elem_size = list->collection.elem_size;
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
389
1319
aa1f580f8f59 add convenience macros for using the default allocator - relates to #669
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
390 void *tmp = cxMallocDefault(elem_size);
875
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
391 if (tmp == NULL) return 1;
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
392
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
393 void *ip = invoke_list_func(at, list, i);
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
394 void *jp = invoke_list_func(at, list, j);
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
395
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
396 memcpy(tmp, ip, elem_size);
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
397 memcpy(ip, jp, elem_size);
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
398 memcpy(jp, tmp, elem_size);
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
399
1319
aa1f580f8f59 add convenience macros for using the default allocator - relates to #669
Mike Becker <universe@uap-core.de>
parents: 1318
diff changeset
400 cxFreeDefault(tmp);
875
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
401
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
402 return 0;
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
403 }
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
404
1111
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
405 void cx_list_init(
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
406 struct cx_list_s *list,
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
407 struct cx_list_class_s *cl,
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
408 const struct cx_allocator_s *allocator,
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
409 cx_compare_func comparator,
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
410 size_t elem_size
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
411 ) {
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
412 list->cl = cl;
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
413 list->collection.allocator = allocator;
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
414 list->collection.cmpfunc = comparator;
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
415 if (elem_size > 0) {
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
416 list->collection.elem_size = elem_size;
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
417 } else {
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
418 list->collection.elem_size = sizeof(void *);
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
419 if (list->collection.cmpfunc == NULL) {
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
420 list->collection.cmpfunc = cx_cmp_ptr;
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
421 }
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
422 list->collection.store_pointer = true;
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
423 list->climpl = list->cl;
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
424 list->cl = &cx_pointer_list_class;
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
425 }
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
426 }
78eeeb950883 remove API for changing the store_pointer property after list creation
Mike Becker <universe@uap-core.de>
parents: 1109
diff changeset
427
618
1f5a8f6f3015 #219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents: 528
diff changeset
428 int cxListCompare(
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
429 const CxList *list,
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 877
diff changeset
430 const CxList *other
618
1f5a8f6f3015 #219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents: 528
diff changeset
431 ) {
875
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
432 bool cannot_optimize = false;
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
433
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
434 // if one is storing pointers but the other is not
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
435 cannot_optimize |= list->collection.store_pointer ^ other->collection.store_pointer;
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
436
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
437 // if one class is wrapped but the other is not
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
438 cannot_optimize |= (list->climpl == NULL) ^ (other->climpl == NULL);
705
0d5447230044 fix cxListCompare if-condition
Mike Becker <universe@uap-core.de>
parents: 704
diff changeset
439
875
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
440 // if the compare functions do not match or both are NULL
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
441 if (!cannot_optimize) {
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
442 cx_compare_func list_cmp = (cx_compare_func) (list->climpl != NULL ?
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
443 list->climpl->compare : list->cl->compare);
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
444 cx_compare_func other_cmp = (cx_compare_func) (other->climpl != NULL ?
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
445 other->climpl->compare : other->cl->compare);
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
446 cannot_optimize |= list_cmp != other_cmp;
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
447 cannot_optimize |= list_cmp == NULL;
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
448 }
705
0d5447230044 fix cxListCompare if-condition
Mike Becker <universe@uap-core.de>
parents: 704
diff changeset
449
875
ee84ac776cbc add default impl for some list member functions
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
450 if (cannot_optimize) {
680
19379743e5a0 fix wrong operator precedence in destructor macros
Mike Becker <universe@uap-core.de>
parents: 677
diff changeset
451 // lists are definitely different - cannot use internal compare function
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
452 if (list->collection.size == other->collection.size) {
776
874cc78cdffd fix accidental generation of cxListIterator() symbol
Mike Becker <universe@uap-core.de>
parents: 764
diff changeset
453 CxIterator left = list->cl->iterator(list, 0, false);
802
30473af255b4 fix #356 : broken cxListCompare
Mike Becker <universe@uap-core.de>
parents: 776
diff changeset
454 CxIterator right = other->cl->iterator(other, 0, false);
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
455 for (size_t i = 0; i < list->collection.size; i++) {
630
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
456 void *leftValue = cxIteratorCurrent(left);
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
457 void *rightValue = cxIteratorCurrent(right);
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
458 int d = list->collection.cmpfunc(leftValue, rightValue);
618
1f5a8f6f3015 #219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents: 528
diff changeset
459 if (d != 0) {
1f5a8f6f3015 #219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents: 528
diff changeset
460 return d;
1f5a8f6f3015 #219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents: 528
diff changeset
461 }
630
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
462 cxIteratorNext(left);
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
463 cxIteratorNext(right);
618
1f5a8f6f3015 #219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents: 528
diff changeset
464 }
1f5a8f6f3015 #219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents: 528
diff changeset
465 return 0;
1f5a8f6f3015 #219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents: 528
diff changeset
466 } else {
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
467 return list->collection.size < other->collection.size ? -1 : 1;
618
1f5a8f6f3015 #219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents: 528
diff changeset
468 }
680
19379743e5a0 fix wrong operator precedence in destructor macros
Mike Becker <universe@uap-core.de>
parents: 677
diff changeset
469 } else {
19379743e5a0 fix wrong operator precedence in destructor macros
Mike Becker <universe@uap-core.de>
parents: 677
diff changeset
470 // lists are compatible
19379743e5a0 fix wrong operator precedence in destructor macros
Mike Becker <universe@uap-core.de>
parents: 677
diff changeset
471 return list->cl->compare(list, other);
618
1f5a8f6f3015 #219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents: 528
diff changeset
472 }
1f5a8f6f3015 #219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents: 528
diff changeset
473 }
640
55cc3b373c5e simplify list class - fixes #236
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
474
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 802
diff changeset
475 CxIterator cxListMutIteratorAt(
640
55cc3b373c5e simplify list class - fixes #236
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
476 CxList *list,
55cc3b373c5e simplify list class - fixes #236
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
477 size_t index
55cc3b373c5e simplify list class - fixes #236
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
478 ) {
655
7340c4255f1f implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents: 647
diff changeset
479 CxIterator it = list->cl->iterator(list, index, false);
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
480 it.base.mutating = true;
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 802
diff changeset
481 return it;
640
55cc3b373c5e simplify list class - fixes #236
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
482 }
655
7340c4255f1f implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents: 647
diff changeset
483
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 802
diff changeset
484 CxIterator cxListMutBackwardsIteratorAt(
655
7340c4255f1f implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents: 647
diff changeset
485 CxList *list,
7340c4255f1f implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents: 647
diff changeset
486 size_t index
7340c4255f1f implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents: 647
diff changeset
487 ) {
7340c4255f1f implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents: 647
diff changeset
488 CxIterator it = list->cl->iterator(list, index, true);
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
489 it.base.mutating = true;
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 802
diff changeset
490 return it;
655
7340c4255f1f implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents: 647
diff changeset
491 }
1109
89ec23988b88 free functions should not be inline in release mode - relates to #541
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
492
89ec23988b88 free functions should not be inline in release mode - relates to #541
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
493 void cxListFree(CxList *list) {
89ec23988b88 free functions should not be inline in release mode - relates to #541
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
494 if (list == NULL) return;
89ec23988b88 free functions should not be inline in release mode - relates to #541
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
495 list->cl->deallocate(list);
89ec23988b88 free functions should not be inline in release mode - relates to #541
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
496 }
1287
3a3ffc27813f adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents: 1163
diff changeset
497
3a3ffc27813f adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents: 1163
diff changeset
498 int cxListSet(
3a3ffc27813f adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents: 1163
diff changeset
499 CxList *list,
3a3ffc27813f adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents: 1163
diff changeset
500 size_t index,
3a3ffc27813f adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents: 1163
diff changeset
501 const void *elem
3a3ffc27813f adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents: 1163
diff changeset
502 ) {
3a3ffc27813f adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents: 1163
diff changeset
503 if (index >= list->collection.size) {
3a3ffc27813f adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents: 1163
diff changeset
504 return 1;
3a3ffc27813f adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents: 1163
diff changeset
505 }
3a3ffc27813f adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents: 1163
diff changeset
506
3a3ffc27813f adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents: 1163
diff changeset
507 if (list->collection.store_pointer) {
3a3ffc27813f adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents: 1163
diff changeset
508 // For pointer collections, always use climpl
3a3ffc27813f adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents: 1163
diff changeset
509 void **target = list->climpl->at(list, index);
3a3ffc27813f adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents: 1163
diff changeset
510 *target = (void *)elem;
3a3ffc27813f adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents: 1163
diff changeset
511 } else {
3a3ffc27813f adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents: 1163
diff changeset
512 void *target = list->cl->at(list, index);
3a3ffc27813f adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents: 1163
diff changeset
513 memcpy(target, elem, list->collection.elem_size);
3a3ffc27813f adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents: 1163
diff changeset
514 }
3a3ffc27813f adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents: 1163
diff changeset
515
3a3ffc27813f adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents: 1163
diff changeset
516 return 0;
3a3ffc27813f adds cxListSet() - resolves #642
Mike Becker <universe@uap-core.de>
parents: 1163
diff changeset
517 }

mercurial