From ea71d6eb4fd676320678b74779aadcceb1cf5752 Mon Sep 17 00:00:00 2001 From: bonefish Date: Sat, 11 Jun 2011 13:02:41 +0000 Subject: [PATCH] Made Remove() safe to be called with an element not in the list and changed its return type to bool to indicate whether it was. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@42096 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/kernel/util/SinglyLinkedList.h | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/headers/private/kernel/util/SinglyLinkedList.h b/headers/private/kernel/util/SinglyLinkedList.h index 755ad680aa..ed13417178 100644 --- a/headers/private/kernel/util/SinglyLinkedList.h +++ b/headers/private/kernel/util/SinglyLinkedList.h @@ -142,7 +142,7 @@ class SinglyLinkedList { inline bool IsEmpty() const { return (fFirst == NULL); } inline void Add(Element* element); - inline void Remove(Element* element); + inline bool Remove(Element* element); inline void Remove(Element* previous, Element* element); inline void MoveFrom(SINGLY_LINKED_LIST_CLASS_NAME* fromList); @@ -184,16 +184,25 @@ SINGLY_LINKED_LIST_CLASS_NAME::Add(Element* element) } +/*! Removes \a element from the list. + It is safe to call the list with a \c NULL element or an element that isn't + in the list. + \param element The element to be removed. + \return \c true, if the element was in the list and has been removed, + \c false otherwise. +*/ SINGLY_LINKED_LIST_TEMPLATE_LIST -void +bool SINGLY_LINKED_LIST_CLASS_NAME::Remove(Element* element) { if (element == NULL) - return; + return false; Element* next = fFirst; Element* last = NULL; - while (next != NULL && element != next) { + while (element != next) { + if (next == NULL) + return false; last = next; next = sGetLink(next)->next; } @@ -205,6 +214,7 @@ SINGLY_LINKED_LIST_CLASS_NAME::Remove(Element* element) sGetLink(last)->next = elementLink->next; elementLink->next = NULL; + return true; }