universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: ucx: /home/mike/workspace/c/ucx/src/ucx/list.h Source File universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
universe@390:
ucx universe@390:
universe@390:
UAP Common Extensions
universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390:
universe@390: universe@390:
universe@390: universe@390: universe@390:
universe@390:
universe@390:
universe@390:
list.h
universe@390:
universe@390:
universe@390: 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 = (UcxList*) 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(const UcxList *list, copy_func cpyfnc, void* data);
103 
120 UcxList *ucx_list_clone_a(UcxAllocator *allocator, const 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 
215 
230 UcxList *ucx_list_prepend(UcxList *list, void *data);
231 
244 UcxList *ucx_list_prepend_a(UcxAllocator *allocator, UcxList *list, void *data);
245 
260 UcxList *ucx_list_concat(UcxList *list1, UcxList *list2);
261 
272 UcxList *ucx_list_first(const UcxList *elem);
273 
284 UcxList *ucx_list_last(const UcxList *elem);
285 
294 UcxList *ucx_list_get(const UcxList *list, size_t index);
295 
304 ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem);
305 
312 size_t ucx_list_size(const UcxList *list);
313 
331 ssize_t ucx_list_find(const UcxList *list, void *elem,
332  cmp_func cmpfnc, void *data);
333 
346 int ucx_list_contains(const UcxList *list, void *elem,
347  cmp_func cmpfnc, void *data);
348 
363 UcxList *ucx_list_sort(UcxList *list, cmp_func cmpfnc, void *data);
364 
377 UcxList *ucx_list_remove(UcxList *list, UcxList *element);
378 
390 UcxList *ucx_list_remove_a(UcxAllocator *allocator, UcxList *list,
391  UcxList *element);
392 
407 UcxList* ucx_list_union(const UcxList *left, const UcxList *right,
408  cmp_func cmpfnc, void* cmpdata,
409  copy_func cpfnc, void* cpdata);
410 
427  const UcxList *left, const UcxList *right,
428  cmp_func cmpfnc, void* cmpdata,
429  copy_func cpfnc, void* cpdata);
430 
445 UcxList* ucx_list_intersection(const UcxList *left, const UcxList *right,
446  cmp_func cmpfnc, void* cmpdata,
447  copy_func cpfnc, void* cpdata);
448 
465  const UcxList *left, const UcxList *right,
466  cmp_func cmpfnc, void* cmpdata,
467  copy_func cpfnc, void* cpdata);
468 
483 UcxList* ucx_list_difference(const UcxList *left, const UcxList *right,
484  cmp_func cmpfnc, void* cmpdata,
485  copy_func cpfnc, void* cpdata);
486 
503  const UcxList *left, const UcxList *right,
504  cmp_func cmpfnc, void* cmpdata,
505  copy_func cpfnc, void* cpdata);
506 
507 #ifdef __cplusplus
508 }
509 #endif
510 
511 #endif /* UCX_LIST_H */
512 
void *(* copy_func)(const void *, void *)
Function pointer to a copy function.
Definition: ucx.h:106
universe@390:
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
universe@390:
UcxList * ucx_list_get(const UcxList *list, size_t index)
Returns the list element at the specified index.
Definition: list.c:163
universe@390:
UcxList * ucx_list_append(UcxList *list, void *data)
Inserts an element at the end of the list.
Definition: list.c:87
universe@390:
int(* cmp_func)(const void *, const void *, void *)
Function pointer to a compare function.
Definition: ucx.h:84
universe@390:
UcxList * ucx_list_prepend(UcxList *list, void *data)
Inserts an element at the beginning of the list.
Definition: list.c:110
universe@390:
Main UCX Header providing most common definitions.
universe@390:
UcxList * ucx_list_first(const UcxList *elem)
Returns the first element of a list.
Definition: list.c:307
universe@390:
void ucx_list_free_a(UcxAllocator *allocator, UcxList *list)
Destroys the entire list using a UcxAllocator.
Definition: list.c:70
universe@390:
UCX list structure.
Definition: list.h:71
universe@390:
ssize_t ucx_list_find(const UcxList *list, void *elem, cmp_func cmpfnc, void *data)
Returns the index of an element containing the specified data.
Definition: list.c:175
universe@390:
UcxList * ucx_list_difference_a(UcxAllocator *allocator, const UcxList *left, const UcxList *right, cmp_func cmpfnc, void *cmpdata, copy_func cpfnc, void *cpdata)
Returns the difference of two lists.
Definition: list.c:421
universe@390:
UcxList * ucx_list_clone_a(UcxAllocator *allocator, const UcxList *list, copy_func cpyfnc, void *data)
Creates an element-wise copy of a list using a UcxAllocator.
Definition: list.c:35
universe@390:
UcxList * ucx_list_union(const UcxList *left, const UcxList *right, cmp_func cmpfnc, void *cmpdata, copy_func cpfnc, void *cpdata)
Returns the union of two lists.
Definition: list.c:382
universe@390:
UcxList * ucx_list_concat(UcxList *list1, UcxList *list2)
Concatenates two lists.
Definition: list.c:128
universe@390:
UcxList * ucx_list_last(const UcxList *elem)
Returns the last element of a list.
Definition: list.c:141
universe@390:
UcxList * ucx_list_remove(UcxList *list, UcxList *element)
Removes an element from the list.
Definition: list.c:319
universe@390:
void * data
List element payload.
Definition: list.h:75
universe@390:
ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem)
Returns the index of an element.
Definition: list.c:151
universe@390:
UcxList * ucx_list_remove_a(UcxAllocator *allocator, UcxList *list, UcxList *element)
Removes an element from the list using a UcxAllocator.
Definition: list.c:323
universe@390:
UcxList * ucx_list_difference(const UcxList *left, const UcxList *right, cmp_func cmpfnc, void *cmpdata, copy_func cpfnc, void *cpdata)
Returns the difference of two lists.
Definition: list.c:414
universe@390:
UCX allocator data structure containing memory management functions.
Definition: allocator.h:88
universe@390:
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
universe@390:
UcxList * next
Pointer to the next list element or NULL, if this is the last element.
Definition: list.h:80
universe@390:
UcxList * ucx_list_intersection_a(UcxAllocator *allocator, const UcxList *left, const UcxList *right, cmp_func cmpfnc, void *cmpdata, copy_func cpfnc, void *cpdata)
Returns the intersection of two lists.
Definition: list.c:405
universe@390:
UcxList * ucx_list_intersection(const UcxList *left, const UcxList *right, cmp_func cmpfnc, void *cmpdata, copy_func cpfnc, void *cpdata)
Returns the intersection of two lists.
Definition: list.c:398
universe@390:
UcxList * ucx_list_sort(UcxList *list, cmp_func cmpfnc, void *data)
Sorts a UcxList with natural merge sort.
Definition: list.c:254
universe@390:
UcxList * ucx_list_clone(const UcxList *list, copy_func cpyfnc, void *data)
Creates an element-wise copy of a list.
Definition: list.c:31
universe@390:
Allocator for custom memory management.
universe@390:
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:114
universe@390:
void ucx_list_free(UcxList *list)
Destroys the entire list.
Definition: list.c:66
universe@390:
UcxList * prev
Pointer to the previous list element or NULL, if this is the first element.
Definition: list.h:85
universe@390:
UcxList * ucx_list_union_a(UcxAllocator *allocator, const UcxList *left, const UcxList *right, cmp_func cmpfnc, void *cmpdata, copy_func cpfnc, void *cpdata)
Returns the union of two lists.
Definition: list.c:389
universe@390:
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
universe@390:
int ucx_list_contains(const UcxList *list, void *elem, cmp_func cmpfnc, void *data)
Checks, if a list contains a specific element.
Definition: list.c:193
universe@390:
size_t ucx_list_size(const UcxList *list)
Returns the element count of the list.
Definition: list.c:198
universe@390:
void(* ucx_destructor)(void *)
A function pointer to a destructor function.
Definition: ucx.h:72
universe@390:
universe@390: universe@390: universe@390: universe@390: