2007-01-19 04:28:44 -05:00
|
|
|
/*
|
|
|
|
* alpm_list.c
|
2007-11-16 21:18:45 -05:00
|
|
|
*
|
2007-01-19 04:28:44 -05:00
|
|
|
* Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
|
2007-11-16 21:18:45 -05:00
|
|
|
*
|
2007-01-19 04:28:44 -05:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2007-11-16 21:18:45 -05:00
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
2007-01-19 04:28:44 -05:00
|
|
|
* USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2007-03-05 17:13:33 -05:00
|
|
|
|
2007-01-19 04:28:44 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2007-03-05 17:13:33 -05:00
|
|
|
/* libalpm */
|
2007-01-19 04:28:44 -05:00
|
|
|
#include "alpm_list.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2007-03-29 00:40:49 -04:00
|
|
|
/**
|
|
|
|
* @addtogroup alpm_list List Functions
|
|
|
|
* @brief Functions to manipulate alpm_list_t lists.
|
|
|
|
*
|
|
|
|
* These functions are designed to create, destroy, and modify lists of
|
|
|
|
* type alpm_list_t. This is an internal list type used by libalpm that is
|
|
|
|
* publicly exposed for use by frontends if desired.
|
|
|
|
*
|
|
|
|
* @{ */
|
2007-01-19 04:28:44 -05:00
|
|
|
|
|
|
|
/* Allocation */
|
|
|
|
|
2007-03-29 00:40:49 -04:00
|
|
|
/**
|
|
|
|
* @brief Allocate a new alpm_list_t.
|
|
|
|
*
|
|
|
|
* @return a new alpm_list_t item, or NULL on failure
|
2007-01-19 04:28:44 -05:00
|
|
|
*/
|
2007-07-18 18:22:02 -04:00
|
|
|
alpm_list_t SYMEXPORT *alpm_list_new()
|
2007-01-19 04:28:44 -05:00
|
|
|
{
|
|
|
|
alpm_list_t *list = NULL;
|
|
|
|
|
2007-05-14 03:16:55 -04:00
|
|
|
list = malloc(sizeof(alpm_list_t));
|
2007-01-19 04:28:44 -05:00
|
|
|
if(list) {
|
|
|
|
list->data = NULL;
|
2007-11-06 01:55:45 -05:00
|
|
|
list->prev = list; /* maintain a back reference to the tail pointer */
|
2007-01-19 04:28:44 -05:00
|
|
|
list->next = NULL;
|
|
|
|
}
|
2007-03-29 00:40:49 -04:00
|
|
|
|
2007-01-19 04:28:44 -05:00
|
|
|
return(list);
|
|
|
|
}
|
|
|
|
|
2007-03-29 00:40:49 -04:00
|
|
|
/**
|
|
|
|
* @brief Free a list, but not the contained data.
|
|
|
|
*
|
|
|
|
* @param list the list to free
|
2007-01-19 04:28:44 -05:00
|
|
|
*/
|
2007-01-30 02:47:19 -05:00
|
|
|
void SYMEXPORT alpm_list_free(alpm_list_t *list)
|
2007-01-19 04:28:44 -05:00
|
|
|
{
|
|
|
|
alpm_list_t *it = list;
|
|
|
|
|
|
|
|
while(it) {
|
2007-01-24 03:51:50 -05:00
|
|
|
alpm_list_t *tmp = it->next;
|
|
|
|
free(it);
|
|
|
|
it = tmp;
|
2007-01-19 04:28:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-29 00:40:49 -04:00
|
|
|
/**
|
|
|
|
* @brief Free the internal data of a list structure.
|
|
|
|
*
|
|
|
|
* @param list the list to free
|
|
|
|
* @param fn a free function for the internal data
|
2007-01-19 04:28:44 -05:00
|
|
|
*/
|
2007-01-30 02:47:19 -05:00
|
|
|
void SYMEXPORT alpm_list_free_inner(alpm_list_t *list, alpm_list_fn_free fn)
|
2007-01-19 04:28:44 -05:00
|
|
|
{
|
2007-01-24 03:51:50 -05:00
|
|
|
alpm_list_t *it = list;
|
|
|
|
|
|
|
|
while(it) {
|
|
|
|
if(fn && it->data) {
|
|
|
|
fn(it->data);
|
|
|
|
}
|
|
|
|
it = it->next;
|
|
|
|
}
|
2007-01-19 04:28:44 -05:00
|
|
|
}
|
|
|
|
|
2007-01-24 03:51:50 -05:00
|
|
|
|
2007-01-19 04:28:44 -05:00
|
|
|
/* Mutators */
|
|
|
|
|
2007-03-29 00:40:49 -04:00
|
|
|
/**
|
2007-11-14 06:42:15 -05:00
|
|
|
* @brief Add a new item to the end of the list.
|
2007-03-29 00:40:49 -04:00
|
|
|
*
|
|
|
|
* @param list the list to add to
|
|
|
|
* @param data the new item to be added to the list
|
|
|
|
*
|
|
|
|
* @return the resultant list
|
2007-01-19 04:28:44 -05:00
|
|
|
*/
|
2007-01-30 02:47:19 -05:00
|
|
|
alpm_list_t SYMEXPORT *alpm_list_add(alpm_list_t *list, void *data)
|
2007-01-19 04:28:44 -05:00
|
|
|
{
|
|
|
|
alpm_list_t *ptr, *lp;
|
|
|
|
|
|
|
|
ptr = list;
|
|
|
|
if(ptr == NULL) {
|
|
|
|
ptr = alpm_list_new();
|
|
|
|
if(ptr == NULL) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lp = alpm_list_last(ptr);
|
|
|
|
if(lp == ptr && lp->data == NULL) {
|
|
|
|
/* nada */
|
|
|
|
} else {
|
|
|
|
lp->next = alpm_list_new();
|
|
|
|
if(lp->next == NULL) {
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
lp->next->prev = lp;
|
|
|
|
lp = lp->next;
|
2007-11-06 01:55:45 -05:00
|
|
|
list->prev = lp;
|
2007-01-19 04:28:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
lp->data = data;
|
|
|
|
|
|
|
|
return(ptr);
|
|
|
|
}
|
|
|
|
|
2007-03-29 00:40:49 -04:00
|
|
|
/**
|
|
|
|
* @brief Add items to a list in sorted order.
|
|
|
|
*
|
|
|
|
* @param list the list to add to
|
|
|
|
* @param data the new item to be added to the list
|
|
|
|
* @param fn the comparison function to use to determine order
|
|
|
|
*
|
|
|
|
* @return the resultant list
|
2007-01-19 04:28:44 -05:00
|
|
|
*/
|
2007-07-18 18:22:02 -04:00
|
|
|
alpm_list_t SYMEXPORT *alpm_list_add_sorted(alpm_list_t *list, void *data, alpm_list_fn_cmp fn)
|
2007-01-19 04:28:44 -05:00
|
|
|
{
|
|
|
|
if(!fn) {
|
|
|
|
return alpm_list_add(list, data);
|
|
|
|
} else {
|
|
|
|
alpm_list_t *add = NULL, *prev = NULL, *next = list;
|
|
|
|
|
|
|
|
add = alpm_list_new();
|
|
|
|
add->data = data;
|
|
|
|
|
|
|
|
/* Find insertion point. */
|
|
|
|
while(next) {
|
|
|
|
if(fn(add->data, next->data) <= 0) break;
|
|
|
|
prev = next;
|
|
|
|
next = next->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert node before insertion point. */
|
|
|
|
add->prev = prev;
|
|
|
|
add->next = next;
|
|
|
|
|
|
|
|
if(next != NULL) {
|
|
|
|
next->prev = add; /* Not at end. */
|
|
|
|
}
|
|
|
|
|
|
|
|
if(prev != NULL) {
|
|
|
|
prev->next = add; /* In middle. */
|
2007-01-22 20:34:58 -05:00
|
|
|
} else {
|
|
|
|
list = add; /* At beginning, or new list */
|
2007-01-19 04:28:44 -05:00
|
|
|
}
|
2007-11-06 01:55:45 -05:00
|
|
|
|
|
|
|
if(next == NULL) {
|
|
|
|
/* At end, adjust tail pointer on head node */
|
|
|
|
list->prev = add;
|
|
|
|
}
|
2007-11-16 21:18:45 -05:00
|
|
|
|
2007-01-19 04:28:44 -05:00
|
|
|
return(list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-20 03:11:40 -05:00
|
|
|
/**
|
|
|
|
* @brief Join two lists.
|
|
|
|
* The two lists must be independent. Do not free the original lists after
|
|
|
|
* calling this function, as this is not a copy operation. The list pointers
|
|
|
|
* passed in should be considered invalid after calling this function.
|
|
|
|
*
|
|
|
|
* @param first the first list
|
|
|
|
* @param second the second list
|
|
|
|
*
|
|
|
|
* @return the resultant joined list
|
|
|
|
*/
|
|
|
|
alpm_list_t SYMEXPORT *alpm_list_join(alpm_list_t *first, alpm_list_t *second)
|
|
|
|
{
|
|
|
|
alpm_list_t *tmp;
|
|
|
|
|
|
|
|
if (first == NULL) {
|
|
|
|
return second;
|
|
|
|
}
|
|
|
|
if (second == NULL) {
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
/* tmp is the last element of the first list */
|
|
|
|
tmp = first->prev;
|
|
|
|
/* link the first list to the second */
|
|
|
|
tmp->next = second;
|
|
|
|
/* link the second list to the first */
|
|
|
|
first->prev = second->prev;
|
|
|
|
/* set the back reference to the tail */
|
|
|
|
second->prev = tmp;
|
|
|
|
|
|
|
|
return(first);
|
|
|
|
}
|
|
|
|
|
2007-03-29 00:40:49 -04:00
|
|
|
/**
|
|
|
|
* @brief Merge the two sorted sublists into one sorted list.
|
|
|
|
*
|
|
|
|
* @param left the first list
|
2007-01-19 04:28:44 -05:00
|
|
|
* @param right the second list
|
2007-03-29 00:40:49 -04:00
|
|
|
* @param fn comparison function for determining merge order
|
|
|
|
*
|
|
|
|
* @return the resultant list
|
2007-01-19 04:28:44 -05:00
|
|
|
*/
|
2007-07-18 18:22:02 -04:00
|
|
|
alpm_list_t SYMEXPORT *alpm_list_mmerge(alpm_list_t *left, alpm_list_t *right, alpm_list_fn_cmp fn)
|
2007-01-19 04:28:44 -05:00
|
|
|
{
|
|
|
|
alpm_list_t *newlist, *lp;
|
|
|
|
|
2007-11-16 21:18:45 -05:00
|
|
|
if (left == NULL)
|
2007-01-19 04:28:44 -05:00
|
|
|
return right;
|
|
|
|
if (right == NULL)
|
|
|
|
return left;
|
|
|
|
|
|
|
|
if (fn(left->data, right->data) <= 0) {
|
|
|
|
newlist = left;
|
|
|
|
left = left->next;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
newlist = right;
|
|
|
|
right = right->next;
|
|
|
|
}
|
|
|
|
newlist->prev = NULL;
|
|
|
|
newlist->next = NULL;
|
|
|
|
lp = newlist;
|
|
|
|
|
|
|
|
while ((left != NULL) && (right != NULL)) {
|
|
|
|
if (fn(left->data, right->data) <= 0) {
|
|
|
|
lp->next = left;
|
|
|
|
left->prev = lp;
|
|
|
|
left = left->next;
|
2007-11-16 21:18:45 -05:00
|
|
|
}
|
2007-01-19 04:28:44 -05:00
|
|
|
else {
|
|
|
|
lp->next = right;
|
|
|
|
right->prev = lp;
|
|
|
|
right = right->next;
|
|
|
|
}
|
|
|
|
lp = lp->next;
|
|
|
|
lp->next = NULL;
|
|
|
|
}
|
|
|
|
if (left != NULL) {
|
|
|
|
lp->next = left;
|
|
|
|
left->prev = lp;
|
|
|
|
}
|
|
|
|
else if (right != NULL) {
|
|
|
|
lp->next = right;
|
|
|
|
right->prev = lp;
|
|
|
|
}
|
2007-11-06 01:55:45 -05:00
|
|
|
|
|
|
|
/* Find our tail pointer
|
|
|
|
* TODO maintain this in the algorithm itself */
|
|
|
|
lp = newlist;
|
|
|
|
while(lp && lp->next) {
|
|
|
|
lp = lp->next;
|
|
|
|
}
|
|
|
|
newlist->prev = lp;
|
|
|
|
|
2007-01-19 04:28:44 -05:00
|
|
|
return(newlist);
|
|
|
|
}
|
|
|
|
|
2007-03-29 00:40:49 -04:00
|
|
|
/**
|
|
|
|
* @brief Sort a list of size `n` using mergesort algorithm.
|
|
|
|
*
|
|
|
|
* @param list the list to sort
|
|
|
|
* @param n the size of the list
|
|
|
|
* @param fn the comparison function for determining order
|
|
|
|
*
|
|
|
|
* @return the resultant list
|
2007-01-19 04:28:44 -05:00
|
|
|
*/
|
2007-07-18 18:22:02 -04:00
|
|
|
alpm_list_t SYMEXPORT *alpm_list_msort(alpm_list_t *list, int n, alpm_list_fn_cmp fn)
|
2007-01-19 04:28:44 -05:00
|
|
|
{
|
|
|
|
if (n > 1) {
|
|
|
|
alpm_list_t *left = list;
|
|
|
|
alpm_list_t *lastleft = alpm_list_nth(list, n/2 - 1);
|
|
|
|
alpm_list_t *right = lastleft->next;
|
|
|
|
/* terminate first list */
|
2007-11-16 21:18:45 -05:00
|
|
|
lastleft->next = NULL;
|
2007-01-19 04:28:44 -05:00
|
|
|
|
|
|
|
left = alpm_list_msort(left, n/2, fn);
|
|
|
|
right = alpm_list_msort(right, n - (n/2), fn);
|
|
|
|
list = alpm_list_mmerge(left, right, fn);
|
|
|
|
}
|
|
|
|
return(list);
|
|
|
|
}
|
|
|
|
|
2007-03-29 00:40:49 -04:00
|
|
|
/**
|
|
|
|
* @brief Remove an item from the list.
|
|
|
|
*
|
|
|
|
* @param haystack the list to remove the item from
|
|
|
|
* @param needle the data member of the item we're removing
|
|
|
|
* @param fn the comparison function for searching
|
|
|
|
* @param data output parameter containing data of the removed item
|
|
|
|
*
|
|
|
|
* @return the resultant list
|
2007-01-19 04:28:44 -05:00
|
|
|
*/
|
2007-07-18 18:22:02 -04:00
|
|
|
alpm_list_t SYMEXPORT *alpm_list_remove(alpm_list_t *haystack, const void *needle, alpm_list_fn_cmp fn, void **data)
|
2007-11-06 01:55:45 -05:00
|
|
|
{
|
2007-01-19 04:28:44 -05:00
|
|
|
alpm_list_t *i = haystack, *tmp = NULL;
|
|
|
|
|
|
|
|
if(data) {
|
|
|
|
*data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(i) {
|
|
|
|
if(i->data == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tmp = i->next;
|
|
|
|
if(fn(needle, i->data) == 0) {
|
|
|
|
/* we found a matching item */
|
|
|
|
if(i == haystack) {
|
2007-11-06 01:55:45 -05:00
|
|
|
/* Special case: removing the head node which has a back reference to
|
|
|
|
* the tail node */
|
|
|
|
haystack = i->next;
|
|
|
|
if(haystack) {
|
|
|
|
haystack->prev = i->prev;
|
|
|
|
}
|
|
|
|
i->prev = NULL;
|
2007-11-11 11:47:28 -05:00
|
|
|
} else if(i == haystack->prev) {
|
|
|
|
/* Special case: removing the tail node, so we need to fix the back
|
|
|
|
* reference on the head node. We also know tail != head. */
|
|
|
|
if(i->prev) {
|
|
|
|
/* i->next should always be null */
|
|
|
|
i->prev->next = i->next;
|
|
|
|
haystack->prev = i->prev;
|
|
|
|
i->prev = NULL;
|
|
|
|
}
|
2007-11-06 01:55:45 -05:00
|
|
|
} else {
|
2007-11-11 11:47:28 -05:00
|
|
|
/* Normal case, non-head and non-tail node */
|
2007-11-06 01:55:45 -05:00
|
|
|
if(i->next) {
|
|
|
|
i->next->prev = i->prev;
|
|
|
|
}
|
|
|
|
if(i->prev) {
|
|
|
|
i->prev->next = i->next;
|
|
|
|
}
|
2007-01-19 04:28:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if(data) {
|
|
|
|
*data = i->data;
|
|
|
|
}
|
|
|
|
i->data = NULL;
|
|
|
|
free(i);
|
2007-11-06 01:55:45 -05:00
|
|
|
i = NULL;
|
|
|
|
} else {
|
|
|
|
i = tmp;
|
2007-01-19 04:28:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(haystack);
|
|
|
|
}
|
|
|
|
|
2007-03-29 00:40:49 -04:00
|
|
|
/**
|
|
|
|
* @brief Create a new list without any duplicates.
|
|
|
|
*
|
|
|
|
* This does NOT copy data members.
|
|
|
|
*
|
|
|
|
* @param list the list to copy
|
|
|
|
*
|
|
|
|
* @return a new list containing non-duplicate items
|
2007-01-19 04:28:44 -05:00
|
|
|
*/
|
2007-06-05 17:34:33 -04:00
|
|
|
alpm_list_t SYMEXPORT *alpm_list_remove_dupes(const alpm_list_t *list)
|
2007-10-31 12:37:49 -04:00
|
|
|
{
|
2007-06-05 17:34:33 -04:00
|
|
|
const alpm_list_t *lp = list;
|
|
|
|
alpm_list_t *newlist = NULL;
|
2007-01-19 04:28:44 -05:00
|
|
|
while(lp) {
|
2007-11-16 14:50:58 -05:00
|
|
|
if(!alpm_list_find_ptr(newlist, lp->data)) {
|
2007-01-19 04:28:44 -05:00
|
|
|
newlist = alpm_list_add(newlist, lp->data);
|
|
|
|
}
|
|
|
|
lp = lp->next;
|
|
|
|
}
|
|
|
|
return(newlist);
|
|
|
|
}
|
|
|
|
|
2007-03-29 00:40:49 -04:00
|
|
|
/**
|
|
|
|
* @brief Copy a string list, including data.
|
2007-11-16 21:18:45 -05:00
|
|
|
*
|
2007-03-29 00:40:49 -04:00
|
|
|
* @param list the list to copy
|
|
|
|
*
|
|
|
|
* @return a copy of the original list
|
2007-01-19 04:28:44 -05:00
|
|
|
*/
|
2007-07-18 18:22:02 -04:00
|
|
|
alpm_list_t SYMEXPORT *alpm_list_strdup(const alpm_list_t *list)
|
2007-01-19 04:28:44 -05:00
|
|
|
{
|
2007-06-05 17:34:33 -04:00
|
|
|
const alpm_list_t *lp = list;
|
|
|
|
alpm_list_t *newlist = NULL;
|
2007-01-19 04:28:44 -05:00
|
|
|
while(lp) {
|
|
|
|
newlist = alpm_list_add(newlist, strdup(lp->data));
|
|
|
|
lp = lp->next;
|
|
|
|
}
|
|
|
|
return(newlist);
|
|
|
|
}
|
|
|
|
|
2007-10-19 13:17:51 -04:00
|
|
|
/**
|
|
|
|
* @brief Copy a list, without copying data.
|
|
|
|
*
|
|
|
|
* @param list the list to copy
|
|
|
|
*
|
|
|
|
* @return a copy of the original list
|
|
|
|
*/
|
|
|
|
alpm_list_t SYMEXPORT *alpm_list_copy(const alpm_list_t *list)
|
|
|
|
{
|
|
|
|
const alpm_list_t *lp = list;
|
|
|
|
alpm_list_t *newlist = NULL;
|
|
|
|
while(lp) {
|
|
|
|
newlist = alpm_list_add(newlist, lp->data);
|
|
|
|
lp = lp->next;
|
|
|
|
}
|
|
|
|
return(newlist);
|
|
|
|
}
|
|
|
|
|
2007-10-29 22:03:41 -04:00
|
|
|
/**
|
|
|
|
* @brief Copy a list and copy the data.
|
2007-11-14 23:51:16 -05:00
|
|
|
* Note that the data elements to be copied should not contain pointers
|
|
|
|
* and should also be of constant size.
|
2007-10-29 22:03:41 -04:00
|
|
|
*
|
|
|
|
* @param list the list to copy
|
2007-11-14 23:51:16 -05:00
|
|
|
* @param size the size of each data element
|
2007-10-29 22:03:41 -04:00
|
|
|
*
|
|
|
|
* @return a copy of the original list, data copied as well
|
|
|
|
*/
|
2007-11-14 23:51:16 -05:00
|
|
|
alpm_list_t SYMEXPORT *alpm_list_copy_data(const alpm_list_t *list,
|
|
|
|
size_t size)
|
2007-10-29 22:03:41 -04:00
|
|
|
{
|
|
|
|
const alpm_list_t *lp = list;
|
|
|
|
alpm_list_t *newlist = NULL;
|
|
|
|
while(lp) {
|
2007-11-14 23:51:16 -05:00
|
|
|
void *newdata = calloc(1, size);
|
2007-10-29 22:03:41 -04:00
|
|
|
if(newdata) {
|
2007-11-14 23:51:16 -05:00
|
|
|
memcpy(newdata, lp->data, size);
|
2007-10-29 22:03:41 -04:00
|
|
|
newlist = alpm_list_add(newlist, newdata);
|
|
|
|
lp = lp->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(newlist);
|
|
|
|
}
|
|
|
|
|
2007-03-29 00:40:49 -04:00
|
|
|
/**
|
|
|
|
* @brief Create a new list in reverse order.
|
|
|
|
*
|
|
|
|
* @param list the list to copy
|
|
|
|
*
|
|
|
|
* @return a new list in reverse order
|
2007-01-19 04:28:44 -05:00
|
|
|
*/
|
2007-07-18 18:22:02 -04:00
|
|
|
alpm_list_t SYMEXPORT *alpm_list_reverse(alpm_list_t *list)
|
2007-10-31 12:37:49 -04:00
|
|
|
{
|
|
|
|
const alpm_list_t *lp;
|
|
|
|
alpm_list_t *newlist = NULL;
|
2007-01-19 04:28:44 -05:00
|
|
|
|
|
|
|
lp = alpm_list_last(list);
|
2007-11-06 01:55:45 -05:00
|
|
|
if(list) {
|
|
|
|
/* break our reverse circular list */
|
|
|
|
list->prev = NULL;
|
|
|
|
}
|
|
|
|
|
2007-01-19 04:28:44 -05:00
|
|
|
while(lp) {
|
|
|
|
newlist = alpm_list_add(newlist, lp->data);
|
|
|
|
lp = lp->prev;
|
|
|
|
}
|
|
|
|
return(newlist);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Accessors */
|
|
|
|
|
2007-03-29 00:40:49 -04:00
|
|
|
/**
|
|
|
|
* @brief Get the first element of a list.
|
|
|
|
*
|
2007-01-19 04:28:44 -05:00
|
|
|
* @param list the list
|
2007-03-29 00:40:49 -04:00
|
|
|
*
|
2007-01-19 04:28:44 -05:00
|
|
|
* @return the first element in the list
|
|
|
|
*/
|
2007-06-05 17:34:33 -04:00
|
|
|
inline alpm_list_t SYMEXPORT *alpm_list_first(const alpm_list_t *list)
|
2007-01-19 04:28:44 -05:00
|
|
|
{
|
2007-06-05 17:34:33 -04:00
|
|
|
return((alpm_list_t*)list);
|
2007-01-19 04:28:44 -05:00
|
|
|
}
|
|
|
|
|
2007-03-29 00:40:49 -04:00
|
|
|
/**
|
|
|
|
* @brief Return nth element from list (starting from 0).
|
|
|
|
*
|
|
|
|
* @param list the list
|
|
|
|
* @param n the index of the item to find
|
|
|
|
*
|
|
|
|
* @return an alpm_list_t node for index `n`
|
2007-01-19 04:28:44 -05:00
|
|
|
*/
|
2007-07-18 18:22:02 -04:00
|
|
|
alpm_list_t SYMEXPORT *alpm_list_nth(const alpm_list_t *list, int n)
|
2007-01-19 04:28:44 -05:00
|
|
|
{
|
2007-06-05 17:34:33 -04:00
|
|
|
const alpm_list_t *i = list;
|
2007-01-19 04:28:44 -05:00
|
|
|
while(n--) {
|
|
|
|
i = i->next;
|
|
|
|
}
|
2007-06-05 17:34:33 -04:00
|
|
|
return((alpm_list_t*)i);
|
2007-01-19 04:28:44 -05:00
|
|
|
}
|
|
|
|
|
2007-03-29 00:40:49 -04:00
|
|
|
/**
|
|
|
|
* @brief Get the next element of a list.
|
|
|
|
*
|
|
|
|
* @param node the list node
|
|
|
|
*
|
2007-01-19 04:28:44 -05:00
|
|
|
* @return the next element, or NULL when no more elements exist
|
|
|
|
*/
|
2007-06-05 17:34:33 -04:00
|
|
|
inline alpm_list_t SYMEXPORT *alpm_list_next(const alpm_list_t *node)
|
2007-01-19 04:28:44 -05:00
|
|
|
{
|
2007-03-29 00:40:49 -04:00
|
|
|
return(node->next);
|
2007-01-19 04:28:44 -05:00
|
|
|
}
|
2007-03-29 00:40:49 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the last item in the list.
|
|
|
|
*
|
|
|
|
* @param list the list
|
|
|
|
*
|
|
|
|
* @return the last element in the list
|
2007-01-19 04:28:44 -05:00
|
|
|
*/
|
2007-07-18 18:22:02 -04:00
|
|
|
alpm_list_t SYMEXPORT *alpm_list_last(const alpm_list_t *list)
|
2007-01-19 04:28:44 -05:00
|
|
|
{
|
2007-11-06 01:55:45 -05:00
|
|
|
if(list) {
|
|
|
|
return(list->prev);
|
|
|
|
} else {
|
|
|
|
return(NULL);
|
2007-01-19 04:28:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-29 00:40:49 -04:00
|
|
|
/**
|
|
|
|
* @brief Get the data member of a list node.
|
|
|
|
*
|
|
|
|
* @param node the list node
|
|
|
|
*
|
2007-01-19 04:28:44 -05:00
|
|
|
* @return the contained data, or NULL if none
|
|
|
|
*/
|
2007-03-29 00:40:49 -04:00
|
|
|
void SYMEXPORT *alpm_list_getdata(const alpm_list_t *node)
|
2007-01-19 04:28:44 -05:00
|
|
|
{
|
2007-03-29 00:40:49 -04:00
|
|
|
if(node == NULL) return(NULL);
|
|
|
|
return(node->data);
|
2007-01-19 04:28:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Misc */
|
|
|
|
|
2007-03-29 00:40:49 -04:00
|
|
|
/**
|
|
|
|
* @brief Get the number of items in a list.
|
|
|
|
*
|
|
|
|
* @param list the list
|
|
|
|
*
|
|
|
|
* @return the number of list items
|
2007-01-19 04:28:44 -05:00
|
|
|
*/
|
2007-01-30 02:47:19 -05:00
|
|
|
int SYMEXPORT alpm_list_count(const alpm_list_t *list)
|
2007-01-19 04:28:44 -05:00
|
|
|
{
|
|
|
|
unsigned int i = 0;
|
|
|
|
const alpm_list_t *lp = list;
|
|
|
|
while(lp) {
|
|
|
|
++i;
|
|
|
|
lp = lp->next;
|
|
|
|
}
|
|
|
|
return(i);
|
|
|
|
}
|
|
|
|
|
2007-03-29 00:40:49 -04:00
|
|
|
/**
|
|
|
|
* @brief Find an item in a list.
|
2007-11-16 21:18:45 -05:00
|
|
|
*
|
2007-11-16 14:50:58 -05:00
|
|
|
* @param needle the item to search
|
2007-03-29 00:40:49 -04:00
|
|
|
* @param haystack the list
|
2007-11-16 14:50:58 -05:00
|
|
|
* @param fn the comparison function for searching (!= NULL)
|
2007-03-29 00:40:49 -04:00
|
|
|
*
|
2007-12-02 17:48:12 -05:00
|
|
|
* @return `needle` if found, NULL otherwise
|
2007-01-19 04:28:44 -05:00
|
|
|
*/
|
2007-12-02 17:48:12 -05:00
|
|
|
void SYMEXPORT *alpm_list_find(const alpm_list_t *haystack, const void *needle,
|
2007-11-16 14:50:58 -05:00
|
|
|
alpm_list_fn_cmp fn)
|
2007-01-19 04:28:44 -05:00
|
|
|
{
|
2007-06-05 17:34:33 -04:00
|
|
|
const alpm_list_t *lp = haystack;
|
2007-01-19 04:28:44 -05:00
|
|
|
while(lp) {
|
2007-11-16 14:50:58 -05:00
|
|
|
if(lp->data && fn(lp->data, needle) == 0) {
|
2007-12-02 17:48:12 -05:00
|
|
|
return(lp->data);
|
2007-01-19 04:28:44 -05:00
|
|
|
}
|
|
|
|
lp = lp->next;
|
|
|
|
}
|
2007-12-02 17:48:12 -05:00
|
|
|
return(NULL);
|
2007-01-19 04:28:44 -05:00
|
|
|
}
|
|
|
|
|
2007-11-16 14:50:58 -05:00
|
|
|
/* trivial helper function for alpm_list_find_ptr */
|
|
|
|
static int ptrcmp(const void *p, const void *q)
|
|
|
|
{
|
|
|
|
return(p != q);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Find an item in a list.
|
|
|
|
*
|
|
|
|
* Search for the item whos data matches that of the `needle`.
|
|
|
|
*
|
|
|
|
* @param needle the data to search for (== comparison)
|
|
|
|
* @param haystack the list
|
|
|
|
*
|
2007-12-02 17:48:12 -05:00
|
|
|
* @return `needle` if found, NULL otherwise
|
2007-11-16 14:50:58 -05:00
|
|
|
*/
|
2007-12-02 17:48:12 -05:00
|
|
|
void SYMEXPORT *alpm_list_find_ptr(const alpm_list_t *haystack, const void *needle)
|
2007-11-16 14:50:58 -05:00
|
|
|
{
|
|
|
|
return(alpm_list_find(haystack, needle, ptrcmp));
|
|
|
|
}
|
|
|
|
|
2007-03-29 00:40:49 -04:00
|
|
|
/**
|
|
|
|
* @brief Find a string in a list.
|
|
|
|
*
|
|
|
|
* @param needle the string to search for
|
|
|
|
* @param haystack the list
|
|
|
|
*
|
2007-12-02 17:48:12 -05:00
|
|
|
* @return `needle` if found, NULL otherwise
|
2007-01-19 04:28:44 -05:00
|
|
|
*/
|
2007-12-02 17:48:12 -05:00
|
|
|
char SYMEXPORT *alpm_list_find_str(const alpm_list_t *haystack, const char *needle)
|
2007-01-19 04:28:44 -05:00
|
|
|
{
|
2007-12-02 17:48:12 -05:00
|
|
|
return((char *)alpm_list_find(haystack, (const void*)needle, (alpm_list_fn_cmp)strcmp));
|
2007-01-19 04:28:44 -05:00
|
|
|
}
|
|
|
|
|
2007-03-29 00:40:49 -04:00
|
|
|
/**
|
|
|
|
* @brief Find the items in list `lhs` that are not present in list `rhs`.
|
|
|
|
*
|
|
|
|
* Entries are not duplicated. Operation is O(m*n). The first list is stepped
|
|
|
|
* through one node at a time, and for each node in the first list, each node
|
|
|
|
* in the second list is compared to it.
|
|
|
|
*
|
2007-03-01 02:03:05 -05:00
|
|
|
* @param lhs the first list
|
|
|
|
* @param rhs the second list
|
2007-03-29 00:40:49 -04:00
|
|
|
* @param fn the comparison function
|
|
|
|
*
|
|
|
|
* @return a list containing all items in `lhs` not present in `rhs`
|
2007-03-01 02:03:05 -05:00
|
|
|
*/
|
2007-07-18 18:22:02 -04:00
|
|
|
alpm_list_t SYMEXPORT *alpm_list_diff(const alpm_list_t *lhs,
|
2007-06-05 17:34:33 -04:00
|
|
|
const alpm_list_t *rhs, alpm_list_fn_cmp fn)
|
2007-03-01 02:03:05 -05:00
|
|
|
{
|
2007-06-05 17:34:33 -04:00
|
|
|
const alpm_list_t *i, *j;
|
|
|
|
alpm_list_t *ret = NULL;
|
2007-03-01 02:03:05 -05:00
|
|
|
for(i = lhs; i; i = i->next) {
|
|
|
|
int found = 0;
|
|
|
|
for(j = rhs; j; j = j->next) {
|
|
|
|
if(fn(i->data, j->data) == 0) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!found) {
|
|
|
|
ret = alpm_list_add(ret, i->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
2007-01-19 04:28:44 -05:00
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/* vim: set ts=2 sw=2 noet: */
|