Commit 1fd7d8a7 authored by nagayama15's avatar nagayama15

Make streams to accept any uint types

parent d6dc6169
...@@ -45,8 +45,10 @@ namespace kyut { ...@@ -45,8 +45,10 @@ namespace kyut {
pos_write_++; pos_write_++;
} }
void write(std::uint64_t value, std::size_t length) { template <typename UInt>
assert(length <= 64); void write(UInt value, std::size_t length) {
static_assert(std::is_unsigned_v<UInt>);
assert(length <= sizeof(UInt) * 8);
for (std::size_t i = 0; i < length; i++) { for (std::size_t i = 0; i < length; i++) {
writeBit((value & (1 << (length - i - 1))) != 0); writeBit((value & (1 << (length - i - 1))) != 0);
......
...@@ -48,8 +48,12 @@ namespace kyut { ...@@ -48,8 +48,12 @@ namespace kyut {
return ((data_[pos / 8 % data_.size()] << (pos % 8)) & 0x80) != 0; return ((data_[pos / 8 % data_.size()] << (pos % 8)) & 0x80) != 0;
} }
std::uint64_t read(std::size_t countBits) { template <typename UInt>
std::uint64_t value = 0; UInt read(std::size_t countBits) {
static_assert(std::is_unsigned_v<UInt>);
assert(countBits <= sizeof(UInt) * 8);
UInt value = 0;
for (std::size_t i = 0; i < countBits; i++) { for (std::size_t i = 0; i < countBits; i++) {
value <<= 1; value <<= 1;
......
...@@ -61,7 +61,7 @@ namespace kyut::watermarker { ...@@ -61,7 +61,7 @@ namespace kyut::watermarker {
std::sort(chunkBegin, chunkEnd, [](const auto &a, const auto &b) { return a->name < b->name; }); std::sort(chunkBegin, chunkEnd, [](const auto &a, const auto &b) { return a->name < b->name; });
// A watermark to embed in the chunk // A watermark to embed in the chunk
auto watermark = stream.read(numBitsEmbeddedInChunk); auto watermark = stream.read<std::uint64_t>(numBitsEmbeddedInChunk);
// Reorder the functions // Reorder the functions
for (auto it = chunkBegin; it != chunkEnd; ++it) { for (auto it = chunkBegin; it != chunkEnd; ++it) {
...@@ -109,8 +109,8 @@ namespace kyut::watermarker { ...@@ -109,8 +109,8 @@ namespace kyut::watermarker {
return a->name < b->name; return a->name < b->name;
}); });
std::int64_t watermark = 0; std::uint64_t watermark = 0;
std::int64_t base = 1; std::uint64_t base = 1;
for (auto it = chunkBegin; it != chunkEnd; ++it) { for (auto it = chunkBegin; it != chunkEnd; ++it) {
// Get index of the function `*it` // Get index of the function `*it`
......
...@@ -42,7 +42,7 @@ BOOST_AUTO_TEST_CASE(write) { ...@@ -42,7 +42,7 @@ BOOST_AUTO_TEST_CASE(write) {
BOOST_REQUIRE_EQUAL(s.data().size(), std::size_t{0}); BOOST_REQUIRE_EQUAL(s.data().size(), std::size_t{0});
BOOST_REQUIRE_EQUAL(s.tell(), std::size_t{0}); BOOST_REQUIRE_EQUAL(s.tell(), std::size_t{0});
s.write(0xabcd, 16); s.write(std::uint16_t{0xabcd}, 16);
BOOST_REQUIRE_EQUAL(s.data().size(), std::size_t{2}); BOOST_REQUIRE_EQUAL(s.data().size(), std::size_t{2});
BOOST_REQUIRE_EQUAL(s.data()[0], std::uint8_t{0xab}); BOOST_REQUIRE_EQUAL(s.data()[0], std::uint8_t{0xab});
BOOST_REQUIRE_EQUAL(s.data()[1], std::uint8_t{0xcd}); BOOST_REQUIRE_EQUAL(s.data()[1], std::uint8_t{0xcd});
......
...@@ -43,18 +43,18 @@ BOOST_AUTO_TEST_CASE(read) { ...@@ -43,18 +43,18 @@ BOOST_AUTO_TEST_CASE(read) {
CircularBitStreamReader reader{std::begin(data), std::end(data)}; CircularBitStreamReader reader{std::begin(data), std::end(data)};
BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{0}); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{0});
BOOST_REQUIRE_EQUAL(reader.read(4), std::uint64_t{0x8}); BOOST_REQUIRE_EQUAL(reader.read<std::uint32_t>(4), std::uint32_t{0x8});
BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{4}); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{4});
BOOST_REQUIRE_EQUAL(reader.read(4), std::uint64_t{0x9}); BOOST_REQUIRE_EQUAL(reader.read<std::uint32_t>(4), std::uint32_t{0x9});
BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{8}); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{8});
BOOST_REQUIRE_EQUAL(reader.read(8), std::uint64_t{0xab}); BOOST_REQUIRE_EQUAL(reader.read<std::uint32_t>(8), std::uint32_t{0xab});
BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{16}); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{16});
BOOST_REQUIRE_EQUAL(reader.read(16), std::uint64_t{0xcdef}); BOOST_REQUIRE_EQUAL(reader.read<std::uint32_t>(16), std::uint32_t{0xcdef});
BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{32}); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{32});
BOOST_REQUIRE_EQUAL(reader.read(24), std::uint64_t{0x89abcd}); BOOST_REQUIRE_EQUAL(reader.read<std::uint32_t>(24), std::uint32_t{0x89abcd});
BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{56}); BOOST_REQUIRE_EQUAL(reader.tell(), std::size_t{56});
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment