Skip to content

Commit

Permalink
Add contains() and contains_node() to etl::intrusive_forward_list and…
Browse files Browse the repository at this point in the history
… etl::intrusive_list (#1036)

Co-authored-by: John Wellbelove <[email protected]>
  • Loading branch information
rolandreichweinbmw and jwellbelove authored Mar 2, 2025
1 parent 64ae22a commit 12743be
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 1 deletion.
42 changes: 42 additions & 0 deletions include/etl/intrusive_forward_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,27 @@ namespace etl
return current_size;
}

//*************************************************************************
/// Detects existence of specified node in list.
///\param search_link The node to find in list
//*************************************************************************
bool contains_node(const link_type& search_link) const
{
const link_type* p_link = start.etl_next;

while (p_link != ETL_NULLPTR)
{
if (&search_link == p_link)
{
return true;
}

p_link = p_link->link_type::etl_next;
}

return false;
}

protected:

link_type start; ///< The link pointer that acts as the intrusive_forward_list start.
Expand Down Expand Up @@ -1210,6 +1231,27 @@ namespace etl
}
}

//*************************************************************************
/// Detects existence of specified value in list.
///\param value The value to find in list
//*************************************************************************
bool contains(const_reference value) const
{
const_iterator i_item = begin();

while (i_item != end())
{
if (*i_item == value)
{
return true;
}

++i_item;
}

return false;
}

private:

#if ETL_USING_CPP17
Expand Down
44 changes: 43 additions & 1 deletion include/etl/intrusive_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,27 @@ namespace etl
return current_size;
}

//*************************************************************************
/// Detects existence of specified node in list.
///\param search_link The node to find in list
//*************************************************************************
bool contains_node(link_type& search_link) const
{
link_type* p_link = terminal_link.link_type::etl_next;

while (p_link != &terminal_link)
{
if (&search_link == p_link)
{
return true;
}

p_link = p_link->link_type::etl_next;
}

return false;
}

protected:

/// The link that acts as the intrusive_list start & end.
Expand Down Expand Up @@ -404,7 +425,7 @@ namespace etl
{
link_type* result = ETL_NULLPTR;

if (is_link_in_list(link))
if (contains_node(link))
{
link_type* p_next = link->etl_next;

Expand Down Expand Up @@ -1218,6 +1239,27 @@ namespace etl
}
}

//*************************************************************************
/// Detects existence of specified value in list.
///\param value The value to find in list
//*************************************************************************
bool contains(const_reference value) const
{
const_iterator i_item = begin();

while (i_item != end())
{
if (*i_item == value)
{
return true;
}

++i_item;
}

return false;
}

private:

#if ETL_USING_CPP17
Expand Down
72 changes: 72 additions & 0 deletions test/test_intrusive_forward_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1302,5 +1302,77 @@ namespace
CHECK_EQUAL(size_t(std::distance(compare0.begin(), compare0.end())), data0.size());
CHECK_EQUAL(size_t(std::distance(compare1.begin(), compare1.end())), data1.size());
}

//*************************************************************************
TEST_FIXTURE(SetupFixture, test_contains_node)
{
static ItemNDCNode node0("0");
static ItemNDCNode node1("1");
static ItemNDCNode node2("2");
static ItemNDCNode node3("3");
static ItemNDCNode node4("4");
static ItemNDCNode node5("5");
static ItemNDCNode node6("6");
static ItemNDCNode node7("7");
static ItemNDCNode node8("8");
static ItemNDCNode node9("9");

DataNDC0 data0;

data0.push_front(node0);
data0.push_front(node1);
data0.push_front(node2);
data0.push_front(node3);
data0.push_front(node4);
data0.push_front(node5);

CHECK_TRUE(data0.contains_node(node0));
CHECK_TRUE(data0.contains_node(node1));
CHECK_TRUE(data0.contains_node(node2));
CHECK_TRUE(data0.contains_node(node3));
CHECK_TRUE(data0.contains_node(node4));
CHECK_TRUE(data0.contains_node(node5));

CHECK_FALSE(data0.contains_node(node6));
CHECK_FALSE(data0.contains_node(node7));
CHECK_FALSE(data0.contains_node(node8));
CHECK_FALSE(data0.contains_node(node9));
}

