00001
00002
00003
00004
00005
00006
00007
00008 #include <algorithm>
00009 #include <iomanip>
00010 #include <cassert>
00011 #include <boost/bind.hpp>
00012
00013 #include "libtorrent/kademlia/node_id.hpp"
00014
00015 using boost::bind;
00016
00017 namespace libtorrent { namespace dht
00018 {
00019
00020 node_id distance(node_id const& n1, node_id const& n2)
00021 {
00022 node_id ret;
00023 node_id::iterator k = ret.begin();
00024 for (node_id::const_iterator i = n1.begin(), j = n2.begin()
00025 , end(n1.end()); i != end; ++i, ++j, ++k)
00026 {
00027 *k = *i ^ *j;
00028 }
00029 return ret;
00030 }
00031
00032 bool compare_ref(node_id const& n1, node_id const& n2, node_id const& ref)
00033 {
00034 for (node_id::const_iterator i = n1.begin(), j = n2.begin()
00035 , k = ref.begin(), end(n1.end()); i != end; ++i, ++j, ++k)
00036 {
00037 boost::uint8_t lhs = (*i ^ *k);
00038 boost::uint8_t rhs = (*j ^ *k);
00039 if (lhs < rhs) return true;
00040 if (lhs > rhs) return false;
00041 }
00042 return false;
00043 }
00044
00045 int distance_exp(node_id const& n1, node_id const& n2)
00046 {
00047 int byte = node_id::size - 1;
00048 for (node_id::const_iterator i = n1.begin(), j = n2.begin()
00049 , end(n1.end()); i != end; ++i, ++j, --byte)
00050 {
00051 assert(byte >= 0);
00052 boost::uint8_t t = *i ^ *j;
00053 if (t == 0) continue;
00054
00055
00056
00057 int bit = byte * 8;
00058 for (int b = 7; b > 0; --b)
00059 if (t >= (1 << b)) return bit + b;
00060 return bit;
00061 }
00062
00063 return 0;
00064 }
00065
00066 } }
00067