Skip to content

Commit

Permalink
remove Options which has only level, use level
Browse files Browse the repository at this point in the history
  • Loading branch information
ianic committed Jan 29, 2024
1 parent d478fb9 commit 35dd751
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 32 deletions.
10 changes: 5 additions & 5 deletions bin/deflate_bench.zig
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ pub fn run(output: anytype, opt: Options) !void {
return;
}
//var fbs = std.io.fixedBufferStream(input);
const f_opt: flate.Options = .{ .level = @enumFromInt(opt.level) };
const level: flate.Level = @enumFromInt(opt.level);
switch (opt.alg) {
.deflate => try flate.compress(input, output, f_opt),
.zlib => try zlib.compress(input, output, f_opt),
.gzip => try gzip.compress(input, output, f_opt),
.deflate => try flate.compress(input, output, level),
.zlib => try zlib.compress(input, output, level),
.gzip => try gzip.compress(input, output, level),
// .gzip => {
// var buf: [4096]u8 = undefined;
// var cmp = try gzip.compressor(output, f_opt);
// var cmp = try gzip.compressor(output, level);
// while (true) {
// const n = try input.readAll(&buf);
// _ = try cmp.write(buf[0..n]);
Expand Down
2 changes: 1 addition & 1 deletion bin/gzip.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ pub fn main() !void {
var output_file = try std.fs.cwd().createFile(output_file_name, .{ .truncate = true });
defer output_file.close();

try gzip.compress(input_file.reader(), output_file.writer(), .{});
try gzip.compress(input_file.reader(), output_file.writer(), .default);
}
}
18 changes: 7 additions & 11 deletions src/deflate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,20 @@ const LevelArgs = struct {
}
};

pub const Options = struct {
level: Level = .default,
};

