Various tweaks to support building with excessive GCC warning flags
[pacman-ng.git] / lib / libalpm / alpm_list.c
blob39eded13b49bd803cec3ffca4ff1d6bff7674c35
1 /*
2 * alpm_list.c
4 * Copyright (c) 2006-2012 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
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.
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.
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/>.
21 #include <stdlib.h>
22 #include <string.h>
24 /* libalpm */
25 #include "alpm_list.h"
27 /* check exported library symbols with: nm -C -D <lib> */
28 #define SYMEXPORT __attribute__((visibility("default")))
29 #define SYMHIDDEN __attribute__((visibility("internal")))
31 /**
32 * @addtogroup alpm_list List Functions
33 * @brief Functions to manipulate alpm_list_t lists.
35 * These functions are designed to create, destroy, and modify lists of
36 * type alpm_list_t. This is an internal list type used by libalpm that is
37 * publicly exposed for use by frontends if desired.
39 * @{ */
41 /* Allocation */
43 /**
44 * @brief Free a list, but not the contained data.
46 * @param list the list to free
48 void SYMEXPORT alpm_list_free(alpm_list_t *list)
50 alpm_list_t *it = list;
52 while(it) {
53 alpm_list_t *tmp = it->next;
54 free(it);
55 it = tmp;
59 /**
60 * @brief Free the internal data of a list structure.
62 * @param list the list to free
63 * @param fn a free function for the internal data
65 void SYMEXPORT alpm_list_free_inner(alpm_list_t *list, alpm_list_fn_free fn)
67 alpm_list_t *it = list;
69 while(it) {
70 if(fn && it->data) {
71 fn(it->data);
73 it = it->next;
78 /* Mutators */
80 /**
81 * @brief Add a new item to the end of the list.
83 * @param list the list to add to
84 * @param data the new item to be added to the list
86 * @return the resultant list
88 alpm_list_t SYMEXPORT *alpm_list_add(alpm_list_t *list, void *data)
90 alpm_list_t *ptr, *lp;
92 ptr = malloc(sizeof(alpm_list_t));
93 if(ptr == NULL) {
94 return list;
97 ptr->data = data;
98 ptr->next = NULL;
100 /* Special case: the input list is empty */
101 if(list == NULL) {
102 ptr->prev = ptr;
103 return ptr;
106 lp = alpm_list_last(list);
107 lp->next = ptr;
108 ptr->prev = lp;
109 list->prev = ptr;
111 return list;
115 * @brief Add items to a list in sorted order.
117 * @param list the list to add to
118 * @param data the new item to be added to the list
119 * @param fn the comparison function to use to determine order
121 * @return the resultant list
123 alpm_list_t SYMEXPORT *alpm_list_add_sorted(alpm_list_t *list, void *data, alpm_list_fn_cmp fn)
125 if(!fn || !list) {
126 return alpm_list_add(list, data);
127 } else {
128 alpm_list_t *add = NULL, *prev = NULL, *next = list;
130 add = malloc(sizeof(alpm_list_t));
131 if(add == NULL) {
132 return list;
134 add->data = data;
136 /* Find insertion point. */
137 while(next) {
138 if(fn(add->data, next->data) <= 0) break;
139 prev = next;
140 next = next->next;
143 /* Insert the add node to the list */
144 if(prev == NULL) { /* special case: we insert add as the first element */
145 add->prev = list->prev; /* list != NULL */
146 add->next = list;
147 list->prev = add;
148 return add;
149 } else if(next == NULL) { /* another special case: add last element */
150 add->prev = prev;
151 add->next = NULL;
152 prev->next = add;
153 list->prev = add;
154 return list;
155 } else {
156 add->prev = prev;
157 add->next = next;
158 next->prev = add;
159 prev->next = add;
160 return list;
166 * @brief Join two lists.
167 * The two lists must be independent. Do not free the original lists after
168 * calling this function, as this is not a copy operation. The list pointers
169 * passed in should be considered invalid after calling this function.
171 * @param first the first list
172 * @param second the second list
174 * @return the resultant joined list
176 alpm_list_t SYMEXPORT *alpm_list_join(alpm_list_t *first, alpm_list_t *second)
178 alpm_list_t *tmp;
180 if(first == NULL) {
181 return second;
183 if(second == NULL) {
184 return first;
186 /* tmp is the last element of the first list */
187 tmp = first->prev;
188 /* link the first list to the second */
189 tmp->next = second;
190 /* link the second list to the first */
191 first->prev = second->prev;
192 /* set the back reference to the tail */
193 second->prev = tmp;
195 return first;
199 * @brief Merge the two sorted sublists into one sorted list.
201 * @param left the first list
202 * @param right the second list
203 * @param fn comparison function for determining merge order
205 * @return the resultant list
207 alpm_list_t SYMEXPORT *alpm_list_mmerge(alpm_list_t *left, alpm_list_t *right,
208 alpm_list_fn_cmp fn)
210 alpm_list_t *newlist, *lp, *tail_ptr, *left_tail_ptr, *right_tail_ptr;
212 if(left == NULL) {
213 return right;
215 if(right == NULL) {
216 return left;
219 /* Save tail node pointers for future use */
220 left_tail_ptr = left->prev;
221 right_tail_ptr = right->prev;
223 if(fn(left->data, right->data) <= 0) {
224 newlist = left;
225 left = left->next;
227 else {
228 newlist = right;
229 right = right->next;
231 newlist->prev = NULL;
232 newlist->next = NULL;
233 lp = newlist;
235 while((left != NULL) && (right != NULL)) {
236 if(fn(left->data, right->data) <= 0) {
237 lp->next = left;
238 left->prev = lp;
239 left = left->next;
241 else {
242 lp->next = right;
243 right->prev = lp;
244 right = right->next;
246 lp = lp->next;
247 lp->next = NULL;
249 if(left != NULL) {
250 lp->next = left;
251 left->prev = lp;
252 tail_ptr = left_tail_ptr;
254 else if(right != NULL) {
255 lp->next = right;
256 right->prev = lp;
257 tail_ptr = right_tail_ptr;
259 else {
260 tail_ptr = lp;
263 newlist->prev = tail_ptr;
265 return newlist;
269 * @brief Sort a list of size `n` using mergesort algorithm.
271 * @param list the list to sort
272 * @param n the size of the list
273 * @param fn the comparison function for determining order
275 * @return the resultant list
277 alpm_list_t SYMEXPORT *alpm_list_msort(alpm_list_t *list, size_t n,
278 alpm_list_fn_cmp fn)
280 if(n > 1) {
281 size_t half = n / 2;
282 size_t i = half - 1;
283 alpm_list_t *left = list, *lastleft = list, *right;
285 while(i--) {
286 lastleft = lastleft->next;
288 right = lastleft->next;
290 /* tidy new lists */
291 lastleft->next = NULL;
292 right->prev = left->prev;
293 left->prev = lastleft;
295 left = alpm_list_msort(left, half, fn);
296 right = alpm_list_msort(right, n - half, fn);
297 list = alpm_list_mmerge(left, right, fn);
299 return list;
303 * @brief Remove an item from the list.
304 * item is not freed; this is the responsibility of the caller.
306 * @param haystack the list to remove the item from
307 * @param item the item to remove from the list
309 * @return the resultant list
311 alpm_list_t SYMEXPORT *alpm_list_remove_item(alpm_list_t *haystack,
312 alpm_list_t *item)
314 if(haystack == NULL || item == NULL) {
315 return haystack;
318 if(item == haystack) {
319 /* Special case: removing the head node which has a back reference to
320 * the tail node */
321 haystack = item->next;
322 if(haystack) {
323 haystack->prev = item->prev;
325 item->prev = NULL;
326 } else if(item == haystack->prev) {
327 /* Special case: removing the tail node, so we need to fix the back
328 * reference on the head node. We also know tail != head. */
329 if(item->prev) {
330 /* i->next should always be null */
331 item->prev->next = item->next;
332 haystack->prev = item->prev;
333 item->prev = NULL;
335 } else {
336 /* Normal case, non-head and non-tail node */
337 if(item->next) {
338 item->next->prev = item->prev;
340 if(item->prev) {
341 item->prev->next = item->next;
345 return haystack;
350 * @brief Remove an item from the list.
352 * @param haystack the list to remove the item from
353 * @param needle the data member of the item we're removing
354 * @param fn the comparison function for searching
355 * @param data output parameter containing data of the removed item
357 * @return the resultant list
359 alpm_list_t SYMEXPORT *alpm_list_remove(alpm_list_t *haystack,
360 const void *needle, alpm_list_fn_cmp fn, void **data)
362 alpm_list_t *i = haystack;
364 if(data) {
365 *data = NULL;
368 if(needle == NULL) {
369 return haystack;
372 while(i) {
373 if(i->data == NULL) {
374 i = i->next;
375 continue;
377 if(fn(i->data, needle) == 0) {
378 haystack = alpm_list_remove_item(haystack, i);
380 if(data) {
381 *data = i->data;
383 free(i);
384 break;
385 } else {
386 i = i->next;
390 return haystack;
394 * @brief Remove a string from a list.
396 * @param haystack the list to remove the item from
397 * @param needle the data member of the item we're removing
398 * @param data output parameter containing data of the removed item
400 * @return the resultant list
402 alpm_list_t SYMEXPORT *alpm_list_remove_str(alpm_list_t *haystack,
403 const char *needle, char **data)
405 return alpm_list_remove(haystack, (const void *)needle,
406 (alpm_list_fn_cmp)strcmp, (void **)data);
410 * @brief Create a new list without any duplicates.
412 * This does NOT copy data members.
414 * @param list the list to copy
416 * @return a new list containing non-duplicate items
418 alpm_list_t SYMEXPORT *alpm_list_remove_dupes(const alpm_list_t *list)
420 const alpm_list_t *lp = list;
421 alpm_list_t *newlist = NULL;
422 while(lp) {
423 if(!alpm_list_find_ptr(newlist, lp->data)) {
424 newlist = alpm_list_add(newlist, lp->data);
426 lp = lp->next;
428 return newlist;
432 * @brief Copy a string list, including data.
434 * @param list the list to copy
436 * @return a copy of the original list
438 alpm_list_t SYMEXPORT *alpm_list_strdup(const alpm_list_t *list)
440 const alpm_list_t *lp = list;
441 alpm_list_t *newlist = NULL;
442 while(lp) {
443 newlist = alpm_list_add(newlist, strdup(lp->data));
444 lp = lp->next;
446 return newlist;
450 * @brief Copy a list, without copying data.
452 * @param list the list to copy
454 * @return a copy of the original list
456 alpm_list_t SYMEXPORT *alpm_list_copy(const alpm_list_t *list)
458 const alpm_list_t *lp = list;
459 alpm_list_t *newlist = NULL;
460 while(lp) {
461 newlist = alpm_list_add(newlist, lp->data);
462 lp = lp->next;
464 return newlist;
468 * @brief Copy a list and copy the data.
469 * Note that the data elements to be copied should not contain pointers
470 * and should also be of constant size.
472 * @param list the list to copy
473 * @param size the size of each data element
475 * @return a copy of the original list, data copied as well
477 alpm_list_t SYMEXPORT *alpm_list_copy_data(const alpm_list_t *list,
478 size_t size)
480 const alpm_list_t *lp = list;
481 alpm_list_t *newlist = NULL;
482 while(lp) {
483 void *newdata = malloc(size);
484 if(newdata) {
485 memcpy(newdata, lp->data, size);
486 newlist = alpm_list_add(newlist, newdata);
487 lp = lp->next;
490 return newlist;
494 * @brief Create a new list in reverse order.
496 * @param list the list to copy
498 * @return a new list in reverse order
500 alpm_list_t SYMEXPORT *alpm_list_reverse(alpm_list_t *list)
502 const alpm_list_t *lp;
503 alpm_list_t *newlist = NULL, *backup;
505 if(list == NULL) {
506 return NULL;
509 lp = alpm_list_last(list);
510 /* break our reverse circular list */
511 backup = list->prev;
512 list->prev = NULL;
514 while(lp) {
515 newlist = alpm_list_add(newlist, lp->data);
516 lp = lp->prev;
518 list->prev = backup; /* restore tail pointer */
519 return newlist;
522 /* Accessors */
525 * @brief Return nth element from list (starting from 0).
527 * @param list the list
528 * @param n the index of the item to find (n < alpm_list_count(list) IS needed)
530 * @return an alpm_list_t node for index `n`
532 alpm_list_t SYMEXPORT *alpm_list_nth(const alpm_list_t *list, size_t n)
534 const alpm_list_t *i = list;
535 while(n--) {
536 i = i->next;
538 return (alpm_list_t *)i;
542 * @brief Get the next element of a list.
544 * @param node the list node
546 * @return the next element, or NULL when no more elements exist
548 inline alpm_list_t SYMEXPORT *alpm_list_next(const alpm_list_t *node)
550 if(node) {
551 return node->next;
552 } else {
553 return NULL;
558 * @brief Get the previous element of a list.
560 * @param list the list head
562 * @return the previous element, or NULL when no previous element exist
564 inline alpm_list_t SYMEXPORT *alpm_list_previous(const alpm_list_t *list)
566 if(list && list->prev->next) {
567 return list->prev;
568 } else {
569 return NULL;
574 * @brief Get the last item in the list.
576 * @param list the list
578 * @return the last element in the list
580 alpm_list_t SYMEXPORT *alpm_list_last(const alpm_list_t *list)
582 if(list) {
583 return list->prev;
584 } else {
585 return NULL;
589 /* Misc */
592 * @brief Get the number of items in a list.
594 * @param list the list
596 * @return the number of list items
598 size_t SYMEXPORT alpm_list_count(const alpm_list_t *list)
600 size_t i = 0;
601 const alpm_list_t *lp = list;
602 while(lp) {
603 ++i;
604 lp = lp->next;
606 return i;
610 * @brief Find an item in a list.
612 * @param needle the item to search
613 * @param haystack the list
614 * @param fn the comparison function for searching (!= NULL)
616 * @return `needle` if found, NULL otherwise
618 void SYMEXPORT *alpm_list_find(const alpm_list_t *haystack, const void *needle,
619 alpm_list_fn_cmp fn)
621 const alpm_list_t *lp = haystack;
622 while(lp) {
623 if(lp->data && fn(lp->data, needle) == 0) {
624 return lp->data;
626 lp = lp->next;
628 return NULL;
631 /* trivial helper function for alpm_list_find_ptr */
632 static int ptr_cmp(const void *p, const void *q)
634 return (p != q);
638 * @brief Find an item in a list.
640 * Search for the item whose data matches that of the `needle`.
642 * @param needle the data to search for (== comparison)
643 * @param haystack the list
645 * @return `needle` if found, NULL otherwise
647 void SYMEXPORT *alpm_list_find_ptr(const alpm_list_t *haystack,
648 const void *needle)
650 return alpm_list_find(haystack, needle, ptr_cmp);
654 * @brief Find a string in a list.
656 * @param needle the string to search for
657 * @param haystack the list
659 * @return `needle` if found, NULL otherwise
661 char SYMEXPORT *alpm_list_find_str(const alpm_list_t *haystack,
662 const char *needle)
664 return (char *)alpm_list_find(haystack, (const void *)needle,
665 (alpm_list_fn_cmp)strcmp);
669 * @brief Find the differences between list `left` and list `right`
671 * The two lists must be sorted. Items only in list `left` are added to the
672 * `onlyleft` list. Items only in list `right` are added to the `onlyright`
673 * list.
675 * @param left the first list
676 * @param right the second list
677 * @param fn the comparison function
678 * @param onlyleft pointer to the first result list
679 * @param onlyright pointer to the second result list
682 void SYMEXPORT alpm_list_diff_sorted(const alpm_list_t *left,
683 const alpm_list_t *right, alpm_list_fn_cmp fn,
684 alpm_list_t **onlyleft, alpm_list_t **onlyright)
686 const alpm_list_t *l = left;
687 const alpm_list_t *r = right;
689 if(!onlyleft && !onlyright) {
690 return;
693 while(l != NULL && r != NULL) {
694 int cmp = fn(l->data, r->data);
695 if(cmp < 0) {
696 if(onlyleft) {
697 *onlyleft = alpm_list_add(*onlyleft, l->data);
699 l = l->next;
701 else if(cmp > 0) {
702 if(onlyright) {
703 *onlyright = alpm_list_add(*onlyright, r->data);
705 r = r->next;
706 } else {
707 l = l->next;
708 r = r->next;
711 while(l != NULL) {
712 if(onlyleft) {
713 *onlyleft = alpm_list_add(*onlyleft, l->data);
715 l = l->next;
717 while(r != NULL) {
718 if(onlyright) {
719 *onlyright = alpm_list_add(*onlyright, r->data);
721 r = r->next;
727 * @brief Find the items in list `lhs` that are not present in list `rhs`.
729 * @param lhs the first list
730 * @param rhs the second list
731 * @param fn the comparison function
733 * @return a list containing all items in `lhs` not present in `rhs`
735 alpm_list_t SYMEXPORT *alpm_list_diff(const alpm_list_t *lhs,
736 const alpm_list_t *rhs, alpm_list_fn_cmp fn)
738 alpm_list_t *left, *right;
739 alpm_list_t *ret = NULL;
741 left = alpm_list_copy(lhs);
742 left = alpm_list_msort(left, alpm_list_count(left), fn);
743 right = alpm_list_copy(rhs);
744 right = alpm_list_msort(right, alpm_list_count(right), fn);
746 alpm_list_diff_sorted(left, right, fn, &ret, NULL);
748 alpm_list_free(left);
749 alpm_list_free(right);
750 return ret;
754 * @brief Copy a list and data into a standard C array of fixed length.
755 * Note that the data elements are shallow copied so any contained pointers
756 * will point to the original data.
758 * @param list the list to copy
759 * @param n the size of the list
760 * @param size the size of each data element
762 * @return an array version of the original list, data copied as well
764 void SYMEXPORT *alpm_list_to_array(const alpm_list_t *list, size_t n,
765 size_t size)
767 size_t i;
768 const alpm_list_t *item;
769 char *array;
771 if(n == 0) {
772 return NULL;
775 array = malloc(n * size);
776 if(array == NULL) {
777 return NULL;
779 for(i = 0, item = list; i < n && item; i++, item = item->next) {
780 memcpy(array + i * size, item->data, size);
782 return array;
785 /** @} */
787 /* vim: set ts=2 sw=2 noet: */