2009-12-06 18:47:34 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2005-2009, Ingo Weinhold, bonefish@users.sf.net.
|
2009-04-07 01:06:19 +04:00
|
|
|
* Copyright 2006-2009, Axel Dörfler, axeld@pinc-software.de.
|
|
|
|
*
|
2006-03-27 14:27:05 +04:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
2003-10-13 05:57:05 +04:00
|
|
|
#ifndef KERNEL_UTIL_DOUBLY_LINKED_LIST_H
|
|
|
|
#define KERNEL_UTIL_DOUBLY_LINKED_LIST_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <SupportDefs.h>
|
2007-03-11 16:56:45 +03:00
|
|
|
|
|
|
|
#ifdef _KERNEL_MODE
|
2007-10-04 02:22:21 +04:00
|
|
|
# include <debug.h>
|
2007-03-11 16:56:45 +03:00
|
|
|
# include <util/kernel_cpp.h>
|
2003-10-13 05:57:05 +04:00
|
|
|
|
2007-10-04 02:22:21 +04:00
|
|
|
# if !defined(_BOOT_MODE) && KDEBUG
|
|
|
|
# define DEBUG_DOUBLY_LINKED_LIST KDEBUG
|
|
|
|
# endif
|
|
|
|
#endif
|
2003-10-13 05:57:05 +04:00
|
|
|
|
2010-01-03 20:07:56 +03:00
|
|
|
|
2003-10-13 05:57:05 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
2005-02-02 04:14:47 +03:00
|
|
|
// DoublyLinkedListLink
|
|
|
|
template<typename Element>
|
|
|
|
class DoublyLinkedListLink {
|
|
|
|
public:
|
2009-04-07 01:13:06 +04:00
|
|
|
Element* next;
|
|
|
|
Element* previous;
|
2005-02-02 04:14:47 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// DoublyLinkedListLinkImpl
|
|
|
|
template<typename Element>
|
|
|
|
class DoublyLinkedListLinkImpl {
|
|
|
|
private:
|
2005-02-02 05:31:06 +03:00
|
|
|
typedef DoublyLinkedListLink<Element> DLL_Link;
|
2005-02-02 04:14:47 +03:00
|
|
|
|
|
|
|
public:
|
2009-12-06 18:47:34 +03:00
|
|
|
DLL_Link* GetDoublyLinkedListLink()
|
2005-02-02 04:14:47 +03:00
|
|
|
{ return &fDoublyLinkedListLink; }
|
2009-04-07 01:06:19 +04:00
|
|
|
const DLL_Link* GetDoublyLinkedListLink() const
|
2005-02-02 04:14:47 +03:00
|
|
|
{ return &fDoublyLinkedListLink; }
|
|
|
|
|
|
|
|
private:
|
2005-02-02 05:31:06 +03:00
|
|
|
DLL_Link fDoublyLinkedListLink;
|
2005-02-02 04:14:47 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// DoublyLinkedListStandardGetLink
|
|
|
|
template<typename Element>
|
|
|
|
class DoublyLinkedListStandardGetLink {
|
|
|
|
private:
|
|
|
|
typedef DoublyLinkedListLink<Element> Link;
|
|
|
|
|
|
|
|
public:
|
2009-04-07 01:06:19 +04:00
|
|
|
inline Link* operator()(Element* element) const
|
2005-02-02 04:14:47 +03:00
|
|
|
{
|
|
|
|
return element->GetDoublyLinkedListLink();
|
|
|
|
}
|
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
inline const Link* operator()(const Element* element) const
|
2005-02-02 04:14:47 +03:00
|
|
|
{
|
|
|
|
return element->GetDoublyLinkedListLink();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2005-02-02 04:48:43 +03:00
|
|
|
// DoublyLinkedListMemberGetLink
|
|
|
|
template<typename Element,
|
|
|
|
DoublyLinkedListLink<Element> Element::* LinkMember = &Element::fLink>
|
|
|
|
class DoublyLinkedListMemberGetLink {
|
|
|
|
private:
|
|
|
|
typedef DoublyLinkedListLink<Element> Link;
|
|
|
|
|
|
|
|
public:
|
2009-04-07 01:06:19 +04:00
|
|
|
inline Link* operator()(Element* element) const
|
2005-02-02 04:48:43 +03:00
|
|
|
{
|
|
|
|
return &(element->*LinkMember);
|
|
|
|
}
|
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
inline const Link* operator()(const Element* element) const
|
2005-02-02 04:48:43 +03:00
|
|
|
{
|
|
|
|
return &(element->*LinkMember);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-07-19 01:08:52 +04:00
|
|
|
// DoublyLinkedListCLink - interface to struct list
|
|
|
|
template<typename Element>
|
|
|
|
class DoublyLinkedListCLink {
|
|
|
|
private:
|
|
|
|
typedef DoublyLinkedListLink<Element> Link;
|
|
|
|
|
|
|
|
public:
|
2009-04-07 01:06:19 +04:00
|
|
|
inline Link* operator()(Element* element) const
|
2006-07-19 01:08:52 +04:00
|
|
|
{
|
2009-04-07 01:06:19 +04:00
|
|
|
return (Link*)&element->link;
|
2006-07-19 01:08:52 +04:00
|
|
|
}
|
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
inline const Link* operator()(const Element* element) const
|
2006-07-19 01:08:52 +04:00
|
|
|
{
|
2009-04-07 01:06:19 +04:00
|
|
|
return (const Link*)&element->link;
|
2006-07-19 01:08:52 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2005-02-02 04:14:47 +03:00
|
|
|
// for convenience
|
|
|
|
#define DOUBLY_LINKED_LIST_TEMPLATE_LIST \
|
|
|
|
template<typename Element, typename GetLink>
|
|
|
|
#define DOUBLY_LINKED_LIST_CLASS_NAME DoublyLinkedList<Element, GetLink>
|
|
|
|
|
|
|
|
// DoublyLinkedList
|
|
|
|
template<typename Element,
|
|
|
|
typename GetLink = DoublyLinkedListStandardGetLink<Element> >
|
|
|
|
class DoublyLinkedList {
|
|
|
|
private:
|
|
|
|
typedef DoublyLinkedList<Element, GetLink> List;
|
|
|
|
typedef DoublyLinkedListLink<Element> Link;
|
|
|
|
|
|
|
|
public:
|
|
|
|
class Iterator {
|
|
|
|
public:
|
2009-04-07 01:06:19 +04:00
|
|
|
Iterator(List* list)
|
2006-03-27 14:27:05 +04:00
|
|
|
:
|
|
|
|
fList(list)
|
2005-02-02 04:14:47 +03:00
|
|
|
{
|
2006-03-27 14:27:05 +04:00
|
|
|
Rewind();
|
2005-02-02 04:14:47 +03:00
|
|
|
}
|
|
|
|
|
2007-08-01 18:28:50 +04:00
|
|
|
Iterator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-02-02 04:14:47 +03:00
|
|
|
Iterator(const Iterator &other)
|
|
|
|
{
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasNext() const
|
|
|
|
{
|
|
|
|
return fNext;
|
|
|
|
}
|
2006-03-27 14:27:05 +04:00
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
Element* Next()
|
2005-02-02 04:14:47 +03:00
|
|
|
{
|
|
|
|
fCurrent = fNext;
|
|
|
|
if (fNext)
|
|
|
|
fNext = fList->GetNext(fNext);
|
|
|
|
return fCurrent;
|
|
|
|
}
|
2006-03-27 14:27:05 +04:00
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
Element* Current()
|
2007-08-01 18:28:50 +04:00
|
|
|
{
|
|
|
|
return fCurrent;
|
|
|
|
}
|
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
Element* Remove()
|
2005-02-02 04:14:47 +03:00
|
|
|
{
|
2009-04-07 01:06:19 +04:00
|
|
|
Element* element = fCurrent;
|
2005-02-02 04:14:47 +03:00
|
|
|
if (fCurrent) {
|
|
|
|
fList->Remove(fCurrent);
|
|
|
|
fCurrent = NULL;
|
|
|
|
}
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
Iterator &operator=(const Iterator& other)
|
2005-02-02 04:14:47 +03:00
|
|
|
{
|
|
|
|
fList = other.fList;
|
|
|
|
fCurrent = other.fCurrent;
|
|
|
|
fNext = other.fNext;
|
|
|
|
return *this;
|
|
|
|
}
|
2006-03-27 14:27:05 +04:00
|
|
|
|
|
|
|
void Rewind()
|
|
|
|
{
|
|
|
|
fCurrent = NULL;
|
|
|
|
fNext = fList->First();
|
|
|
|
}
|
|
|
|
|
2005-02-02 04:14:47 +03:00
|
|
|
private:
|
2009-04-07 01:06:19 +04:00
|
|
|
List* fList;
|
|
|
|
Element* fCurrent;
|
|
|
|
Element* fNext;
|
2005-02-02 04:14:47 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class ConstIterator {
|
|
|
|
public:
|
2009-04-07 01:06:19 +04:00
|
|
|
ConstIterator(const List* list)
|
2006-03-27 14:27:05 +04:00
|
|
|
:
|
|
|
|
fList(list)
|
2005-02-02 04:14:47 +03:00
|
|
|
{
|
2006-03-27 14:27:05 +04:00
|
|
|
Rewind();
|
2005-02-02 04:14:47 +03:00
|
|
|
}
|
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
ConstIterator(const ConstIterator& other)
|
2005-02-02 04:14:47 +03:00
|
|
|
{
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasNext() const
|
|
|
|
{
|
|
|
|
return fNext;
|
|
|
|
}
|
2006-03-27 14:27:05 +04:00
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
Element* Next()
|
2005-02-02 04:14:47 +03:00
|
|
|
{
|
2009-04-07 01:06:19 +04:00
|
|
|
Element* element = fNext;
|
2005-02-02 04:14:47 +03:00
|
|
|
if (fNext)
|
|
|
|
fNext = fList->GetNext(fNext);
|
|
|
|
return element;
|
|
|
|
}
|
2006-03-27 14:27:05 +04:00
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
ConstIterator& operator=(const ConstIterator& other)
|
2005-02-02 04:14:47 +03:00
|
|
|
{
|
|
|
|
fList = other.fList;
|
|
|
|
fNext = other.fNext;
|
|
|
|
return *this;
|
|
|
|
}
|
2006-03-27 14:27:05 +04:00
|
|
|
|
|
|
|
void Rewind()
|
|
|
|
{
|
|
|
|
fNext = fList->First();
|
|
|
|
}
|
|
|
|
|
2005-02-02 04:14:47 +03:00
|
|
|
private:
|
2009-04-07 01:06:19 +04:00
|
|
|
const List* fList;
|
|
|
|
Element* fNext;
|
2005-02-02 04:14:47 +03:00
|
|
|
};
|
|
|
|
|
2006-07-29 21:58:17 +04:00
|
|
|
class ReverseIterator {
|
|
|
|
public:
|
2009-04-07 01:06:19 +04:00
|
|
|
ReverseIterator(List* list)
|
2006-07-29 21:58:17 +04:00
|
|
|
:
|
|
|
|
fList(list)
|
|
|
|
{
|
|
|
|
Rewind();
|
|
|
|
}
|
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
ReverseIterator(const ReverseIterator& other)
|
2006-07-29 21:58:17 +04:00
|
|
|
{
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasNext() const
|
|
|
|
{
|
|
|
|
return fNext;
|
|
|
|
}
|
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
Element* Next()
|
2006-07-29 21:58:17 +04:00
|
|
|
{
|
|
|
|
fCurrent = fNext;
|
|
|
|
if (fNext)
|
|
|
|
fNext = fList->GetPrevious(fNext);
|
|
|
|
return fCurrent;
|
|
|
|
}
|
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
Element* Remove()
|
2006-07-29 21:58:17 +04:00
|
|
|
{
|
2009-04-07 01:06:19 +04:00
|
|
|
Element* element = fCurrent;
|
2006-07-29 21:58:17 +04:00
|
|
|
if (fCurrent) {
|
|
|
|
fList->Remove(fCurrent);
|
|
|
|
fCurrent = NULL;
|
|
|
|
}
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
ReverseIterator &operator=(const ReverseIterator& other)
|
2006-07-29 21:58:17 +04:00
|
|
|
{
|
|
|
|
fList = other.fList;
|
|
|
|
fCurrent = other.fCurrent;
|
|
|
|
fNext = other.fNext;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rewind()
|
|
|
|
{
|
|
|
|
fCurrent = NULL;
|
|
|
|
fNext = fList->Last();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2009-04-07 01:06:19 +04:00
|
|
|
List* fList;
|
|
|
|
Element* fCurrent;
|
|
|
|
Element* fNext;
|
2006-07-29 21:58:17 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class ConstReverseIterator {
|
|
|
|
public:
|
2009-04-07 01:06:19 +04:00
|
|
|
ConstReverseIterator(const List* list)
|
2006-07-29 21:58:17 +04:00
|
|
|
:
|
|
|
|
fList(list)
|
|
|
|
{
|
|
|
|
Rewind();
|
|
|
|
}
|
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
ConstReverseIterator(const ConstReverseIterator& other)
|
2006-07-29 21:58:17 +04:00
|
|
|
{
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasNext() const
|
|
|
|
{
|
|
|
|
return fNext;
|
|
|
|
}
|
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
Element* Next()
|
2006-07-29 21:58:17 +04:00
|
|
|
{
|
2009-04-07 01:06:19 +04:00
|
|
|
Element* element = fNext;
|
2006-07-29 21:58:17 +04:00
|
|
|
if (fNext)
|
|
|
|
fNext = fList->GetPrevious(fNext);
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
ConstReverseIterator& operator=(const ConstReverseIterator& other)
|
2006-07-29 21:58:17 +04:00
|
|
|
{
|
|
|
|
fList = other.fList;
|
|
|
|
fNext = other.fNext;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rewind()
|
|
|
|
{
|
|
|
|
fNext = fList->Last();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2009-04-07 01:06:19 +04:00
|
|
|
const List* fList;
|
|
|
|
Element* fNext;
|
2006-07-29 21:58:17 +04:00
|
|
|
};
|
|
|
|
|
2005-02-02 04:14:47 +03:00
|
|
|
public:
|
|
|
|
DoublyLinkedList() : fFirst(NULL), fLast(NULL) {}
|
|
|
|
~DoublyLinkedList() {}
|
|
|
|
|
|
|
|
inline bool IsEmpty() const { return (fFirst == NULL); }
|
|
|
|
|
2010-01-03 20:07:56 +03:00
|
|
|
inline void InsertBefore(Element* insertBefore, Element* element);
|
|
|
|
inline void InsertAfter(Element* insertAfter, Element* element);
|
2009-04-07 01:06:19 +04:00
|
|
|
inline void Insert(Element* element, bool back = true);
|
|
|
|
inline void Insert(Element* before, Element* element);
|
2010-01-03 20:07:56 +03:00
|
|
|
// TODO: Obsolete! Use InsertBefore() instead!
|
2009-04-07 01:06:19 +04:00
|
|
|
inline void Add(Element* element, bool back = true);
|
|
|
|
inline void Remove(Element* element);
|
2005-02-02 04:14:47 +03:00
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
inline void Swap(Element* a, Element* b);
|
2005-02-02 04:14:47 +03:00
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
inline void MoveFrom(DOUBLY_LINKED_LIST_CLASS_NAME* fromList);
|
2005-02-02 04:14:47 +03:00
|
|
|
|
|
|
|
inline void RemoveAll();
|
|
|
|
inline void MakeEmpty() { RemoveAll(); }
|
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
inline Element* First() const { return fFirst; }
|
|
|
|
inline Element* Last() const { return fLast; }
|
2005-02-02 04:14:47 +03:00
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
inline Element* Head() const { return fFirst; }
|
|
|
|
inline Element* Tail() const { return fLast; }
|
2005-02-02 04:14:47 +03:00
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
inline Element* RemoveHead();
|
|
|
|
inline Element* RemoveTail();
|
2006-03-27 14:27:05 +04:00
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
inline Element* GetPrevious(Element* element) const;
|
|
|
|
inline Element* GetNext(Element* element) const;
|
2005-02-02 04:14:47 +03:00
|
|
|
|
2011-11-01 01:30:13 +04:00
|
|
|
inline bool Contains(Element* element) const;
|
|
|
|
// O(n)!
|
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
inline int32 Count() const;
|
2005-02-02 04:14:47 +03:00
|
|
|
// O(n)!
|
|
|
|
|
2013-11-20 18:38:20 +04:00
|
|
|
template<typename Less>
|
|
|
|
void Sort(const Less& less);
|
|
|
|
// O(n^2)
|
|
|
|
|
2005-02-02 04:14:47 +03:00
|
|
|
inline Iterator GetIterator() { return Iterator(this); }
|
|
|
|
inline ConstIterator GetIterator() const { return ConstIterator(this); }
|
|
|
|
|
2006-07-29 21:58:17 +04:00
|
|
|
inline ReverseIterator GetReverseIterator()
|
|
|
|
{ return ReverseIterator(this); }
|
|
|
|
inline ConstReverseIterator GetReverseIterator() const
|
|
|
|
{ return ConstReverseIterator(this); }
|
|
|
|
|
2005-02-02 04:14:47 +03:00
|
|
|
private:
|
2009-04-07 01:06:19 +04:00
|
|
|
Element* fFirst;
|
|
|
|
Element* fLast;
|
2005-02-02 04:48:43 +03:00
|
|
|
|
|
|
|
static GetLink sGetLink;
|
2005-02-02 04:14:47 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// inline methods
|
|
|
|
|
|
|
|
// Insert
|
|
|
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
|
|
|
void
|
2009-04-07 01:06:19 +04:00
|
|
|
DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element* element, bool back)
|
2005-02-02 04:14:47 +03:00
|
|
|
{
|
|
|
|
if (element) {
|
2007-10-04 02:22:21 +04:00
|
|
|
#if DEBUG_DOUBLY_LINKED_LIST
|
|
|
|
ASSERT_PRINT(fFirst == NULL ? fLast == NULL : fLast != NULL,
|
|
|
|
"list: %p\n", this);
|
|
|
|
#endif
|
|
|
|
|
2005-02-02 04:14:47 +03:00
|
|
|
if (back) {
|
|
|
|
// append
|
2009-04-07 01:06:19 +04:00
|
|
|
Link* elLink = sGetLink(element);
|
2005-02-02 04:14:47 +03:00
|
|
|
elLink->previous = fLast;
|
|
|
|
elLink->next = NULL;
|
|
|
|
if (fLast)
|
2005-02-02 04:48:43 +03:00
|
|
|
sGetLink(fLast)->next = element;
|
2005-02-02 04:14:47 +03:00
|
|
|
else
|
|
|
|
fFirst = element;
|
|
|
|
fLast = element;
|
|
|
|
} else {
|
|
|
|
// prepend
|
2009-04-07 01:06:19 +04:00
|
|
|
Link* elLink = sGetLink(element);
|
2005-02-02 04:14:47 +03:00
|
|
|
elLink->previous = NULL;
|
|
|
|
elLink->next = fFirst;
|
|
|
|
if (fFirst)
|
2005-02-02 04:48:43 +03:00
|
|
|
sGetLink(fFirst)->previous = element;
|
2005-02-02 04:14:47 +03:00
|
|
|
else
|
|
|
|
fLast = element;
|
|
|
|
fFirst = element;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-03 20:07:56 +03:00
|
|
|
|
2006-07-19 01:08:52 +04:00
|
|
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
|
|
|
void
|
2010-01-03 20:07:56 +03:00
|
|
|
DOUBLY_LINKED_LIST_CLASS_NAME::InsertBefore(Element* before, Element* element)
|
2006-07-19 01:08:52 +04:00
|
|
|
{
|
2010-01-03 20:07:56 +03:00
|
|
|
ASSERT(element != NULL);
|
|
|
|
|
2006-07-19 01:08:52 +04:00
|
|
|
if (before == NULL) {
|
2006-07-29 21:58:17 +04:00
|
|
|
Insert(element);
|
2006-07-19 01:08:52 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-10-04 02:22:21 +04:00
|
|
|
#if DEBUG_DOUBLY_LINKED_LIST
|
|
|
|
ASSERT_PRINT(fFirst == NULL ? fLast == NULL : fLast != NULL,
|
|
|
|
"list: %p\n", this);
|
|
|
|
#endif
|
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
Link* beforeLink = sGetLink(before);
|
|
|
|
Link* link = sGetLink(element);
|
2006-07-19 01:08:52 +04:00
|
|
|
|
2006-07-29 21:58:17 +04:00
|
|
|
link->next = before;
|
|
|
|
link->previous = beforeLink->previous;
|
2010-01-03 20:07:56 +03:00
|
|
|
beforeLink->previous = element;
|
|
|
|
|
2006-07-29 21:58:17 +04:00
|
|
|
if (link->previous != NULL)
|
|
|
|
sGetLink(link->previous)->next = element;
|
2010-01-03 20:07:56 +03:00
|
|
|
else
|
|
|
|
fFirst = element;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
|
|
|
void
|
|
|
|
DOUBLY_LINKED_LIST_CLASS_NAME::InsertAfter(Element* insertAfter,
|
|
|
|
Element* element)
|
|
|
|
{
|
|
|
|
ASSERT(element != NULL);
|
|
|
|
|
|
|
|
#if DEBUG_DOUBLY_LINKED_LIST
|
|
|
|
ASSERT_PRINT(fFirst == NULL ? fLast == NULL : fLast != NULL,
|
|
|
|
"list: %p\n", this);
|
|
|
|
#endif
|
2006-07-19 01:08:52 +04:00
|
|
|
|
2010-01-03 20:07:56 +03:00
|
|
|
if (insertAfter == NULL) {
|
|
|
|
// insert at the head
|
|
|
|
Link* elLink = sGetLink(element);
|
|
|
|
elLink->previous = NULL;
|
|
|
|
elLink->next = fFirst;
|
|
|
|
if (fFirst != NULL)
|
|
|
|
sGetLink(fFirst)->previous = element;
|
|
|
|
else
|
|
|
|
fLast = element;
|
2006-07-29 21:58:17 +04:00
|
|
|
fFirst = element;
|
2010-01-03 20:07:56 +03:00
|
|
|
} else {
|
|
|
|
Link* afterLink = sGetLink(insertAfter);
|
|
|
|
Link* link = sGetLink(element);
|
|
|
|
|
|
|
|
link->previous = insertAfter;
|
|
|
|
link->next = afterLink->next;
|
|
|
|
afterLink->next = element;
|
|
|
|
|
|
|
|
if (link->next != NULL)
|
|
|
|
sGetLink(link->next)->previous = element;
|
|
|
|
else
|
|
|
|
fLast = element;
|
|
|
|
}
|
2006-07-19 01:08:52 +04:00
|
|
|
}
|
|
|
|
|
2010-01-03 20:07:56 +03:00
|
|
|
|
|
|
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
|
|
|
void
|
|
|
|
DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element* before, Element* element)
|
|
|
|
{
|
|
|
|
InsertBefore(before, element);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-02 04:48:43 +03:00
|
|
|
// Add
|
|
|
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
|
|
|
void
|
2009-04-07 01:06:19 +04:00
|
|
|
DOUBLY_LINKED_LIST_CLASS_NAME::Add(Element* element, bool back)
|
2005-02-02 04:48:43 +03:00
|
|
|
{
|
|
|
|
Insert(element, back);
|
|
|
|
}
|
|
|
|
|
2005-02-02 04:14:47 +03:00
|
|
|
// Remove
|
|
|
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
|
|
|
void
|
2009-04-07 01:06:19 +04:00
|
|
|
DOUBLY_LINKED_LIST_CLASS_NAME::Remove(Element* element)
|
2005-02-02 04:14:47 +03:00
|
|
|
{
|
|
|
|
if (element) {
|
2007-10-04 02:22:21 +04:00
|
|
|
#if DEBUG_DOUBLY_LINKED_LIST
|
|
|
|
ASSERT_PRINT(fFirst != NULL && fLast != NULL
|
|
|
|
&& (fFirst != fLast || element == fFirst),
|
|
|
|
"list: %p, element: %p\n", this, element);
|
|
|
|
#endif
|
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
Link* elLink = sGetLink(element);
|
2005-02-02 04:14:47 +03:00
|
|
|
if (elLink->previous)
|
2005-02-02 04:48:43 +03:00
|
|
|
sGetLink(elLink->previous)->next = elLink->next;
|
2005-02-02 04:14:47 +03:00
|
|
|
else
|
|
|
|
fFirst = elLink->next;
|
|
|
|
if (elLink->next)
|
2005-02-02 04:48:43 +03:00
|
|
|
sGetLink(elLink->next)->previous = elLink->previous;
|
2005-02-02 04:14:47 +03:00
|
|
|
else
|
|
|
|
fLast = elLink->previous;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap
|
|
|
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
|
|
|
void
|
2009-04-07 01:06:19 +04:00
|
|
|
DOUBLY_LINKED_LIST_CLASS_NAME::Swap(Element* a, Element* b)
|
2005-02-02 04:14:47 +03:00
|
|
|
{
|
|
|
|
if (a && b && a != b) {
|
2009-04-07 01:06:19 +04:00
|
|
|
Element* aNext = sGetLink(a)->next;
|
|
|
|
Element* bNext = sGetLink(b)->next;
|
2008-07-23 00:36:32 +04:00
|
|
|
if (a == bNext) {
|
|
|
|
Remove(a);
|
|
|
|
Insert(b, a);
|
|
|
|
} else if (b == aNext) {
|
|
|
|
Remove(b);
|
|
|
|
Insert(a, b);
|
|
|
|
} else {
|
|
|
|
Remove(a);
|
|
|
|
Remove(b);
|
|
|
|
Insert(aNext, b);
|
|
|
|
Insert(bNext, a);
|
|
|
|
}
|
2005-02-02 04:14:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MoveFrom
|
|
|
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
|
|
|
void
|
2009-04-07 01:06:19 +04:00
|
|
|
DOUBLY_LINKED_LIST_CLASS_NAME::MoveFrom(DOUBLY_LINKED_LIST_CLASS_NAME* fromList)
|
2005-02-02 04:14:47 +03:00
|
|
|
{
|
|
|
|
if (fromList && fromList->fFirst) {
|
|
|
|
if (fFirst) {
|
2005-02-02 04:48:43 +03:00
|
|
|
sGetLink(fLast)->next = fromList->fFirst;
|
2007-10-05 20:56:48 +04:00
|
|
|
sGetLink(fromList->fFirst)->previous = fLast;
|
2005-02-02 04:14:47 +03:00
|
|
|
fLast = fromList->fLast;
|
|
|
|
} else {
|
|
|
|
fFirst = fromList->fFirst;
|
|
|
|
fLast = fromList->fLast;
|
|
|
|
}
|
|
|
|
fromList->fFirst = NULL;
|
|
|
|
fromList->fLast = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveAll
|
|
|
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
|
|
|
void
|
|
|
|
DOUBLY_LINKED_LIST_CLASS_NAME::RemoveAll()
|
|
|
|
{
|
|
|
|
fFirst = NULL;
|
|
|
|
fLast = NULL;
|
|
|
|
}
|
|
|
|
|
2006-03-27 14:27:05 +04:00
|
|
|
// RemoveHead
|
|
|
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
2009-12-06 18:47:34 +03:00
|
|
|
Element*
|
2006-03-27 14:27:05 +04:00
|
|
|
DOUBLY_LINKED_LIST_CLASS_NAME::RemoveHead()
|
|
|
|
{
|
2009-04-07 01:06:19 +04:00
|
|
|
Element* element = Head();
|
|
|
|
Remove(element);
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveTail
|
|
|
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
2009-12-06 18:47:34 +03:00
|
|
|
Element*
|
2009-04-07 01:06:19 +04:00
|
|
|
DOUBLY_LINKED_LIST_CLASS_NAME::RemoveTail()
|
|
|
|
{
|
|
|
|
Element* element = Tail();
|
2006-03-27 14:27:05 +04:00
|
|
|
Remove(element);
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
2005-02-02 04:14:47 +03:00
|
|
|
// GetPrevious
|
|
|
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
2009-12-06 18:47:34 +03:00
|
|
|
Element*
|
2009-04-07 01:06:19 +04:00
|
|
|
DOUBLY_LINKED_LIST_CLASS_NAME::GetPrevious(Element* element) const
|
2005-02-02 04:14:47 +03:00
|
|
|
{
|
2009-04-07 01:06:19 +04:00
|
|
|
Element* result = NULL;
|
2005-02-02 04:14:47 +03:00
|
|
|
if (element)
|
2005-02-02 04:48:43 +03:00
|
|
|
result = sGetLink(element)->previous;
|
2005-02-02 04:14:47 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNext
|
|
|
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
2009-12-06 18:47:34 +03:00
|
|
|
Element*
|
2009-04-07 01:06:19 +04:00
|
|
|
DOUBLY_LINKED_LIST_CLASS_NAME::GetNext(Element* element) const
|
2005-02-02 04:14:47 +03:00
|
|
|
{
|
2009-04-07 01:06:19 +04:00
|
|
|
Element* result = NULL;
|
2005-02-02 04:14:47 +03:00
|
|
|
if (element)
|
2005-02-02 04:48:43 +03:00
|
|
|
result = sGetLink(element)->next;
|
2005-02-02 04:14:47 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-11-01 01:30:13 +04:00
|
|
|
|
|
|
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
|
|
|
bool
|
|
|
|
DOUBLY_LINKED_LIST_CLASS_NAME::Contains(Element* _element) const
|
|
|
|
{
|
|
|
|
for (Element* element = First(); element; element = GetNext(element)) {
|
|
|
|
if (element == _element)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-07 01:06:19 +04:00
|
|
|
// Count
|
2005-02-02 04:14:47 +03:00
|
|
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
|
|
|
int32
|
2009-04-07 01:06:19 +04:00
|
|
|
DOUBLY_LINKED_LIST_CLASS_NAME::Count() const
|
2005-02-02 04:14:47 +03:00
|
|
|
{
|
|
|
|
int32 count = 0;
|
2005-02-02 05:12:35 +03:00
|
|
|
for (Element* element = First(); element; element = GetNext(element))
|
2005-02-02 04:14:47 +03:00
|
|
|
count++;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2013-11-20 18:38:20 +04:00
|
|
|
|
|
|
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
|
|
|
template<typename Less>
|
|
|
|
void
|
|
|
|
DOUBLY_LINKED_LIST_CLASS_NAME::Sort(const Less& less)
|
|
|
|
{
|
|
|
|
// selection sort
|
|
|
|
Element* tail = Head();
|
|
|
|
while (tail != NULL) {
|
|
|
|
Element* leastElement = tail;
|
|
|
|
Element* element = tail;
|
|
|
|
while ((element = GetNext(element)) != NULL) {
|
|
|
|
if (less(element, leastElement))
|
|
|
|
leastElement = element;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (leastElement != tail) {
|
|
|
|
Remove(leastElement);
|
|
|
|
InsertBefore(tail, leastElement);
|
|
|
|
} else
|
|
|
|
tail = GetNext(tail);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-02 04:48:43 +03:00
|
|
|
// sGetLink
|
|
|
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
|
|
|
GetLink DOUBLY_LINKED_LIST_CLASS_NAME::sGetLink;
|
|
|
|
|
2005-02-02 04:14:47 +03:00
|
|
|
#endif /* __cplusplus */
|
|
|
|
|
|
|
|
#endif // _KERNEL_UTIL_DOUBLY_LINKED_LIST_H
|