-
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.
define huffman::table::canonicalize()
huffman::table::canonicalize() updates the existing codes in a table to canonical form for DEFLATE: * All codes of a given bit length have lexicographically consecutive values, in the same order as the symbols they represent; * Shorter codes lexicographically precede longer codes. Change-Id: Idc3dceefe1b5d17a54f1dab29155a499c7e1d138
- Loading branch information
Showing
5 changed files
with
165 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#include "huffman/huffman.hpp" | ||
|
||
#include <boost/ut.hpp> | ||
|
||
#include <algorithm> | ||
#include <utility> | ||
|
||
auto main() -> int | ||
{ | ||
using ::boost::ut::expect; | ||
using ::boost::ut::test; | ||
|
||
namespace huffman = ::starflate::huffman; | ||
using namespace huffman::literals; | ||
|
||
test("table with DEFLATE canonical code, example 1") = [] { | ||
static constexpr auto t1 = // clang-format off | ||
huffman::table{ | ||
huffman::table_contents, | ||
{std::pair{010_c, 'D'}, | ||
{011_c, 'C'}, | ||
{00_c, 'A'}, | ||
{1_c, 'B'}}}.canonicalize(); | ||
// clang-format on | ||
|
||
static constexpr auto t2 = // clang-format off | ||
huffman::table{ | ||
huffman::table_contents, | ||
{std::pair{111_c, 'D'}, | ||
{110_c, 'C'}, | ||
{10_c, 'A'}, | ||
{0_c, 'B'}}}; | ||
// clang-format on | ||
|
||
expect(std::ranges::equal(t1, t2)); | ||
}; | ||
|
||
test("table with DEFLATE canonical code, example 2") = [] { | ||
// NOTE: t1 is an *invalid* table (as initially specified) as it does not | ||
// contain prefix free codes. | ||
static constexpr auto t1 = // clang-format off | ||
huffman::table{ | ||
huffman::table_contents, | ||
{std::pair{1111_c, 'H'}, | ||
{0111_c, 'G'}, | ||
{100_c, 'E'}, | ||
{011_c, 'D'}, | ||
{010_c, 'C'}, | ||
{001_c, 'B'}, | ||
{000_c, 'A'}, | ||
{11_c, 'F'}}}.canonicalize(); | ||
// clang-format on | ||
|
||
static constexpr auto t2 = // clang-format off | ||
huffman::table{ | ||
huffman::table_contents, | ||
{std::pair{1111_c, 'H'}, | ||
{1110_c, 'G'}, | ||
{110_c, 'E'}, | ||
{101_c, 'D'}, | ||
{100_c, 'C'}, | ||
{011_c, 'B'}, | ||
{010_c, 'A'}, | ||
{00_c, 'F'}}}; | ||
// clang-format on | ||
|
||
expect(std::ranges::equal(t1, t2)); | ||
}; | ||
|
||
test("canonicalization is idempotent") = [] { | ||
static constexpr auto t1 = // clang-format off | ||
huffman::table{ | ||
huffman::table_contents, | ||
{std::pair{1111_c, 'H'}, | ||
{1110_c, 'G'}, | ||
{110_c, 'E'}, | ||
{101_c, 'D'}, | ||
{100_c, 'C'}, | ||
{011_c, 'B'}, | ||
{010_c, 'A'}, | ||
{00_c, 'F'}}}; | ||
// clang-format on | ||
|
||
auto t2 = t1; | ||
t2.canonicalize(); | ||
|
||
expect(std::ranges::equal(t1, t2)); | ||
expect(std::ranges::equal(t1, t2.canonicalize())); | ||
}; | ||
} |