Factor out move_it_backward from misc_language.h

Relevant commit from old PR:
330df2952c
This commit is contained in:
Jeffrey 2022-03-07 20:49:19 -06:00
parent 7764d69782
commit 1f25aa27c5
2 changed files with 16 additions and 17 deletions

View file

@ -37,15 +37,7 @@ namespace epee
namespace misc_utils namespace misc_utils
{ {
template<typename t_iterator> bool sleep_no_w(long ms);
t_iterator move_it_backward(t_iterator it, size_t count)
{
while(count--)
it--;
return it;
}
bool sleep_no_w(long ms );
template <typename T> template <typename T>
T get_mid(const T &a, const T &b) T get_mid(const T &a, const T &b)

View file

@ -31,6 +31,7 @@
#pragma once #pragma once
#include <iosfwd> #include <iosfwd>
#include <iterator>
#include <list> #include <list>
#include <string> #include <string>
#include <vector> #include <vector>
@ -46,7 +47,6 @@
#include "crypto/crypto.h" #include "crypto/crypto.h"
#include "cryptonote_config.h" #include "cryptonote_config.h"
#include "net/enums.h" #include "net/enums.h"
#include "net/local_ip.h"
#include "p2p_protocol_defs.h" #include "p2p_protocol_defs.h"
#include "syncobj.h" #include "syncobj.h"
@ -184,6 +184,7 @@ namespace nodetool
private: private:
void trim_white_peerlist(); void trim_white_peerlist();
void trim_gray_peerlist(); void trim_gray_peerlist();
static peerlist_entry get_nth_latest_peer(peers_indexed& peerlist, size_t n);
friend class boost::serialization::access; friend class boost::serialization::access;
epee::critical_section m_peerlist_lock; 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 inline
bool peerlist_manager::merge_peerlist(const std::vector<peerlist_entry>& outer_bs, const std::function<bool(const peerlist_entry&)> &f) 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()) if(i >= m_peers_white.size())
return false; return false;
peers_indexed::index<by_time>::type& by_time_index = m_peers_white.get<by_time>(); p = peerlist_manager::get_nth_latest_peer(m_peers_white, i);
p = *epee::misc_utils::move_it_backward(--by_time_index.end(), i);
return true; return true;
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@ -247,8 +257,7 @@ namespace nodetool
if(i >= m_peers_gray.size()) if(i >= m_peers_gray.size())
return false; return false;
peers_indexed::index<by_time>::type& by_time_index = m_peers_gray.get<by_time>(); p = peerlist_manager::get_nth_latest_peer(m_peers_gray, i);
p = *epee::misc_utils::move_it_backward(--by_time_index.end(), i);
return true; return true;
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@ -437,9 +446,7 @@ namespace nodetool
} }
size_t random_index = crypto::rand_idx(m_peers_gray.size()); size_t random_index = crypto::rand_idx(m_peers_gray.size());
pe = peerlist_manager::get_nth_latest_peer(m_peers_gray, random_index);
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);
return true; return true;