~aldats/the

e2272f88d0112f53e9183efb22e7ea22ca961c90 — aldats 1 year, 10 months ago f993a47
refactor: `xcoding.zig` imports `huffman.zig` only
3 files changed, 15 insertions(+), 9 deletions(-)

M src/canonical.zig
M src/huffman.zig
M src/xcoding.zig
M src/canonical.zig => src/canonical.zig +2 -1
@@ 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 });

M src/huffman.zig => src/huffman.zig +5 -1
@@ 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.

M src/xcoding.zig => src/xcoding.zig +8 -7
@@ 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);