src/kademlia/routing_table.cpp
// fills the vector with the k nodes from our buckets that
// are nearest to the given id.
fills the vector with the k nodes from our buckets that
are nearest to the given id.
fills the vector with the k nodes from our buckets that are nearest to the given id .
void routing_table::find_node(node_id const& target
	, std::vector<node_entry>& l, int options, int count)
{
	l.clear();
	if (count == 0) count = m_bucket_size;
	table_t::iterator i = find_bucket(target);
	int bucket_index = std::distance(m_buckets.begin(), i);
	int bucket_size_limit = bucket_limit(bucket_index);
	l.reserve(bucket_size_limit);
	table_t::iterator j = i;
	int unsorted_start_idx = 0;
	for (; j != m_buckets.end() && int(l.size()) < count; ++j)
	{
		bucket_t& b = j->live_nodes;
		if (options & include_failed)
		{
			copy(b.begin(), b.end()
				, std::back_inserter(l));
		}
		else
		{
			std::remove_copy_if(b.begin(), b.end(), std::back_inserter(l)
				, [](node_entry const& ne) { return ne.confirmed() == false; });
		}
		if (int(l.size()) == count) return;
		if (int(l.size()) > count)
		{
			std::sort(l.begin() + unsorted_start_idx, l.end()
				, [&target](node_entry const& lhs, node_entry const& rhs)
				{ return compare_ref(lhs.id, rhs.id, target); });
			l.resize(count);
			return;
		}
		unsorted_start_idx = int(l.size());
	}
	if (i == m_buckets.begin())
		return;
	j = i;
	unsorted_start_idx = int(l.size());
	do
	{
		--j;
		bucket_t& b = j->live_nodes;
		if (options & include_failed)
		{
			std::copy(b.begin(), b.end(), std::back_inserter(l));
		}
		else
		{
			std::remove_copy_if(b.begin(), b.end(), std::back_inserter(l)
				, [](node_entry const& ne) { return ne.confirmed() == false; });
		}
		if (int(l.size()) == count) return;
		if (int(l.size()) > count)
		{
			std::sort(l.begin() + unsorted_start_idx, l.end()
				, [&target](node_entry const& lhs, node_entry const& rhs)
				{ return compare_ref(lhs.id, rhs.id, target); });
			l.resize(count);
			return;
		}
		unsorted_start_idx = int(l.size());
	}
	while (j != m_buckets.begin() && int(l.size()) < count);
	TORRENT_ASSERT(int(l.size()) <= count);
}
void routing_table :: find_node ( node_id const & target , std :: vector < node_entry > & l , int options , int count ) { l . clear ( ) ; if ( count == 0 ) count = m_bucket_size ; table_t :: iterator i = find_bucket ( target ) ; int bucket_index = std :: distance ( m_buckets . begin ( ) , i ) ; int bucket_size_limit = bucket_limit ( bucket_index ) ; l . reserve ( bucket_size_limit ) ; table_t :: iterator j = i ; int unsorted_start_idx = 0 ; for ( ; j != m_buckets . end ( ) && int ( l . size ( ) ) < count ; ++ j ) { bucket_t & b = j -> live_nodes ; if ( options & include_failed ) { copy ( b . begin ( ) , b . end ( ) , std :: back_inserter ( l ) ) ; } else { std :: remove_copy_if ( b . begin ( ) , b . end ( ) , std :: back_inserter ( l ) , [ ] ( node_entry const & ne ) { return ne . confirmed ( ) == false ; } ) ; } if ( int ( l . size ( ) ) == count ) return ; if ( int ( l . size ( ) ) > count ) { std :: sort ( l . begin ( ) + unsorted_start_idx , l . end ( ) , [ & target ] ( node_entry const & lhs , node_entry const & rhs ) { return compare_ref ( lhs . id , rhs . id , target ) ; } ) ; l . resize ( count ) ; return ; } unsorted_start_idx = int ( l . size ( ) ) ; } if ( i == m_buckets . begin ( ) ) return ; j = i ; unsorted_start_idx = int ( l . size ( ) ) ; do { -- j ; bucket_t & b = j -> live_nodes ; if ( options & include_failed ) { std :: copy ( b . begin ( ) , b . end ( ) , std :: back_inserter ( l ) ) ; } else { std :: remove_copy_if ( b . begin ( ) , b . end ( ) , std :: back_inserter ( l ) , [ ] ( node_entry const & ne ) { return ne . confirmed ( ) == false ; } ) ; } if ( int ( l . size ( ) ) == count ) return ; if ( int ( l . size ( ) ) > count ) { std :: sort ( l . begin ( ) + unsorted_start_idx , l . end ( ) , [ & target ] ( node_entry const & lhs , node_entry const & rhs ) { return compare_ref ( lhs . id , rhs . id , target ) ; } ) ; l . resize ( count ) ; return ; } unsorted_start_idx = int ( l . size ( ) ) ; } while ( j != m_buckets . begin ( ) && int ( l . size ( ) ) < count ) ; TORRENT_ASSERT ( int ( l . size ( ) ) <= count ) ; }
fills the vector with the k nodes from our buckets that
are nearest to the given id.
fills the vector with the k nodes from our buckets that are nearest to the given id .
// sort the nodes by how close they are to the target // if we still don't have enough nodes, copy nodes // further away from us // sort the nodes by how close they are to the target