move bitfield's find_first_set and find_last_clear into cpp file (#1155)

This commit is contained in:
Arvid Norberg 2016-09-26 10:58:09 -04:00 committed by GitHub
parent 97c6a75084
commit 179e238561
2 changed files with 23 additions and 19 deletions

View File

@ -154,25 +154,8 @@ namespace libtorrent
// count the number of bits in the bitfield that are set to 1.
int count() const;
int find_first_set() const
{
size_t const num = num_words();
if (num == 0) return -1;
int const count = aux::count_leading_zeros({&m_buf[1], num});
return count != int(num) * 32 ? count : -1;
}
int find_last_clear() const
{
size_t const num = num_words();
if (num == 0) return - 1;
int const size = this->size();
std::uint32_t const mask = 0xffffffff << (32 - (size & 31));
std::uint32_t const last = m_buf[num] ^ aux::host_to_network(mask);
int const ext = aux::count_trailing_ones(~last) - (31 - (size % 32));
return last != 0
? (int(num) - 1) * 32 + ext
: size - (aux::count_trailing_ones({&m_buf[1], num - 1}) + ext);
}
int find_first_set() const;
int find_last_clear() const;
struct const_iterator
{

View File

@ -190,4 +190,25 @@ namespace libtorrent
clear_trailing_bits();
TORRENT_ASSERT(size() == bits);
}
int bitfield::find_first_set() const
{
size_t const num = num_words();
if (num == 0) return -1;
int const count = aux::count_leading_zeros({&m_buf[1], num});
return count != int(num) * 32 ? count : -1;
}
int bitfield::find_last_clear() const
{
size_t const num = num_words();
if (num == 0) return - 1;
int const size = this->size();
std::uint32_t const mask = 0xffffffff << (32 - (size & 31));
std::uint32_t const last = m_buf[num] ^ aux::host_to_network(mask);
int const ext = aux::count_trailing_ones(~last) - (31 - (size % 32));
return last != 0
? (int(num) - 1) * 32 + ext
: size - (aux::count_trailing_ones({&m_buf[1], num - 1}) + ext);
}
}