35 */ |
35 */ |
36 |
36 |
37 #ifndef UCX_LIST_H |
37 #ifndef UCX_LIST_H |
38 #define UCX_LIST_H |
38 #define UCX_LIST_H |
39 |
39 |
40 #include "common.h" |
40 #include "collection.h" |
41 #include "allocator.h" |
|
42 #include "iterator.h" |
|
43 |
41 |
44 #ifdef __cplusplus |
42 #ifdef __cplusplus |
45 extern "C" { |
43 extern "C" { |
46 #endif |
44 #endif |
47 |
45 |
48 #ifndef CX_STORE_POINTERS |
|
49 /** |
|
50 * Special constant used for creating collections that are storing pointers. |
|
51 */ |
|
52 #define CX_STORE_POINTERS 0 |
|
53 #endif |
|
54 |
|
55 /** |
|
56 * A comparator function comparing two list elements. |
|
57 */ |
|
58 typedef int(*CxListComparator)( |
|
59 void const *left, |
|
60 void const *right |
|
61 ); |
|
62 |
|
63 /** |
46 /** |
64 * List class type. |
47 * List class type. |
65 */ |
48 */ |
66 typedef struct cx_list_class_s cx_list_class; |
49 typedef struct cx_list_class_s cx_list_class; |
67 |
50 |
68 /** |
51 /** |
69 * Structure for holding the base data of a list. |
52 * Structure for holding the base data of a list. |
70 */ |
53 */ |
71 struct cx_list_s { |
54 struct cx_list_s { |
|
55 CX_COLLECTION_MEMBERS |
72 /** |
56 /** |
73 * The list class definition. |
57 * The list class definition. |
74 */ |
58 */ |
75 cx_list_class const *cl; |
59 cx_list_class const *cl; |
76 /** |
60 /** |
77 * The actual implementation in case the list class is delegating. |
61 * The actual implementation in case the list class is delegating. |
78 */ |
62 */ |
79 cx_list_class const *climpl; |
63 cx_list_class const *climpl; |
80 /** |
|
81 * The allocator to use. |
|
82 */ |
|
83 CxAllocator const *allocator; |
|
84 /** |
|
85 * The comparator function for the elements. |
|
86 */ |
|
87 CxListComparator cmpfunc; |
|
88 /** |
|
89 * The size of each element (payload only). |
|
90 */ |
|
91 size_t itemsize; |
|
92 /** |
|
93 * The size of the list (number of currently stored elements). |
|
94 */ |
|
95 size_t size; |
|
96 /** |
|
97 * The capacity of the list (maximum number of elements). |
|
98 */ |
|
99 size_t capacity; |
|
100 union { |
|
101 /** |
|
102 * An optional simple destructor for the list contents that admits the free() interface. |
|
103 * |
|
104 * @remark Set content_destructor_type to #CX_DESTRUCTOR_SIMPLE. |
|
105 * |
|
106 * @attention Read the documentation of the particular list implementation |
|
107 * whether this destructor shall only destroy the contents or also free the memory. |
|
108 */ |
|
109 cx_destructor_func simple_destructor; |
|
110 /** |
|
111 * An optional advanced destructor for the list contents providing additional data. |
|
112 * |
|
113 * @remark Set content_destructor_type to #CX_DESTRUCTOR_ADVANCED. |
|
114 * |
|
115 * @attention Read the documentation of the particular list implementation |
|
116 * whether this destructor shall only destroy the contents or also free the memory. |
|
117 */ |
|
118 cx_advanced_destructor advanced_destructor; |
|
119 }; |
|
120 /** |
|
121 * The type of destructor to use. |
|
122 */ |
|
123 enum cx_destructor_type content_destructor_type; |
|
124 }; |
64 }; |
125 |
65 |
126 /** |
66 /** |
127 * The class definition for arbitrary lists. |
67 * The class definition for arbitrary lists. |
128 */ |
68 */ |
232 * Common type for all list implementations. |
172 * Common type for all list implementations. |
233 */ |
173 */ |
234 typedef struct cx_list_s CxList; |
174 typedef struct cx_list_s CxList; |
235 |
175 |
236 /** |
176 /** |
237 * Invokes the configured destructor function for a specific element. |
|
238 * |
|
239 * Usually only used by list implementations. There should be no need |
|
240 * to invoke this function manually. |
|
241 * |
|
242 * @param list the list |
|
243 * @param elem the element |
|
244 */ |
|
245 __attribute__((__nonnull__)) |
|
246 void cx_list_invoke_destructor( |
|
247 struct cx_list_s const *list, |
|
248 void *elem |
|
249 ); |
|
250 |
|
251 /** |
|
252 * Invokes the simple destructor function for a specific element. |
|
253 * |
|
254 * Usually only used by list implementations. There should be no need |
|
255 * to invoke this function manually. |
|
256 * |
|
257 * @param list the list |
|
258 * @param elem the element |
|
259 */ |
|
260 __attribute__((__nonnull__)) |
|
261 void cx_list_invoke_simple_destructor( |
|
262 struct cx_list_s const *list, |
|
263 void *elem |
|
264 ); |
|
265 |
|
266 /** |
|
267 * Invokes the advanced destructor function for a specific element. |
|
268 * |
|
269 * Usually only used by list implementations. There should be no need |
|
270 * to invoke this function manually. |
|
271 * |
|
272 * @param list the list |
|
273 * @param elem the element |
|
274 */ |
|
275 __attribute__((__nonnull__)) |
|
276 void cx_list_invoke_advanced_destructor( |
|
277 struct cx_list_s const *list, |
|
278 void *elem |
|
279 ); |
|
280 |
|
281 /** |
|
282 * Advises the list to store copies of the objects (default mode of operation). |
177 * Advises the list to store copies of the objects (default mode of operation). |
283 * |
178 * |
284 * Retrieving objects from this list will yield pointers to the copies stored |
179 * Retrieving objects from this list will yield pointers to the copies stored |
285 * within this list. |
180 * within this list. |
286 * |
181 * |
307 |
202 |
308 /** |
203 /** |
309 * Returns true, if this list is storing pointers instead of the actual data. |
204 * Returns true, if this list is storing pointers instead of the actual data. |
310 * |
205 * |
311 * @param list |
206 * @param list |
312 * @return |
207 * @return true, if this list is storing pointers |
313 * @see cxListStorePointers() |
208 * @see cxListStorePointers() |
314 */ |
209 */ |
315 __attribute__((__nonnull__)) |
210 __attribute__((__nonnull__)) |
316 bool cxListIsStoringPointers(CxList const *list); |
211 static inline bool cxListIsStoringPointers(CxList const *list) { |
|
212 return list->store_pointer; |
|
213 } |
|
214 |
|
215 /** |
|
216 * Returns the number of elements currently stored in the list. |
|
217 * |
|
218 * @param list the list |
|
219 * @return the number of currently stored elements |
|
220 */ |
|
221 __attribute__((__nonnull__)) |
|
222 static inline size_t cxListSize(CxList const *list) { |
|
223 return list->size; |
|
224 } |
317 |
225 |
318 /** |
226 /** |
319 * Adds an item to the end of the list. |
227 * Adds an item to the end of the list. |
320 * |
228 * |
321 * @param list the list |
229 * @param list the list |