From 7cc2619aad02f43ec292ca9f12f66f2a16b0f9de Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Thu, 7 Sep 2017 11:34:23 -0400 Subject: [PATCH] more use of auto and minor code refactor in routing_table.cpp --- src/kademlia/routing_table.cpp | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/kademlia/routing_table.cpp b/src/kademlia/routing_table.cpp index 7d66d80e0..e4752a68f 100644 --- a/src/kademlia/routing_table.cpp +++ b/src/kademlia/routing_table.cpp @@ -63,7 +63,7 @@ namespace { template void erase_one(T& container, K const& key) { - typename T::iterator i = container.find(key); + auto const i = container.find(key); TORRENT_ASSERT(i != container.end()); container.erase(i); } @@ -244,10 +244,8 @@ node_entry const* routing_table::next_refresh() node_entry* candidate = nullptr; // this will have a bias towards pinging nodes close to us first. - // TODO: why idx is never used here? - int idx = int(m_buckets.size()) - 1; for (table_t::reverse_iterator i = m_buckets.rbegin() - , end(m_buckets.rend()); i != end; ++i, --idx) + , end(m_buckets.rend()); i != end; ++i) { for (bucket_t::iterator j = i->live_nodes.begin() , end2(i->live_nodes.end()); j != end2; ++j) @@ -293,7 +291,7 @@ routing_table::table_t::iterator routing_table::find_bucket(node_id const& id) TORRENT_ASSERT(bucket_index < int(m_buckets.size())); TORRENT_ASSERT(bucket_index >= 0); - table_t::iterator i = m_buckets.begin(); + auto i = m_buckets.begin(); std::advance(i, bucket_index); return i; } @@ -373,7 +371,7 @@ void routing_table::fill_from_replacements(table_t::iterator bucket) while (int(b.size()) < bucket_size && !rb.empty()) { - bucket_t::iterator j = std::find_if(rb.begin(), rb.end(), std::bind(&node_entry::pinged, _1)); + auto j = std::find_if(rb.begin(), rb.end(), std::bind(&node_entry::pinged, _1)); if (j == rb.end()) j = rb.begin(); b.push_back(*j); rb.erase(j); @@ -395,7 +393,7 @@ void routing_table::remove_node_internal(node_entry* n, bucket_t& b) void routing_table::remove_node(node_entry* n , routing_table::table_t::iterator bucket) - { +{ INVARIANT_CHECK; remove_node_internal(n, bucket->replacements); @@ -543,7 +541,7 @@ routing_table::add_node_status_t routing_table::add_node_impl(node_entry e) // don't add ourself if (e.id == m_id) return failed_to_add; - table_t::iterator i = find_bucket(e.id); + auto const i = find_bucket(e.id); bucket_t& b = i->live_nodes; bucket_t& rb = i->replacements; int const bucket_index = int(std::distance(m_buckets.begin(), i)); @@ -657,7 +655,7 @@ ip_ok: // we may replace other nodes that aren't pinged j = std::find_if(b.begin(), b.end() - , [](node_entry const& ne) { return ne.pinged() == false; }); + , [](node_entry const& ne) { return !ne.pinged(); }); if (j != b.end() && !j->pinged()) { @@ -839,7 +837,7 @@ ip_ok: // but prefer nodes that haven't been pinged, since they are // less reliable than this one, that has been pinged j = std::find_if(rb.begin(), rb.end() - , [] (node_entry const& ne) { return ne.pinged() == false; }); + , [] (node_entry const& ne) { return !ne.pinged(); }); if (j == rb.end()) j = rb.begin(); m_ips.erase(j->addr()); rb.erase(j); @@ -874,7 +872,7 @@ void routing_table::split_bucket() // move any node whose (160 - distance_exp(m_id, id)) >= (i - m_buckets.begin()) // to the new bucket int const new_bucket_size = bucket_limit(bucket_index + 1); - for (bucket_t::iterator j = b.begin(); j != b.end();) + for (auto j = b.begin(); j != b.end();) { int const d = distance_exp(m_id, j->id); if (d >= 159 - bucket_index) @@ -974,11 +972,11 @@ void routing_table::node_failed(node_id const& nid, udp::endpoint const& ep) // if messages to ourself fails, ignore it if (nid == m_id) return; - table_t::iterator i = find_bucket(nid); + auto const i = find_bucket(nid); bucket_t& b = i->live_nodes; bucket_t& rb = i->replacements; - bucket_t::iterator j = std::find_if(b.begin(), b.end() + auto j = std::find_if(b.begin(), b.end() , [&nid](node_entry const& ne) { return ne.id == nid; }); if (j == b.end()) @@ -1076,7 +1074,7 @@ void routing_table::find_node(node_id const& target else { std::remove_copy_if(b.begin(), b.end(), std::back_inserter(l) - , [](node_entry const& ne) { return ne.confirmed() == false; }); + , [](node_entry const& ne) { return !ne.confirmed(); }); } if (int(l.size()) == count) return; @@ -1115,7 +1113,7 @@ void routing_table::find_node(node_id const& target else { std::remove_copy_if(b.begin(), b.end(), std::back_inserter(l) - , [](node_entry const& ne) { return ne.confirmed() == false; }); + , [](node_entry const& ne) { return !ne.confirmed(); }); } if (int(l.size()) == count) return;