Verified Commit 2363ea59 authored by nagayama15's avatar nagayama15

feat(stream): add BitStreamWriter

parent bdf62cf6
...@@ -26,5 +26,6 @@ include(cmake/cmdline.cmake) ...@@ -26,5 +26,6 @@ include(cmake/cmdline.cmake)
include(cmake/fmt.cmake) include(cmake/fmt.cmake)
include(cmake/googletest.cmake) include(cmake/googletest.cmake)
add_subdirectory(lib)
add_subdirectory(src) add_subdirectory(src)
add_subdirectory(test) add_subdirectory(test)
add_library(kyut INTERFACE)
target_include_directories(kyut INTERFACE
"."
)
target_link_libraries(kyut INTERFACE
binaryen::binaryen
fmtlib::fmt
)
#ifndef INCLUDE_kyut_BitStreamWriter_hpp
#define INCLUDE_kyut_BitStreamWriter_hpp
#include <cassert>
#include <string_view>
#include <vector>
namespace kyut {
class BitStreamWriter {
public:
BitStreamWriter()
: data_()
, pos_bits_(0) {
}
// Uncopyable and unmovable
BitStreamWriter(const BitStreamWriter&) = delete;
BitStreamWriter(BitStreamWriter&&) = delete;
BitStreamWriter& operator=(const BitStreamWriter&) = delete;
BitStreamWriter& operator=(BitStreamWriter&&) = delete;
~BitStreamWriter() noexcept = default;
void write(std::uint64_t x, std::size_t size_bits) {
assert(size_bits <= 64);
for (std::size_t i = 0; i < size_bits; i++) {
const auto bit = (x >> (size_bits - i - 1)) & 1;
write_bit(bit != 0);
}
}
void write_bit(bool x) {
const auto n = pos_bits_ >> 3;
const auto k = pos_bits_ & 7;
if (k == 0) {
data_.emplace_back(0);
}
data_[n] |= (x ? 1 : 0) << (7 - k);
pos_bits_++;
}
std::size_t position_bits() const noexcept {
return pos_bits_;
}
const std::vector<std::uint8_t>& data() const noexcept {
return data_;
}
std::string_view data_as_str() const noexcept {
return std::string_view(reinterpret_cast<const char*>(data_.data()), data_.size());
}
private:
std::vector<std::uint8_t> data_;
std::size_t pos_bits_;
};
} // namespace kyut
#endif // INCLUDE_kyut_BitStreamWriter_hpp
...@@ -3,9 +3,8 @@ add_executable(snpi ...@@ -3,9 +3,8 @@ add_executable(snpi
) )
target_link_libraries(snpi target_link_libraries(snpi
kyut
cmdline::cmdline cmdline::cmdline
binaryen::binaryen
fmtlib::fmt
) )
add_executable(pisn add_executable(pisn
...@@ -13,7 +12,6 @@ add_executable(pisn ...@@ -13,7 +12,6 @@ add_executable(pisn
) )
target_link_libraries(pisn target_link_libraries(pisn
kyut
cmdline::cmdline cmdline::cmdline
binaryen::binaryen
fmtlib::fmt
) )
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/test") set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/test")
add_executable(test_kyut add_executable(test_kyut
test_kyut.cpp test_BitStreamWriter.cpp
) )
target_link_libraries(test_kyut target_link_libraries(test_kyut
kyut
Threads::Threads Threads::Threads
fmtlib::fmt
googletest::gtest googletest::gtest
googletest::gtest_main googletest::gtest_main
) )
......
#include "kyut/BitStreamWriter.hpp"
#include <gtest/gtest.h>
TEST(kyut, BitStreamWriter) {
kyut::BitStreamWriter w{};
EXPECT_EQ(w.position_bits(), 0);
EXPECT_EQ(w.data_as_str(), "");
w.write(0x0A, 4);
EXPECT_EQ(w.position_bits(), 4);
EXPECT_EQ(w.data_as_str(), "\xA0");
w.write(0xBC, 8);
EXPECT_EQ(w.position_bits(), 12);
EXPECT_EQ(w.data_as_str(), "\xAB\xC0");
w.write(0xDEF, 12);
EXPECT_EQ(w.position_bits(), 24);
EXPECT_EQ(w.data_as_str(), "\xAB\xCD\xEF");
}
#include <gtest/gtest.h>
TEST(kyut, dummy) {
}
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