2022-02-23 15:25:55 +02:00
|
|
|
const std = @import("std");
|
|
|
|
const Allocator = std.mem.Allocator;
|
|
|
|
const bdz = @import("bdz.zig");
|
|
|
|
|
|
|
|
const c = @cImport({
|
|
|
|
@cInclude("cmph.h");
|
|
|
|
});
|
|
|
|
|
2022-02-23 20:30:23 +02:00
|
|
|
// pack packs cmph hashes for the given input and returns a slice ("cmph pack
|
|
|
|
// minus first 4 bytes") for further storage. The slice must be freed by the
|
|
|
|
// caller.
|
2022-02-23 15:25:55 +02:00
|
|
|
const packErr = Allocator.Error || error{Overflow};
|
|
|
|
pub fn pack(allocator: Allocator, input: [][*:0]const u8) packErr![]const u8 {
|
|
|
|
var cvector = @ptrCast([*c][*c]u8, input.ptr);
|
|
|
|
const len = try std.math.cast(c_uint, input.len);
|
|
|
|
var source = c.cmph_io_vector_adapter(cvector, len);
|
|
|
|
defer c.cmph_io_vector_adapter_destroy(source);
|
|
|
|
var config: *c.cmph_config_t = c.cmph_config_new(source) orelse return error.OutOfMemory;
|
|
|
|
c.cmph_config_set_algo(config, c.CMPH_BDZ);
|
|
|
|
c.cmph_config_set_b(config, 7);
|
|
|
|
var hash: *c.cmph_t = c.cmph_new(config) orelse return error.OutOfMemory;
|
|
|
|
c.cmph_config_destroy(config);
|
|
|
|
|
|
|
|
const size = c.cmph_packed_size(hash);
|
|
|
|
var buf = try allocator.alloc(u8, size);
|
|
|
|
c.cmph_pack(hash, &buf[0]);
|
|
|
|
c.cmph_destroy(hash);
|
2022-02-23 20:30:23 +02:00
|
|
|
return buf[4..];
|
2022-02-23 15:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const testing = std.testing;
|
|
|
|
|
|
|
|
const items = .{
|
|
|
|
"aaaaaaaaaa",
|
|
|
|
"bbbbbbbbbb",
|
|
|
|
"cccccccccc",
|
|
|
|
"dddddddddd",
|
|
|
|
"eeeeeeeeee",
|
|
|
|
"ffffffffff",
|
|
|
|
"gggggggggg",
|
|
|
|
"hhhhhhhhhh",
|
|
|
|
"iiiiiiiiii",
|
|
|
|
"jjjjjjjjjj",
|
|
|
|
};
|
|
|
|
const items_len = items.len;
|
|
|
|
|
|
|
|
fn samplePack(allocator: Allocator) ![]const u8 {
|
|
|
|
var vector = std.ArrayList([*:0]const u8).init(allocator);
|
|
|
|
defer vector.deinit();
|
|
|
|
try vector.appendSlice(&items);
|
|
|
|
return pack(allocator, vector.items);
|
|
|
|
}
|
|
|
|
|
|
|
|
test "basic pack/unpack" {
|
|
|
|
const buf = try samplePack(testing.allocator);
|
|
|
|
defer testing.allocator.free(buf);
|
|
|
|
try testing.expect(buf.len < 100);
|
|
|
|
|
|
|
|
var used: [items_len]bool = undefined;
|
|
|
|
|
|
|
|
inline for (items) |elem| {
|
2022-02-24 05:51:04 +02:00
|
|
|
const hashed = try bdz.search(buf, elem);
|
2022-02-23 15:25:55 +02:00
|
|
|
used[hashed] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (used) |item| {
|
|
|
|
try testing.expect(item);
|
|
|
|
}
|
|
|
|
}
|