@@ 2,10 2,11 @@ const std = @import("std");
const huffman = @import("huffman.zig");
const Allocator = std.mem.Allocator;
+
const Symbol = huffman.Symbol;
const HuffmanPair = huffman.HuffmanPair;
+const CodeLength = huffman.CodeLength;
-pub const CodeLength = usize;
pub const CanonicalCode = u32;
pub const CanonicalCodeBook = std.AutoHashMap(Symbol, CanonicalCode);
pub const CodeLengthPair = std.meta.Tuple(&[_]type{ CanonicalCode, CodeLength });
@@ 1,6 1,7 @@
const std = @import("std");
const frequencies = @import("frequencies.zig");
const tree = @import("tree.zig");
+const canonical = @import("canonical.zig");
const Allocator = std.mem.Allocator;
const File = std.fs.File;
@@ 13,8 14,11 @@ pub const Frequency = u32;
pub const CodeLength = usize;
pub const HuffmanPair = struct { symbol: Symbol, code_length: CodeLength };
-pub const getFrequencies = frequencies.getFrequencies;
pub const RecordTree = tree.RecordTree;
+pub const Codebook = canonical.Codebook;
+
+pub const getFrequencies = frequencies.getFrequencies;
+pub const getLengthCount = canonical.getLengthCount;
/// Struct containing a character and its absolute frequency within a
/// read file.
@@ 2,23 2,24 @@
const std = @import("std");
const huffman = @import("huffman.zig");
-const canonical = @import("canonical.zig");
const Allocator = std.mem.Allocator;
-const Codebook = canonical.Codebook;
-const CodeLength = canonical.CodeLength;
const Symbol = huffman.Symbol;
const Record = huffman.Record;
const HuffmanPair = huffman.HuffmanPair;
const PsuedoEOF = huffman.PsuedoEOF;
+const CodeLength = huffman.CodeLength;
+const Codebook = huffman.Codebook;
+
+const getLengthCount = huffman.getLengthCount;
pub fn compress(reader: anytype, writer: anytype, frequencies: []const Record, allocator: Allocator) !void {
// Get the length of the Huffman codes for each unique `Symbol`.
const huffman_tree = try huffman.RecordTree.initFromList(frequencies, allocator);
defer huffman_tree.deinit();
const pairs = try huffman_tree.getHuffmanPairs();
- const length_counts = try canonical.getLengthCount(pairs, allocator);
+ const length_counts = try getLengthCount(pairs, allocator);
// Get the codebook to translate `Symbol`s to `CanonicalCode`s.
var codebook = try Codebook.init(pairs, allocator);
@@ 33,7 34,7 @@ pub fn compress(reader: anytype, writer: anytype, frequencies: []const Record, a
pub fn decompress(reader: anytype, writer: anytype, allocator: Allocator) !void {
const pairs = try readHuffmanPairsFromHeader(reader, allocator);
- const length_counts = try canonical.getLengthCount(pairs, allocator);
+ const length_counts = try getLengthCount(pairs, allocator);
const cw2_symbol = try getCw2Symbol(pairs, allocator);
defer allocator.free(pairs);
defer allocator.free(length_counts);
@@ 248,7 249,7 @@ fn getCw2Symbol(pairs: []const HuffmanPair, allocator: Allocator) ![]const []Sym
/// Return the most common length among the Huffman pairs, i.e. the
/// length that represents the most Huffman codes.
fn getMaxCountOfLength(pairs: []const HuffmanPair, allocator: Allocator) !usize {
- const count_of_lengths = try canonical.getLengthCount(pairs, allocator);
+ const count_of_lengths = try getLengthCount(pairs, allocator);
defer allocator.free(count_of_lengths);
return std.mem.max(CodeLength, count_of_lengths);
@@ 286,7 287,7 @@ test "writing header" {
.{ .symbol = PsuedoEOF, .code_length = 3 },
};
// zig fmt: on
- const length_counts = try canonical.getLengthCount(&list, std.testing.allocator);
+ const length_counts = try getLengthCount(&list, std.testing.allocator);
defer std.testing.allocator.free(length_counts);
var stream = std.ArrayList(u8).init(std.testing.allocator);