pub fn compress(comptime wrap: Wrapper, reader: anytype, writer: anytype, options: Options) !void {
var df = try compressor(wrap, writer, options);
pub fn compress(comptime wrap: Wrapper, reader: anytype, writer: anytype, level: Level) !void {
var df = try compressor(wrap, writer, level);
try df.stream(reader);
try df.close();
}

pub fn compressor(comptime wrap: Wrapper, writer: anytype, options: Options) !Deflate(
pub fn compressor(comptime wrap: Wrapper, writer: anytype, level: Level) !Deflate(
wrap,
@TypeOf(writer),
hbw.HuffmanBitWriter(@TypeOf(writer)),
) {
const WriterType = @TypeOf(writer);
const TokenWriter = hbw.HuffmanBitWriter(WriterType);
return try Deflate(wrap, WriterType, TokenWriter).init(writer, options);
return try Deflate(wrap, WriterType, TokenWriter).init(writer, level);
}

// Default compression algorithm. First finds tokens from the non compressed
Expand Down Expand Up @@ -86,11 +82,11 @@ fn Deflate(comptime wrap: Wrapper, comptime WriterType: type, comptime TokenWrit
prev_literal: ?u8 = null,

const Self = @This();
pub fn init(wrt: WriterType, options: Options) !Self {
pub fn init(wrt: WriterType, level: Level) !Self {
var self = Self{
.wrt = wrt,
.token_writer = TokenWriter.init(wrt),
.level = LevelArgs.get(options.level),
.level = LevelArgs.get(level),
};
try self.writeHeader();
return self;
Expand Down Expand Up @@ -399,7 +395,7 @@ test "deflate: tokenization" {
inline for (Wrapper.list) |wrap| { // for each wrapping
var cw = io.countingWriter(io.null_writer);
const cww = cw.writer();
var df = try Deflate(wrap, @TypeOf(cww), TestTokenWriter).init(cww, .{});
var df = try Deflate(wrap, @TypeOf(cww), TestTokenWriter).init(cww, .default);

_ = try df.write(c.data);
try df.flush();
Expand Down
29 changes: 14 additions & 15 deletions src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const deflate = @import("deflate.zig");
const inflate = @import("inflate.zig");

pub const Level = deflate.Level;
pub const Options = deflate.Options;

pub fn decompress(reader: anytype, writer: anytype) !void {
try inflate.decompress(.raw, reader, writer);
Expand All @@ -12,16 +11,16 @@ pub fn decompressor(reader: anytype) inflate.Inflate(.raw, @TypeOf(reader)) {
return inflate.decompressor(.raw, reader);
}

pub fn compress(reader: anytype, writer: anytype, options: Options) !void {
try deflate.compress(.raw, reader, writer, options);
pub fn compress(reader: anytype, writer: anytype, level: Level) !void {
try deflate.compress(.raw, reader, writer, level);
}

pub fn compressHuffmanOnly(reader: anytype, writer: anytype) !void {
try deflate.compressHuffmanOnly(.raw, reader, writer);
}

pub fn compressor(writer: anytype, options: Options) !void {
try deflate.compressor(.raw, writer, options);
pub fn compressor(writer: anytype, level: Level) !void {
try deflate.compressor(.raw, writer, level);
}

pub const gzip = struct {
Expand All @@ -33,16 +32,16 @@ pub const gzip = struct {
return inflate.decompressor(.gzip, reader);
}

pub fn compress(reader: anytype, writer: anytype, options: Options) !void {
try deflate.compress(.gzip, reader, writer, options);
pub fn compress(reader: anytype, writer: anytype, level: Level) !void {
try deflate.compress(.gzip, reader, writer, level);
}

pub fn compressHuffmanOnly(reader: anytype, writer: anytype) !void {
try deflate.compressHuffmanOnly(.gzip, reader, writer);
}

pub fn compressor(writer: anytype, options: Options) !deflate.Compressor(.gzip, @TypeOf(writer)) {
return try deflate.compressor(.gzip, writer, options);
pub fn compressor(writer: anytype, level: Level) !deflate.Compressor(.gzip, @TypeOf(writer)) {
return try deflate.compressor(.gzip, writer, level);
}
};

Expand All @@ -55,16 +54,16 @@ pub const zlib = struct {
return inflate.decompressor(.zlib, reader);
}

pub fn compress(reader: anytype, writer: anytype, options: Options) !void {
try deflate.compress(.zlib, reader, writer, options);
pub fn compress(reader: anytype, writer: anytype, level: Level) !void {
try deflate.compress(.zlib, reader, writer, level);
}

pub fn compressHuffmanOnly(reader: anytype, writer: anytype) !void {
try deflate.compressHuffmanOnly(.zlib, reader, writer);
}

pub fn compressor(writer: anytype, options: Options) !deflate.Compressor(.zlib, @TypeOf(writer)) {
return try deflate.compressor(.zlib, writer, options);
pub fn compressor(writer: anytype, level: Level) !deflate.Compressor(.zlib, @TypeOf(writer)) {
return try deflate.compressor(.zlib, writer, level);
}
};

Expand Down Expand Up @@ -201,7 +200,7 @@ test "compress/decompress" {
var original = fixedBufferStream(data);
var compressed = fixedBufferStream(&cmp_buf);

try deflate.compress(wrap, original.reader(), compressed.writer(), .{ .level = level });
try deflate.compress(wrap, original.reader(), compressed.writer(), level);

try testing.expectEqual(compressed_size, compressed.pos);
}
Expand All @@ -219,7 +218,7 @@ test "compress/decompress" {
{
var compressed = fixedBufferStream(&cmp_buf);

var cmp = try deflate.compressor(wrap, compressed.writer(), .{ .level = level });
var cmp = try deflate.compressor(wrap, compressed.writer(), level);
var cmp_wrt = cmp.writer();
try cmp_wrt.writeAll(data);
try cmp.close();
Expand Down

0 comments on commit 35dd751

Please sign in to comment.