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

Intrusive forward list add remove by pointer #1026

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions include/etl/intrusive_forward_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,29 @@ namespace etl
}
}

//*************************************************************************
// Removes the element specified by pointer.
//*************************************************************************
void remove(const_pointer element)
{
iterator i_item = begin();
iterator i_last_item = before_begin();

while (i_item != end())
{
if (&i_item == element)
{
i_item = erase_after(i_last_item);
return;
}
else
{
++i_item;
++i_last_item;
}
}
}

//*************************************************************************
/// Removes according to a predicate.
//*************************************************************************
Expand Down
29 changes: 29 additions & 0 deletions test/test_intrusive_forward_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,35 @@ namespace
CHECK_EQUAL(sorted_data.size(), size_t(std::distance(data1.begin(), data1.end())));
}

//*************************************************************************
TEST_FIXTURE(SetupFixture, test_remove_by_pointer)
{
std::forward_list<ItemNDCNode> compare_data(sorted_data.begin(), sorted_data.end());
DataNDC0 data0(sorted_data.begin(), sorted_data.end());
DataNDC1 data1(sorted_data.begin(), sorted_data.end());

auto it = data0.begin();
for (int i = 0; i < 7; ++i)
{
it++;
}
ItemNDCNode* element = &it;

compare_data.remove(ItemNDCNode("7"));
data0.remove(*element);

bool are_equal = std::equal(data0.begin(), data0.end(), compare_data.begin());

CHECK(are_equal);
CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data0.size());
CHECK_EQUAL(std::distance(compare_data.begin(), compare_data.end()), std::distance(data0.begin(), data0.end()));

are_equal = std::equal(data1.begin(), data1.end(), sorted_data.begin());
CHECK(are_equal);
CHECK_EQUAL(sorted_data.size(), data1.size());
CHECK_EQUAL(sorted_data.size(), size_t(std::distance(data1.begin(), data1.end())));
}

//*************************************************************************
TEST_FIXTURE(SetupFixture, test_remove_if)
{
Expand Down
Loading