ucx/list.h

changeset 152
62921b370c60
parent 124
80609f9675f1
equal deleted inserted replaced
151:11f3bb408051 152:62921b370c60
1 /* 1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * 3 *
4 * Copyright 2015 Olaf Wintermann. All rights reserved. 4 * Copyright 2016 Olaf Wintermann. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
44 #endif 44 #endif
45 45
46 /** 46 /**
47 * Loop statement for UCX lists. 47 * Loop statement for UCX lists.
48 * 48 *
49 * The first argument is a pointer to the list. In most cases this will be the 49 * The first argument is the name of the iteration variable. The scope of
50 * this variable is limited to the <code>UCX_FOREACH</code> statement.
51 *
52 * The second argument is a pointer to the list. In most cases this will be the
50 * pointer to the first element of the list, but it may also be an arbitrary 53 * pointer to the first element of the list, but it may also be an arbitrary
51 * element of the list. The iteration will then start with that element. 54 * element of the list. The iteration will then start with that element.
52 *
53 * The second argument is the name of the iteration variable. The scope of
54 * this variable is limited to the <code>UCX_FOREACH</code> statement.
55 * 55 *
56 * @param list The first element of the list 56 * @param list The first element of the list
57 * @param elem The variable name of the element 57 * @param elem The variable name of the element
58 */ 58 */
59 #define UCX_FOREACH(elem,list) \ 59 #define UCX_FOREACH(elem,list) \
100 * @return a pointer to the copy 100 * @return a pointer to the copy
101 */ 101 */
102 UcxList *ucx_list_clone(UcxList *list, copy_func cpyfnc, void* data); 102 UcxList *ucx_list_clone(UcxList *list, copy_func cpyfnc, void* data);
103 103
104 /** 104 /**
105 * Creates an element-wise copy of a list using an UcxAllocator. 105 * Creates an element-wise copy of a list using a UcxAllocator.
106 * 106 *
107 * See ucx_list_clone() for details. 107 * See ucx_list_clone() for details.
108 * 108 *
109 * Keep in mind, that you might want to pass the allocator as an (part of the) 109 * You might want to pass the allocator via the <code>data</code> parameter,
110 * argument for the <code>data</code> parameter, if you want the copy_func() to 110 * to access it within the copy function for making deep copies.
111 * make use of the allocator.
112 * 111 *
113 * @param allocator the allocator to use 112 * @param allocator the allocator to use
114 * @param list the list to copy 113 * @param list the list to copy
115 * @param cpyfnc a pointer to the function that shall copy an element (may be 114 * @param cpyfnc a pointer to the function that shall copy an element (may be
116 * <code>NULL</code>) 115 * <code>NULL</code>)
144 143
145 /** 144 /**
146 * Destroys the entire list. 145 * Destroys the entire list.
147 * 146 *
148 * The members of the list are not automatically freed, so ensure they are 147 * The members of the list are not automatically freed, so ensure they are
149 * otherwise referenced or a memory leak will occur. 148 * otherwise referenced or destroyed by ucx_list_free_contents().
149 * Otherwise, a memory leak is likely to occur.
150 * 150 *
151 * <b>Caution:</b> the argument <b>MUST</b> denote an entire list (i.e. a call 151 * <b>Caution:</b> the argument <b>MUST</b> denote an entire list (i.e. a call
152 * to ucx_list_first() on the argument must return the argument itself) 152 * to ucx_list_first() on the argument must return the argument itself)
153 * 153 *
154 * @param list the list to free 154 * @param list the list to free
155 * @see ucx_list_free_contents()
155 */ 156 */
156 void ucx_list_free(UcxList *list); 157 void ucx_list_free(UcxList *list);
157 158
158 /** 159 /**
159 * Destroys the entire list using an UcxAllocator. 160 * Destroys the entire list using a UcxAllocator.
160 * 161 *
161 * See ucx_list_free() for details. 162 * See ucx_list_free() for details.
162 * 163 *
163 * @param allocator the allocator to use 164 * @param allocator the allocator to use
164 * @param list the list to free 165 * @param list the list to free
165 * @see ucx_list_free() 166 * @see ucx_list_free()
166 */ 167 */
167 void ucx_list_free_a(UcxAllocator *allocator, UcxList *list); 168 void ucx_list_free_a(UcxAllocator *allocator, UcxList *list);
168 169
169 /** 170 /**
171 * Destroys the contents of the specified list by calling the specified
172 * destructor on each of them.
173 *
174 * Note, that the contents are not usable afterwards and the list should be
175 * destroyed with ucx_list_free().
176 *
177 * @param list the list for which the contents shall be freed
178 * @param destr the destructor function (e.g. stdlib free())
179 * @see ucx_list_free()
180 */
181 void ucx_list_free_content(UcxList* list, ucx_destructor destr);
182
183
184 /**
170 * Inserts an element at the end of the list. 185 * Inserts an element at the end of the list.
171 * 186 *
172 * This is generally an O(n) operation, as the end of the list is retrieved with 187 * This is generally an O(n) operation, as the end of the list is retrieved with
173 * ucx_list_last(). 188 * ucx_list_last().
174 * 189 *
179 * the newly created list otherwise 194 * the newly created list otherwise
180 */ 195 */
181 UcxList *ucx_list_append(UcxList *list, void *data); 196 UcxList *ucx_list_append(UcxList *list, void *data);
182 197
183 /** 198 /**
184 * Inserts an element at the end of the list using an UcxAllocator. 199 * Inserts an element at the end of the list using a UcxAllocator.
185 * 200 *
186 * See ucx_list_append() for details. 201 * See ucx_list_append() for details.
187 * 202 *
188 * @param allocator the allocator to use 203 * @param allocator the allocator to use
189 * @param list the list where to append the data, or <code>NULL</code> to 204 * @param list the list where to append the data, or <code>NULL</code> to
192 * @return <code>list</code>, if it is not <code>NULL</code> or a pointer to 207 * @return <code>list</code>, if it is not <code>NULL</code> or a pointer to
193 * the newly created list otherwise 208 * the newly created list otherwise
194 * @see ucx_list_append() 209 * @see ucx_list_append()
195 */ 210 */
196 UcxList *ucx_list_append_a(UcxAllocator *allocator, UcxList *list, void *data); 211 UcxList *ucx_list_append_a(UcxAllocator *allocator, UcxList *list, void *data);
212
213 /**
214 * Inserts an element at the end of the list, if it is not present in the list.
215 *
216 *
217 * @param list the list where to append the data, or <code>NULL</code> to
218 * create a new list
219 * @param data the data to insert
220 * @param cmpfnc the compare function
221 * @param cmpdata additional data for the compare function
222 * @return <code>list</code>, if it is not <code>NULL</code> or a pointer to
223 * the newly created list otherwise
224 * @see ucx_list_append()
225 */
226 UcxList *ucx_list_append_once(UcxList *list, void *data,
227 cmp_func cmpfnc, void *cmpdata);
228
229 /**
230 * Inserts an element at the end of the list, if it is not present in the list,
231 * using a UcxAllocator.
232 *
233 * See ucx_list_append() for details.
234 *
235 * @param allocator the allocator to use
236 * @param list the list where to append the data, or <code>NULL</code> to
237 * create a new list
238 * @param data the data to insert
239 * @param cmpfnc the compare function
240 * @param cmpdata additional data for the compare function
241 * @return <code>list</code>, if it is not <code>NULL</code> or a pointer to
242 * the newly created list otherwise
243 * @see ucx_list_append_a()
244 */
245 UcxList *ucx_list_append_once_a(UcxAllocator *allocator,
246 UcxList *list, void *data, cmp_func cmpfnc, void *cmpdata);
197 247
198 /** 248 /**
199 * Inserts an element at the beginning of the list. 249 * Inserts an element at the beginning of the list.
200 * 250 *
201 * You <i>should</i> overwrite the old list pointer by calling 251 * You <i>should</i> overwrite the old list pointer by calling
210 * @return a pointer to the new list head 260 * @return a pointer to the new list head
211 */ 261 */
212 UcxList *ucx_list_prepend(UcxList *list, void *data); 262 UcxList *ucx_list_prepend(UcxList *list, void *data);
213 263
214 /** 264 /**
215 * Inserts an element at the beginning of the list using an UcxAllocator. 265 * Inserts an element at the beginning of the list using a UcxAllocator.
216 * 266 *
217 * See ucx_list_prepend() for details. 267 * See ucx_list_prepend() for details.
218 * 268 *
219 * @param allocator the allocator to use 269 * @param allocator the allocator to use
220 * @param list the list where to insert the data or <code>NULL</code> to create 270 * @param list the list where to insert the data or <code>NULL</code> to create
325 * @see ucx_list_find() 375 * @see ucx_list_find()
326 */ 376 */
327 int ucx_list_contains(UcxList *list, void *elem, cmp_func cmpfnc, void *data); 377 int ucx_list_contains(UcxList *list, void *elem, cmp_func cmpfnc, void *data);
328 378
329 /** 379 /**
330 * Sorts an UcxList with natural merge sort. 380 * Sorts a UcxList with natural merge sort.
331 * 381 *
332 * This function uses O(n) additional temporary memory for merge operations 382 * This function uses O(n) additional temporary memory for merge operations
333 * that is automatically freed after each merge. 383 * that is automatically freed after each merge.
334 * 384 *
335 * As the head of the list might change, you <b>MUST</b> call this function 385 * As the head of the list might change, you <b>MUST</b> call this function
355 * is now empty 405 * is now empty
356 */ 406 */
357 UcxList *ucx_list_remove(UcxList *list, UcxList *element); 407 UcxList *ucx_list_remove(UcxList *list, UcxList *element);
358 408
359 /** 409 /**
360 * Removes an element from the list using an UcxAllocator. 410 * Removes an element from the list using a UcxAllocator.
361 * 411 *
362 * See ucx_list_remove() for details. 412 * See ucx_list_remove() for details.
363 * 413 *
364 * @param allocator the allocator to use 414 * @param allocator the allocator to use
365 * @param list the list from which the element shall be removed 415 * @param list the list from which the element shall be removed

mercurial