Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make "contains" take a const argument in intrusive list #13348

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/lib/support/IntrusiveList.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class IntrusiveListBase
void InsertAfter(IteratorBase pos, IntrusiveListNodeBase * node) { pos.mCurrent->Append(node); }
void Remove(IntrusiveListNodeBase * node) { node->Remove(); }

bool Contains(IntrusiveListNodeBase * node) const
bool Contains(const IntrusiveListNodeBase * node) const
{
for (auto & iter : *this)
{
Expand All @@ -225,8 +225,12 @@ class IntrusiveListBaseHook
{
public:
static_assert(std::is_base_of<IntrusiveListNodeBase, T>::value, "T must be derived from IntrusiveListNodeBase");

static T * ToObject(IntrusiveListNodeBase * node) { return static_cast<T *>(node); }
static const T * ToObject(const IntrusiveListNodeBase * node) { return static_cast<T *>(node); }

static IntrusiveListNodeBase * ToNode(T * object) { return static_cast<IntrusiveListNodeBase *>(object); }
static const IntrusiveListNodeBase * ToNode(const T * object) { return static_cast<const IntrusiveListNodeBase *>(object); }
};

/// A double-linked list where the data is stored together with the previous/next pointers for cache efficiency / and compactness.
Expand Down Expand Up @@ -289,7 +293,7 @@ class IntrusiveList : public IntrusiveListBase
void InsertBefore(Iterator pos, T * value) { IntrusiveListBase::InsertBefore(pos, Hook::ToNode(value)); }
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) const { return IntrusiveListBase::Contains(Hook::ToNode(value)); }
bool Contains(const T * value) const { return IntrusiveListBase::Contains(Hook::ToNode(value)); }
};

} // namespace chip
40 changes: 39 additions & 1 deletion src/lib/support/tests/TestIntrusiveList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,40 @@ void TestIntrusiveListRandom(nlTestSuite * inSuite, void * inContext)
}
}

void TestContains(nlTestSuite * inSuite, void * inContext)
{
ListNode a, b, c;
IntrusiveList<ListNode> list;

NL_TEST_ASSERT(inSuite, !list.Contains(&a));
NL_TEST_ASSERT(inSuite, !list.Contains(&b));
NL_TEST_ASSERT(inSuite, !list.Contains(&c));

list.PushBack(&a);
list.PushFront(&c);

NL_TEST_ASSERT(inSuite, list.Contains(&a));
NL_TEST_ASSERT(inSuite, !list.Contains(&b));
NL_TEST_ASSERT(inSuite, list.Contains(&c));

list.PushBack(&b);

NL_TEST_ASSERT(inSuite, list.Contains(&a));
NL_TEST_ASSERT(inSuite, list.Contains(&b));
NL_TEST_ASSERT(inSuite, list.Contains(&c));

list.Remove(&a);
list.Remove(&c);

NL_TEST_ASSERT(inSuite, !list.Contains(&a));
NL_TEST_ASSERT(inSuite, list.Contains(&b));
NL_TEST_ASSERT(inSuite, !list.Contains(&c));

// all nodes have to be removed from the list on destruction. Lists do NOT do
// this automatically
list.Remove(&b);
}

int Setup(void * inContext)
{
return SUCCESS;
Expand All @@ -113,7 +147,11 @@ int Teardown(void * inContext)
/**
* Test Suite. It lists all the test functions.
*/
static const nlTest sTests[] = { NL_TEST_DEF_FN(TestIntrusiveListRandom), NL_TEST_SENTINEL() };
static const nlTest sTests[] = {
NL_TEST_DEF_FN(TestIntrusiveListRandom), //
NL_TEST_DEF_FN(TestContains), //
NL_TEST_SENTINEL(), //
};

int TestIntrusiveList()
{
Expand Down