replaced vector<bool> with a custom bitfield type

This commit is contained in:
Arvid Norberg
2008-05-28 02:35:02 +00:00
parent 523c48e069
commit 68c31e48dc
19 changed files with 370 additions and 112 deletions

View File

@@ -1,5 +1,6 @@
#include "libtorrent/piece_picker.hpp"
#include "libtorrent/policy.hpp"
#include "libtorrent/bitfield.hpp"
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
#include <algorithm>
@@ -12,12 +13,12 @@ using namespace libtorrent;
const int blocks_per_piece = 4;
std::vector<bool> string2vec(char const* have_str)
bitfield string2vec(char const* have_str)
{
const int num_pieces = strlen(have_str);
std::vector<bool> have(num_pieces, false);
bitfield have(num_pieces, false);
for (int i = 0; i < num_pieces; ++i)
if (have_str[i] != ' ') have[i] = true;
if (have_str[i] != ' ') have.set_bit(i);
return have;
}
@@ -37,7 +38,7 @@ boost::shared_ptr<piece_picker> setup_picker(
boost::shared_ptr<piece_picker> p(new piece_picker(blocks_per_piece, num_pieces * blocks_per_piece));
std::vector<bool> have = string2vec(have_str);
bitfield have = string2vec(have_str);
for (int i = 0; i < num_pieces; ++i)
{

View File

@@ -4,6 +4,7 @@
#include "libtorrent/xml_parse.hpp"
#include "libtorrent/upnp.hpp"
#include "libtorrent/entry.hpp"
#include "libtorrent/bitfield.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/escape_string.hpp"
#include "libtorrent/broadcast_socket.hpp"
@@ -473,6 +474,31 @@ int test_main()
TEST_CHECK(common_bits(&h1[0], &h2[0], 20) == 12);
h2 = boost::lexical_cast<sha1_hash>("0123456789abcdef11232456789abcdef0123456");
TEST_CHECK(common_bits(&h1[0], &h2[0], 20) == 16 * 4 + 3);
// test bitfield
bitfield test1(10, false);
TEST_CHECK(test1.count() == 0);
test1.set_bit(9);
TEST_CHECK(test1.count() == 1);
test1.clear_bit(9);
TEST_CHECK(test1.count() == 0);
test1.set_bit(2);
TEST_CHECK(test1.count() == 1);
test1.set_bit(1);
test1.set_bit(9);
TEST_CHECK(test1.count() == 3);
test1.clear_bit(2);
TEST_CHECK(test1.count() == 2);
int distance = std::distance(test1.begin(), test1.end());
std::cerr << distance << std::endl;
TEST_CHECK(distance == 10);
test1.set_all();
TEST_CHECK(test1.count() == 10);
test1.clear_all();
TEST_CHECK(test1.count() == 0);
return 0;
}