Tue, 28 Dec 2021 17:49:52 +0100
fix missing cleanup in test_hl_linked_list_from_array
390 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
453
bb144d08cd44
add some documentation and changes some signatures
Mike Becker <universe@uap-core.de>
parents:
439
diff
changeset
|
28 | /** |
bb144d08cd44
add some documentation and changes some signatures
Mike Becker <universe@uap-core.de>
parents:
439
diff
changeset
|
29 | * \file list.h |
bb144d08cd44
add some documentation and changes some signatures
Mike Becker <universe@uap-core.de>
parents:
439
diff
changeset
|
30 | * \brief Interface for list implementations. |
bb144d08cd44
add some documentation and changes some signatures
Mike Becker <universe@uap-core.de>
parents:
439
diff
changeset
|
31 | * \author Mike Becker |
bb144d08cd44
add some documentation and changes some signatures
Mike Becker <universe@uap-core.de>
parents:
439
diff
changeset
|
32 | * \author Olaf Wintermann |
bb144d08cd44
add some documentation and changes some signatures
Mike Becker <universe@uap-core.de>
parents:
439
diff
changeset
|
33 | * \version 3.0 |
bb144d08cd44
add some documentation and changes some signatures
Mike Becker <universe@uap-core.de>
parents:
439
diff
changeset
|
34 | * \copyright 2-Clause BSD License |
bb144d08cd44
add some documentation and changes some signatures
Mike Becker <universe@uap-core.de>
parents:
439
diff
changeset
|
35 | */ |
390 | 36 | |
37 | #ifndef UCX_LIST_H | |
38 | #define UCX_LIST_H | |
39 | ||
484
9e6900b1cf9d
add common.h include to all other header files
Mike Becker <universe@uap-core.de>
parents:
474
diff
changeset
|
40 | #include "common.h" |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
393
diff
changeset
|
41 | #include "allocator.h" |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
393
diff
changeset
|
42 | |
415 | 43 | #ifdef __cplusplus |
44 | extern "C" { | |
45 | #endif | |
46 | ||
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
47 | /** |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
48 | * A comparator function comparing two list elements. |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
49 | */ |
489
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
50 | typedef int(*CxListComparator)( |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
51 | void const *left, |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
52 | void const *right |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
53 | ); |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
393
diff
changeset
|
54 | |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
55 | /** |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
56 | * Internal type for the list structure - use CxList instead. |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
57 | */ |
435
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
415
diff
changeset
|
58 | typedef struct cx_list_s cx_list_s; |
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
415
diff
changeset
|
59 | |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
60 | /** |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
61 | * The class definition for arbitrary lists. |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
62 | */ |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
393
diff
changeset
|
63 | typedef struct { |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
64 | /** |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
65 | * Member function for adding an element. |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
66 | */ |
489
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
67 | int (*add)( |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
68 | cx_list_s *list, |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
69 | void const *elem |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
70 | ); |
435
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
415
diff
changeset
|
71 | |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
72 | /** |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
73 | * Member function for inserting an element. |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
74 | */ |
489
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
75 | int (*insert)( |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
76 | cx_list_s *list, |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
77 | size_t index, |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
78 | void const *elem |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
79 | ); |
435
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
415
diff
changeset
|
80 | |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
81 | /** |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
82 | * Member function for removing an element. |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
83 | */ |
489
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
84 | int (*remove)( |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
85 | cx_list_s *list, |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
86 | size_t index |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
87 | ); |
435
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
415
diff
changeset
|
88 | |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
89 | /** |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
90 | * Member function for element lookup. |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
91 | */ |
489
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
92 | void *(*at)( |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
93 | cx_list_s const *list, |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
94 | size_t index |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
95 | ); |
439
9a5adedd6de6
add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents:
438
diff
changeset
|
96 | |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
97 | /** |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
98 | * Member function for finding an element. |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
99 | */ |
488
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
100 | size_t (*find)( |
489
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
101 | cx_list_s const *list, |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
102 | void const *elem |
488
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
103 | ); |
435
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
415
diff
changeset
|
104 | |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
105 | /** |
469
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
106 | * Member function for sorting the list in place. |
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
107 | */ |
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
108 | void (*sort)(cx_list_s *list); |
488
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
109 | |
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
110 | /** |
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
111 | * Member function for comparing this list to another list of the same type. |
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
112 | */ |
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
113 | int (*compare)( |
489
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
114 | cx_list_s const *list, |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
115 | cx_list_s const *other |
488
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
116 | ); |
490 | 117 | |
118 | /** | |
119 | * Member function for reversing the order of the items. | |
120 | */ | |
121 | void (*reverse)(cx_list_s *list); | |
435
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
415
diff
changeset
|
122 | } cx_list_class; |
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
415
diff
changeset
|
123 | |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
124 | /** |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
125 | * Structure for holding the base data of a list. |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
126 | */ |
435
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
415
diff
changeset
|
127 | struct cx_list_s { |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
128 | /** |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
129 | * The list class definition. |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
130 | */ |
435
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
415
diff
changeset
|
131 | cx_list_class *cl; |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
132 | /** |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
133 | * The allocator to use. |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
134 | */ |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
393
diff
changeset
|
135 | CxAllocator allocator; |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
136 | /** |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
137 | * The comparator function for the elements. |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
138 | */ |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
393
diff
changeset
|
139 | CxListComparator cmpfunc; |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
140 | /** |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
141 | * The size of each element (payload only). |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
142 | */ |
401
e6a8f7fb0c45
copy list items when they are added to the list
Mike Becker <universe@uap-core.de>
parents:
398
diff
changeset
|
143 | size_t itemsize; |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
144 | /** |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
145 | * The size of the list (number of currently stored elements). |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
146 | */ |
401
e6a8f7fb0c45
copy list items when they are added to the list
Mike Becker <universe@uap-core.de>
parents:
398
diff
changeset
|
147 | size_t size; |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
148 | /** |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
149 | * The capacity of the list (maximum number of elements). |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
150 | */ |
401
e6a8f7fb0c45
copy list items when they are added to the list
Mike Becker <universe@uap-core.de>
parents:
398
diff
changeset
|
151 | size_t capacity; |
435
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
415
diff
changeset
|
152 | }; |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
393
diff
changeset
|
153 | |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
154 | /** |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
155 | * Common type for all list implementations. |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
156 | */ |
412
af766caea48d
removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate
Mike Becker <universe@uap-core.de>
parents:
406
diff
changeset
|
157 | typedef cx_list_s *CxList; |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
393
diff
changeset
|
158 | |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
159 | /** |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
160 | * Adds an item to the end of the list. |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
161 | * |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
162 | * \remark It is implementation defined whether |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
163 | * the contents of the element are copied or the |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
164 | * pointer is added to the list. In the latter case |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
165 | * the \c itemsize of the list SHALL be \c sizeof(void*). |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
166 | * |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
167 | * @param list the list |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
168 | * @param elem a pointer to the element to add |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
169 | * @return zero on success, non-zero on memory allocation failure |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
170 | */ |
489
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
171 | static inline int cxListAdd( |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
172 | CxList list, |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
173 | void const *elem |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
174 | ) { |
469
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
175 | return list->cl->add(list, elem); |
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
176 | } |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
393
diff
changeset
|
177 | |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
178 | /** |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
179 | * Inserts an item at the specified index. |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
180 | * |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
181 | * If \p index equals the list \c size, this is effectively cxListAdd(). |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
182 | * |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
183 | * \remark It is implementation defined whether |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
184 | * the contents of the element are copied or the |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
185 | * pointer is added to the list. In the latter case |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
186 | * the \c itemsize of the list SHALL be \c sizeof(void*). |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
187 | * |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
188 | * @param list the list |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
189 | * @param index the index the element shall have |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
190 | * @param elem a pointer to the element to add |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
191 | * @return zero on success, non-zero on memory allocation failure |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
192 | * or when the index is out of bounds |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
193 | */ |
489
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
194 | static inline int cxListInsert( |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
195 | CxList list, |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
196 | size_t index, |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
197 | void const *elem |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
198 | ) { |
469
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
199 | return list->cl->insert(list, index, elem); |
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
200 | } |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
393
diff
changeset
|
201 | |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
202 | /** |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
203 | * Removes the element at the specified index. |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
204 | * @param list the list |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
205 | * @param index the index of the element |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
206 | * @return zero on success, non-zero if the index is out of bounds |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
207 | */ |
489
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
208 | static inline int cxListRemove( |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
209 | CxList list, |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
210 | size_t index |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
211 | ) { |
469
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
212 | return list->cl->remove(list, index); |
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
213 | } |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
393
diff
changeset
|
214 | |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
215 | /** |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
216 | * Returns a pointer to the element at the specified index. |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
217 | * |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
218 | * @param list the list |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
219 | * @param index the index of the element |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
220 | * @return a pointer to the element or \c NULL if the index is out of bounds |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
221 | */ |
489
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
222 | static inline void *cxListAt( |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
223 | CxList list, |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
224 | size_t index |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
225 | ) { |
469
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
226 | return list->cl->at(list, index); |
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
227 | } |
439
9a5adedd6de6
add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents:
438
diff
changeset
|
228 | |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
229 | /** |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
230 | * Returns the index of the first element that equals \p elem. |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
231 | * |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
232 | * Determining equality is performed by the list's comparator function. |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
233 | * |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
234 | * @param list the list |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
235 | * @param elem the element to find |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
236 | * @return the index of the element or \c (size+1) if the element is not found |
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
237 | */ |
489
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
238 | static inline size_t cxListFind( |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
239 | CxList list, |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
240 | void const *elem |
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
488
diff
changeset
|
241 | ) { |
469
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
242 | return list->cl->find(list, elem); |
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
243 | } |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
393
diff
changeset
|
244 | |
464
7fafc95968fc
add documentation for list.h
Mike Becker <universe@uap-core.de>
parents:
460
diff
changeset
|
245 | /** |
469
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
246 | * Sorts the list in place. |
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
247 | * |
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
248 | * \remark The underlying sort algorithm is implementation defined. |
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
249 | * |
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
250 | * @param list the list |
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
251 | */ |
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
252 | static inline void cxListSort(CxList list) { |
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
253 | list->cl->sort(list); |
0458bff0b1cd
add high level list sort and inlines method invocation functions
Mike Becker <universe@uap-core.de>
parents:
464
diff
changeset
|
254 | } |
404 | 255 | |
488
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
256 | /** |
490 | 257 | * Reverses the order of the items. |
258 | * | |
259 | * @param list the list | |
260 | */ | |
261 | static inline void cxListReverse(CxList list) { | |
262 | list->cl->reverse(list); | |
263 | } | |
264 | ||
265 | /** | |
488
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
266 | * Compares a list to another list of the same type. |
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
267 | * |
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
268 | * First, the list sizes are compared. If they match, the lists are compared element-wise. |
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
269 | * |
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
270 | * @param list the list |
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
271 | * @param other the list to compare to |
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
272 | * @return zero, if both lists are equal element wise, negative if the first list is smaller, zero if the first list is larger |
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
273 | */ |
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
274 | static inline int cxListCompare( |
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
275 | CxList list, |
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
276 | CxList other |
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
277 | ) { |
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
278 | return list->cl->compare(list, other); |
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
279 | } |
9138acaa494b
add cxLinkedListFromArray() and cxListCompare()
Mike Becker <universe@uap-core.de>
parents:
484
diff
changeset
|
280 | |
415 | 281 | #ifdef __cplusplus |
282 | } /* extern "C" */ | |
283 | #endif | |
284 | ||
393 | 285 | #endif /* UCX_LIST_H */ |