Skip to content

Commit

Permalink
Improve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kghost committed Jan 5, 2022
1 parent 39c310d commit 6578796
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions src/lib/support/IntrusiveList.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,38 +131,24 @@ class IntrusiveListBase
protected:
// The list is formed as a ring with mNode being the end.
//
// begin end
// v v
// item -> item -> ... -> mNode
// ^ |
// \--------------------/
// begin end
// v v
// item(first) -> item -> ... -> item(last) -> mNode
// ^ |
// \------------------------------------------/
//
IntrusiveListBase() : mNode(&mNode, &mNode) {}
~IntrusiveListBase() { mNode.Remove(); }
~IntrusiveListBase() { mNode.Remove(); /* clear mNode such that the destructor checking mNode.IsInList doesn't fail */ }

IteratorBase begin() { return IteratorBase(mNode.mNext); }
IteratorBase end() { return IteratorBase(&mNode); }

void PushFront(IntrusiveListNodeBase * node) { mNode.Append(node); }
void PushBack(IntrusiveListNodeBase * node) { mNode.Prepend(node); }

void InsertBefore(IteratorBase pos, IntrusiveListNodeBase * node)
{
VerifyOrDie(pos.mCurrent->IsInList());
pos.mCurrent->Prepend(node);
}

void InsertAfter(IteratorBase pos, IntrusiveListNodeBase * node)
{
VerifyOrDie(pos.mCurrent->IsInList());
pos.mCurrent->Append(node);
}

void Remove(IntrusiveListNodeBase * node)
{
VerifyOrDie(Contains(node));
node->Remove();
}
void InsertBefore(IteratorBase pos, IntrusiveListNodeBase * node) { pos.mCurrent->Prepend(node); }
void InsertAfter(IteratorBase pos, IntrusiveListNodeBase * node) { pos.mCurrent->Append(node); }
void Remove(IntrusiveListNodeBase * node) { node->Remove(); }

bool Contains(IntrusiveListNodeBase * node)
{
Expand All @@ -187,6 +173,12 @@ class IntrusiveListBaseHook
static IntrusiveListNodeBase * ToNode(T * object) { return static_cast<IntrusiveListNodeBase *>(object); }
};

/**
* @brief An intrusive double linked list.
*
* @tparam T Type of element in the list.
* @tparam Hook A hook to convert between object T and the IntrusiveListNodeBase
*/
template <typename T, typename Hook = IntrusiveListBaseHook<T>>
class IntrusiveList : public IntrusiveListBase
{
Expand Down Expand Up @@ -215,8 +207,6 @@ class IntrusiveList : public IntrusiveListBase
void InsertAfter(Iterator pos, T * value) { IntrusiveListBase::InsertAfter(pos, Hook::ToNode(value)); }
void Remove(T * value) { IntrusiveListBase::Remove(Hook::ToNode(value)); }
bool Contains(T * value) { return IntrusiveListBase::Contains(Hook::ToNode(value)); }

private:
};

} // namespace chip

0 comments on commit 6578796

Please sign in to comment.