Trim fat from misc_language.h and refactor net_peerlist.h

The following were removed from misc_language.h:
* Includes to `<limit>` and `<functional>`
* Macros `STD_TRY_BEGIN` and `STD_TRY_CATCH`
* Function `get_max_t_val`
* Iterator-related functions `move_it_forward` and `move_it_backward`
* struct `less_as_pod`
* memory compare function `is_less_as_pod`

All the above were unmentioned throughout the rest of the project except for `move_it_backward`,
which was mentioned 3 times in `net_peerlist.h`. However, the same exact behaviour can be
acheived with `std::advance()`, so I unloaded the iterator logic into a private static inline
method `peerlist_manager::get_nth_latest_peer()`. Personally, I think this has the side effect
of making the functions `get_white_peer_by_index()`, `get_gray_peer_by_index()`, and
`get_random_gray_peer()` clearer in their implementation.
This commit is contained in:
Jeffrey 2022-03-07 11:24:34 -06:00
parent aabba128ae
commit 330df2952c
2 changed files with 16 additions and 68 deletions

View File

@ -30,74 +30,14 @@
#include <boost/utility/value_init.hpp>
#include <boost/shared_ptr.hpp>
#include <limits>
#include <functional>
#include <vector>
namespace epee
{
#define STD_TRY_BEGIN() try {
#define STD_TRY_CATCH(where_, ret_val) \
} \
catch (const std::exception &e) \
{ \
LOG_ERROR("EXCEPTION: " << where_ << ", mes: "<< e.what()); \
return ret_val; \
} \
catch (...) \
{ \
LOG_ERROR("EXCEPTION: " << where_ ); \
return ret_val; \
}
#define AUTO_VAL_INIT(v) boost::value_initialized<decltype(v)>()
namespace misc_utils
{
template<typename t_type>
t_type get_max_t_val(t_type t)
{
return (std::numeric_limits<t_type>::max)();
}
template<typename t_iterator>
t_iterator move_it_forward(t_iterator it, size_t count)
{
while(count--)
it++;
return it;
}
template<typename t_iterator>
t_iterator move_it_backward(t_iterator it, size_t count)
{
while(count--)
it--;
return it;
}
// TEMPLATE STRUCT less
template<class _Ty>
struct less_as_pod
: public std::binary_function<_Ty, _Ty, bool>
{ // functor for operator<
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{ // apply operator< to operands
return memcmp(&_Left, &_Right, sizeof(_Left)) < 0;
}
};
template<class _Ty>
bool is_less_as_pod(const _Ty& _Left, const _Ty& _Right)
{ // apply operator< to operands
return memcmp(&_Left, &_Right, sizeof(_Left)) < 0;
}
bool sleep_no_w(long ms );
bool sleep_no_w(long ms);
template <typename T>
T get_mid(const T &a, const T &b)

View File

@ -31,6 +31,7 @@
#pragma once
#include <iosfwd>
#include <iterator>
#include <list>
#include <string>
#include <vector>
@ -184,6 +185,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 +216,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 +247,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 +258,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 +447,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;