| 1 |
/// @file |
| 2 |
/// @brief internal implementation details of list.h |
| 3 |
/// @ingroup cgraph_utils |
| 4 |
/// |
| 5 |
/// Everything in this header should be considered “private” in the sense that |
| 6 |
/// it should not be called except by macros in list.h. |
| 7 |
|
| 8 |
#pragma once |
| 9 |
|
| 10 |
#include <stdbool.h> |
| 11 |
#include <stddef.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <string.h> |
| 15 |
#include <util/api.h> |
| 16 |
|
| 17 |
#ifdef __cplusplus |
| 18 |
extern "C" { |
| 19 |
#endif |
| 20 |
|
| 21 |
/// generic list, agnostic to the list item type |
| 22 |
/// |
| 23 |
/// There is no way to know the size of list items from this structure alone. |
| 24 |
/// List item size is expected to be supplied externally. |
| 25 |
typedef struct { |
| 26 |
void *base; ///< start of the allocation for backing memory |
| 27 |
///< (base == NULL && capacity == 0) || (base != NULL && capacity > 0) |
| 28 |
size_t head; ///< index of the first element |
| 29 |
///< (capacity == 0 && head == 0) || (capacity > 0 && head < capacity) |
| 30 |
size_t size; ///< number of elements in the list |
| 31 |
///< size <= capacity |
| 32 |
size_t capacity; ///< available storage slots |
| 33 |
} list_t_; |
| 34 |
|
| 35 |
/// get the number of elements in a list |
| 36 |
/// |
| 37 |
/// @param list List to inspect |
| 38 |
/// @return Size of the list |
| 39 |
static inline size_t gv_list_size_(const list_t_ list) { return list.size; } |
| 40 |
|
| 41 |
/// add an empty space for an item to the end of a list |
| 42 |
/// |
| 43 |
/// This function calls `exit` on failure. |
| 44 |
/// |
| 45 |
/// @param list List to operate on |
| 46 |
/// @param item_size Byte size of each list item |
| 47 |
/// @return Index of the new (empty) slot |
| 48 |
UTIL_API size_t gv_list_append_slot_(list_t_ *list, size_t item_size); |
| 49 |
|
| 50 |
/// try to append a new item to a list |
| 51 |
/// |
| 52 |
/// @param list List to operate on |
| 53 |
/// @param item Pointer to item to append |
| 54 |
/// @param item_size Byte size of each list item |
| 55 |
/// @return True if the append succeeded |
| 56 |
UTIL_API bool gv_list_try_append_(list_t_ *list, const void *item, |
| 57 |
size_t item_size); |
| 58 |
|
| 59 |
/// add an empty space for an item to the beginning of a list |
| 60 |
/// |
| 61 |
/// This function calls `exit` on failure. |
| 62 |
/// |
| 63 |
/// @param list List to operate on |
| 64 |
/// @param item_size Byte size of each list item |
| 65 |
/// @return Index of the new (empty) slot |
| 66 |
UTIL_API size_t gv_list_prepend_slot_(list_t_ *list, size_t item_size); |
| 67 |
|
| 68 |
/// get the slot index of a given list item index |
| 69 |
/// |
| 70 |
/// @param list List to operate on |
| 71 |
/// @param index Index of item to lookup |
| 72 |
/// @return Slot index corresponding to the given index |
| 73 |
UTIL_API size_t gv_list_get_(const list_t_ list, size_t index); |
| 74 |
|
| 75 |
/// run the destructor of a list on a given slot |
| 76 |
/// |
| 77 |
/// Though this uses the public type `LIST(<type>)` defined in list.h, this is |
| 78 |
/// an internal API not expected to be called by anything other than the macros |
| 79 |
/// in list.h. |
| 80 |
/// |
| 81 |
/// You can think of this macro as having the following C type: |
| 82 |
/// |
| 83 |
/// void LIST_DTOR_(LIST(<type>) *list, size_t slot); |
| 84 |
/// |
| 85 |
/// @param list List to operate on |
| 86 |
/// @param slot Slot to destruct |
| 87 |
#define LIST_DTOR_(list, slot) \ |
| 88 |
do { \ |
| 89 |
if ((list)->dtor == LIST_DTOR_FREE) { \ |
| 90 |
/* we need to juggle the element into a pointer to avoid compilation */ \ |
| 91 |
/* errors from this (untaken) branch when the element type is not a */ \ |
| 92 |
/* pointer */ \ |
| 93 |
void *ptr_; \ |
| 94 |
sizeof((list)->base[0]) == sizeof(ptr_) \ |
| 95 |
? (void)0 \ |
| 96 |
: (void)(fprintf(stderr, "list element type is not a pointer, but " \ |
| 97 |
"`free` used as destructor\n"), \ |
| 98 |
abort()); \ |
| 99 |
memcpy(&ptr_, &(list)->base[slot], sizeof(ptr_)); \ |
| 100 |
free(ptr_); \ |
| 101 |
} else if ((list)->dtor != NULL) { \ |
| 102 |
(list)->dtor((list)->base[slot]); \ |
| 103 |
} \ |
| 104 |
} while (0) |
| 105 |
|
| 106 |
/// find the slot containing the given item |
| 107 |
/// |
| 108 |
/// @param list List to search |
| 109 |
/// @param needle Item to search for |
| 110 |
/// @param item_size Byte size of each list item |
| 111 |
/// @return Slot index on success or `SIZE_MAX` if not found |
| 112 |
UTIL_API size_t gv_list_find_(const list_t_ list, const void *needle, |
| 113 |
size_t item_size); |
| 114 |
|
| 115 |
/// remove a slot from a list |
| 116 |
/// |
| 117 |
/// @param list List to operate on |
| 118 |
/// @param index Slot index to remove |
| 119 |
/// @param item_size Byte size of each list item |
| 120 |
UTIL_API void gv_list_remove_(list_t_ *list, size_t index, size_t item_size); |
| 121 |
|
| 122 |
/// remove all items from a list |
| 123 |
/// |
| 124 |
/// @param list List to operate on |
| 125 |
/// @param item_size Byte size of list items |
| 126 |
UTIL_API void gv_list_clear_(list_t_ *list, size_t item_size); |
| 127 |
|
| 128 |
/// reserve space for new items in a list |
| 129 |
/// |
| 130 |
/// This function is a no-op if sufficient space is already available. |
| 131 |
/// |
| 132 |
/// @param list List to operate on |
| 133 |
/// @param capacity Total number of slots to make available |
| 134 |
/// @param item_size Byte size of list items |
| 135 |
UTIL_API void gv_list_reserve_(list_t_ *list, size_t capacity, |
| 136 |
size_t item_size); |
| 137 |
|
| 138 |
/// does a list contain a given item? |
| 139 |
/// |
| 140 |
/// @param list List to search |
| 141 |
/// @param needle Item to search for |
| 142 |
/// @param item_size Byte size of each list item |
| 143 |
/// @return True if the item was found |
| 144 |
UTIL_API bool gv_list_contains_(const list_t_ list, const void *needle, |
| 145 |
size_t item_size); |
| 146 |
|
| 147 |
/// make a copy of a list |
| 148 |
/// |
| 149 |
/// This function calls `exit` on failure. |
| 150 |
/// |
| 151 |
/// @param list List to copy |
| 152 |
/// @param item_size Byte size of each list item |
| 153 |
/// @return A copy of the original list |
| 154 |
UTIL_API list_t_ gv_list_copy_(const list_t_ list, size_t item_size); |
| 155 |
|
| 156 |
/// does the list wrap past its end? |
| 157 |
/// |
| 158 |
/// This checks whether the list is discontiguous in how its elements |
| 159 |
/// appear in memory: |
| 160 |
/// |
| 161 |
/// ┌───┬───┬───┬───┬───┬───┬───┬───┐ |
| 162 |
/// a contiguous list: │ │ │ w │ x │ y │ z │ │ │ |
| 163 |
/// └───┴───┴───┴───┴───┴───┴───┴───┘ |
| 164 |
/// 0 1 2 3 |
| 165 |
/// |
| 166 |
/// ┌───┬───┬───┬───┬───┬───┬───┬───┐ |
| 167 |
/// a discontiguous list: │ y │ z │ │ │ │ │ x │ y │ |
| 168 |
/// └───┴───┴───┴───┴───┴───┴───┴───┘ |
| 169 |
/// 2 3 0 1 |
| 170 |
/// |
| 171 |
/// @param list List to inspect |
| 172 |
/// @return True if the list is contiguous |
| 173 |
UTIL_API bool gv_list_is_contiguous_(const list_t_ list); |
| 174 |
|
| 175 |
/// shuffle the populated contents to reset `head` to 0 |
| 176 |
/// |
| 177 |
/// See the `gv_list_is_contiguous_` leading comment for a better understanding |
| 178 |
/// of what it means for `head` to be non-zero. |
| 179 |
/// |
| 180 |
/// @param list List to operate on |
| 181 |
/// @param item_size Byte size of each list item |
| 182 |
UTIL_API void gv_list_sync_(list_t_ *list, size_t item_size); |
| 183 |
|
| 184 |
/// sort a list |
| 185 |
/// |
| 186 |
/// @param list List to operate on |
| 187 |
/// @param cmp Comparator for ordering list items |
| 188 |
/// @param item_size Byte size of each list item |
| 189 |
UTIL_API void gv_list_sort_(list_t_ *list, |
| 190 |
int (*cmp)(const void *, const void *), |
| 191 |
size_t item_size); |
| 192 |
|
| 193 |
/// reverse the item order of a list |
| 194 |
/// |
| 195 |
/// @param list List to operate on |
| 196 |
/// @param item_size Byte size of each list item |
| 197 |
UTIL_API void gv_list_reverse_(list_t_ *list, size_t item_size); |
| 198 |
|
| 199 |
/// decrease the allocated capacity of a list to minimum |
| 200 |
/// |
| 201 |
/// @param list List to operate on |
| 202 |
/// @param item_size Byte size of list items |
| 203 |
UTIL_API void gv_list_shrink_to_fit_(list_t_ *list, size_t item_size); |
| 204 |
|
| 205 |
/// free resources associated with a list |
| 206 |
/// |
| 207 |
/// @param list List to operate on |
| 208 |
UTIL_API void gv_list_free_(list_t_ *list); |
| 209 |
|
| 210 |
/// remove and return the first item of a list |
| 211 |
/// |
| 212 |
/// @param list List to operate on |
| 213 |
/// @param [out] into Destination to pop the item into |
| 214 |
/// @param item_size Byte size of each list item |
| 215 |
UTIL_API void gv_list_pop_front_(list_t_ *list, void *into, size_t item_size); |
| 216 |
|
| 217 |
/// remove and return the last item of a list |
| 218 |
/// |
| 219 |
/// @param list List to operate on |
| 220 |
/// @param [out] into Destination to pop the item into |
| 221 |
/// @param item_size Byte size of each list item |
| 222 |
UTIL_API void gv_list_pop_back_(list_t_ *list, void *into, size_t item_size); |
| 223 |
|
| 224 |
/// transform a managed list into a bare array |
| 225 |
/// |
| 226 |
/// @param list List to operate on |
| 227 |
/// @param [out] datap The list data on completion |
| 228 |
/// @param [out] sizep The list size on completion; optional |
| 229 |
/// @param item_size Byte size of each list item |
| 230 |
UTIL_API void gv_list_detach_(list_t_ *list, void *datap, size_t *sizep, |
| 231 |
size_t item_size); |
| 232 |
|
| 233 |
#ifdef __cplusplus |
| 234 |
} |
| 235 |
#endif |