//*************************************************************************
TEST_FIXTURE(SetupFixture, test_contains)
{
static ItemNDCNode node0("0");
static ItemNDCNode node1("1");
static ItemNDCNode node2("2");
static ItemNDCNode node3("3");
static ItemNDCNode node4("4");
static ItemNDCNode node5("5");
static ItemNDCNode node6("6");
static ItemNDCNode node7("7");
static ItemNDCNode node8("8");
static ItemNDCNode node9("9");

DataNDC0 data0;

data0.push_front(node0);
data0.push_front(node1);
data0.push_front(node2);
data0.push_front(node3);
data0.push_front(node4);
data0.push_front(node5);

CHECK_TRUE(data0.contains(ItemNDCNode("0")));

ItemNDCNode compare_node1("1");

CHECK_TRUE(data0.contains(compare_node1));

CHECK_FALSE(data0.contains(ItemNDCNode("6")));

ItemNDCNode compare_node2("7");

CHECK_FALSE(data0.contains(compare_node2));
}
};
}
72 changes: 72 additions & 0 deletions test/test_intrusive_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1519,5 +1519,77 @@ namespace
CHECK_EQUAL(data0.size(), compare0.size());
CHECK_EQUAL(data1.size(), compare1.size());
}

//*************************************************************************
TEST_FIXTURE(SetupFixture, test_contains_node)
{
static ItemNDCNode node0("0");
static ItemNDCNode node1("1");
static ItemNDCNode node2("2");
static ItemNDCNode node3("3");
static ItemNDCNode node4("4");
static ItemNDCNode node5("5");
static ItemNDCNode node6("6");
static ItemNDCNode node7("7");
static ItemNDCNode node8("8");
static ItemNDCNode node9("9");

DataNDC0 data0;

data0.push_front(node0);
data0.push_front(node1);
data0.push_front(node2);
data0.push_front(node3);
data0.push_front(node4);
data0.push_front(node5);

CHECK_TRUE(data0.contains_node(node0));
CHECK_TRUE(data0.contains_node(node1));
CHECK_TRUE(data0.contains_node(node2));
CHECK_TRUE(data0.contains_node(node3));
CHECK_TRUE(data0.contains_node(node4));
CHECK_TRUE(data0.contains_node(node5));

CHECK_FALSE(data0.contains_node(node6));
CHECK_FALSE(data0.contains_node(node7));
CHECK_FALSE(data0.contains_node(node8));
CHECK_FALSE(data0.contains_node(node9));
}

//*************************************************************************
TEST_FIXTURE(SetupFixture, test_contains)
{
static ItemNDCNode node0("0");
static ItemNDCNode node1("1");
static ItemNDCNode node2("2");
static ItemNDCNode node3("3");
static ItemNDCNode node4("4");
static ItemNDCNode node5("5");
static ItemNDCNode node6("6");
static ItemNDCNode node7("7");
static ItemNDCNode node8("8");
static ItemNDCNode node9("9");

DataNDC0 data0;

data0.push_front(node0);
data0.push_front(node1);
data0.push_front(node2);
data0.push_front(node3);
data0.push_front(node4);
data0.push_front(node5);

CHECK_TRUE(data0.contains(ItemNDCNode("0")));

ItemNDCNode compare_node1("1");

CHECK_TRUE(data0.contains(compare_node1));

CHECK_FALSE(data0.contains(ItemNDCNode("6")));

ItemNDCNode compare_node2("7");

CHECK_FALSE(data0.contains(compare_node2));
}
};
}

0 comments on commit 12743be

Please sign in to comment.