Use erase_if pattern (#4429)

This commit is contained in:
Piotr Wójcik 2024-02-13 14:38:34 +01:00 committed by GitHub
commit 1b9807af6d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 27 additions and 59 deletions

View file

@ -443,18 +443,10 @@ void nano::bootstrap_connections::clear_pulls (uint64_t bootstrap_id_a)
{
{
nano::lock_guard<nano::mutex> lock{ mutex };
auto i (pulls.begin ());
while (i != pulls.end ())
{
if (i->bootstrap_id == bootstrap_id_a)
{
i = pulls.erase (i);
}
else
{
++i;
}
}
erase_if (pulls, [bootstrap_id_a] (auto const & pull) {
return pull.bootstrap_id == bootstrap_id_a;
});
}
condition.notify_all ();
}

View file

@ -82,18 +82,18 @@ std::size_t nano::bootstrap_ascending::peer_scoring::size () const
void nano::bootstrap_ascending::peer_scoring::timeout ()
{
auto & index = scoring.get<tag_channel> ();
for (auto score = index.begin (), n = index.end (); score != n;)
{
if (auto channel = score->shared ())
erase_if (index, [] (auto const & score) {
if (auto channel = score.shared ())
{
if (channel->alive ())
{
++score;
continue;
return false; // Keep
}
}
score = index.erase (score);
}
return true;
});
for (auto score = scoring.begin (), n = scoring.end (); score != n; ++score)
{
scoring.modify (score, [] (auto & score_a) {

View file

@ -647,17 +647,10 @@ void nano::election::remove_block (nano::block_hash const & hash_a)
{
if (auto existing = last_blocks.find (hash_a); existing != last_blocks.end ())
{
for (auto i (last_votes.begin ()); i != last_votes.end ();)
{
if (i->second.hash == hash_a)
{
i = last_votes.erase (i);
}
else
{
++i;
}
}
erase_if (last_votes, [hash_a] (auto const & entry) {
return entry.second.hash == hash_a;
});
node.network.publish_filter.clear (existing->second);
last_blocks.erase (hash_a);
}

View file

@ -242,7 +242,7 @@ void nano::telemetry::cleanup ()
{
debug_assert (!mutex.try_lock ());
nano::erase_if (telemetries, [this] (entry const & entry) {
erase_if (telemetries, [this] (entry const & entry) {
// Remove if telemetry data is stale
if (!check_timeout (entry))
{

View file

@ -1,5 +1,6 @@
#include <nano/lib/config.hpp>
#include <nano/lib/stats.hpp>
#include <nano/lib/utility.hpp>
#include <nano/node/node.hpp>
#include <nano/node/transport/message_deserializer.hpp>
#include <nano/node/transport/tcp.hpp>
@ -463,13 +464,9 @@ void nano::transport::tcp_channels::purge (std::chrono::steady_clock::time_point
nano::lock_guard<nano::mutex> lock{ mutex };
// Remove channels with dead underlying sockets
for (auto it = channels.begin (); it != channels.end (); ++it)
{
if (!it->socket->alive ())
{
it = channels.erase (it);
}
}
erase_if (channels, [] (auto const & entry) {
return !entry.channel->alive ();
});
auto disconnect_cutoff (channels.get<last_packet_sent_tag> ().lower_bound (cutoff_a));
channels.get<last_packet_sent_tag> ().erase (channels.get<last_packet_sent_tag> ().begin (), disconnect_cutoff);

View file

@ -231,15 +231,10 @@ void nano::transport::tcp_listener::on_connection_requeue_delayed (std::function
void nano::transport::tcp_listener::evict_dead_connections ()
{
debug_assert (strand.running_in_this_thread ());
for (auto it = connections_per_address.begin (); it != connections_per_address.end ();)
{
if (it->second.expired ())
{
it = connections_per_address.erase (it);
continue;
}
++it;
}
erase_if (connections_per_address, [] (auto const & entry) {
return entry.second.expired ();
});
}
void nano::transport::tcp_listener::accept_action (boost::system::error_code const & ec, std::shared_ptr<nano::transport::socket> const & socket_a)

View file

@ -245,18 +245,9 @@ void nano::vote_cache::cleanup ()
auto const cutoff = std::chrono::steady_clock::now () - config.age_cutoff;
auto it = cache.begin ();
while (it != cache.end ())
{
if (it->last_vote () < cutoff)
{
it = cache.erase (it);
}
else
{
++it;
}
}
erase_if (cache, [cutoff] (auto const & entry) {
return entry.last_vote () < cutoff;
});
}
std::unique_ptr<nano::container_info_component> nano::vote_cache::collect_container_info (const std::string & name) const