libalpm
Arch Linux Package Manager Library
alpm_list.h
Go to the documentation of this file.
1/*
2 * alpm_list.h
3 *
4 * Copyright (c) 2006-2024 Pacman Development Team <pacman-dev@lists.archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21
22#ifndef ALPM_LIST_H
23#define ALPM_LIST_H
24
25#include <stdlib.h> /* size_t */
26
27/* Note: alpm_list.{c,h} are intended to be standalone files. Do not include
28 * any other libalpm headers.
29 */
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35/**
36 * @ingroup libalpm
37 * @addtogroup libalpm_list libalpm_list(3)
38 * @brief Functions to manipulate alpm_list_t lists.
39 *
40 * These functions are designed to create, destroy, and modify lists of
41 * type alpm_list_t. This is an internal list type used by libalpm that is
42 * publicly exposed for use by frontends if desired.
43 *
44 * It is exposed so front ends can use it to prevent the need to reimplement
45 * lists of their own; however, it is not required that the front end uses
46 * it.
47 * @{
48 */
49
50/** A doubly linked list */
51typedef struct _alpm_list_t {
52 /** data held by the list node */
53 void *data;
54 /** pointer to the previous node */
55 struct _alpm_list_t *prev;
56 /** pointer to the next node */
57 struct _alpm_list_t *next;
59
60/** Frees a list and its contents */
61#define FREELIST(p) do { alpm_list_free_inner(p, free); alpm_list_free(p); p = NULL; } while(0)
62
63/** item deallocation callback.
64 * @param item the item to free
65 */
66typedef void (*alpm_list_fn_free)(void * item);
67
68/** item comparison callback */
69typedef int (*alpm_list_fn_cmp)(const void *, const void *);
70
71/* allocation */
72
73/** Free a list, but not the contained data.
74 *
75 * @param list the list to free
76 */
77void alpm_list_free(alpm_list_t *list);
78
79/** Free the internal data of a list structure but not the list itself.
80 *
81 * @param list the list to free
82 * @param fn a free function for the internal data
83 */
85
86/* item mutators */
87
88/** Add a new item to the end of the list.
89 *
90 * @param list the list to add to
91 * @param data the new item to be added to the list
92 *
93 * @return the resultant list
94 */
95alpm_list_t *alpm_list_add(alpm_list_t *list, void *data);
96
97/**
98 * @brief Add a new item to the end of the list.
99 *
100 * @param list the list to add to
101 * @param data the new item to be added to the list
102 *
103 * @return the newly added item
104 */
105alpm_list_t *alpm_list_append(alpm_list_t **list, void *data);
106
107/**
108 * @brief Duplicate and append a string to a list.
109 *
110 * @param list the list to append to
111 * @param data the string to duplicate and append
112 *
113 * @return the newly added item
114 */
115alpm_list_t *alpm_list_append_strdup(alpm_list_t **list, const char *data);
116
117/**
118 * @brief Add items to a list in sorted order.
119 *
120 * @param list the list to add to
121 * @param data the new item to be added to the list
122 * @param fn the comparison function to use to determine order
123 *
124 * @return the resultant list
125 */
127
128/**
129 * @brief Join two lists.
130 * The two lists must be independent. Do not free the original lists after
131 * calling this function, as this is not a copy operation. The list pointers
132 * passed in should be considered invalid after calling this function.
133 *
134 * @param first the first list
135 * @param second the second list
136 *
137 * @return the resultant joined list
138 */
140
141/**
142 * @brief Merge the two sorted sublists into one sorted list.
143 *
144 * @param left the first list
145 * @param right the second list
146 * @param fn comparison function for determining merge order
147 *
148 * @return the resultant list
149 */
151
152/**
153 * @brief Sort a list of size `n` using mergesort algorithm.
154 *
155 * @param list the list to sort
156 * @param n the size of the list
157 * @param fn the comparison function for determining order
158 *
159 * @return the resultant list
160 */
162
163/**
164 * @brief Remove an item from the list.
165 * item is not freed; this is the responsibility of the caller.
166 *
167 * @param haystack the list to remove the item from
168 * @param item the item to remove from the list
169 *
170 * @return the resultant list
171 */
173
174/**
175 * @brief Remove an item from the list.
176 *
177 * @param haystack the list to remove the item from
178 * @param needle the data member of the item we're removing
179 * @param fn the comparison function for searching
180 * @param data output parameter containing data of the removed item
181 *
182 * @return the resultant list
183 */
184alpm_list_t *alpm_list_remove(alpm_list_t *haystack, const void *needle, alpm_list_fn_cmp fn, void **data);
185
186/**
187 * @brief Remove a string from a list.
188 *
189 * @param haystack the list to remove the item from
190 * @param needle the data member of the item we're removing
191 * @param data output parameter containing data of the removed item
192 *
193 * @return the resultant list
194 */
195alpm_list_t *alpm_list_remove_str(alpm_list_t *haystack, const char *needle, char **data);
196
197/**
198 * @brief Create a new list without any duplicates.
199 *
200 * This does NOT copy data members.
201 *
202 * @param list the list to copy
203 *
204 * @return a new list containing non-duplicate items
205 */
207
208/**
209 * @brief Copy a string list, including data.
210 *
211 * @param list the list to copy
212 *
213 * @return a copy of the original list
214 */
216
217/**
218 * @brief Copy a list, without copying data.
219 *
220 * @param list the list to copy
221 *
222 * @return a copy of the original list
223 */
225
226/**
227 * @brief Copy a list and copy the data.
228 * Note that the data elements to be copied should not contain pointers
229 * and should also be of constant size.
230 *
231 * @param list the list to copy
232 * @param size the size of each data element
233 *
234 * @return a copy of the original list, data copied as well
235 */
236alpm_list_t *alpm_list_copy_data(const alpm_list_t *list, size_t size);
237
238/**
239 * @brief Create a new list in reverse order.
240 *
241 * @param list the list to copy
242 *
243 * @return a new list in reverse order
244 */
246
247/* item accessors */
248
249
250/**
251 * @brief Return nth element from list (starting from 0).
252 *
253 * @param list the list
254 * @param n the index of the item to find (n < alpm_list_count(list) IS needed)
255 *
256 * @return an alpm_list_t node for index `n`
257 */
258alpm_list_t *alpm_list_nth(const alpm_list_t *list, size_t n);
259
260/**
261 * @brief Get the next element of a list.
262 *
263 * @param list the list node
264 *
265 * @return the next element, or NULL when no more elements exist
266 */
268
269/**
270 * @brief Get the previous element of a list.
271 *
272 * @param list the list head
273 *
274 * @return the previous element, or NULL when no previous element exist
275 */
277
278/**
279 * @brief Get the last item in the list.
280 *
281 * @param list the list
282 *
283 * @return the last element in the list
284 */
286
287/* misc */
288
289/**
290 * @brief Get the number of items in a list.
291 *
292 * @param list the list
293 *
294 * @return the number of list items
295 */
296size_t alpm_list_count(const alpm_list_t *list);
297
298/**
299 * @brief Find an item in a list.
300 *
301 * @param needle the item to search
302 * @param haystack the list
303 * @param fn the comparison function for searching (!= NULL)
304 *
305 * @return `needle` if found, NULL otherwise
306 */
307void *alpm_list_find(const alpm_list_t *haystack, const void *needle, alpm_list_fn_cmp fn);
308
309/**
310 * @brief Find an item in a list.
311 *
312 * Search for the item whose data matches that of the `needle`.
313 *
314 * @param needle the data to search for (== comparison)
315 * @param haystack the list
316 *
317 * @return `needle` if found, NULL otherwise
318 */
319void *alpm_list_find_ptr(const alpm_list_t *haystack, const void *needle);
320
321/**
322 * @brief Find a string in a list.
323 *
324 * @param needle the string to search for
325 * @param haystack the list
326 *
327 * @return `needle` if found, NULL otherwise
328 */
329char *alpm_list_find_str(const alpm_list_t *haystack, const char *needle);
330
331
332/**
333 * @brief Check if two lists contain the same data, ignoring order.
334 *
335 * Lists are considered equal if they both contain the same data regardless
336 * of order.
337 *
338 * @param left the first list
339 * @param right the second list
340 * @param fn the comparison function
341 *
342 * @return 1 if the lists are equal, 0 if not equal, -1 on error.
343 */
344int alpm_list_cmp_unsorted(const alpm_list_t *left,
345 const alpm_list_t *right, alpm_list_fn_cmp fn);
346
347/**
348 * @brief Find the differences between list `left` and list `right`
349 *
350 * The two lists must be sorted. Items only in list `left` are added to the
351 * `onlyleft` list. Items only in list `right` are added to the `onlyright`
352 * list.
353 *
354 * @param left the first list
355 * @param right the second list
356 * @param fn the comparison function
357 * @param onlyleft pointer to the first result list
358 * @param onlyright pointer to the second result list
359 *
360 */
361void alpm_list_diff_sorted(const alpm_list_t *left, const alpm_list_t *right,
362 alpm_list_fn_cmp fn, alpm_list_t **onlyleft, alpm_list_t **onlyright);
363
364/**
365 * @brief Find the items in list `lhs` that are not present in list `rhs`.
366 *
367 * @param lhs the first list
368 * @param rhs the second list
369 * @param fn the comparison function
370 *
371 * @return a list containing all items in `lhs` not present in `rhs`
372 */
373
375
376/**
377 * @brief Copy a list and data into a standard C array of fixed length.
378 * Note that the data elements are shallow copied so any contained pointers
379 * will point to the original data.
380 *
381 * @param list the list to copy
382 * @param n the size of the list
383 * @param size the size of each data element
384 *
385 * @return an array version of the original list, data copied as well
386 */
387void *alpm_list_to_array(const alpm_list_t *list, size_t n, size_t size);
388
389/* End of alpm_list */
390/** @} */
391
392#ifdef __cplusplus
393}
394#endif
395#endif /* ALPM_LIST_H */
struct _alpm_list_t * next
pointer to the next node
Definition alpm_list.h:57
struct _alpm_list_t * prev
pointer to the previous node
Definition alpm_list.h:55
void * data
data held by the list node
Definition alpm_list.h:53
void * alpm_list_to_array(const alpm_list_t *list, size_t n, size_t size)
Copy a list and data into a standard C array of fixed length.
Definition alpm_list.c:631
alpm_list_t * alpm_list_add(alpm_list_t *list, void *data)
Add a new item to the end of the list.
Definition alpm_list.c:64
alpm_list_t * alpm_list_add_sorted(alpm_list_t *list, void *data, alpm_list_fn_cmp fn)
Add items to a list in sorted order.
Definition alpm_list.c:108
void(* alpm_list_fn_free)(void *item)
item deallocation callback.
Definition alpm_list.h:66
alpm_list_t * alpm_list_copy_data(const alpm_list_t *list, size_t size)
Copy a list and copy the data.
Definition alpm_list.c:380
alpm_list_t * alpm_list_previous(const alpm_list_t *list)
Get the previous element of a list.
Definition alpm_list.c:449
alpm_list_t * alpm_list_next(const alpm_list_t *list)
Get the next element of a list.
Definition alpm_list.c:440
size_t alpm_list_count(const alpm_list_t *list)
Get the number of items in a list.
Definition alpm_list.c:469
alpm_list_t * alpm_list_append_strdup(alpm_list_t **list, const char *data)
Duplicate and append a string to a list.
Definition alpm_list.c:96
alpm_list_t * alpm_list_remove_str(alpm_list_t *haystack, const char *needle, char **data)
Remove a string from a list.
Definition alpm_list.c:329
void alpm_list_free_inner(alpm_list_t *list, alpm_list_fn_free fn)
Free the internal data of a list structure but not the list itself.
Definition alpm_list.c:47
alpm_list_t * alpm_list_nth(const alpm_list_t *list, size_t n)
Return nth element from list (starting from 0).
Definition alpm_list.c:431
alpm_list_t * alpm_list_remove(alpm_list_t *haystack, const void *needle, alpm_list_fn_cmp fn, void **data)
Remove an item from the list.
Definition alpm_list.c:295
void alpm_list_diff_sorted(const alpm_list_t *left, const alpm_list_t *right, alpm_list_fn_cmp fn, alpm_list_t **onlyleft, alpm_list_t **onlyright)
Find the differences between list left and list right
Definition alpm_list.c:569
char * alpm_list_find_str(const alpm_list_t *haystack, const char *needle)
Find a string in a list.
Definition alpm_list.c:505
alpm_list_t * alpm_list_reverse(alpm_list_t *list)
Create a new list in reverse order.
Definition alpm_list.c:403
alpm_list_t * alpm_list_strdup(const alpm_list_t *list)
Copy a string list, including data.
Definition alpm_list.c:352
alpm_list_t * alpm_list_diff(const alpm_list_t *lhs, const alpm_list_t *rhs, alpm_list_fn_cmp fn)
Find the items in list lhs that are not present in list rhs.
Definition alpm_list.c:613
alpm_list_t * alpm_list_join(alpm_list_t *first, alpm_list_t *second)
Join two lists.
Definition alpm_list.c:150
void * alpm_list_find(const alpm_list_t *haystack, const void *needle, alpm_list_fn_cmp fn)
Find an item in a list.
Definition alpm_list.c:480
alpm_list_t * alpm_list_remove_item(alpm_list_t *haystack, alpm_list_t *item)
Remove an item from the list.
Definition alpm_list.c:258
int(* alpm_list_fn_cmp)(const void *, const void *)
item comparison callback
Definition alpm_list.h:69
void alpm_list_free(alpm_list_t *list)
Free a list, but not the contained data.
Definition alpm_list.c:36
alpm_list_t * alpm_list_last(const alpm_list_t *list)
Get the last item in the list.
Definition alpm_list.c:458
alpm_list_t * alpm_list_append(alpm_list_t **list, void *data)
Add a new item to the end of the list.
Definition alpm_list.c:70
alpm_list_t * alpm_list_msort(alpm_list_t *list, size_t n, alpm_list_fn_cmp fn)
Sort a list of size n using mergesort algorithm.
Definition alpm_list.c:233
void * alpm_list_find_ptr(const alpm_list_t *haystack, const void *needle)
Find an item in a list.
Definition alpm_list.c:499
int alpm_list_cmp_unsorted(const alpm_list_t *left, const alpm_list_t *right, alpm_list_fn_cmp fn)
Check if two lists contain the same data, ignoring order.
Definition alpm_list.c:512
alpm_list_t * alpm_list_mmerge(alpm_list_t *left, alpm_list_t *right, alpm_list_fn_cmp fn)
Merge the two sorted sublists into one sorted list.
Definition alpm_list.c:172
alpm_list_t * alpm_list_copy(const alpm_list_t *list)
Copy a list, without copying data.
Definition alpm_list.c:366
alpm_list_t * alpm_list_remove_dupes(const alpm_list_t *list)
Create a new list without any duplicates.
Definition alpm_list.c:336
A doubly linked list.
Definition alpm_list.h:51