src/cx/linked_list.h

Mon, 18 Dec 2023 16:14:07 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 Dec 2023 16:14:07 +0100
changeset 763
741a2040fa33
parent 759
475335643af4
child 764
ccbdbd088455
permissions
-rw-r--r--

make cx_cmp_ptr default comparator for pointer lists - relates to #340

     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  */
    28 /**
    29  * \file linked_list.h
    30  * \brief Linked list implementation.
    31  * \details Also provides several low-level functions for custom linked list implementations.
    32  * \author Mike Becker
    33  * \author Olaf Wintermann
    34  * \copyright 2-Clause BSD License
    35  */
    37 #ifndef UCX_LINKED_LIST_H
    38 #define UCX_LINKED_LIST_H
    40 #include "common.h"
    41 #include "list.h"
    43 #ifdef __cplusplus
    44 extern "C" {
    45 #endif
    47 /**
    48  * Set this flag to true, if you want to disable the use of SBO for
    49  * linked list swap operations.
    50  */
    51 extern bool CX_DISABLE_LINKED_LIST_SWAP_SBO;
    53 /**
    54  * Allocates a linked list for storing elements with \p item_size bytes each.
    55  *
    56  * If \p item_size is CX_STORE_POINTERS, the created list will be created as if
    57  * cxListStorePointers() was called immediately after creation and the compare
    58  * function will be automatically set to cx_cmp_ptr(), if none is given.
    59  *
    60  * @param allocator the allocator for allocating the list nodes
    61  * (if \c NULL the cxDefaultAllocator will be used)
    62  * @param comparator the comparator for the elements
    63  * (if \c NULL, and the list is not storing pointers, sort and find
    64  * functions will not work)
    65  * @param item_size the size of each element in bytes
    66  * @return the created list
    67  */
    68 CxList *cxLinkedListCreate(
    69         CxAllocator const *allocator,
    70         cx_compare_func comparator,
    71         size_t item_size
    72 );
    74 /**
    75  * Allocates a linked list for storing elements with \p item_size bytes each.
    76  *
    77  * The list will use cxDefaultAllocator and no comparator function. If you want
    78  * to call functions that need a comparator, you must either set one immediately
    79  * after list creation or use cxLinkedListCreate().
    80  *
    81  * If \p item_size is CX_STORE_POINTERS, the created list will be created as if
    82  * cxListStorePointers() was called immediately after creation and the compare
    83  * function will be automatically set to cx_cmp_ptr().
    84  *
    85  * @param item_size the size of each element in bytes
    86  * @return the created list
    87  */
    88 #define cxLinkedListCreateSimple(item_size) \
    89     cxLinkedListCreate(NULL, NULL, item_size)
    91 /**
    92  * Finds the node at a certain index.
    93  *
    94  * This function can be used to start at an arbitrary position within the list.
    95  * If the search index is large than the start index, \p loc_advance must denote
    96  * the location of some sort of \c next pointer (i.e. a pointer to the next node).
    97  * But it is also possible that the search index is smaller than the start index
    98  * (e.g. in cases where traversing a list backwards is faster) in which case
    99  * \p loc_advance must denote the location of some sort of \c prev pointer
   100  * (i.e. a pointer to the previous node).
   101  *
   102  * @param start a pointer to the start node
   103  * @param start_index the start index
   104  * @param loc_advance the location of the pointer to advance
   105  * @param index the search index
   106  * @return the node found at the specified index
   107  */
   108 void *cx_linked_list_at(
   109         void const *start,
   110         size_t start_index,
   111         ptrdiff_t loc_advance,
   112         size_t index
   113 ) __attribute__((__nonnull__));
   115 /**
   116  * Finds the index of an element within a linked list.
   117  *
   118  * @param start a pointer to the start node
   119  * @param loc_advance the location of the pointer to advance
   120  * @param loc_data the location of the \c data pointer within your node struct
   121  * @param cmp_func a compare function to compare \p elem against the node data
   122  * @param elem a pointer to the element to find
   123  * @return the index of the element or a negative value if it could not be found
   124  */
   125 ssize_t cx_linked_list_find(
   126         void const *start,
   127         ptrdiff_t loc_advance,
   128         ptrdiff_t loc_data,
   129         cx_compare_func cmp_func,
   130         void const *elem
   131 ) __attribute__((__nonnull__));
   133 /**
   134  * Finds the first node in a linked list.
   135  *
   136  * The function starts with the pointer denoted by \p node and traverses the list
   137  * along a prev pointer whose location within the node struct is
   138  * denoted by \p loc_prev.
   139  *
   140  * @param node a pointer to a node in the list
   141  * @param loc_prev the location of the \c prev pointer
   142  * @return a pointer to the first node
   143  */
   144 void *cx_linked_list_first(
   145         void const *node,
   146         ptrdiff_t loc_prev
   147 ) __attribute__((__nonnull__));
   149 /**
   150  * Finds the last node in a linked list.
   151  *
   152  * The function starts with the pointer denoted by \p node and traverses the list
   153  * along a next pointer whose location within the node struct is
   154  * denoted by \p loc_next.
   155  *
   156  * @param node a pointer to a node in the list
   157  * @param loc_next the location of the \c next pointer
   158  * @return a pointer to the last node
   159  */
   160 void *cx_linked_list_last(
   161         void const *node,
   162         ptrdiff_t loc_next
   163 ) __attribute__((__nonnull__));
   165 /**
   166  * Finds the predecessor of a node in case it is not linked.
   167  *
   168  * \remark If \p node is not contained in the list starting with \p begin, the behavior is undefined.
   169  *
   170  * @param begin the node where to start the search
   171  * @param loc_next the location of the \c next pointer
   172  * @param node the successor of the node to find
   173  * @return the node or \c NULL if \p node has no predecessor
   174  */
   175 void *cx_linked_list_prev(
   176         void const *begin,
   177         ptrdiff_t loc_next,
   178         void const *node
   179 ) __attribute__((__nonnull__));
   181 /**
   182  * Adds a new node to a linked list.
   183  * The node must not be part of any list already.
   184  *
   185  * \remark One of the pointers \p begin or \p end may be \c NULL, but not both.
   186  *
   187  * @param begin a pointer to the begin node pointer (if your list has one)
   188  * @param end a pointer to the end node pointer (if your list has one)
   189  * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one)
   190  * @param loc_next the location of a \c next pointer within your node struct (required)
   191  * @param new_node a pointer to the node that shall be appended
   192  */
   193 void cx_linked_list_add(
   194         void **begin,
   195         void **end,
   196         ptrdiff_t loc_prev,
   197         ptrdiff_t loc_next,
   198         void *new_node
   199 ) __attribute__((__nonnull__(5)));
   201 /**
   202  * Prepends a new node to a linked list.
   203  * The node must not be part of any list already.
   204  *
   205  * \remark One of the pointers \p begin or \p end may be \c NULL, but not both.
   206  *
   207  * @param begin a pointer to the begin node pointer (if your list has one)
   208  * @param end a pointer to the end node pointer (if your list has one)
   209  * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one)
   210  * @param loc_next the location of a \c next pointer within your node struct (required)
   211  * @param new_node a pointer to the node that shall be prepended
   212  */
   213 void cx_linked_list_prepend(
   214         void **begin,
   215         void **end,
   216         ptrdiff_t loc_prev,
   217         ptrdiff_t loc_next,
   218         void *new_node
   219 ) __attribute__((__nonnull__(5)));
   221 /**
   222  * Links two nodes.
   223  *
   224  * @param left the new predecessor of \p right
   225  * @param right the new successor of \p left
   226  * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one)
   227  * @param loc_next the location of a \c next pointer within your node struct (required)
   228  */
   229 void cx_linked_list_link(
   230         void *left,
   231         void *right,
   232         ptrdiff_t loc_prev,
   233         ptrdiff_t loc_next
   234 ) __attribute__((__nonnull__));
   236 /**
   237  * Unlinks two nodes.
   238  *
   239  * If right is not the successor of left, the behavior is undefined.
   240  *
   241  * @param left the predecessor of \p right
   242  * @param right the successor of \p left
   243  * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one)
   244  * @param loc_next the location of a \c next pointer within your node struct (required)
   245  */
   246 void cx_linked_list_unlink(
   247         void *left,
   248         void *right,
   249         ptrdiff_t loc_prev,
   250         ptrdiff_t loc_next
   251 ) __attribute__((__nonnull__));
   253 /**
   254  * Inserts a new node after a given node of a linked list.
   255  * The new node must not be part of any list already.
   256  *
   257  * \note If you specify \c NULL as the \p node to insert after, this function needs either the \p begin or
   258  * the \p end pointer to determine the start of the list. Then the new node will be prepended to the list.
   259  *
   260  * @param begin a pointer to the begin node pointer (if your list has one)
   261  * @param end a pointer to the end node pointer (if your list has one)
   262  * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one)
   263  * @param loc_next the location of a \c next pointer within your node struct (required)
   264  * @param node the node after which to insert (\c NULL if you want to prepend the node to the list)
   265  * @param new_node a pointer to the node that shall be prepended
   266  */
   267 void cx_linked_list_insert(
   268         void **begin,
   269         void **end,
   270         ptrdiff_t loc_prev,
   271         ptrdiff_t loc_next,
   272         void *node,
   273         void *new_node
   274 ) __attribute__((__nonnull__(6)));
   276 /**
   277  * Inserts a chain of nodes after a given node of a linked list.
   278  * The chain must not be part of any list already.
   279  *
   280  * If you do not explicitly specify the end of the chain, it will be determined by traversing
   281  * the \c next pointer.
   282  *
   283  * \note If you specify \c NULL as the \p node to insert after, this function needs either the \p begin or
   284  * the \p end pointer to determine the start of the list. If only the \p end pointer is specified, you also need
   285  * to provide a valid \p loc_prev location.
   286  * Then the chain will be prepended to the list.
   287  *
   288  * @param begin a pointer to the begin node pointer (if your list has one)
   289  * @param end a pointer to the end node pointer (if your list has one)
   290  * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one)
   291  * @param loc_next the location of a \c next pointer within your node struct (required)
   292  * @param node the node after which to insert (\c NULL to prepend the chain to the list)
   293  * @param insert_begin a pointer to the first node of the chain that shall be inserted
   294  * @param insert_end a pointer to the last node of the chain (or NULL if the last node shall be determined)
   295  */
   296 void cx_linked_list_insert_chain(
   297         void **begin,
   298         void **end,
   299         ptrdiff_t loc_prev,
   300         ptrdiff_t loc_next,
   301         void *node,
   302         void *insert_begin,
   303         void *insert_end
   304 ) __attribute__((__nonnull__(6)));
   306 /**
   307  * Removes a node from the linked list.
   308  *
   309  * If the node to remove is the begin (resp. end) node of the list and if \p begin (resp. \p end)
   310  * addresses are provided, the pointers are adjusted accordingly.
   311  *
   312  * The following combinations of arguments are valid (more arguments are optional):
   313  * \li \p loc_next and \p loc_prev (ancestor node is determined by using the prev pointer, overall O(1) performance)
   314  * \li \p loc_next and \p begin (ancestor node is determined by list traversal, overall O(n) performance)
   315  *
   316  * \remark The \c next and \c prev pointers of the removed node are not cleared by this function and may still be used
   317  * to traverse to a former adjacent node in the list.
   318  *
   319  * @param begin a pointer to the begin node pointer (optional)
   320  * @param end a pointer to the end node pointer (optional)
   321  * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one)
   322  * @param loc_next the location of a \c next pointer within your node struct (required)
   323  * @param node the node to remove
   324  */
   325 void cx_linked_list_remove(
   326         void **begin,
   327         void **end,
   328         ptrdiff_t loc_prev,
   329         ptrdiff_t loc_next,
   330         void *node
   331 ) __attribute__((__nonnull__(5)));
   334 /**
   335  * Determines the size of a linked list starting with \p node.
   336  * @param node the first node
   337  * @param loc_next the location of the \c next pointer within the node struct
   338  * @return the size of the list or zero if \p node is \c NULL
   339  */
   340 size_t cx_linked_list_size(
   341         void const *node,
   342         ptrdiff_t loc_next
   343 );
   345 /**
   346  * Sorts a linked list based on a comparison function.
   347  *
   348  * This function can work with linked lists of the following structure:
   349  * \code
   350  * typedef struct node node;
   351  * struct node {
   352  *   node* prev;
   353  *   node* next;
   354  *   my_payload data;
   355  * }
   356  * \endcode
   357  *
   358  * @note This is a recursive function with at most logarithmic recursion depth.
   359  *
   360  * @param begin a pointer to the begin node pointer (required)
   361  * @param end a pointer to the end node pointer (optional)
   362  * @param loc_prev the location of a \c prev pointer within your node struct (negative if not present)
   363  * @param loc_next the location of a \c next pointer within your node struct (required)
   364  * @param loc_data the location of the \c data pointer within your node struct
   365  * @param cmp_func the compare function defining the sort order
   366  */
   367 void cx_linked_list_sort(
   368         void **begin,
   369         void **end,
   370         ptrdiff_t loc_prev,
   371         ptrdiff_t loc_next,
   372         ptrdiff_t loc_data,
   373         cx_compare_func cmp_func
   374 ) __attribute__((__nonnull__(1, 6)));
   377 /**
   378  * Compares two lists element wise.
   379  *
   380  * \note Both list must have the same structure.
   381  *
   382  * @param begin_left the begin of the left list (\c NULL denotes an empty list)
   383  * @param begin_right the begin of the right list  (\c NULL denotes an empty list)
   384  * @param loc_advance the location of the pointer to advance
   385  * @param loc_data the location of the \c data pointer within your node struct
   386  * @param cmp_func the function to compare the elements
   387  * @return the first non-zero result of invoking \p cmp_func or: negative if the left list is smaller than the
   388  * right list, positive if the left list is larger than the right list, zero if both lists are equal.
   389  */
   390 int cx_linked_list_compare(
   391         void const *begin_left,
   392         void const *begin_right,
   393         ptrdiff_t loc_advance,
   394         ptrdiff_t loc_data,
   395         cx_compare_func cmp_func
   396 ) __attribute__((__nonnull__(5)));
   398 /**
   399  * Reverses the order of the nodes in a linked list.
   400  *
   401  * @param begin a pointer to the begin node pointer (required)
   402  * @param end a pointer to the end node pointer (optional)
   403  * @param loc_prev the location of a \c prev pointer within your node struct (negative if your struct does not have one)
   404  * @param loc_next the location of a \c next pointer within your node struct (required)
   405  */
   406 void cx_linked_list_reverse(
   407         void **begin,
   408         void **end,
   409         ptrdiff_t loc_prev,
   410         ptrdiff_t loc_next
   411 ) __attribute__((__nonnull__(1)));
   413 #ifdef __cplusplus
   414 } // extern "C"
   415 #endif
   417 #endif // UCX_LINKED_LIST_H

mercurial