diff options
author | Jeffrey <jeffryan@tamu.edu> | 2022-03-07 20:49:19 -0600 |
---|---|---|
committer | Jeffrey <jeffryan@tamu.edu> | 2022-04-18 09:55:21 -0500 |
commit | 1f25aa27c5cf83afd8e332bd5fe84c88c3dd7ffd (patch) | |
tree | 42f45ab350bcf55cdd99c829b64ad9fdc5346220 | |
parent | Move copyable_atomic into connection_context (diff) | |
download | monero-1f25aa27c5cf83afd8e332bd5fe84c88c3dd7ffd.tar.xz |
Factor out move_it_backward from misc_language.h
Relevant commit from old PR:
330df2952cb2863a591158b984c0fb7f652887ac
-rw-r--r-- | contrib/epee/include/misc_language.h | 10 | ||||
-rw-r--r-- | src/p2p/net_peerlist.h | 23 |
2 files changed, 16 insertions, 17 deletions
diff --git a/contrib/epee/include/misc_language.h b/contrib/epee/include/misc_language.h index bf75da23f..ee07bbe8f 100644 --- a/contrib/epee/include/misc_language.h +++ b/contrib/epee/include/misc_language.h @@ -37,15 +37,7 @@ namespace epee namespace misc_utils { - template<typename t_iterator> - t_iterator move_it_backward(t_iterator it, size_t count) - { - while(count--) - it--; - return it; - } - - bool sleep_no_w(long ms ); + bool sleep_no_w(long ms); template <typename T> T get_mid(const T &a, const T &b) diff --git a/src/p2p/net_peerlist.h b/src/p2p/net_peerlist.h index 2cb85d270..8a56bb378 100644 --- a/src/p2p/net_peerlist.h +++ b/src/p2p/net_peerlist.h @@ -31,6 +31,7 @@ #pragma once #include <iosfwd> +#include <iterator> #include <list> #include <string> #include <vector> @@ -46,7 +47,6 @@ #include "crypto/crypto.h" #include "cryptonote_config.h" #include "net/enums.h" -#include "net/local_ip.h" #include "p2p_protocol_defs.h" #include "syncobj.h" @@ -184,6 +184,7 @@ namespace nodetool private: void trim_white_peerlist(); void trim_gray_peerlist(); + static peerlist_entry get_nth_latest_peer(peers_indexed& peerlist, size_t n); friend class boost::serialization::access; epee::critical_section m_peerlist_lock; @@ -214,6 +215,16 @@ namespace nodetool } } //-------------------------------------------------------------------------------------------------- + inline + peerlist_entry peerlist_manager::get_nth_latest_peer(peers_indexed& peerlist, const size_t n) + { + // Is not thread-safe nor does it check bounds. Do this before calling. Indexing starts at 0. + peers_indexed::index<by_time>::type& by_time_index = peerlist.get<by_time>(); + auto by_time_it = --by_time_index.end(); + std::advance(by_time_it, -((long long) n)); + return *by_time_it; + } + //-------------------------------------------------------------------------------------------------- inline bool peerlist_manager::merge_peerlist(const std::vector<peerlist_entry>& outer_bs, const std::function<bool(const peerlist_entry&)> &f) { @@ -235,8 +246,7 @@ namespace nodetool if(i >= m_peers_white.size()) return false; - peers_indexed::index<by_time>::type& by_time_index = m_peers_white.get<by_time>(); - p = *epee::misc_utils::move_it_backward(--by_time_index.end(), i); + p = peerlist_manager::get_nth_latest_peer(m_peers_white, i); return true; } //-------------------------------------------------------------------------------------------------- @@ -247,8 +257,7 @@ namespace nodetool if(i >= m_peers_gray.size()) return false; - peers_indexed::index<by_time>::type& by_time_index = m_peers_gray.get<by_time>(); - p = *epee::misc_utils::move_it_backward(--by_time_index.end(), i); + p = peerlist_manager::get_nth_latest_peer(m_peers_gray, i); return true; } //-------------------------------------------------------------------------------------------------- @@ -437,9 +446,7 @@ namespace nodetool } size_t random_index = crypto::rand_idx(m_peers_gray.size()); - - peers_indexed::index<by_time>::type& by_time_index = m_peers_gray.get<by_time>(); - pe = *epee::misc_utils::move_it_backward(--by_time_index.end(), random_index); + pe = peerlist_manager::get_nth_latest_peer(m_peers_gray, random_index); return true; |