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 
183 void ucx_list_free_content(UcxList* list, ucx_destructor destr);
184 
185 
198 UcxList *ucx_list_append(UcxList *list, void *data);
199 
213 UcxList *ucx_list_append_a(UcxAllocator *allocator, UcxList *list, void *data);
214 
235  cmp_func cmpfnc, void *cmpdata);
236 
259  UcxList *list, void *data, cmp_func cmpfnc, void *cmpdata);
260 
281  cmp_func cmpfnc, void *cmpdata);
282 
304  UcxList *list, void *data, cmp_func cmpfnc, void *cmpdata);
305 
320 UcxList *ucx_list_prepend(UcxList *list, void *data);
321 
334 UcxList *ucx_list_prepend_a(UcxAllocator *allocator, UcxList *list, void *data);
335 
350 UcxList *ucx_list_concat(UcxList *list1, UcxList *list2);
351 
362 UcxList *ucx_list_first(const UcxList *elem);
363 
374 UcxList *ucx_list_last(const UcxList *elem);
375 
384 UcxList *ucx_list_get(const UcxList *list, size_t index);
385 
394 ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem);
395 
402 size_t ucx_list_size(const UcxList *list);
403 
421 ssize_t ucx_list_find(UcxList *list, void *elem, cmp_func cmpfnc, void *data);
422 
435 int ucx_list_contains(UcxList *list, void *elem, cmp_func cmpfnc, void *data);
436 
451 UcxList *ucx_list_sort(UcxList *list, cmp_func cmpfnc, void *data);
452 
465 UcxList *ucx_list_remove(UcxList *list, UcxList *element);
466 
478 UcxList *ucx_list_remove_a(UcxAllocator *allocator, UcxList *list,
479  UcxList *element);
480 
481 #ifdef __cplusplus
482 }
483 #endif
484 
485 #endif /* UCX_LIST_H */
486 
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:91
UcxList * ucx_list_get(const UcxList *list, size_t index)
Returns the list element at the specified index.
Definition: list.c:231
UcxList * ucx_list_append(UcxList *list, void *data)
Inserts an element at the end of the list.
Definition: list.c:87
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:178
Main UCX Header providing most common definitions.
UcxList * ucx_list_first(const UcxList *elem)
Returns the first element of a list.
Definition: list.c:373
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:196
UcxList * ucx_list_last(const UcxList *elem)
Returns the last element of a list.
Definition: list.c:209
UcxList * ucx_list_remove(UcxList *list, UcxList *element)
Removes an element from the list.
Definition: list.c:385
void * data
List element payload.
Definition: list.h:75
UcxList * ucx_list_prepend_once_a(UcxAllocator *allocator, UcxList *list, void *data, cmp_func cmpfnc, void *cmpdata)
Inserts an element at the beginning of the list, if it is not present in the list, using a UcxAllocator.
Definition: list.c:151
ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem)
Returns the index of an element.
Definition: list.c:219
UcxList * ucx_list_remove_a(UcxAllocator *allocator, UcxList *list, UcxList *element)
Removes an element from the list using a UcxAllocator.
Definition: list.c:389
int ucx_list_contains(UcxList *list, void *elem, cmp_func cmpfnc, void *data)
Checks, if a list contains a specific element.
Definition: list.c:260
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:320
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:182
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:116
UcxList * ucx_list_prepend_once(UcxList *list, void *data, cmp_func cmpfnc, void *cmpdata)
Inserts an element at the beginning of the list, if it is not present in the list.
Definition: list.c:145
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:110
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:243
size_t ucx_list_size(const UcxList *list)
Returns the element count of the list.
Definition: list.c:264
void(* ucx_destructor)(void *)
A function pointer to a destructor function.
Definition: ucx.h:72