* Added RemoveTail() method.

* Renamed DoublyLinkedList::Size() to Count(), since it actually counts the
  items (ie. O(n)).


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@29979 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2009-04-06 21:06:19 +00:00
parent f9ae64e9a5
commit fd0803f300
6 changed files with 105 additions and 92 deletions
+97 -84
View File
@@ -1,5 +1,7 @@
/* /*
* Copyright 2005-2007, Ingo Weinhold, [email protected]. All rights reserved. * Copyright 2005-2007, Ingo Weinhold, [email protected].
* Copyright 2006-2009, Axel Dörfler, [email protected].
*
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#ifndef KERNEL_UTIL_DOUBLY_LINKED_LIST_H #ifndef KERNEL_UTIL_DOUBLY_LINKED_LIST_H
@@ -40,9 +42,9 @@ public:
DoublyLinkedListLinkImpl() : fDoublyLinkedListLink() {} DoublyLinkedListLinkImpl() : fDoublyLinkedListLink() {}
~DoublyLinkedListLinkImpl() {} ~DoublyLinkedListLinkImpl() {}
DLL_Link *GetDoublyLinkedListLink() DLL_Link* GetDoublyLinkedListLink()
{ return &fDoublyLinkedListLink; } { return &fDoublyLinkedListLink; }
const DLL_Link *GetDoublyLinkedListLink() const const DLL_Link* GetDoublyLinkedListLink() const
{ return &fDoublyLinkedListLink; } { return &fDoublyLinkedListLink; }
private: private:
@@ -56,12 +58,12 @@ private:
typedef DoublyLinkedListLink<Element> Link; typedef DoublyLinkedListLink<Element> Link;
public: public:
inline Link *operator()(Element *element) const inline Link* operator()(Element* element) const
{ {
return element->GetDoublyLinkedListLink(); return element->GetDoublyLinkedListLink();
} }
inline const Link *operator()(const Element *element) const inline const Link* operator()(const Element* element) const
{ {
return element->GetDoublyLinkedListLink(); return element->GetDoublyLinkedListLink();
} }
@@ -75,12 +77,12 @@ private:
typedef DoublyLinkedListLink<Element> Link; typedef DoublyLinkedListLink<Element> Link;
public: public:
inline Link *operator()(Element *element) const inline Link* operator()(Element* element) const
{ {
return &(element->*LinkMember); return &(element->*LinkMember);
} }
inline const Link *operator()(const Element *element) const inline const Link* operator()(const Element* element) const
{ {
return &(element->*LinkMember); return &(element->*LinkMember);
} }
@@ -93,14 +95,14 @@ class DoublyLinkedListCLink {
typedef DoublyLinkedListLink<Element> Link; typedef DoublyLinkedListLink<Element> Link;
public: public:
inline Link *operator()(Element *element) const inline Link* operator()(Element* element) const
{ {
return (Link *)&element->link; return (Link*)&element->link;
} }
inline const Link *operator()(const Element *element) const inline const Link* operator()(const Element* element) const
{ {
return (const Link *)&element->link; return (const Link*)&element->link;
} }
}; };
@@ -120,7 +122,7 @@ private:
public: public:
class Iterator { class Iterator {
public: public:
Iterator(List *list) Iterator(List* list)
: :
fList(list) fList(list)
{ {
@@ -141,7 +143,7 @@ public:
return fNext; return fNext;
} }
Element *Next() Element* Next()
{ {
fCurrent = fNext; fCurrent = fNext;
if (fNext) if (fNext)
@@ -149,14 +151,14 @@ public:
return fCurrent; return fCurrent;
} }
Element *Current() Element* Current()
{ {
return fCurrent; return fCurrent;
} }
Element *Remove() Element* Remove()
{ {
Element *element = fCurrent; Element* element = fCurrent;
if (fCurrent) { if (fCurrent) {
fList->Remove(fCurrent); fList->Remove(fCurrent);
fCurrent = NULL; fCurrent = NULL;
@@ -164,7 +166,7 @@ public:
return element; return element;
} }
Iterator &operator=(const Iterator &other) Iterator &operator=(const Iterator& other)
{ {
fList = other.fList; fList = other.fList;
fCurrent = other.fCurrent; fCurrent = other.fCurrent;
@@ -179,21 +181,21 @@ public:
} }
private: private:
List *fList; List* fList;
Element *fCurrent; Element* fCurrent;
Element *fNext; Element* fNext;
}; };
class ConstIterator { class ConstIterator {
public: public:
ConstIterator(const List *list) ConstIterator(const List* list)
: :
fList(list) fList(list)
{ {
Rewind(); Rewind();
} }
ConstIterator(const ConstIterator &other) ConstIterator(const ConstIterator& other)
{ {
*this = other; *this = other;
} }
@@ -203,15 +205,15 @@ public:
return fNext; return fNext;
} }
Element *Next() Element* Next()
{ {
Element *element = fNext; Element* element = fNext;
if (fNext) if (fNext)
fNext = fList->GetNext(fNext); fNext = fList->GetNext(fNext);
return element; return element;
} }
ConstIterator &operator=(const ConstIterator &other) ConstIterator& operator=(const ConstIterator& other)
{ {
fList = other.fList; fList = other.fList;
fNext = other.fNext; fNext = other.fNext;
@@ -224,20 +226,20 @@ public:
} }
private: private:
const List *fList; const List* fList;
Element *fNext; Element* fNext;
}; };
class ReverseIterator { class ReverseIterator {
public: public:
ReverseIterator(List *list) ReverseIterator(List* list)
: :
fList(list) fList(list)
{ {
Rewind(); Rewind();
} }
ReverseIterator(const ReverseIterator &other) ReverseIterator(const ReverseIterator& other)
{ {
*this = other; *this = other;
} }
@@ -247,7 +249,7 @@ public:
return fNext; return fNext;
} }
Element *Next() Element* Next()
{ {
fCurrent = fNext; fCurrent = fNext;
if (fNext) if (fNext)
@@ -255,9 +257,9 @@ public:
return fCurrent; return fCurrent;
} }
Element *Remove() Element* Remove()
{ {
Element *element = fCurrent; Element* element = fCurrent;
if (fCurrent) { if (fCurrent) {
fList->Remove(fCurrent); fList->Remove(fCurrent);
fCurrent = NULL; fCurrent = NULL;
@@ -265,7 +267,7 @@ public:
return element; return element;
} }
ReverseIterator &operator=(const ReverseIterator &other) ReverseIterator &operator=(const ReverseIterator& other)
{ {
fList = other.fList; fList = other.fList;
fCurrent = other.fCurrent; fCurrent = other.fCurrent;
@@ -280,21 +282,21 @@ public:
} }
private: private:
List *fList; List* fList;
Element *fCurrent; Element* fCurrent;
Element *fNext; Element* fNext;
}; };
class ConstReverseIterator { class ConstReverseIterator {
public: public:
ConstReverseIterator(const List *list) ConstReverseIterator(const List* list)
: :
fList(list) fList(list)
{ {
Rewind(); Rewind();
} }
ConstReverseIterator(const ConstReverseIterator &other) ConstReverseIterator(const ConstReverseIterator& other)
{ {
*this = other; *this = other;
} }
@@ -304,15 +306,15 @@ public:
return fNext; return fNext;
} }
Element *Next() Element* Next()
{ {
Element *element = fNext; Element* element = fNext;
if (fNext) if (fNext)
fNext = fList->GetPrevious(fNext); fNext = fList->GetPrevious(fNext);
return element; return element;
} }
ConstReverseIterator &operator=(const ConstReverseIterator &other) ConstReverseIterator& operator=(const ConstReverseIterator& other)
{ {
fList = other.fList; fList = other.fList;
fNext = other.fNext; fNext = other.fNext;
@@ -325,8 +327,8 @@ public:
} }
private: private:
const List *fList; const List* fList;
Element *fNext; Element* fNext;
}; };
public: public:
@@ -335,30 +337,31 @@ public:
inline bool IsEmpty() const { return (fFirst == NULL); } inline bool IsEmpty() const { return (fFirst == NULL); }
inline void Insert(Element *element, bool back = true); inline void Insert(Element* element, bool back = true);
inline void Insert(Element *before, Element *element); inline void Insert(Element* before, Element* element);
inline void Add(Element *element, bool back = true); inline void Add(Element* element, bool back = true);
inline void Remove(Element *element); inline void Remove(Element* element);
inline void Swap(Element *a, Element *b); inline void Swap(Element* a, Element* b);
inline void MoveFrom(DOUBLY_LINKED_LIST_CLASS_NAME *fromList); inline void MoveFrom(DOUBLY_LINKED_LIST_CLASS_NAME* fromList);
inline void RemoveAll(); inline void RemoveAll();
inline void MakeEmpty() { RemoveAll(); } inline void MakeEmpty() { RemoveAll(); }
inline Element *First() const { return fFirst; } inline Element* First() const { return fFirst; }
inline Element *Last() const { return fLast; } inline Element* Last() const { return fLast; }
inline Element *Head() const { return fFirst; } inline Element* Head() const { return fFirst; }
inline Element *Tail() const { return fLast; } inline Element* Tail() const { return fLast; }
inline Element *RemoveHead(); inline Element* RemoveHead();
inline Element* RemoveTail();
inline Element *GetPrevious(Element *element) const; inline Element* GetPrevious(Element* element) const;
inline Element *GetNext(Element *element) const; inline Element* GetNext(Element* element) const;
inline int32 Size() const; inline int32 Count() const;
// O(n)! // O(n)!
inline Iterator GetIterator() { return Iterator(this); } inline Iterator GetIterator() { return Iterator(this); }
@@ -370,8 +373,8 @@ public:
{ return ConstReverseIterator(this); } { return ConstReverseIterator(this); }
private: private:
Element *fFirst; Element* fFirst;
Element *fLast; Element* fLast;
static GetLink sGetLink; static GetLink sGetLink;
}; };
@@ -382,7 +385,7 @@ private:
// Insert // Insert
DOUBLY_LINKED_LIST_TEMPLATE_LIST DOUBLY_LINKED_LIST_TEMPLATE_LIST
void void
DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element *element, bool back) DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element* element, bool back)
{ {
if (element) { if (element) {
#if DEBUG_DOUBLY_LINKED_LIST #if DEBUG_DOUBLY_LINKED_LIST
@@ -392,7 +395,7 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element *element, bool back)
if (back) { if (back) {
// append // append
Link *elLink = sGetLink(element); Link* elLink = sGetLink(element);
elLink->previous = fLast; elLink->previous = fLast;
elLink->next = NULL; elLink->next = NULL;
if (fLast) if (fLast)
@@ -402,7 +405,7 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element *element, bool back)
fLast = element; fLast = element;
} else { } else {
// prepend // prepend
Link *elLink = sGetLink(element); Link* elLink = sGetLink(element);
elLink->previous = NULL; elLink->previous = NULL;
elLink->next = fFirst; elLink->next = fFirst;
if (fFirst) if (fFirst)
@@ -417,7 +420,7 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element *element, bool back)
// Insert // Insert
DOUBLY_LINKED_LIST_TEMPLATE_LIST DOUBLY_LINKED_LIST_TEMPLATE_LIST
void void
DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element *before, Element *element) DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element* before, Element* element)
{ {
if (before == NULL) { if (before == NULL) {
Insert(element); Insert(element);
@@ -431,8 +434,8 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element *before, Element *element)
"list: %p\n", this); "list: %p\n", this);
#endif #endif
Link *beforeLink = sGetLink(before); Link* beforeLink = sGetLink(before);
Link *link = sGetLink(element); Link* link = sGetLink(element);
link->next = before; link->next = before;
link->previous = beforeLink->previous; link->previous = beforeLink->previous;
@@ -447,7 +450,7 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element *before, Element *element)
// Add // Add
DOUBLY_LINKED_LIST_TEMPLATE_LIST DOUBLY_LINKED_LIST_TEMPLATE_LIST
void void
DOUBLY_LINKED_LIST_CLASS_NAME::Add(Element *element, bool back) DOUBLY_LINKED_LIST_CLASS_NAME::Add(Element* element, bool back)
{ {
Insert(element, back); Insert(element, back);
} }
@@ -455,7 +458,7 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Add(Element *element, bool back)
// Remove // Remove
DOUBLY_LINKED_LIST_TEMPLATE_LIST DOUBLY_LINKED_LIST_TEMPLATE_LIST
void void
DOUBLY_LINKED_LIST_CLASS_NAME::Remove(Element *element) DOUBLY_LINKED_LIST_CLASS_NAME::Remove(Element* element)
{ {
if (element) { if (element) {
#if DEBUG_DOUBLY_LINKED_LIST #if DEBUG_DOUBLY_LINKED_LIST
@@ -464,7 +467,7 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Remove(Element *element)
"list: %p, element: %p\n", this, element); "list: %p, element: %p\n", this, element);
#endif #endif
Link *elLink = sGetLink(element); Link* elLink = sGetLink(element);
if (elLink->previous) if (elLink->previous)
sGetLink(elLink->previous)->next = elLink->next; sGetLink(elLink->previous)->next = elLink->next;
else else
@@ -481,11 +484,11 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Remove(Element *element)
// Swap // Swap
DOUBLY_LINKED_LIST_TEMPLATE_LIST DOUBLY_LINKED_LIST_TEMPLATE_LIST
void void
DOUBLY_LINKED_LIST_CLASS_NAME::Swap(Element *a, Element *b) DOUBLY_LINKED_LIST_CLASS_NAME::Swap(Element* a, Element* b)
{ {
if (a && b && a != b) { if (a && b && a != b) {
Element *aNext = sGetLink(a)->next; Element* aNext = sGetLink(a)->next;
Element *bNext = sGetLink(b)->next; Element* bNext = sGetLink(b)->next;
if (a == bNext) { if (a == bNext) {
Remove(a); Remove(a);
Insert(b, a); Insert(b, a);
@@ -504,7 +507,7 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Swap(Element *a, Element *b)
// MoveFrom // MoveFrom
DOUBLY_LINKED_LIST_TEMPLATE_LIST DOUBLY_LINKED_LIST_TEMPLATE_LIST
void void
DOUBLY_LINKED_LIST_CLASS_NAME::MoveFrom(DOUBLY_LINKED_LIST_CLASS_NAME *fromList) DOUBLY_LINKED_LIST_CLASS_NAME::MoveFrom(DOUBLY_LINKED_LIST_CLASS_NAME* fromList)
{ {
if (fromList && fromList->fFirst) { if (fromList && fromList->fFirst) {
if (fFirst) { if (fFirst) {
@@ -525,9 +528,9 @@ DOUBLY_LINKED_LIST_TEMPLATE_LIST
void void
DOUBLY_LINKED_LIST_CLASS_NAME::RemoveAll() DOUBLY_LINKED_LIST_CLASS_NAME::RemoveAll()
{ {
Element *element = fFirst; Element* element = fFirst;
while (element) { while (element) {
Link *elLink = sGetLink(element); Link* elLink = sGetLink(element);
element = elLink->next; element = elLink->next;
elLink->previous = NULL; elLink->previous = NULL;
elLink->next = NULL; elLink->next = NULL;
@@ -538,20 +541,30 @@ DOUBLY_LINKED_LIST_CLASS_NAME::RemoveAll()
// RemoveHead // RemoveHead
DOUBLY_LINKED_LIST_TEMPLATE_LIST DOUBLY_LINKED_LIST_TEMPLATE_LIST
Element * Element*
DOUBLY_LINKED_LIST_CLASS_NAME::RemoveHead() DOUBLY_LINKED_LIST_CLASS_NAME::RemoveHead()
{ {
Element *element = Head(); Element* element = Head();
Remove(element);
return element;
}
// RemoveTail
DOUBLY_LINKED_LIST_TEMPLATE_LIST
Element*
DOUBLY_LINKED_LIST_CLASS_NAME::RemoveTail()
{
Element* element = Tail();
Remove(element); Remove(element);
return element; return element;
} }
// GetPrevious // GetPrevious
DOUBLY_LINKED_LIST_TEMPLATE_LIST DOUBLY_LINKED_LIST_TEMPLATE_LIST
Element * Element*
DOUBLY_LINKED_LIST_CLASS_NAME::GetPrevious(Element *element) const DOUBLY_LINKED_LIST_CLASS_NAME::GetPrevious(Element* element) const
{ {
Element *result = NULL; Element* result = NULL;
if (element) if (element)
result = sGetLink(element)->previous; result = sGetLink(element)->previous;
return result; return result;
@@ -559,19 +572,19 @@ DOUBLY_LINKED_LIST_CLASS_NAME::GetPrevious(Element *element) const
// GetNext // GetNext
DOUBLY_LINKED_LIST_TEMPLATE_LIST DOUBLY_LINKED_LIST_TEMPLATE_LIST
Element * Element*
DOUBLY_LINKED_LIST_CLASS_NAME::GetNext(Element *element) const DOUBLY_LINKED_LIST_CLASS_NAME::GetNext(Element* element) const
{ {
Element *result = NULL; Element* result = NULL;
if (element) if (element)
result = sGetLink(element)->next; result = sGetLink(element)->next;
return result; return result;
} }
// Size // Count
DOUBLY_LINKED_LIST_TEMPLATE_LIST DOUBLY_LINKED_LIST_TEMPLATE_LIST
int32 int32
DOUBLY_LINKED_LIST_CLASS_NAME::Size() const DOUBLY_LINKED_LIST_CLASS_NAME::Count() const
{ {
int32 count = 0; int32 count = 0;
for (Element* element = First(); element; element = GetNext(element)) for (Element* element = First(); element; element = GetNext(element))
@@ -120,7 +120,7 @@ void
BasicThreadProfileResult::PrintResults() BasicThreadProfileResult::PrintResults()
{ {
// get hit images // get hit images
BasicThreadImage* images[fOldImages.Size() + fImages.Size()]; BasicThreadImage* images[fOldImages.Count() + fImages.Count()];
int32 imageCount = GetHitImages(images); int32 imageCount = GetHitImages(images);
// count symbols // count symbols
@@ -204,7 +204,7 @@ CallgrindThreadProfileResult::PrintResults()
fprintf(out, "summary: %lld %lld\n", fTotalTicks, fTotalTicks * fInterval); fprintf(out, "summary: %lld %lld\n", fTotalTicks, fTotalTicks * fInterval);
// get hit images // get hit images
CallgrindThreadImage* images[fOldImages.Size() + fImages.Size()]; CallgrindThreadImage* images[fOldImages.Count() + fImages.Count()];
int32 imageCount = GetHitImages(images); int32 imageCount = GetHitImages(images);
for (int32 i = 0; i < imageCount; i++) { for (int32 i = 0; i < imageCount; i++) {
+2 -2
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2004-2008, Axel Dörfler, axeld@pinc-software.de. * Copyright 2004-2009, Axel Dörfler, axeld@pinc-software.de.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
@@ -1523,7 +1523,7 @@ dump_cache(int argc, char** argv)
kprintf(" %ld blocks total, %ld dirty, %ld discarded, %ld referenced, %ld " kprintf(" %ld blocks total, %ld dirty, %ld discarded, %ld referenced, %ld "
"in unused.\n", count, dirty, discarded, referenced, "in unused.\n", count, dirty, discarded, referenced,
cache->unused_blocks.Size()); cache->unused_blocks.Count());
hash_close(cache->hash, &iterator, false); hash_close(cache->hash, &iterator, false);
return 0; return 0;
+1 -1
View File
@@ -264,7 +264,7 @@ ConditionVariable::ListAll()
ConditionVariableHash::Iterator it(&sConditionVariableHash); ConditionVariableHash::Iterator it(&sConditionVariableHash);
while (ConditionVariable* variable = it.Next()) { while (ConditionVariable* variable = it.Next()) {
// count waiting threads // count waiting threads
int count = variable->fEntries.Size(); int count = variable->fEntries.Count();
kprintf("%p %p %-20s %15d\n", variable, variable->fObject, kprintf("%p %p %-20s %15d\n", variable, variable->fObject,
variable->fObjectType, count); variable->fObjectType, count);
@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved. * Copyright 2002-2009, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
@@ -723,7 +723,7 @@ dump_driver(int argc, char** argv)
kprintf("%p %5ld %3ld %5ld %c %3ld %s\n", driver, kprintf("%p %5ld %3ld %5ld %c %3ld %s\n", driver,
driver->image < 0 ? -1 : driver->image, driver->image < 0 ? -1 : driver->image,
driver->devices_used, driver->devices.Size(), driver->devices_used, driver->devices.Count(),
driver->binary_updated ? 'U' : ' ', driver->priority, driver->binary_updated ? 'U' : ' ', driver->priority,
driver->name); driver->name);
} }
@@ -751,7 +751,7 @@ dump_driver(int argc, char** argv)
kprintf(" node: %Ld\n", driver->node); kprintf(" node: %Ld\n", driver->node);
kprintf(" last modified: %ld\n", driver->last_modified); kprintf(" last modified: %ld\n", driver->last_modified);
kprintf(" devs used: %ld\n", driver->devices_used); kprintf(" devs used: %ld\n", driver->devices_used);
kprintf(" devs published: %ld\n", driver->devices.Size()); kprintf(" devs published: %ld\n", driver->devices.Count());
kprintf(" binary updated: %d\n", driver->binary_updated); kprintf(" binary updated: %d\n", driver->binary_updated);
kprintf(" priority: %ld\n", driver->priority); kprintf(" priority: %ld\n", driver->priority);
kprintf(" api version: %ld\n", driver->api_version); kprintf(" api version: %ld\n", driver->api_version);