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:
parent
aabba128ae
commit
330df2952c
|
@ -30,74 +30,14 @@
|
||||||
|
|
||||||
#include <boost/utility/value_init.hpp>
|
#include <boost/utility/value_init.hpp>
|
||||||
#include <boost/shared_ptr.hpp>
|
#include <boost/shared_ptr.hpp>
|
||||||
#include <limits>
|
|
||||||
#include <functional>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
namespace epee
|
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)>()
|
#define AUTO_VAL_INIT(v) boost::value_initialized<decltype(v)>()
|
||||||
|
|
||||||
namespace misc_utils
|
namespace misc_utils
|
||||||
{
|
{
|
||||||
template<typename t_type>
|
bool sleep_no_w(long ms);
|
||||||
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 );
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T get_mid(const T &a, const T &b)
|
T get_mid(const T &a, const T &b)
|
||||||
|
|
|
@ -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>
|
||||||
|
@ -184,6 +185,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;
|
||||||
|
@ -215,6 +217,16 @@ namespace nodetool
|
||||||
}
|
}
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
inline
|
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)
|
bool peerlist_manager::merge_peerlist(const std::vector<peerlist_entry>& outer_bs, const std::function<bool(const peerlist_entry&)> &f)
|
||||||
{
|
{
|
||||||
CRITICAL_REGION_LOCAL(m_peerlist_lock);
|
CRITICAL_REGION_LOCAL(m_peerlist_lock);
|
||||||
|
@ -235,8 +247,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 +258,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 +447,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;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue