Skip to content

Commit

Permalink
Make "contains" take a const argument in intrusive list
Browse files Browse the repository at this point in the history
  • Loading branch information
andy31415 authored and bzbarsky-apple committed Jan 8, 2022
1 parent 5d0c314 commit b4406e4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
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

0 comments on commit b4406e4

Please sign in to comment.