ucx
UAP Common Extensions
Loading...
Searching...
No Matches
linked_list.h
Go to the documentation of this file.
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 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 */
35
36#ifndef UCX_LINKED_LIST_H
37#define UCX_LINKED_LIST_H
38
39#include "common.h"
40#include "list.h"
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
49typedef struct cx_linked_list_s {
55 off_t loc_prev;
59 off_t loc_next;
63 off_t loc_data;
71 void *begin;
75 void *end;
77
95 cx_compare_func comparator, size_t elem_size);
96
111#define cxLinkedListCreateSimple(elem_size) \
112 cxLinkedListCreate(NULL, NULL, elem_size)
113
132CX_EXPORT void *cx_linked_list_at(const void *start,size_t start_index,
133 ptrdiff_t loc_advance, size_t index);
134
147cx_attr_nonnull_arg(1, 4, 5)
148CX_EXPORT void *cx_linked_list_find(const void *start, ptrdiff_t loc_advance,
149 ptrdiff_t loc_data, cx_compare_func cmp_func, const void *elem,
150 size_t *found_index);
151
164CX_EXPORT void *cx_linked_list_first(const void *node, ptrdiff_t loc_prev);
165
178CX_EXPORT void *cx_linked_list_last(const void *node, ptrdiff_t loc_next);
179
191CX_EXPORT void *cx_linked_list_prev(const void *begin, ptrdiff_t loc_next, const void *node);
192
206CX_EXPORT void cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node);
207
221CX_EXPORT void cx_linked_list_prepend(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node);
222
232CX_EXPORT void cx_linked_list_link(void *left, void *right, ptrdiff_t loc_prev, ptrdiff_t loc_next);
233
245CX_EXPORT void cx_linked_list_unlink(void *left, void *right, ptrdiff_t loc_prev, ptrdiff_t loc_next);
246
262CX_EXPORT void cx_linked_list_insert(void **begin, void **end,
263 ptrdiff_t loc_prev, ptrdiff_t loc_next, void *node, void *new_node);
264
286CX_EXPORT void cx_linked_list_insert_chain(void **begin, void **end,
287 ptrdiff_t loc_prev, ptrdiff_t loc_next, void *node, void *insert_begin, void *insert_end);
288
303cx_attr_nonnull_arg(1, 5, 6)
304CX_EXPORT void cx_linked_list_insert_sorted(void **begin, void **end,
305 ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node, cx_compare_func cmp_func);
306
326cx_attr_nonnull_arg(1, 5, 6)
327CX_EXPORT void cx_linked_list_insert_sorted_chain(void **begin, void **end,
328 ptrdiff_t loc_prev, ptrdiff_t loc_next, void *insert_begin, cx_compare_func cmp_func);
329
346cx_attr_nonnull_arg(1, 5, 6)
347CX_EXPORT int cx_linked_list_insert_unique(void **begin, void **end,
348 ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node, cx_compare_func cmp_func);
349
369CX_EXPORT void *cx_linked_list_insert_unique_chain(void **begin, void **end,
370 ptrdiff_t loc_prev, ptrdiff_t loc_next, void *insert_begin, cx_compare_func cmp_func);
371
394CX_EXPORT size_t cx_linked_list_remove_chain(void **begin, void **end,
395 ptrdiff_t loc_prev, ptrdiff_t loc_next, void *node, size_t num);
396
417CX_EXPORT void cx_linked_list_remove(void **begin, void **end,
418 ptrdiff_t loc_prev, ptrdiff_t loc_next, void *node);
419
428CX_EXPORT size_t cx_linked_list_size(const void *node, ptrdiff_t loc_next);
429
453CX_EXPORT void cx_linked_list_sort(void **begin, void **end,
454 ptrdiff_t loc_prev, ptrdiff_t loc_next, ptrdiff_t loc_data, cx_compare_func cmp_func);
455
456
471CX_EXPORT int cx_linked_list_compare(const void *begin_left, const void *begin_right,
472 ptrdiff_t loc_advance, ptrdiff_t loc_data, cx_compare_func cmp_func);
473
483CX_EXPORT void cx_linked_list_reverse(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next);
484
485#ifdef __cplusplus
486} // extern "C"
487#endif
488
489#endif // UCX_LINKED_LIST_H
struct cx_allocator_s CxAllocator
High-Level type alias for the allocator type.
Definition allocator.h:84
Common definitions and feature checks.
#define cx_attr_returns_nonnull
The returned value is guaranteed to be non-NULL.
Definition common.h:151
#define cx_attr_nonnull
All pointer arguments must be non-NULL.
Definition common.h:141
#define CX_EXPORT
Only used for building Windows DLLs.
Definition common.h:278
#define cx_attr_nodiscard
Warn about discarded return value.
Definition common.h:256
#define cx_attr_malloc
The attributed function always returns freshly allocated memory.
Definition common.h:156
#define cx_attr_nonnull_arg(...)
The specified pointer arguments must be non-NULL.
Definition common.h:146
#define cx_attr_dealloc(...)
Not supported in clang.
Definition common.h:172
int(* cx_compare_func)(const void *left, const void *right)
A comparator function comparing two arbitrary values.
Definition compare.h:57
size_t cx_linked_list_remove_chain(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *node, size_t num)
Removes a chain of nodes from the linked list.
void cx_linked_list_prepend(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node)
Prepends a new node to a linked list.
void * cx_linked_list_first(const void *node, ptrdiff_t loc_prev)
Finds the first node in a linked list.
void cx_linked_list_insert_sorted_chain(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *insert_begin, cx_compare_func cmp_func)
Inserts a chain of nodes into a sorted linked list.
void cx_linked_list_reverse(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next)
Reverses the order of the nodes in a linked list.
void cx_linked_list_sort(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, ptrdiff_t loc_data, cx_compare_func cmp_func)
Sorts a linked list based on a comparison function.
void * cx_linked_list_last(const void *node, ptrdiff_t loc_next)
Finds the last node in a linked list.
struct cx_linked_list_s cx_linked_list
Metadata for a linked list.
void cx_linked_list_unlink(void *left, void *right, ptrdiff_t loc_prev, ptrdiff_t loc_next)
Unlinks two nodes.
CxList * cxLinkedListCreate(const CxAllocator *allocator, cx_compare_func comparator, size_t elem_size)
Allocates a linked list for storing elements with elem_size bytes each.
int cx_linked_list_insert_unique(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node, cx_compare_func cmp_func)
Inserts a node into a sorted linked list if no other node with the same value already exists.
int cx_linked_list_compare(const void *begin_left, const void *begin_right, ptrdiff_t loc_advance, ptrdiff_t loc_data, cx_compare_func cmp_func)
Compares two lists element wise.
size_t cx_linked_list_size(const void *node, ptrdiff_t loc_next)
Determines the size of a linked list starting with node.
void cx_linked_list_insert_sorted(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node, cx_compare_func cmp_func)
Inserts a node into a sorted linked list.
void * cx_linked_list_prev(const void *begin, ptrdiff_t loc_next, const void *node)
Finds the predecessor of a node in case it is not linked.
void cx_linked_list_remove(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *node)
Removes a node from the linked list.
void * cx_linked_list_insert_unique_chain(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *insert_begin, cx_compare_func cmp_func)
Inserts a chain of nodes into a sorted linked list, avoiding duplicates.
void cx_linked_list_insert_chain(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *node, void *insert_begin, void *insert_end)
Inserts a chain of nodes after a given node of a linked list.
void * cx_linked_list_at(const void *start, size_t start_index, ptrdiff_t loc_advance, size_t index)
Finds the node at a certain index.
void cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node)
Adds a new node to a linked list.
void * cx_linked_list_find(const void *start, ptrdiff_t loc_advance, ptrdiff_t loc_data, cx_compare_func cmp_func, const void *elem, size_t *found_index)
Finds the node containing an element within a linked list.
void cx_linked_list_link(void *left, void *right, ptrdiff_t loc_prev, ptrdiff_t loc_next)
Links two nodes.
void cx_linked_list_insert(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *node, void *new_node)
Inserts a new node after a given node of a linked list.
Interface for list implementations.
struct cx_list_s CxList
Common type for all list implementations.
Definition list.h:187
void cxListFree(CxList *list)
Deallocates the memory of the specified list structure.
Metadata for a linked list.
Definition linked_list.h:49
off_t loc_prev
Location of the prev pointer (mandatory).
Definition linked_list.h:55
void * begin
Pointer to the first node.
Definition linked_list.h:71
off_t loc_data
Location of the payload (mandatory).
Definition linked_list.h:63
void * end
Pointer to the last node.
Definition linked_list.h:75
off_t loc_next
Location of the next pointer (mandatory).
Definition linked_list.h:59
struct cx_list_s base
Base members.
Definition linked_list.h:51
size_t extra_data_len
Additional bytes to allocate behind the payload (e.g.
Definition linked_list.h:67
Structure for holding the base data of a list.
Definition list.h:54