1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-11-11 11:55:12 -05:00

Generalized alpm_list_find.

The old alpm_list_find was renamed to alpm_list_find_ptr, and a new
alpm_list_find was introduced, which uses the fn comparison-function
parameter in its decision.
Now both alpm_list_find_ptr (a new ptrcmp helper function was also
added) and alpm_list_find_str are just an alpm_list_find call.

Signed-off-by: Nagy Gabor <ngaba@bibl.u-szeged.hu>
Signed-off-by: Chantry Xavier <shiningxc@gmail.com>
[Dan: made ptrcmp a static function]
Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Nagy Gabor 2007-11-16 20:50:58 +01:00 committed by Dan McGee
parent 04b7d2ad14
commit d311ad067f
5 changed files with 34 additions and 20 deletions

View File

@ -282,7 +282,7 @@ static int upgrade_remove(pmpkg_t *oldpkg, pmpkg_t *newpkg, pmtrans_t *trans, pm
for(b = alpm_pkg_get_backup(newpkg); b; b = b->next) { for(b = alpm_pkg_get_backup(newpkg); b; b = b->next) {
const char *backup = b->data; const char *backup = b->data;
/* make sure we don't add duplicate entries */ /* make sure we don't add duplicate entries */
if(!alpm_list_find(handle->noupgrade, backup)) { if(!alpm_list_find_ptr(handle->noupgrade, backup)) {
_alpm_log(PM_LOG_DEBUG, "adding %s to the NoUpgrade array temporarily\n", _alpm_log(PM_LOG_DEBUG, "adding %s to the NoUpgrade array temporarily\n",
backup); backup);
handle->noupgrade = alpm_list_add(handle->noupgrade, strdup(backup)); handle->noupgrade = alpm_list_add(handle->noupgrade, strdup(backup));

View File

@ -353,7 +353,7 @@ alpm_list_t SYMEXPORT *alpm_list_remove_dupes(const alpm_list_t *list)
const alpm_list_t *lp = list; const alpm_list_t *lp = list;
alpm_list_t *newlist = NULL; alpm_list_t *newlist = NULL;
while(lp) { while(lp) {
if(!alpm_list_find(newlist, lp->data)) { if(!alpm_list_find_ptr(newlist, lp->data)) {
newlist = alpm_list_add(newlist, lp->data); newlist = alpm_list_add(newlist, lp->data);
} }
lp = lp->next; lp = lp->next;
@ -543,18 +543,18 @@ int SYMEXPORT alpm_list_count(const alpm_list_t *list)
/** /**
* @brief Find an item in a list. * @brief Find an item in a list.
* *
* Search for the item whos data matches that of the `needle`. * @param needle the item to search
*
* @param needle the data to search for (== comparison)
* @param haystack the list * @param haystack the list
* @param fn the comparison function for searching (!= NULL)
* *
* @return 1 if `needle` is found, 0 otherwise * @return 1 if `needle` is found, 0 otherwise
*/ */
int SYMEXPORT alpm_list_find(const alpm_list_t *haystack, const void *needle) int SYMEXPORT alpm_list_find(const alpm_list_t *haystack, const void *needle,
alpm_list_fn_cmp fn)
{ {
const alpm_list_t *lp = haystack; const alpm_list_t *lp = haystack;
while(lp) { while(lp) {
if(lp->data == needle) { if(lp->data && fn(lp->data, needle) == 0) {
return(1); return(1);
} }
lp = lp->next; lp = lp->next;
@ -562,9 +562,29 @@ int SYMEXPORT alpm_list_find(const alpm_list_t *haystack, const void *needle)
return(0); return(0);
} }
/* 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
*
* @return 1 if `needle` is found, 0 otherwise
*/
int SYMEXPORT alpm_list_find_ptr(const alpm_list_t *haystack, const void *needle)
{
return(alpm_list_find(haystack, needle, ptrcmp));
}
/** /**
* @brief Find a string in a list. * @brief Find a string in a list.
* Optimization of alpm_list_find for strings.
* *
* @param needle the string to search for * @param needle the string to search for
* @param haystack the list * @param haystack the list
@ -573,14 +593,7 @@ int SYMEXPORT alpm_list_find(const alpm_list_t *haystack, const void *needle)
*/ */
int SYMEXPORT alpm_list_find_str(const alpm_list_t *haystack, const char *needle) int SYMEXPORT alpm_list_find_str(const alpm_list_t *haystack, const char *needle)
{ {
const alpm_list_t *lp = haystack; return(alpm_list_find(haystack, (const void*)needle, (alpm_list_fn_cmp)strcmp));
while(lp) {
if(lp->data && strcmp((const char *)lp->data, needle) == 0) {
return(1);
}
lp = lp->next;
}
return(0);
} }
/** /**

View File

@ -72,8 +72,9 @@ void *alpm_list_getdata(const alpm_list_t *entry);
/* misc */ /* misc */
int alpm_list_count(const alpm_list_t *list); int alpm_list_count(const alpm_list_t *list);
int alpm_list_find(const alpm_list_t *haystack, const void *needle); int alpm_list_find(const alpm_list_t *haystack, const void *needle, alpm_list_fn_cmp fn);
int alpm_list_find_str(const alpm_list_t *haystack,const char *needle); int alpm_list_find_ptr(const alpm_list_t *haystack, const void *needle);
int alpm_list_find_str(const alpm_list_t *haystack, const char *needle);
alpm_list_t *alpm_list_diff(const alpm_list_t *lhs, const alpm_list_t *rhs, alpm_list_fn_cmp fn); alpm_list_t *alpm_list_diff(const alpm_list_t *lhs, const alpm_list_t *rhs, alpm_list_fn_cmp fn);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -240,7 +240,7 @@ int SYMEXPORT alpm_db_update(int force, pmdb_t *db)
ASSERT(handle->trans->state == STATE_INITIALIZED, RET_ERR(PM_ERR_TRANS_NOT_INITIALIZED, -1)); ASSERT(handle->trans->state == STATE_INITIALIZED, RET_ERR(PM_ERR_TRANS_NOT_INITIALIZED, -1));
ASSERT(handle->trans->type == PM_TRANS_TYPE_SYNC, RET_ERR(PM_ERR_TRANS_TYPE, -1)); ASSERT(handle->trans->type == PM_TRANS_TYPE_SYNC, RET_ERR(PM_ERR_TRANS_TYPE, -1));
if(!alpm_list_find(handle->dbs_sync, db)) { if(!alpm_list_find_ptr(handle->dbs_sync, db)) {
RET_ERR(PM_ERR_DB_NOT_FOUND, -1); RET_ERR(PM_ERR_DB_NOT_FOUND, -1);
} }

View File

@ -170,7 +170,7 @@ static alpm_list_t *shortest_delta_path(alpm_list_t *deltas,
/* If this vertex has already been visited in the path, go to the /* If this vertex has already been visited in the path, go to the
* next vertex. */ * next vertex. */
if(alpm_list_find(path, v)) if(alpm_list_find_ptr(path, v))
continue; continue;
/* Once we find a vertex that starts at the 'from' version, /* Once we find a vertex that starts at the 'from' version,