Skip to content
Draft
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
30 changes: 27 additions & 3 deletions lib/programmemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,34 @@ void ProgramMemory::erase_if(const std::function<bool(const ExprIdToken&)>& pred
if (mValues->empty())
return;

// TODO: how to delay until we actuallly modify?
copyOnWrite();
auto it = std::find_if(mValues->cbegin(), mValues->cend(), [&pred](const std::pair<const ExprIdToken, ValueFlow::Value>& entry) {
return pred(entry.first);
});
if (it == mValues->cend())
return;

// remove the (first) matching entry
if (mValues.use_count() == 1)
{
it = mValues->erase(it);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think this extra branch is needed. Especially if you can move the copyOnWrite out of the loop.

else
{
const auto& exprIdTok = it->first;

copyOnWrite();

it = mValues->cbegin();
for (; it != mValues->cend(); ++it) {
if (it->first == exprIdTok) {
it = mValues->erase(it);
break;
}
}
}

for (auto it = mValues->begin(); it != mValues->end();) {
// remove the remaining matches
for (; it != mValues->cend();) {
if (pred(it->first))
it = mValues->erase(it);
else
Expand Down
Loading