Tue, 23 Jul 2013 14:43:45 +0200
finished documentation of UcxList
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 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 */
28 /**
29 * Doubly linked list implementation.
30 *
31 * @file list.h
32 * @author Mike Becker
33 * @author Olaf Wintermann
34 */
36 #ifndef UCX_LIST_H
37 #define UCX_LIST_H
39 #include "ucx.h"
40 #include "allocator.h"
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
46 /**
47 * Loop statement for UCX lists.
48 *
49 * The first 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
51 * 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 *
56 * @param list The first element of the list
57 * @param elem The variable name of the element
58 */
59 #define UCX_FOREACH(elem,list) \
60 for (UcxList* elem = list ; elem != NULL ; elem = elem->next)
62 /**
63 * UCX list type
64 * @see UcxList
65 */
66 typedef struct UcxList UcxList;
67 struct UcxList {
68 /**
69 * List element payload.
70 */
71 void *data;
72 /**
73 * Pointer to the next list element or <code>NULL</code>, if this is the
74 * last element.
75 */
76 UcxList *next;
77 /**
78 * Pointer to the previous list element or <code>NULL</code>, if this is
79 * the first element.
80 */
81 UcxList *prev;
82 };
84 /**
85 * Creates an element-wise copy of a list.
86 *
87 * This function clones the specified list by creating new list elements and
88 * copying the data with the specified copy_func(). If no copy_func() is
89 * specified, a shallow copy is created and the new list will reference the
90 * same data as the source list.
91 *
92 * @param list the list to copy
93 * @param cpyfnc a pointer to the function that shall copy an element (may be
94 * <code>NULL</code>)
95 * @param data additional data for the copy_func()
96 * @return a pointer to the copy
97 */
98 UcxList *ucx_list_clone(UcxList *list, copy_func cpyfnc, void* data);
99 /**
100 * Creates an element-wise copy of a list using an UcxAllocator.
101 *
102 * See ucx_list_clone() for details.
103 *
104 * Keep in mind, that you might want to pass the allocator as an (part of the)
105 * argument for the <code>data</code> parameter, if you want the copy_func() to
106 * make use of the allocator.
107 *
108 * @param list the list to copy
109 * @param cpyfnc a pointer to the function that shall copy an element (may be
110 * <code>NULL</code>)
111 * @param data additional data for the copy_func()
112 * @return a pointer to the copy
113 * @see ucx_list_clone()
114 */
115 UcxList *ucx_list_clone_a(UcxAllocator *allocator, UcxList *list,
116 copy_func cpyfnc, void* data);
118 /**
119 * Compares two UCX lists element-wise by using a compare function.
120 *
121 * Each element of the two specified lists are compared by using the specified
122 * compare function and the additional data. The type and content of this
123 * additional data depends on the cmp_func() used.
124 *
125 * If the list pointers denote elements within a list, the lists are compared
126 * starting with the denoted elements. Thus any previous elements are not taken
127 * into account. This might be useful to check, if certain list tails match
128 * each other.
129 *
130 * @param list1 the first list
131 * @param list2 the second list
132 * @param cmpfnc the compare function
133 * @param data additional data for the compare function
134 * @return 1, if and only if the two lists equal element-wise, 0 otherwise
135 */
136 int ucx_list_equals(const UcxList *list1, const UcxList *list2,
137 cmp_func cmpfnc, void* data);
139 /**
140 * Destroys the entire list.
141 *
142 * The members of the list are not automatically freed, so ensure they are
143 * otherwise referenced or a memory leak will occur.
144 *
145 * <b>Caution:</b> the argument <b>MUST</b> denote an entire list (i.e. a call
146 * to ucx_list_first() on the argument must return the argument itself)
147 *
148 * @param list the list to free
149 */
150 void ucx_list_free(UcxList *list);
151 /**
152 * Destroys the entire list using an UcxAllocator.
153 *
154 * See ucx_list_free() for details.
155 *
156 * @param allocator the allocator to use
157 * @param list the list to free
158 * @see ucx_list_free()
159 */
160 void ucx_list_free_a(UcxAllocator *allocator, UcxList *list);
161 /**
162 * Inserts an element at the end of the list.
163 *
164 * This is generally an O(n) operation, as the end of the list is seeked with
165 * ucx_list_last().
166 *
167 * @param list the list where to append the data, or <code>NULL</code> to
168 * create a new list
169 * @param data the data to insert
170 * @return <code>list</code>, if it is not <code>NULL</code> or a pointer to
171 * the newly created list otherwise
172 */
173 UcxList *ucx_list_append(UcxList *list, void *data);
174 /**
175 * Inserts an element at the end of the list using an UcxAllocator.
176 *
177 * See ucx_list_append() for details.
178 *
179 * @param allocator the allocator to use
180 * @param list the list where to append the data, or <code>NULL</code> to
181 * create a new list
182 * @param data the data to insert
183 * @return <code>list</code>, if it is not <code>NULL</code> or a pointer to
184 * the newly created list otherwise
185 * @see ucx_list_append()
186 */
187 UcxList *ucx_list_append_a(UcxAllocator *allocator, UcxList *list, void *data);
188 /**
189 * Inserts an element at the beginning of the list.
190 *
191 * You <i>should</i> overwrite the old list pointer by calling
192 * <code>mylist = ucx_list_prepend(mylist, mydata);</code>. However, you may
193 * also perform successive calls of ucx_list_prepend() on the same list pointer,
194 * as this function always searchs for the head of the list with
195 * ucx_list_first().
196 *
197 * @param list the list where to insert the data or <code>NULL</code> to create
198 * a new list
199 * @param data the data to insert
200 * @return a pointer to the new list head
201 */
202 UcxList *ucx_list_prepend(UcxList *list, void *data);
203 /**
204 * Inserts an element at the beginning of the list using an UcxAllocator.
205 *
206 * See ucx_list_prepend() for details.
207 *
208 * @param allocator the allocator to use
209 * @param list the list where to insert the data or <code>NULL</code> to create
210 * a new list
211 * @param data the data to insert
212 * @return a pointer to the new list head
213 * @see ucx_list_prepend()
214 */
215 UcxList *ucx_list_prepend_a(UcxAllocator *allocator, UcxList *list, void *data);
216 /**
217 * Concatenates two lists.
218 *
219 * Either of the two arguments may be <code>NULL</code>.
220 *
221 * This function modifies the references to the next/previous element of
222 * the last/first element of <code>list1</code>/<code>
223 * list2</code>.
224 *
225 * @param list1 first list
226 * @param list2 second list
227 * @return if <code>list1</code> is <code>NULL</code>, <code>list2</code> is
228 * returned, otherwise <code>list1</code> is returned
229 */
230 UcxList *ucx_list_concat(UcxList *list1, UcxList *list2);
231 /**
232 * Returns the first element of a list.
233 *
234 * If the argument is the list pointer, it is directly returned. Otherwise
235 * this function traverses to the first element of the list and returns the
236 * list pointer.
237 *
238 * @param elem one element of the list
239 * @return the first element of the list, the specified element is a member of
240 */
241 UcxList *ucx_list_first(const UcxList *elem);
242 /**
243 * Returns the last element of a list.
244 *
245 * If the argument has no successor, it is the last element and therefore
246 * directly returned. Otherwise this function traverses to the last element of
247 * the list and returns it.
248 *
249 * @param elem one element of the list
250 * @return the last element of the list, the specified element is a member of
251 */
252 UcxList *ucx_list_last(const UcxList *elem);
253 /**
254 * Returns the list element at the specified index.
255 *
256 * @param list the list to retrieve the element from
257 * @param index index of the element to return
258 * @return the element at the specified index or <code>NULL</code>, if the
259 * index is greater than the list size
260 */
261 UcxList *ucx_list_get(const UcxList *list, int index);
262 /**
263 * Returns the index of an element.
264 *
265 * @param list the list where to search for the element
266 * @param elem the element to find
267 * @return the index of the element or -1 if the list does not contain the
268 * element
269 */
270 ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem);
271 /**
272 * Returns the element count of the list.
273 *
274 * @param list the list whose elements are counted
275 * @return the element count
276 */
277 size_t ucx_list_size(const UcxList *list);
278 /**
279 * Returns the index of an element containing the specified data.
280 *
281 * This function uses a cmp_func() to compare the data of each list element
282 * with the specified data. If no cmp_func is provided, the pointers are
283 * compared.
284 *
285 * If the list contains the data more than once, the index of the first
286 * occurrence is returned.
287 *
288 * @param list the list where to search for the data
289 * @param elem the element data
290 * @param cmpfnc the compare function
291 * @param data additional data for the compare function
292 * @return the index of the element containing the specified data or -1 if the
293 * data is not found in this list
294 */
295 ssize_t ucx_list_find(UcxList *list, void *elem, cmp_func cmpfnc, void *data);
296 /**
297 * Checks, if a list contains a specific element.
298 *
299 * An element is found, if ucx_list_find() returns a value greater than -1.
300 *
301 * @param list the list where to search for the data
302 * @param elem the element data
303 * @param cmpfnc the compare function
304 * @param data additional data for the compare function
305 * @return 1, if and only if the list contains the specified element data
306 * @see ucx_list_find()
307 */
308 int ucx_list_contains(UcxList *list, void *elem, cmp_func cmpfnc, void *data);
310 /**
311 * Sorts an UcxList with natural merge sort.
312 *
313 * This function uses O(n) additional temporary memory for merge operations
314 * that is automatically freed after each merge.
315 *
316 * As the head of the list might change, you <b>MUST</b> call this function
317 * as follows: <code>mylist = ucx_list_sort(mylist, mycmpfnc, mydata);</code>.
318 *
319 * @param list the list to sort
320 * @param cmpfnc the function that shall be used to compare the element data
321 * @param data additional data for the cmp_func()
322 * @return the sorted list
323 */
324 UcxList *ucx_list_sort(UcxList *list, cmp_func cmpfnc, void *data);
326 /**
327 * Removes an element from the list.
328 *
329 * If the first element is removed, the list pointer changes. So it is
330 * <i>highly recommended</i> to <i>always</i> update the pointer by calling
331 * <code>mylist = ucx_list_remove(mylist, myelem);</code>.
332 *
333 * @param list the list from which the element shall be removed
334 * @param element the element to removed
335 * @return returns the updated list pointer or <code>NULL</code>, if the list
336 * is now empty
337 */
338 UcxList *ucx_list_remove(UcxList *list, UcxList *element);
339 /**
340 * Removes an element from the list using an UcxAllocator.
341 *
342 * See ucx_list_remove() for details.
343 *
344 * @param allocator the allocator to use
345 * @param list the list from which the element shall be removed
346 * @param element the element to removed
347 * @return returns the updated list pointer or <code>NULL</code>, if the list
348 * @see ucx_list_remove()
349 */
350 UcxList *ucx_list_remove_a(UcxAllocator *allocator, UcxList *list,
351 UcxList *element);
353 #ifdef __cplusplus
354 }
355 #endif
357 #endif /* UCX_LIST_H */