* 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

View File

@ -1,5 +1,7 @@
/*
* Copyright 2005-2007, Ingo Weinhold, bonefish@users.sf.net. All rights reserved.
* Copyright 2005-2007, Ingo Weinhold, bonefish@users.sf.net.
* Copyright 2006-2009, Axel Dörfler, axeld@pinc-software.de.
*
* Distributed under the terms of the MIT License.
*/
#ifndef KERNEL_UTIL_DOUBLY_LINKED_LIST_H
@ -40,9 +42,9 @@ public:
DoublyLinkedListLinkImpl() : fDoublyLinkedListLink() {}
~DoublyLinkedListLinkImpl() {}
DLL_Link *GetDoublyLinkedListLink()
DLL_Link* GetDoublyLinkedListLink()
{ return &fDoublyLinkedListLink; }
const DLL_Link *GetDoublyLinkedListLink() const
const DLL_Link* GetDoublyLinkedListLink() const
{ return &fDoublyLinkedListLink; }
private:
@ -56,12 +58,12 @@ private:
typedef DoublyLinkedListLink<Element> Link;
public:
inline Link *operator()(Element *element) const
inline Link* operator()(Element* element) const
{
return element->GetDoublyLinkedListLink();
}
inline const Link *operator()(const Element *element) const
inline const Link* operator()(const Element* element) const
{
return element->GetDoublyLinkedListLink();
}
@ -75,12 +77,12 @@ private:
typedef DoublyLinkedListLink<Element> Link;
public:
inline Link *operator()(Element *element) const
inline Link* operator()(Element* element) const
{
return &(element->*LinkMember);
}
inline const Link *operator()(const Element *element) const
inline const Link* operator()(const Element* element) const
{
return &(element->*LinkMember);
}
@ -93,14 +95,14 @@ class DoublyLinkedListCLink {
typedef DoublyLinkedListLink<Element> Link;
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:
class Iterator {
public:
Iterator(List *list)
Iterator(List* list)
:
fList(list)
{
@ -141,7 +143,7 @@ public:
return fNext;
}
Element *Next()
Element* Next()
{
fCurrent = fNext;
if (fNext)
@ -149,14 +151,14 @@ public:
return fCurrent;
}
Element *Current()
Element* Current()
{
return fCurrent;
}
Element *Remove()
Element* Remove()
{
Element *element = fCurrent;
Element* element = fCurrent;
if (fCurrent) {
fList->Remove(fCurrent);
fCurrent = NULL;
@ -164,7 +166,7 @@ public:
return element;
}
Iterator &operator=(const Iterator &other)
Iterator &operator=(const Iterator& other)
{
fList = other.fList;
fCurrent = other.fCurrent;
@ -179,21 +181,21 @@ public:
}
private:
List *fList;
Element *fCurrent;
Element *fNext;
List* fList;
Element* fCurrent;
Element* fNext;
};
class ConstIterator {
public:
ConstIterator(const List *list)
ConstIterator(const List* list)
:
fList(list)
{
Rewind();
}
ConstIterator(const ConstIterator &other)
ConstIterator(const ConstIterator& other)
{
*this = other;
}
@ -203,15 +205,15 @@ public:
return fNext;
}
Element *Next()
Element* Next()
{
Element *element = fNext;
Element* element = fNext;
if (fNext)
fNext = fList->GetNext(fNext);
return element;
}
ConstIterator &operator=(const ConstIterator &other)
ConstIterator& operator=(const ConstIterator& other)
{
fList = other.fList;
fNext = other.fNext;
@ -224,20 +226,20 @@ public:
}
private:
const List *fList;
Element *fNext;
const List* fList;
Element* fNext;
};
class ReverseIterator {
public:
ReverseIterator(List *list)
ReverseIterator(List* list)
:
fList(list)
{
Rewind();
}
ReverseIterator(const ReverseIterator &other)
ReverseIterator(const ReverseIterator& other)
{
*this = other;
}
@ -247,7 +249,7 @@ public:
return fNext;
}
Element *Next()
Element* Next()
{
fCurrent = fNext;
if (fNext)
@ -255,9 +257,9 @@ public:
return fCurrent;
}
Element *Remove()
Element* Remove()
{
Element *element = fCurrent;
Element* element = fCurrent;
if (fCurrent) {
fList->Remove(fCurrent);
fCurrent = NULL;
@ -265,7 +267,7 @@ public:
return element;
}
ReverseIterator &operator=(const ReverseIterator &other)
ReverseIterator &operator=(const ReverseIterator& other)
{
fList = other.fList;
fCurrent = other.fCurrent;
@ -280,21 +282,21 @@ public:
}
private:
List *fList;
Element *fCurrent;
Element *fNext;
List* fList;
Element* fCurrent;
Element* fNext;
};
class ConstReverseIterator {
public:
ConstReverseIterator(const List *list)
ConstReverseIterator(const List* list)
:
fList(list)
{
Rewind();
}
ConstReverseIterator(const ConstReverseIterator &other)
ConstReverseIterator(const ConstReverseIterator& other)
{
*this = other;
}
@ -304,15 +306,15 @@ public:
return fNext;
}
Element *Next()
Element* Next()
{
Element *element = fNext;
Element* element = fNext;
if (fNext)
fNext = fList->GetPrevious(fNext);
return element;
}
ConstReverseIterator &operator=(const ConstReverseIterator &other)
ConstReverseIterator& operator=(const ConstReverseIterator& other)
{
fList = other.fList;
fNext = other.fNext;
@ -325,8 +327,8 @@ public:
}
private:
const List *fList;
Element *fNext;
const List* fList;
Element* fNext;
};
public:
@ -335,30 +337,31 @@ public:
inline bool IsEmpty() const { return (fFirst == NULL); }
inline void Insert(Element *element, bool back = true);
inline void Insert(Element *before, Element *element);
inline void Add(Element *element, bool back = true);
inline void Remove(Element *element);
inline void Insert(Element* element, bool back = true);
inline void Insert(Element* before, Element* element);
inline void Add(Element* element, bool back = true);
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 MakeEmpty() { RemoveAll(); }
inline Element *First() const { return fFirst; }
inline Element *Last() const { return fLast; }
inline Element* First() const { return fFirst; }
inline Element* Last() const { return fLast; }
inline Element *Head() const { return fFirst; }
inline Element *Tail() const { return fLast; }
inline Element* Head() const { return fFirst; }
inline Element* Tail() const { return fLast; }
inline Element *RemoveHead();
inline Element* RemoveHead();
inline Element* RemoveTail();
inline Element *GetPrevious(Element *element) const;
inline Element *GetNext(Element *element) const;
inline Element* GetPrevious(Element* element) const;
inline Element* GetNext(Element* element) const;
inline int32 Size() const;
inline int32 Count() const;
// O(n)!
inline Iterator GetIterator() { return Iterator(this); }
@ -370,8 +373,8 @@ public:
{ return ConstReverseIterator(this); }
private:
Element *fFirst;
Element *fLast;
Element* fFirst;
Element* fLast;
static GetLink sGetLink;
};
@ -382,7 +385,7 @@ private:
// Insert
DOUBLY_LINKED_LIST_TEMPLATE_LIST
void
DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element *element, bool back)
DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element* element, bool back)
{
if (element) {
#if DEBUG_DOUBLY_LINKED_LIST
@ -392,7 +395,7 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element *element, bool back)
if (back) {
// append
Link *elLink = sGetLink(element);
Link* elLink = sGetLink(element);
elLink->previous = fLast;
elLink->next = NULL;
if (fLast)
@ -402,7 +405,7 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element *element, bool back)
fLast = element;
} else {
// prepend
Link *elLink = sGetLink(element);
Link* elLink = sGetLink(element);
elLink->previous = NULL;
elLink->next = fFirst;
if (fFirst)
@ -417,7 +420,7 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element *element, bool back)
// Insert
DOUBLY_LINKED_LIST_TEMPLATE_LIST
void
DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element *before, Element *element)
DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element* before, Element* element)
{
if (before == NULL) {
Insert(element);
@ -431,8 +434,8 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element *before, Element *element)
"list: %p\n", this);
#endif
Link *beforeLink = sGetLink(before);
Link *link = sGetLink(element);
Link* beforeLink = sGetLink(before);
Link* link = sGetLink(element);
link->next = before;
link->previous = beforeLink->previous;
@ -447,7 +450,7 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element *before, Element *element)
// Add
DOUBLY_LINKED_LIST_TEMPLATE_LIST
void
DOUBLY_LINKED_LIST_CLASS_NAME::Add(Element *element, bool back)
DOUBLY_LINKED_LIST_CLASS_NAME::Add(Element* element, bool back)
{
Insert(element, back);
}
@ -455,7 +458,7 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Add(Element *element, bool back)
// Remove
DOUBLY_LINKED_LIST_TEMPLATE_LIST
void
DOUBLY_LINKED_LIST_CLASS_NAME::Remove(Element *element)
DOUBLY_LINKED_LIST_CLASS_NAME::Remove(Element* element)
{
if (element) {
#if DEBUG_DOUBLY_LINKED_LIST
@ -464,7 +467,7 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Remove(Element *element)
"list: %p, element: %p\n", this, element);
#endif
Link *elLink = sGetLink(element);
Link* elLink = sGetLink(element);
if (elLink->previous)
sGetLink(elLink->previous)->next = elLink->next;
else
@ -481,11 +484,11 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Remove(Element *element)
// Swap
DOUBLY_LINKED_LIST_TEMPLATE_LIST
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) {
Element *aNext = sGetLink(a)->next;
Element *bNext = sGetLink(b)->next;
Element* aNext = sGetLink(a)->next;
Element* bNext = sGetLink(b)->next;
if (a == bNext) {
Remove(a);
Insert(b, a);
@ -504,7 +507,7 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Swap(Element *a, Element *b)
// MoveFrom
DOUBLY_LINKED_LIST_TEMPLATE_LIST
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 (fFirst) {
@ -525,9 +528,9 @@ DOUBLY_LINKED_LIST_TEMPLATE_LIST
void
DOUBLY_LINKED_LIST_CLASS_NAME::RemoveAll()
{
Element *element = fFirst;
Element* element = fFirst;
while (element) {
Link *elLink = sGetLink(element);
Link* elLink = sGetLink(element);
element = elLink->next;
elLink->previous = NULL;
elLink->next = NULL;
@ -538,20 +541,30 @@ DOUBLY_LINKED_LIST_CLASS_NAME::RemoveAll()
// RemoveHead
DOUBLY_LINKED_LIST_TEMPLATE_LIST
Element *
Element*
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);
return element;
}
// GetPrevious
DOUBLY_LINKED_LIST_TEMPLATE_LIST
Element *
DOUBLY_LINKED_LIST_CLASS_NAME::GetPrevious(Element *element) const
Element*
DOUBLY_LINKED_LIST_CLASS_NAME::GetPrevious(Element* element) const
{
Element *result = NULL;
Element* result = NULL;
if (element)
result = sGetLink(element)->previous;
return result;
@ -559,19 +572,19 @@ DOUBLY_LINKED_LIST_CLASS_NAME::GetPrevious(Element *element) const
// GetNext
DOUBLY_LINKED_LIST_TEMPLATE_LIST
Element *
DOUBLY_LINKED_LIST_CLASS_NAME::GetNext(Element *element) const
Element*
DOUBLY_LINKED_LIST_CLASS_NAME::GetNext(Element* element) const
{
Element *result = NULL;
Element* result = NULL;
if (element)
result = sGetLink(element)->next;
return result;
}
// Size
// Count
DOUBLY_LINKED_LIST_TEMPLATE_LIST
int32
DOUBLY_LINKED_LIST_CLASS_NAME::Size() const
DOUBLY_LINKED_LIST_CLASS_NAME::Count() const
{
int32 count = 0;
for (Element* element = First(); element; element = GetNext(element))

View File

@ -120,7 +120,7 @@ void
BasicThreadProfileResult::PrintResults()
{
// get hit images
BasicThreadImage* images[fOldImages.Size() + fImages.Size()];
BasicThreadImage* images[fOldImages.Count() + fImages.Count()];
int32 imageCount = GetHitImages(images);
// count symbols

View File

@ -204,7 +204,7 @@ CallgrindThreadProfileResult::PrintResults()
fprintf(out, "summary: %lld %lld\n", fTotalTicks, fTotalTicks * fInterval);
// get hit images
CallgrindThreadImage* images[fOldImages.Size() + fImages.Size()];
CallgrindThreadImage* images[fOldImages.Count() + fImages.Count()];
int32 imageCount = GetHitImages(images);
for (int32 i = 0; i < imageCount; i++) {

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.
*/
@ -1523,7 +1523,7 @@ dump_cache(int argc, char** argv)
kprintf(" %ld blocks total, %ld dirty, %ld discarded, %ld referenced, %ld "
"in unused.\n", count, dirty, discarded, referenced,
cache->unused_blocks.Size());
cache->unused_blocks.Count());
hash_close(cache->hash, &iterator, false);
return 0;

View File

@ -264,7 +264,7 @@ ConditionVariable::ListAll()
ConditionVariableHash::Iterator it(&sConditionVariableHash);
while (ConditionVariable* variable = it.Next()) {
// count waiting threads
int count = variable->fEntries.Size();
int count = variable->fEntries.Count();
kprintf("%p %p %-20s %15d\n", variable, variable->fObject,
variable->fObjectType, count);

View File

@ -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.
*/
@ -723,7 +723,7 @@ dump_driver(int argc, char** argv)
kprintf("%p %5ld %3ld %5ld %c %3ld %s\n", driver,
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->name);
}
@ -751,7 +751,7 @@ dump_driver(int argc, char** argv)
kprintf(" node: %Ld\n", driver->node);
kprintf(" last modified: %ld\n", driver->last_modified);
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(" priority: %ld\n", driver->priority);
kprintf(" api version: %ld\n", driver->api_version);