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:
Augustin Cavalier 2023-04-01 12:31:39 -04:00
parent 3d3b89c8cc
commit d0b67fcc80
2 changed files with 59 additions and 29 deletions

View File

@ -327,8 +327,9 @@ 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 InsertBefore(Element* insertBefore, Element* element);
inline void InsertAfter(Element* insertAfter, Element* element);
inline void Insert(Element* element, bool back = true);
inline void Add(Element *element, bool back = true);
inline void Remove(Element *element);
@ -361,6 +362,10 @@ public:
inline ConstReverseIterator GetReverseIterator() const
{ return ConstReverseIterator(this); }
private:
inline void Insert(Element* before, Element* element);
// TODO: Obsolete! Use InsertBefore() instead!
private:
Element *fFirst;
Element *fLast;
@ -401,10 +406,10 @@ 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::InsertBefore(Element* before, Element* element)
{
if (before == NULL) {
Insert(element);
@ -426,6 +431,40 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element *before, Element *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
DOUBLY_LINKED_LIST_TEMPLATE_LIST
void

View File

@ -400,9 +400,9 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element* element, bool back)
"list: %p\n", this);
#endif
Link* elLink = sGetLink(element);
if (back) {
// append
Link* elLink = sGetLink(element);
elLink->previous = fLast;
elLink->next = NULL;
if (fLast)
@ -412,7 +412,6 @@ DOUBLY_LINKED_LIST_CLASS_NAME::Insert(Element* element, bool back)
fLast = element;
} else {
// prepend
Link* elLink = sGetLink(element);
elLink->previous = NULL;
elLink->next = fFirst;
if (fFirst)
@ -457,39 +456,31 @@ DOUBLY_LINKED_LIST_CLASS_NAME::InsertBefore(Element* before, Element* element)
DOUBLY_LINKED_LIST_TEMPLATE_LIST
void
DOUBLY_LINKED_LIST_CLASS_NAME::InsertAfter(Element* insertAfter,
Element* element)
DOUBLY_LINKED_LIST_CLASS_NAME::InsertAfter(Element* after, Element* element)
{
ASSERT(element != NULL);
if (after == NULL) {
Insert(element, false);
return;
}
#if DEBUG_DOUBLY_LINKED_LIST
ASSERT_PRINT(fFirst == NULL ? fLast == NULL : fLast != NULL,
"list: %p\n", this);
#endif
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;
fFirst = element;
} else {
Link* afterLink = sGetLink(insertAfter);
Link* link = sGetLink(element);
Link* afterLink = sGetLink(after);
Link* link = sGetLink(element);
link->previous = insertAfter;
link->next = afterLink->next;
afterLink->next = element;
link->previous = after;
link->next = afterLink->next;
afterLink->next = element;
if (link->next != NULL)
sGetLink(link->next)->previous = element;
else
fLast = element;
}
if (link->next != NULL)
sGetLink(link->next)->previous = element;
else
fLast = element;
}