ucx
UAP Common Extensions
list.h
Go to the documentation of this file.
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright 2017 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  */
36 #ifndef UCX_LIST_H
37 #define UCX_LIST_H
38 
39 #include "ucx.h"
40 #include "allocator.h"
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
59 #define UCX_FOREACH(elem,list) \
60  for (UcxList* elem = list ; elem != NULL ; elem = elem->next)
61 
66 typedef struct UcxList UcxList;
67 
71 struct UcxList {
75  void *data;
86 };
87 
102 UcxList *ucx_list_clone(UcxList *list, copy_func cpyfnc, void* data);
103 
120 UcxList *ucx_list_clone_a(UcxAllocator *allocator, UcxList *list,
121  copy_func cpyfnc, void* data);
122 
141 int ucx_list_equals(const UcxList *list1, const UcxList *list2,
142  cmp_func cmpfnc, void* data);
143 
157 void ucx_list_free(UcxList *list);
158 
168 void ucx_list_free_a(UcxAllocator *allocator, UcxList *list);
169 
181 void ucx_list_free_content(UcxList* list, ucx_destructor destr);
182 
183 
196 UcxList *ucx_list_append(UcxList *list, void *data);
197 
211 UcxList *ucx_list_append_a(UcxAllocator *allocator, UcxList *list, void *data);
212 
227  cmp_func cmpfnc, void *cmpdata);
228 
246  UcxList *list, void *data, cmp_func cmpfnc, void *cmpdata);
247 
262 UcxList *ucx_list_prepend(UcxList *list, void *data);
263 
276 UcxList *ucx_list_prepend_a(UcxAllocator *allocator, UcxList *list, void *data);
277 
292 UcxList *ucx_list_concat(UcxList *list1, UcxList *list2);
293 
304 UcxList *ucx_list_first(const UcxList *elem);
305 
316 UcxList *ucx_list_last(const UcxList *elem);
317 
326 UcxList *ucx_list_get(const UcxList *list, size_t index);
327 
336 ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem);
337 
344 size_t ucx_list_size(const UcxList *list);
345 
363 ssize_t ucx_list_find(UcxList *list, void *elem, cmp_func cmpfnc, void *data);
364 
377 int ucx_list_contains(UcxList *list, void *elem, cmp_func cmpfnc, void *data);
378 
393 UcxList *ucx_list_sort(UcxList *list, cmp_func cmpfnc, void *data);
394 
407 UcxList *ucx_list_remove(UcxList *list, UcxList *element);
408 
420 UcxList *ucx_list_remove_a(UcxAllocator *allocator, UcxList *list,
421  UcxList *element);
422 
423 #ifdef __cplusplus
424 }
425 #endif
426 
427 #endif /* UCX_LIST_H */
428 
void *(* copy_func)(const void *, void *)
Function pointer to a copy function.
Definition: ucx.h:106
UcxList * ucx_list_append_a(UcxAllocator *allocator, UcxList *list, void *data)
Inserts an element at the end of the list using a UcxAllocator.
Definition: list.c:90
UcxList * ucx_list_get(const UcxList *list, size_t index)
Returns the list element at the specified index.
Definition: list.c:197
UcxList * ucx_list_append(UcxList *list, void *data)
Inserts an element at the end of the list.
Definition: list.c:86
int(* cmp_func)(const void *, const void *, void *)
Function pointer to a compare function.
Definition: ucx.h:84
UcxList * ucx_list_prepend(UcxList *list, void *data)
Inserts an element at the beginning of the list.
Definition: list.c:144
Main UCX Header providing most common definitions.
UcxList * ucx_list_first(const UcxList *elem)
Returns the first element of a list.
Definition: list.c:339
void ucx_list_free_a(UcxAllocator *allocator, UcxList *list)
Destroys the entire list using a UcxAllocator.
Definition: list.c:70
UCX list structure.
Definition: list.h:71
UcxList * ucx_list_clone(UcxList *list, copy_func cpyfnc, void *data)
Creates an element-wise copy of a list.
Definition: list.c:31
UcxList * ucx_list_concat(UcxList *list1, UcxList *list2)
Concatenates two lists.
Definition: list.c:162
UcxList * ucx_list_last(const UcxList *elem)
Returns the last element of a list.
Definition: list.c:175
UcxList * ucx_list_remove(UcxList *list, UcxList *element)
Removes an element from the list.
Definition: list.c:351
void * data
List element payload.
Definition: list.h:75
ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem)
Returns the index of an element.
Definition: list.c:185
UcxList * ucx_list_remove_a(UcxAllocator *allocator, UcxList *list, UcxList *element)
Removes an element from the list using a UcxAllocator.
Definition: list.c:355
int ucx_list_contains(UcxList *list, void *elem, cmp_func cmpfnc, void *data)
Checks, if a list contains a specific element.
Definition: list.c:226
UcxList * ucx_list_clone_a(UcxAllocator *allocator, UcxList *list, copy_func cpyfnc, void *data)
Creates an element-wise copy of a list using a UcxAllocator.
Definition: list.c:35
UCX allocator data structure containing memory management functions.
Definition: allocator.h:88
void ucx_list_free_content(UcxList *list, ucx_destructor destr)
Destroys the contents of the specified list by calling the specified destructor on each of them...
Definition: list.c:79
UcxList * next
Pointer to the next list element or NULL, if this is the last element.
Definition: list.h:80
UcxList * ucx_list_sort(UcxList *list, cmp_func cmpfnc, void *data)
Sorts a UcxList with natural merge sort.
Definition: list.c:286
Allocator for custom memory management.
UcxList * ucx_list_prepend_a(UcxAllocator *allocator, UcxList *list, void *data)
Inserts an element at the beginning of the list using a UcxAllocator.
Definition: list.c:148
void ucx_list_free(UcxList *list)
Destroys the entire list.
Definition: list.c:66
UcxList * prev
Pointer to the previous list element or NULL, if this is the first element.
Definition: list.h:85
UcxList * ucx_list_append_once_a(UcxAllocator *allocator, UcxList *list, void *data, cmp_func cmpfnc, void *cmpdata)
Inserts an element at the end of the list, if it is not present in the list, using a UcxAllocator...
Definition: list.c:115
UcxList * ucx_list_append_once(UcxList *list, void *data, cmp_func cmpfnc, void *cmpdata)
Inserts an element at the end of the list, if it is not present in the list.
Definition: list.c:109
int ucx_list_equals(const UcxList *list1, const UcxList *list2, cmp_func cmpfnc, void *data)
Compares two UCX lists element-wise by using a compare function.
Definition: list.c:49
ssize_t ucx_list_find(UcxList *list, void *elem, cmp_func cmpfnc, void *data)
Returns the index of an element containing the specified data.
Definition: list.c:209
size_t ucx_list_size(const UcxList *list)
Returns the element count of the list.
Definition: list.c:230
void(* ucx_destructor)(void *)
A function pointer to a destructor function.
Definition: ucx.h:72