-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-Id: I97309a7fd3c7059fe439fe516ba97920b53b5fcd
- Loading branch information
Showing
8 changed files
with
154 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#pragma once | ||
#include "huffman/src/bit_span.hpp" | ||
#include "huffman/src/code.hpp" | ||
#include "huffman/src/table.hpp" | ||
|
||
#include <iterator> | ||
|
||
namespace gpu_deflate::huffman { | ||
/// Decodes a bit stream using a code table. | ||
/// | ||
/// If a code from \p bits is not found in \p code_table, the | ||
/// decoding returns immediately without reading remaining \p bits. | ||
/// | ||
/// @param code_table The code table to use for decoding. | ||
/// @param bits The bit stream to decode. | ||
/// @param output The output iterator to write the decoded symbols to. | ||
/// | ||
/// @returns The output iterator after writing the decoded symbols. | ||
/// @tparam Symbol The type of the symbols in the code table. | ||
/// @tparam Extent The extent of the code table. | ||
/// @tparam O The type of the output iterator. | ||
template < | ||
std::regular Symbol, | ||
std::size_t Extent = std::dynamic_extent, | ||
std::output_iterator<Symbol> O> | ||
constexpr auto | ||
decode(const table<Symbol, Extent>& code_table, bit_span bits, O output) -> O | ||
{ | ||
code current_code{}; | ||
auto code_table_pos = code_table.begin(); | ||
for (auto bit : bits) { | ||
current_code << bit; | ||
auto found = code_table.find(current_code, code_table_pos); | ||
if (found) { | ||
*output = (*found)->symbol; | ||
output++; | ||
code_table_pos = code_table.begin(); | ||
current_code = code{}; | ||
continue; | ||
} | ||
if (found.error() == code_table.end()) { | ||
break; | ||
} | ||
code_table_pos = found.error(); | ||
} | ||
return output; | ||
} | ||
} // namespace gpu_deflate::huffman |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include "huffman/huffman.hpp" | ||
|
||
#include <boost/ut.hpp> | ||
|
||
#include <array> | ||
#include <climits> | ||
#include <cstddef> | ||
#include <stdexcept> | ||
#include <utility> | ||
#include <vector> | ||
|
||
auto main() -> int | ||
{ | ||
using ::boost::ut::expect; | ||
using ::boost::ut::test; | ||
|
||
namespace huffman = ::gpu_deflate::huffman; | ||
using namespace huffman::literals; | ||
|
||
test("basic") = [] { | ||
// encoded data from dahuffman readme.rst, but in hex. | ||
constexpr std::array<std::byte, 6> encoded_bytes = { | ||
std::byte{0x86}, | ||
std::byte{0x7c}, | ||
std::byte{0x25}, | ||
std::byte{0x13}, | ||
std::byte{0x69}, | ||
std::byte{0x40}}; | ||
|
||
constexpr char eot = {'\4'}; | ||
static constexpr auto code_table = // clang-format off | ||
huffman::table{ | ||
huffman::table_contents, | ||
{std::pair{00000_c, eot}, | ||
{00001_c, 'x'}, | ||
{0001_c, 'q'}, | ||
{001_c, 'n'}, | ||
{01_c, 'i'}, | ||
{1_c, 'e'}} | ||
}; // clang-format on | ||
|
||
constexpr std::array expected = { | ||
'e', 'x', 'e', 'n', 'e', 'e', 'e', 'e', 'x', 'n', | ||
'i', 'q', 'n', 'e', 'i', 'e', 'i', 'n', 'i', eot, | ||
}; | ||
constexpr auto output_buf = [&] { | ||
std::array<char, expected.size()> output_buf{}; | ||
auto result = decode(code_table, encoded_bytes, output_buf.begin()); | ||
// result should point to the back of output_buf. | ||
if (output_buf.end() != result) { | ||
throw std::runtime_error("assertion failed"); | ||
} | ||
return output_buf; | ||
}(); | ||
|
||
static_assert(output_buf == expected); | ||
}; | ||
} |