Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=commitdiff;h=2d75db42de0eef81610e615ff16fdc4f25b5dddd
commit 2d75db42de0eef81610e615ff16fdc4f25b5dddd Author: Michel Hermier <[email protected]> Date: Mon Sep 8 21:09:45 2014 +0200 libpacman: Rework list item removal. diff --git a/lib/libpacman/util/flist.h b/lib/libpacman/util/flist.h index 26006cc..ff67e37 100644 --- a/lib/libpacman/util/flist.h +++ b/lib/libpacman/util/flist.h @@ -376,8 +376,6 @@ class FCListItem public: friend struct flib::iterable_traits<FCListItem *>; - friend bool _pacman_list_remove(FPtrList *haystack, void *needle, _pacman_fn_cmp fn, void **data); - typedef void *value_type; typedef value_type *pointer; typedef size_t size_type; @@ -446,8 +444,8 @@ public: return m_previous; } -protected: - void swap(FCListItem &o) { + void swap(FCListItem &o) + { std::swap(m_next, o.m_next); std::swap(m_previous, o.m_previous); if(m_next != &o) { @@ -464,6 +462,13 @@ protected: } } + void remove() + { + m_next->m_previous = m_previous; + m_previous->m_next = m_next; + m_next = m_previous = NULL; + } + protected: FCListItem *m_next; FCListItem *m_previous; @@ -611,7 +616,7 @@ public: virtual ~FList() override { -// clear(); + clear(); } /* Iterators */ @@ -693,10 +698,11 @@ public: /* Element access */ /* Modifiers */ -#if 0 void clear() - { } -#endif + { + // FIXME: lets leak for now + m_next = m_previous = this; + } public: /* extensions */ @@ -728,7 +734,6 @@ public: private: FList(const FList &); - FList &operator = (const FList &); virtual void *c_data() const override diff --git a/lib/libpacman/util/fptrlist.cpp b/lib/libpacman/util/fptrlist.cpp index 0de0955..8b928c8 100644 --- a/lib/libpacman/util/fptrlist.cpp +++ b/lib/libpacman/util/fptrlist.cpp @@ -55,31 +55,10 @@ FPtrList *f_ptrlist_add_sorted(FPtrList *list, void *data, _pacman_fn_cmp fn) * Otherwise, return false and 'data' it is set to NULL. * Return the new list (without the removed element). */ -bool _pacman_list_remove(FPtrList *haystack, void *needle, _pacman_fn_cmp fn, void **data) +bool _pacman_list_remove(FPtrList *self, void *ptr, _pacman_fn_cmp fn, void **data) { - ASSERT(haystack != NULL, RET_ERR(PM_ERR_WRONG_ARGS, false)); - - if(data != NULL) { - *data = NULL; - } - - 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; - return true; - } - } - return false; + ASSERT(self != NULL, RET_ERR(PM_ERR_WRONG_ARGS, false)); + return self->remove(ptr, fn, data); } /* Reverse the order of a list @@ -144,6 +123,26 @@ FPtrList &FPtrList::add(void *data) return *this; } +bool FPtrList::remove(void *ptr, _pacman_fn_cmp fn, void **data) +{ + if(data != NULL) { + *data = NULL; + } + + for(auto i = begin(), end = this->end(); i != end; i = i->next()) { + if(fn(ptr, i->m_data) == 0) { + /* we found a matching item */ + i->remove(); + if(data) { + *data = i->m_data; + } + delete i; + return true; + } + } + return false; +} + FPtrList *f_ptrlist_add(FPtrList *list, void *data) { if (list == NULL) { diff --git a/lib/libpacman/util/fptrlist.h b/lib/libpacman/util/fptrlist.h index a76058e..6420063 100644 --- a/lib/libpacman/util/fptrlist.h +++ b/lib/libpacman/util/fptrlist.h @@ -158,6 +158,7 @@ public: } FPtrList &add(void *data); + bool remove(void *ptr, _pacman_fn_cmp fn, void **data); void clear() { _______________________________________________ Frugalware-git mailing list [email protected] http://frugalware.org/mailman/listinfo/frugalware-git
