src/ucx/cx/linked_list.h

changeset 490
d218607f5a7e
parent 438
22eca559aded
--- a/src/ucx/cx/linked_list.h	Sat Mar 25 17:18:51 2023 +0100
+++ b/src/ucx/cx/linked_list.h	Fri May 05 18:02:11 2023 +0200
@@ -46,38 +46,45 @@
 #endif
 
 /**
+ * Set this flag to true, if you want to disable the use of SBO for
+ * linked list swap operations.
+ */
+extern bool CX_DISABLE_LINKED_LIST_SWAP_SBO;
+
+/**
  * Allocates a linked list for storing elements with \p item_size bytes each.
  *
- * @remark Elements added to the list are copied, therefore a possible destructor
- * MUST NOT free the memory pointed to by its argument.
+ * If \p item_size is CX_STORE_POINTERS, the created list will be created as if
+ * cxListStorePointers() was called immediately after creation.
  *
  * @param allocator the allocator for allocating the list nodes
+ * (if \c NULL the cxDefaultAllocator will be used)
  * @param comparator the comparator for the elements
+ * (if \c NULL sort and find functions will not work)
  * @param item_size the size of each element in bytes
  * @return the created list
  */
 CxList *cxLinkedListCreate(
         CxAllocator const *allocator,
-        CxListComparator comparator,
+        cx_compare_func comparator,
         size_t item_size
-) __attribute__((__nonnull__));
+);
 
 /**
- * Allocates a linked list for storing pointers.
- *
- * If you want to store the elements directly in this list, use cxLinkedListCreate().
+ * Allocates a linked list for storing elements with \p item_size bytes each.
  *
- * @remark Since only pointers are stored in this list, a possible destructor
- * MAY free the memory pointed to by its argument in order to prevent memory leaks.
+ * The list will use cxDefaultAllocator and no comparator function. If you want
+ * to call functions that need a comparator, you must either set one immediately
+ * after list creation or use cxLinkedListCreate().
  *
- * @param allocator the allocator for allocating the list nodes
- * @param comparator the comparator for the elements
+ * If \p item_size is CX_STORE_POINTERS, the created list will be created as if
+ * cxListStorePointers() was called immediately after creation.
+ *
+ * @param item_size the size of each element in bytes
  * @return the created list
  */
-CxList *cxPointerLinkedListCreate(
-        CxAllocator const *allocator,
-        CxListComparator comparator
-) __attribute__((__nonnull__));
+#define cxLinkedListCreateSimple(item_size) \
+    cxLinkedListCreate(NULL, NULL, item_size)
 
 /**
  * Finds the node at a certain index.
@@ -109,18 +116,15 @@
  * @param start a pointer to the start node
  * @param loc_advance the location of the pointer to advance
  * @param loc_data the location of the \c data pointer within your node struct
- * @param follow_ptr \c false if the pointer denoted by \p loc_data shall be passed to the \p cmp_func.
- * If \c true, the data at \p loc_data is assumed to be a pointer, dereferenced, and then passed to \p cmp_func.
  * @param cmp_func a compare function to compare \p elem against the node data
  * @param elem a pointer to the element to find
- * @return the index of the element or a past-one index if the element could not be found
+ * @return the index of the element or a negative value if it could not be found
  */
-size_t cx_linked_list_find(
+ssize_t cx_linked_list_find(
         void const *start,
         ptrdiff_t loc_advance,
         ptrdiff_t loc_data,
-        bool follow_ptr,
-        CxListComparator cmp_func,
+        cx_compare_func cmp_func,
         void const *elem
 ) __attribute__((__nonnull__));
 
@@ -339,20 +343,13 @@
 /**
  * Sorts a linked list based on a comparison function.
  *
- * This function can work with linked lists of the following structures:
+ * This function can work with linked lists of the following structure:
  * \code
  * typedef struct node node;
  * struct node {
  *   node* prev;
  *   node* next;
- *   my_payload data; // in this case set follow_ptr = 0
- * }
- *
- * typedef struct ptr_node ptr_node;
- * struct ptr_node {
- *   ptr_node* prev;
- *   ptr_node* next;
- *   my_payload* data; // in this case set follow_ptr = 1
+ *   my_payload data;
  * }
  * \endcode
  *
@@ -363,8 +360,6 @@
  * @param loc_prev the location of a \c prev pointer within your node struct (negative if not present)
  * @param loc_next the location of a \c next pointer within your node struct (required)
  * @param loc_data the location of the \c data pointer within your node struct
- * @param follow_ptr \c false if the pointer denoted by \p loc_data shall be passed to the \p cmp_func.
- * If \c true, the data at \p loc_data is assumed to be a pointer, dereferenced, and then passed to \p cmp_func.
  * @param cmp_func the compare function defining the sort order
  */
 void cx_linked_list_sort(
@@ -373,26 +368,19 @@
         ptrdiff_t loc_prev,
         ptrdiff_t loc_next,
         ptrdiff_t loc_data,
-        bool follow_ptr,
-        CxListComparator cmp_func
-) __attribute__((__nonnull__(1, 7)));
+        cx_compare_func cmp_func
+) __attribute__((__nonnull__(1, 6)));
 
 
 /**
  * Compares two lists element wise.
  *
- * The \c follow_ptr flags have the following meaning: if \c false, the pointer denoted by \p loc_data shall
- * directly be passed to the \p cmp_func.
- * If \c true, the data at \p loc_data is assumed to be a pointer, dereferenced, and then passed to \p cmp_func.
- *
  * \note Both list must have the same structure.
  *
  * @param begin_left the begin of the left list (\c NULL denotes an empty list)
  * @param begin_right the begin of the right list  (\c NULL denotes an empty list)
  * @param loc_advance the location of the pointer to advance
  * @param loc_data the location of the \c data pointer within your node struct
- * @param follow_ptr_left indicates whether pointers in the left list shall be dereferenced
- * @param follow_ptr_right indicates whether pointers in the right list shall be dereferenced
  * @param cmp_func the function to compare the elements
  * @return the first non-zero result of invoking \p cmp_func or: negative if the left list is smaller than the
  * right list, positive if the left list is larger than the right list, zero if both lists are equal.
@@ -402,10 +390,8 @@
         void const *begin_right,
         ptrdiff_t loc_advance,
         ptrdiff_t loc_data,
-        bool follow_ptr_left,
-        bool follow_ptr_right,
-        CxListComparator cmp_func
-) __attribute__((__nonnull__(7)));
+        cx_compare_func cmp_func
+) __attribute__((__nonnull__(5)));
 
 /**
  * Reverses the order of the nodes in a linked list.

mercurial