[pacman-dev] [PATCH 2/5] Generalized alpm_list_find.
Nagy Gabor
shiningxc at gmail.com
Sat Nov 17 08:51:00 EST 2007
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 at bibl.u-szeged.hu>
Signed-off-by: Chantry Xavier <shiningxc at gmail.com>
---
lib/libalpm/add.c | 2 +-
lib/libalpm/alpm_list.c | 43 ++++++++++++++++++++++++++++---------------
lib/libalpm/alpm_list.h | 5 +++--
lib/libalpm/db.c | 2 +-
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 d242134..1da874a 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 c1e4cb8..08344d6 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;
@@ -542,19 +542,19 @@ 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 */
+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 b364670..b3bfa70 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 150b365..80c1c12 100644
--- a/lib/libalpm/db.c
+++ b/lib/libalpm/db.c
@@ -239,7 +239,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 0e9dcc3..3d1c7d7 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,
--
1.5.3.5
More information about the pacman-dev
mailing list