diff options
author | Nagy Gabor <ngaba@bibl.u-szeged.hu> | 2007-11-16 20:50:58 +0100 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2007-11-17 09:44:48 -0600 |
commit | d311ad067f47608252b9276df90087db98b1100f (patch) | |
tree | 3b51cb2d4949d1bfdca0afcc88ca3a4be2ada4fa | |
parent | 04b7d2ad140058a7cab911fb77ec0285bf76b059 (diff) |
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>
-rw-r--r-- | lib/libalpm/add.c | 2 | ||||
-rw-r--r-- | lib/libalpm/alpm_list.c | 43 | ||||
-rw-r--r-- | lib/libalpm/alpm_list.h | 5 | ||||
-rw-r--r-- | lib/libalpm/db.c | 2 | ||||
-rw-r--r-- | lib/libalpm/delta.c | 2 |
5 files changed, 34 insertions, 20 deletions
diff --git a/lib/libalpm/add.c b/lib/libalpm/add.c index ea661dc4..a85c4d82 100644 --- a/lib/libalpm/add.c +++ b/lib/libalpm/add.c @@ -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) { const char *backup = b->data; /* 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", backup); handle->noupgrade = alpm_list_add(handle->noupgrade, strdup(backup)); diff --git a/lib/libalpm/alpm_list.c b/lib/libalpm/alpm_list.c index 070101de..e854cf63 100644 --- a/lib/libalpm/alpm_list.c +++ b/lib/libalpm/alpm_list.c @@ -353,7 +353,7 @@ alpm_list_t SYMEXPORT *alpm_list_remove_dupes(const alpm_list_t *list) const alpm_list_t *lp = list; alpm_list_t *newlist = NULL; while(lp) { - if(!alpm_list_find(newlist, lp->data)) { + if(!alpm_list_find_ptr(newlist, lp->data)) { newlist = alpm_list_add(newlist, lp->data); } lp = lp->next; @@ -543,18 +543,18 @@ int SYMEXPORT alpm_list_count(const alpm_list_t *list) /** * @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 needle the item to search * @param haystack the list + * @param fn the comparison function for searching (!= NULL) * * @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; while(lp) { - if(lp->data == needle) { + if(lp->data && fn(lp->data, needle) == 0) { return(1); } lp = lp->next; @@ -562,9 +562,29 @@ int SYMEXPORT alpm_list_find(const alpm_list_t *haystack, const void *needle) 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. - * Optimization of alpm_list_find for strings. * * @param needle the string to search for * @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) { - const alpm_list_t *lp = haystack; - while(lp) { - if(lp->data && strcmp((const char *)lp->data, needle) == 0) { - return(1); - } - lp = lp->next; - } - return(0); + return(alpm_list_find(haystack, (const void*)needle, (alpm_list_fn_cmp)strcmp)); } /** diff --git a/lib/libalpm/alpm_list.h b/lib/libalpm/alpm_list.h index d6e9882b..a24aa8db 100644 --- a/lib/libalpm/alpm_list.h +++ b/lib/libalpm/alpm_list.h @@ -72,8 +72,9 @@ void *alpm_list_getdata(const alpm_list_t *entry); /* misc */ 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_str(const alpm_list_t *haystack,const char *needle); +int alpm_list_find(const alpm_list_t *haystack, const void *needle, alpm_list_fn_cmp fn); +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); #ifdef __cplusplus diff --git a/lib/libalpm/db.c b/lib/libalpm/db.c index d02ef144..f7621338 100644 --- a/lib/libalpm/db.c +++ b/lib/libalpm/db.c @@ -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->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); } diff --git a/lib/libalpm/delta.c b/lib/libalpm/delta.c index dfe4a946..8daac0c5 100644 --- a/lib/libalpm/delta.c +++ b/lib/libalpm/delta.c @@ -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 * next vertex. */ - if(alpm_list_find(path, v)) + if(alpm_list_find_ptr(path, v)) continue; /* Once we find a vertex that starts at the 'from' version, |