Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IF: pack size of bitset #57

Merged
merged 6 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions libraries/libfc/include/fc/io/raw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,30 +567,35 @@ namespace fc {

template<typename Stream, typename T>
inline void pack( Stream& s, const boost::dynamic_bitset<T>& value ) {
// pack the size of the bitset, not the number of blocks
const auto num_blocks = value.num_blocks();
FC_ASSERT( num_blocks <= MAX_NUM_ARRAY_ELEMENTS );
fc::raw::pack( s, unsigned_int((uint32_t)num_blocks) );

fc::raw::pack( s, unsigned_int(value.size()) );
constexpr size_t word_size = sizeof(T) * CHAR_BIT;
assert(num_blocks == (value.size() + word_size - 1) / word_size);
// convert bitset to a vector of blocks
std::vector<T> blocks;
blocks.resize(num_blocks);
boost::to_block_range(value, blocks.begin());

// pack the blocks
for (const auto& b: blocks) {
fc::raw::pack( s, b );
fc::raw::pack( s, b );
}
}

template<typename Stream, typename T>
inline void unpack( Stream& s, boost::dynamic_bitset<T>& value ) {
// the packed size is the number of bits in the set, not the number of blocks
unsigned_int size; fc::raw::unpack( s, size );
FC_ASSERT( size.value <= MAX_NUM_ARRAY_ELEMENTS );
std::vector<T> blocks((size_t)size.value);
for( uint64_t i = 0; i < size.value; ++i ) {
fc::raw::unpack( s, blocks[i] );
constexpr size_t word_size = sizeof(T) * CHAR_BIT;
size_t num_blocks = (size + word_size - 1) / word_size;
FC_ASSERT( num_blocks <= MAX_NUM_ARRAY_ELEMENTS );
std::vector<T> blocks(num_blocks);
for( size_t i = 0; i < num_blocks; ++i ) {
fc::raw::unpack( s, blocks[i] );
}
value = { blocks.cbegin(), blocks.cend() };
value.resize(size.value);
}

template<typename Stream, typename T>
Expand Down
41 changes: 40 additions & 1 deletion libraries/libfc/test/io/test_raw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ BOOST_AUTO_TEST_CASE(dynamic_bitset_test)
constexpr uint8_t bits = 0b00011110;
boost::dynamic_bitset<uint8_t> bs1(8, bits); // bit set size 8

char buff[4];
char buff[32];
datastream<char*> ds(buff, sizeof(buff));

fc::raw::pack( ds, bs1 );
Expand All @@ -35,4 +35,43 @@ BOOST_AUTO_TEST_CASE(dynamic_bitset_test)
BOOST_CHECK(!bs2.test(7));
}

BOOST_AUTO_TEST_CASE(dynamic_bitset_large_test)
{
boost::dynamic_bitset<uint32_t> bs1;
bs1.resize(12345);

bs1.set(42);
bs1.set(23);
bs1.set(12000);

auto packed = fc::raw::pack(bs1);
auto unpacked = fc::raw::unpack<boost::dynamic_bitset<uint32_t>>(packed);

BOOST_TEST(unpacked.at(42));
BOOST_TEST(unpacked.at(23));
BOOST_TEST(unpacked.at(12000));
unpacked.flip(42);
unpacked.flip(23);
unpacked.flip(12000);
BOOST_TEST(unpacked.none());
}

BOOST_AUTO_TEST_CASE(dynamic_bitset_small_test)
{
boost::dynamic_bitset<uint32_t> bs1;
bs1.resize(21);

bs1.set(2);
bs1.set(7);

auto packed = fc::raw::pack(bs1);
auto unpacked = fc::raw::unpack<boost::dynamic_bitset<uint32_t>>(packed);

BOOST_TEST(unpacked.at(2));
BOOST_TEST(unpacked.at(7));
unpacked.flip(2);
unpacked.flip(7);
BOOST_TEST(unpacked.none());
}

BOOST_AUTO_TEST_SUITE_END()
Loading