Tue, 28 Mar 2023 21:00:33 +0200
allow NULL for allocator and comparator
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 | |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
35 | static _Thread_local CxListComparator cx_pl_cmpfunc_impl; |
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( |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
38 | void const *l, |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
39 | void const *r |
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; |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
43 | void const *left = lptr == NULL ? NULL : *lptr; |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
44 | void const *right = rptr == NULL ? NULL : *rptr; |
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 | |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
48 | static void cx_pl_hack_cmpfunc(struct cx_list_s const *list) { |
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 |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
50 | struct cx_list_s *l = (struct cx_list_s *) list; |
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 | |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
55 | static void cx_pl_unhack_cmpfunc(struct cx_list_s const *list) { |
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 |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
57 | struct cx_list_s *l = (struct cx_list_s *) list; |
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) { |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
62 | list->climpl->destructor(list); |
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 | |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
65 | static int cx_pl_insert_element( |
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, |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
68 | void const *element |
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, |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
76 | void const *array, |
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 | |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
82 | static int cx_pl_insert_iter( |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
83 | struct cx_mut_iterator_s *iter, |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
84 | void const *elem, |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
85 | int prepend |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
86 | ) { |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
87 | struct cx_list_s *list = iter->src_handle; |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
88 | 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
|
89 | } |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
90 | |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
91 | static int cx_pl_remove( |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
92 | struct cx_list_s *list, |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
93 | size_t index |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
94 | ) { |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
95 | return list->climpl->remove(list, index); |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
96 | } |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
97 | |
664
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
98 | 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
|
99 | list->climpl->clear(list); |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
100 | } |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
101 | |
647
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
102 | static int cx_pl_swap( |
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
103 | struct cx_list_s *list, |
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
104 | size_t i, |
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
105 | size_t j |
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
106 | ) { |
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
107 | 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
|
108 | } |
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
109 | |
641
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
110 | static void *cx_pl_at( |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
111 | struct cx_list_s const *list, |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
112 | size_t index |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
113 | ) { |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
114 | 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
|
115 | return ptr == NULL ? NULL : *ptr; |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
116 | } |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
117 | |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
118 | static size_t cx_pl_find( |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
119 | struct cx_list_s const *list, |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
120 | void const *elem |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
121 | ) { |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
122 | cx_pl_hack_cmpfunc(list); |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
123 | size_t ret = list->climpl->find(list, &elem); |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
124 | cx_pl_unhack_cmpfunc(list); |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
125 | return ret; |
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 | |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
128 | 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
|
129 | cx_pl_hack_cmpfunc(list); |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
130 | list->climpl->sort(list); |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
131 | cx_pl_unhack_cmpfunc(list); |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
132 | } |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
133 | |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
134 | static int cx_pl_compare( |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
135 | struct cx_list_s const *list, |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
136 | struct cx_list_s const *other |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
137 | ) { |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
138 | cx_pl_hack_cmpfunc(list); |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
139 | 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
|
140 | cx_pl_unhack_cmpfunc(list); |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
141 | return ret; |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
142 | } |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
143 | |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
144 | 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
|
145 | list->climpl->reverse(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 void *cx_pl_iter_current(void const *it) { |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
149 | struct cx_iterator_s const *iter = it; |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
150 | void **ptr = iter->base.current_impl(it); |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
151 | return ptr == NULL ? NULL : *ptr; |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
152 | } |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
153 | |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
154 | static struct cx_iterator_s cx_pl_iterator( |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
155 | struct cx_list_s const *list, |
655
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
156 | size_t index, |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
157 | bool backwards |
641
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
158 | ) { |
655
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
159 | struct cx_iterator_s iter = list->climpl->iterator(list, index, backwards); |
641
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
160 | iter.base.current_impl = iter.base.current; |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
161 | iter.base.current = cx_pl_iter_current; |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
162 | return iter; |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
163 | } |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
164 | |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
165 | 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
|
166 | cx_pl_destructor, |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
167 | cx_pl_insert_element, |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
168 | cx_pl_insert_array, |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
169 | cx_pl_insert_iter, |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
170 | cx_pl_remove, |
664
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
171 | cx_pl_clear, |
647
2e6e9d9f2159
implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents:
641
diff
changeset
|
172 | cx_pl_swap, |
641
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
173 | cx_pl_at, |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
174 | cx_pl_find, |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
175 | cx_pl_sort, |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
176 | cx_pl_compare, |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
177 | cx_pl_reverse, |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
178 | cx_pl_iterator, |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
179 | }; |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
180 | |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
181 | void cxListStoreObjects(CxList *list) { |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
182 | if (list->climpl != NULL) { |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
183 | list->cl = list->climpl; |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
184 | list->climpl = NULL; |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
185 | } |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
186 | } |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
187 | |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
188 | void cxListStorePointers(CxList *list) { |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
189 | list->itemsize = sizeof(void *); |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
190 | list->climpl = list->cl; |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
191 | list->cl = &cx_pointer_list_class; |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
192 | } |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
193 | |
666
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
194 | bool cxListIsStoringPointers(CxList const *list) { |
641
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
195 | return list->climpl != NULL; |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
196 | } |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
197 | |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
198 | // </editor-fold> |
d402fead3386
add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents:
640
diff
changeset
|
199 | |
664
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
200 | void cx_list_invoke_destructor( |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
201 | CxList const *list, |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
202 | void *elem |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
203 | ) { |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
204 | switch (list->content_destructor_type) { |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
205 | case CX_DESTRUCTOR_SIMPLE: { |
666
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
206 | cx_list_invoke_simple_destructor(list, elem); |
664
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
207 | break; |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
208 | } |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
209 | case CX_DESTRUCTOR_ADVANCED: { |
666
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
210 | cx_list_invoke_advanced_destructor(list, elem); |
664
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
211 | break; |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
212 | } |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
213 | case CX_DESTRUCTOR_NONE: |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
214 | break; // nothing |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
215 | } |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
216 | } |
af5bf4603a5d
add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents:
655
diff
changeset
|
217 | |
666
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
218 | void cx_list_invoke_simple_destructor( |
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
219 | CxList const *list, |
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
220 | void *elem |
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
221 | ) { |
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
222 | if (cxListIsStoringPointers(list)) { |
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
223 | elem = *((void **) elem); |
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
224 | } |
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
225 | list->simple_destructor(elem); |
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
226 | } |
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
227 | |
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
228 | void cx_list_invoke_advanced_destructor( |
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
229 | CxList const *list, |
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
230 | void *elem |
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
231 | ) { |
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
232 | if (cxListIsStoringPointers(list)) { |
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
233 | elem = *((void **) elem); |
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
234 | } |
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
235 | list->advanced_destructor.func(list->advanced_destructor.data, elem); |
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
236 | } |
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
237 | |
528
4fbfac557df8
#179 improve API for list content destruction
Mike Becker <universe@uap-core.de>
parents:
526
diff
changeset
|
238 | void cxListDestroy(CxList *list) { |
4fbfac557df8
#179 improve API for list content destruction
Mike Becker <universe@uap-core.de>
parents:
526
diff
changeset
|
239 | switch (list->content_destructor_type) { |
4fbfac557df8
#179 improve API for list content destruction
Mike Becker <universe@uap-core.de>
parents:
526
diff
changeset
|
240 | case CX_DESTRUCTOR_SIMPLE: { |
655
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
241 | CxIterator iter = cxListIterator(list); |
528
4fbfac557df8
#179 improve API for list content destruction
Mike Becker <universe@uap-core.de>
parents:
526
diff
changeset
|
242 | cx_foreach(void*, elem, iter) { |
666
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
243 | // already correctly resolved pointer - immediately invoke dtor |
528
4fbfac557df8
#179 improve API for list content destruction
Mike Becker <universe@uap-core.de>
parents:
526
diff
changeset
|
244 | list->simple_destructor(elem); |
4fbfac557df8
#179 improve API for list content destruction
Mike Becker <universe@uap-core.de>
parents:
526
diff
changeset
|
245 | } |
4fbfac557df8
#179 improve API for list content destruction
Mike Becker <universe@uap-core.de>
parents:
526
diff
changeset
|
246 | break; |
503
a89857072ace
add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
247 | } |
528
4fbfac557df8
#179 improve API for list content destruction
Mike Becker <universe@uap-core.de>
parents:
526
diff
changeset
|
248 | case CX_DESTRUCTOR_ADVANCED: { |
655
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
249 | CxIterator iter = cxListIterator(list); |
528
4fbfac557df8
#179 improve API for list content destruction
Mike Becker <universe@uap-core.de>
parents:
526
diff
changeset
|
250 | cx_foreach(void*, elem, iter) { |
666
b5dd654deb3b
add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents:
664
diff
changeset
|
251 | // already correctly resolved pointer - immediately invoke dtor |
528
4fbfac557df8
#179 improve API for list content destruction
Mike Becker <universe@uap-core.de>
parents:
526
diff
changeset
|
252 | list->advanced_destructor.func(list->advanced_destructor.data, elem); |
4fbfac557df8
#179 improve API for list content destruction
Mike Becker <universe@uap-core.de>
parents:
526
diff
changeset
|
253 | } |
4fbfac557df8
#179 improve API for list content destruction
Mike Becker <universe@uap-core.de>
parents:
526
diff
changeset
|
254 | break; |
4fbfac557df8
#179 improve API for list content destruction
Mike Becker <universe@uap-core.de>
parents:
526
diff
changeset
|
255 | } |
4fbfac557df8
#179 improve API for list content destruction
Mike Becker <universe@uap-core.de>
parents:
526
diff
changeset
|
256 | case CX_DESTRUCTOR_NONE: |
4fbfac557df8
#179 improve API for list content destruction
Mike Becker <universe@uap-core.de>
parents:
526
diff
changeset
|
257 | break; // nothing |
503
a89857072ace
add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
258 | } |
528
4fbfac557df8
#179 improve API for list content destruction
Mike Becker <universe@uap-core.de>
parents:
526
diff
changeset
|
259 | |
524 | 260 | list->cl->destructor(list); |
261 | cxFree(list->allocator, list); | |
503
a89857072ace
add new destructor API and apply it to CxList
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
262 | } |
618
1f5a8f6f3015
#219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
263 | |
1f5a8f6f3015
#219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
264 | int cxListCompare( |
1f5a8f6f3015
#219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
265 | CxList const *list, |
1f5a8f6f3015
#219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
266 | CxList const *other |
1f5a8f6f3015
#219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
267 | ) { |
1f5a8f6f3015
#219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
268 | if (list->cl->compare == other->cl->compare) { |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
618
diff
changeset
|
269 | // same compare function, lists are compatible |
618
1f5a8f6f3015
#219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
270 | return list->cl->compare(list, other); |
1f5a8f6f3015
#219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
271 | } else { |
628
1e2be40f0cb5
use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents:
618
diff
changeset
|
272 | // different compare functions, use iterator |
618
1f5a8f6f3015
#219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
273 | if (list->size == other->size) { |
655
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
274 | CxIterator left = cxListIterator(list); |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
275 | CxIterator right = cxListIterator(other); |
618
1f5a8f6f3015
#219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
276 | for (size_t i = 0; i < list->size; i++) { |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
277 | void *leftValue = cxIteratorCurrent(left); |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
278 | void *rightValue = cxIteratorCurrent(right); |
618
1f5a8f6f3015
#219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
279 | int d = list->cmpfunc(leftValue, rightValue); |
1f5a8f6f3015
#219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
280 | if (d != 0) { |
1f5a8f6f3015
#219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
281 | return d; |
1f5a8f6f3015
#219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
282 | } |
630
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
283 | cxIteratorNext(left); |
ac5e7f789048
separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents:
628
diff
changeset
|
284 | cxIteratorNext(right); |
618
1f5a8f6f3015
#219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
285 | } |
1f5a8f6f3015
#219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
286 | return 0; |
1f5a8f6f3015
#219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
287 | } else { |
1f5a8f6f3015
#219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
288 | return list->size < other->size ? -1 : 1; |
1f5a8f6f3015
#219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
289 | } |
1f5a8f6f3015
#219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
290 | } |
1f5a8f6f3015
#219 array list: implement compare
Mike Becker <universe@uap-core.de>
parents:
528
diff
changeset
|
291 | } |
640
55cc3b373c5e
simplify list class - fixes #236
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
292 | |
655
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
293 | CxMutIterator cxListMutIteratorAt( |
640
55cc3b373c5e
simplify list class - fixes #236
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
294 | CxList *list, |
55cc3b373c5e
simplify list class - fixes #236
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
295 | size_t index |
55cc3b373c5e
simplify list class - fixes #236
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
296 | ) { |
655
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
297 | CxIterator it = list->cl->iterator(list, index, false); |
640
55cc3b373c5e
simplify list class - fixes #236
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
298 | it.base.mutating = true; |
55cc3b373c5e
simplify list class - fixes #236
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
299 | |
55cc3b373c5e
simplify list class - fixes #236
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
300 | // we know the iterators share the same memory layout |
55cc3b373c5e
simplify list class - fixes #236
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
301 | CxMutIterator iter; |
55cc3b373c5e
simplify list class - fixes #236
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
302 | memcpy(&iter, &it, sizeof(CxMutIterator)); |
55cc3b373c5e
simplify list class - fixes #236
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
303 | return iter; |
55cc3b373c5e
simplify list class - fixes #236
Mike Becker <universe@uap-core.de>
parents:
630
diff
changeset
|
304 | } |
655
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
305 | |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
306 | CxMutIterator cxListMutBackwardsIteratorAt( |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
307 | CxList *list, |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
308 | size_t index |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
309 | ) { |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
310 | CxIterator it = list->cl->iterator(list, index, true); |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
311 | it.base.mutating = true; |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
312 | |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
313 | // we know the iterators share the same memory layout |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
314 | CxMutIterator iter; |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
315 | memcpy(&iter, &it, sizeof(CxMutIterator)); |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
316 | return iter; |
7340c4255f1f
implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents:
647
diff
changeset
|
317 | } |