Git-Url: 
http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=commitdiff;h=0ea56fb040ef270ff58e9208752569cb7d6f760f

commit 0ea56fb040ef270ff58e9208752569cb7d6f760f
Author: Michel Hermier <[email protected]>
Date:   Tue Sep 2 22:28:14 2014 +0200

libpacman: Simplify _pacman_list_remove (make it not destroy list head, will 
help to make list not pointers in classes).

diff --git a/lib/libpacman/pacman.cpp b/lib/libpacman/pacman.cpp
index 0cf5de6..98a8f38 100644
--- a/lib/libpacman/pacman.cpp
+++ b/lib/libpacman/pacman.cpp
@@ -116,7 +116,7 @@ int pacman_release(void)
pacman_db_unregister(c_cast(handle->db_local));
}
/* and also sync ones */
-       while(handle->dbs_sync) {
+       while(!handle->dbs_sync->empty()) {
/* db_unregister() will also update the handle->dbs_sync list */
pacman_db_unregister(f_ptrlistitem_data(handle->dbs_sync->begin()));
}
diff --git a/lib/libpacman/util/fptrlist.cpp b/lib/libpacman/util/fptrlist.cpp
index c097d1b..4ee403c 100644
--- a/lib/libpacman/util/fptrlist.cpp
+++ b/lib/libpacman/util/fptrlist.cpp
@@ -76,48 +76,55 @@ FPtrList *f_ptrlist_add_sorted(FPtrList *list, void *data, 
_pacman_fn_cmp fn)
*/
FPtrList *_pacman_list_remove(FPtrList *haystack, void *needle, _pacman_fn_cmp 
fn, void **data)
{
-#ifndef F_NOCOMPAT
-       FPtrListIterator *end = f_ptrlist_end(haystack), *i = 
f_ptrlist_first(haystack);
+       ASSERT(haystack != NULL, RET_ERR(PM_ERR_WRONG_ARGS, NULL));

-       if(*data != end) {
+       if(data != NULL) {
*data = NULL;
}

-       while(i) {
-               if(i->m_data == NULL) {
-                       continue;
-               }
-               if(fn(needle, i->m_data) == 0) {
-                       break;
-               }
-               i = i->m_next;
-       }
+#ifndef F_NOCOMPAT
+       if(haystack->empty()) {
+               return haystack;
+       }

-       if(i) {
-               /* we found a matching item */
-               if(i->m_next) {
-                       i->m_next->m_previous = i->m_previous;
-               }
-               if(i->m_previous) {
-                       i->m_previous->m_next = i->m_next;
+       if(fn(needle, haystack->m_data) == 0) {
+               /* The item found is the first in the chain */
+               FCListItem *next = haystack->next();
+
+               if(data != NULL) {
+                       *data = haystack->m_data;
}
-               if(i == haystack) {
-                       /* The item found is the first in the chain */
-                       haystack = haystack->m_next;
+               if(next == NULL) {
+                       /* Mark list empty */
+                       haystack->m_data = NULL;
+               } else {
+                       /* Move data of next item in list head */
+                       haystack->m_data = next->m_data;
+                       haystack->m_next = next->next();
+                       if(haystack->m_next) {
+                               haystack->m_next->m_previous = haystack;
+                       }
}
-
-               if(data) {
-                       *data = i->m_data;
+               return haystack;
+       }
+#endif
+       for(FPtrListIterator *i = f_ptrlist_first(haystack), *end = 
f_ptrlist_end(haystack); i != end; i = i->next()) {
+               if(fn(needle, i->m_data) == 0) {
+                       /* we found a matching item */
+                       if(i->m_next) {
+                               i->m_next->m_previous = i->m_previous;
+                       }
+                       if(i->m_previous) {
+                               i->m_previous->m_next = i->m_next;
+                       }
+                       if(data) {
+                               *data = i->m_data;
+                       }
+                       delete i;
+                       break;
}
-               i->m_data = NULL;
-               free(i);
}
-
-       return(haystack);
-#else
-       // FIXME: Implement me
return haystack;
-#endif
}

/* Reverse the order of a list
_______________________________________________
Frugalware-git mailing list
[email protected]
http://frugalware.org/mailman/listinfo/frugalware-git

Reply via email to