kernel/util: Clean-ups to DoublyLinkedList insertion routines.
* Make some code common in Insert. * Remove unneeded logic from InsertAfter and just call the base Insert. InsertBefore does this, already. * Synchronize Insert code and APIs in the fs_shell DoublyLinkedList.
This commit is contained in:
@@ -327,8 +327,9 @@ public:
|
|||||||
|
|
||||||
inline bool IsEmpty() const { return (fFirst == NULL); }
|
inline bool IsEmpty() const { return (fFirst == NULL); }
|
||||||
|
|
||||||
|
inline void InsertBefore(Element* insertBefore, Element* element);
|
||||||
|
inline void InsertAfter(Element* insertAfter, Element* element);
|
||||||
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 Add(Element *element, bool back = true);
|
inline void Add(Element *element, bool back = true);
|
||||||
inline void Remove(Element *element);
|
inline void Remove(Element *element);
|
||||||
|
|
||||||
@@ -361,6 +362,10 @@ public:
|
|||||||
inline ConstReverseIterator GetReverseIterator() const
|
inline ConstReverseIterator GetReverseIterator() const
|
||||||
{ return ConstReverseIterator(this); }
|
{ return ConstReverseIterator(this); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
inline void Insert(Element* before, Element* element);
|
||||||
|
// TODO: Obsolete! Use InsertBefore() instead!
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Element *fFirst;
|
Element *fFirst;
|
||||||
Element *fLast;
|
Element *fLast;
|
||||||
@@ -401,10 +406,10 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element *element, bool back)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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::InsertBefore(Element* before, Element* element)
|
||||||
{
|
{
|
||||||
if (before == NULL) {
|
if (before == NULL) {
|
||||||
Insert(element);
|
Insert(element);
|
||||||
@@ -426,6 +431,40 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element *before, Element *element)
|
|||||||
fFirst = element;
|
fFirst = element;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
||||||
|
void
|
||||||
|
DOUBLY_LINKED_LIST_CLASS_NAME::InsertAfter(Element* after, Element* element)
|
||||||
|
{
|
||||||
|
ASSERT(element != NULL);
|
||||||
|
|
||||||
|
if (after == NULL) {
|
||||||
|
Insert(element, false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Link* afterLink = sGetLink(after);
|
||||||
|
Link* link = sGetLink(element);
|
||||||
|
|
||||||
|
link->previous = after;
|
||||||
|
link->next = afterLink->next;
|
||||||
|
afterLink->next = element;
|
||||||
|
|
||||||
|
if (link->next != NULL)
|
||||||
|
sGetLink(link->next)->previous = element;
|
||||||
|
else
|
||||||
|
fLast = element;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
||||||
|
void
|
||||||
|
DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element* before, Element* element)
|
||||||
|
{
|
||||||
|
InsertBefore(before, element);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Add
|
// Add
|
||||||
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
||||||
void
|
void
|
||||||
|
|||||||
@@ -400,9 +400,9 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element* element, bool back)
|
|||||||
"list: %p\n", this);
|
"list: %p\n", this);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Link* elLink = sGetLink(element);
|
||||||
if (back) {
|
if (back) {
|
||||||
// append
|
// append
|
||||||
Link* elLink = sGetLink(element);
|
|
||||||
elLink->previous = fLast;
|
elLink->previous = fLast;
|
||||||
elLink->next = NULL;
|
elLink->next = NULL;
|
||||||
if (fLast)
|
if (fLast)
|
||||||
@@ -412,7 +412,6 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element* element, bool back)
|
|||||||
fLast = element;
|
fLast = element;
|
||||||
} else {
|
} else {
|
||||||
// prepend
|
// prepend
|
||||||
Link* elLink = sGetLink(element);
|
|
||||||
elLink->previous = NULL;
|
elLink->previous = NULL;
|
||||||
elLink->next = fFirst;
|
elLink->next = fFirst;
|
||||||
if (fFirst)
|
if (fFirst)
|
||||||
@@ -457,31 +456,24 @@ DOUBLY_LINKED_LIST_CLASS_NAME::InsertBefore(Element* before, Element* element)
|
|||||||
|
|
||||||
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
||||||
void
|
void
|
||||||
DOUBLY_LINKED_LIST_CLASS_NAME::InsertAfter(Element* insertAfter,
|
DOUBLY_LINKED_LIST_CLASS_NAME::InsertAfter(Element* after, Element* element)
|
||||||
Element* element)
|
|
||||||
{
|
{
|
||||||
ASSERT(element != NULL);
|
ASSERT(element != NULL);
|
||||||
|
|
||||||
|
if (after == NULL) {
|
||||||
|
Insert(element, false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
#if DEBUG_DOUBLY_LINKED_LIST
|
#if DEBUG_DOUBLY_LINKED_LIST
|
||||||
ASSERT_PRINT(fFirst == NULL ? fLast == NULL : fLast != NULL,
|
ASSERT_PRINT(fFirst == NULL ? fLast == NULL : fLast != NULL,
|
||||||
"list: %p\n", this);
|
"list: %p\n", this);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (insertAfter == NULL) {
|
Link* afterLink = sGetLink(after);
|
||||||
// insert at the head
|
|
||||||
Link* elLink = sGetLink(element);
|
|
||||||
elLink->previous = NULL;
|
|
||||||
elLink->next = fFirst;
|
|
||||||
if (fFirst != NULL)
|
|
||||||
sGetLink(fFirst)->previous = element;
|
|
||||||
else
|
|
||||||
fLast = element;
|
|
||||||
fFirst = element;
|
|
||||||
} else {
|
|
||||||
Link* afterLink = sGetLink(insertAfter);
|
|
||||||
Link* link = sGetLink(element);
|
Link* link = sGetLink(element);
|
||||||
|
|
||||||
link->previous = insertAfter;
|
link->previous = after;
|
||||||
link->next = afterLink->next;
|
link->next = afterLink->next;
|
||||||
afterLink->next = element;
|
afterLink->next = element;
|
||||||
|
|
||||||
@@ -490,7 +482,6 @@ DOUBLY_LINKED_LIST_CLASS_NAME::InsertAfter(Element* insertAfter,
|
|||||||
else
|
else
|
||||||
fLast = element;
|
fLast = element;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
DOUBLY_LINKED_LIST_TEMPLATE_LIST
|
||||||
|
|||||||
Reference in New Issue
Block a user