ucx/list.h

changeset 128
b79b1ce438dd
parent 127
5418bda21896
child 129
61edec666928
equal deleted inserted replaced
127:5418bda21896 128:b79b1ce438dd
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 /** 28 /**
29 * Double linked list implementation. 29 * Doubly linked list implementation.
30 * 30 *
31 * @file list.h 31 * @file list.h
32 * @author Mike Becker 32 * @author Mike Becker
33 * @author Olaf Wintermann 33 * @author Olaf Wintermann
34 */ 34 */
79 * the first element. 79 * the first element.
80 */ 80 */
81 UcxList *prev; 81 UcxList *prev;
82 }; 82 };
83 83
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 */
84 UcxList *ucx_list_clone(UcxList *list, copy_func cpyfnc, void* data); 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 */
85 UcxList *ucx_list_clone_a(UcxAllocator *allocator, UcxList *list, 115 UcxList *ucx_list_clone_a(UcxAllocator *allocator, UcxList *list,
86 copy_func cpyfnc, void* data); 116 copy_func cpyfnc, void* data);
87 117
88 /** 118 /**
89 * Compares two UCX lists element-wise by using a compare function. 119 * Compares two UCX lists element-wise by using a compare function.
113 * otherwise referenced or a memory leak will occur. 143 * otherwise referenced or a memory leak will occur.
114 * 144 *
115 * <b>Caution:</b> the argument <b>MUST</b> denote an entire list (i.e. a call 145 * <b>Caution:</b> the argument <b>MUST</b> denote an entire list (i.e. a call
116 * to ucx_list_first() on the argument must return the argument itself) 146 * to ucx_list_first() on the argument must return the argument itself)
117 * 147 *
118 * @param list The list to free. 148 * @param list the list to free
119 */ 149 */
120 void ucx_list_free(UcxList *list); 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 */
121 void ucx_list_free_a(UcxAllocator *allocator, UcxList *list); 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 */
122 UcxList *ucx_list_append(UcxList *list, void *data); 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 */
123 UcxList *ucx_list_append_a(UcxAllocator *allocator, UcxList *list, void *data); 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 */
124 UcxList *ucx_list_prepend(UcxList *list, void *data); 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 */
125 UcxList *ucx_list_prepend_a(UcxAllocator *allocator, UcxList *list, void *data); 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 */
126 UcxList *ucx_list_concat(UcxList *list1, UcxList *list2); 230 UcxList *ucx_list_concat(UcxList *list1, UcxList *list2);
127 /** 231 /**
128 * Returns the first element of a list. 232 * Returns the first element of a list.
129 * 233 *
130 * If the argument is the list pointer, it is directly returned. Otherwise 234 * If the argument is the list pointer, it is directly returned. Otherwise
144 * 248 *
145 * @param elem one element of the list 249 * @param elem one element of the list
146 * @return the last element of the list, the specified element is a member of 250 * @return the last element of the list, the specified element is a member of
147 */ 251 */
148 UcxList *ucx_list_last(const UcxList *elem); 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 */
149 UcxList *ucx_list_get(const UcxList *list, int index); 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 */
150 ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem); 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 */
151 size_t ucx_list_size(const UcxList *list); 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 */
152 ssize_t ucx_list_find(UcxList *list, void *elem, cmp_func cmpfnc, void *data); 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 */
153 int ucx_list_contains(UcxList *list, void *elem, cmp_func cmpfnc, void *data); 308 int ucx_list_contains(UcxList *list, void *elem, cmp_func cmpfnc, void *data);
154 309
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 */
155 UcxList *ucx_list_sort(UcxList *list, cmp_func cmpfnc, void *data); 324 UcxList *ucx_list_sort(UcxList *list, cmp_func cmpfnc, void *data);
156 325
157 /** 326 /**
158 * Removes an element from the list. 327 * Removes an element from the list.
159 * 328 *
165 * @param element the element to removed 334 * @param element the element to removed
166 * @return returns the updated list pointer or <code>NULL</code>, if the list 335 * @return returns the updated list pointer or <code>NULL</code>, if the list
167 * is now empty 336 * is now empty
168 */ 337 */
169 UcxList *ucx_list_remove(UcxList *list, UcxList *element); 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 */
170 UcxList *ucx_list_remove_a(UcxAllocator *allocator, UcxList *list, 350 UcxList *ucx_list_remove_a(UcxAllocator *allocator, UcxList *list,
171 UcxList *element); 351 UcxList *element);
172 352
173 #ifdef __cplusplus 353 #ifdef __cplusplus
174 } 354 }

mercurial