first steps
This commit is contained in:
parent
b0d23fd9bc
commit
3b7c63ad9f
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
zig-cache/
|
||||
zig-out/
|
34
build.zig
Normal file
34
build.zig
Normal file
@ -0,0 +1,34 @@
|
||||
const Builder = @import("std").build.Builder;
|
||||
|
||||
pub fn build(b: *Builder) void {
|
||||
// Standard target options allows the person running `zig build` to choose
|
||||
// what target to build for. Here we do not override the defaults, which
|
||||
// means any target is allowed, and the default is native. Other options
|
||||
// for restricting supported target set are available.
|
||||
const target = b.standardTargetOptions(.{});
|
||||
|
||||
// Standard release options allow the person running `zig build` to select
|
||||
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
|
||||
const mode = b.standardReleaseOptions();
|
||||
|
||||
const exe = b.addExecutable("init-exe", "src/main.zig");
|
||||
exe.setTarget(target);
|
||||
exe.setBuildMode(mode);
|
||||
exe.linkLibC();
|
||||
exe.linkSystemLibrary("cmph");
|
||||
exe.install();
|
||||
|
||||
{
|
||||
const turbonss_test = b.addTest("src/main.zig");
|
||||
turbonss_test.linkLibC();
|
||||
turbonss_test.linkSystemLibrary("cmph");
|
||||
const test_step = b.step("test", "Run the tests");
|
||||
test_step.dependOn(&turbonss_test.step);
|
||||
}
|
||||
|
||||
const run_cmd = exe.run();
|
||||
run_cmd.step.dependOn(b.getInstallStep());
|
||||
|
||||
const run_step = b.step("run", "Run the app");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
}
|
99
src/main.zig
Normal file
99
src/main.zig
Normal file
@ -0,0 +1,99 @@
|
||||
const std = @import("std");
|
||||
const ArrayList = std.ArrayList;
|
||||
|
||||
const c = @cImport({
|
||||
@cDefine("DEBUG", "1");
|
||||
@cInclude("stdio.h");
|
||||
@cInclude("cmph.h");
|
||||
});
|
||||
|
||||
const tempMph = "temp.mph";
|
||||
|
||||
pub fn main() !void {
|
||||
|
||||
// var hash: ?*c.cmph_t = c.cmph_new(config);
|
||||
// c.cmph_config_destroy(config);
|
||||
// _ = c.cmph_dump(hash, mphf_fd);
|
||||
// c.cmph_destroy(hash);
|
||||
// _ = c.fclose(mphf_fd);
|
||||
// mphf_fd = c.fopen("temp.mph", "r");
|
||||
// hash = c.cmph_load(mphf_fd);
|
||||
// while (i < nkeys) {
|
||||
// var key: [*c]const u8 = vector[i];
|
||||
// var id: c_uint = c.cmph_search(hash, key, @bitCast(c.cmph_uint32, @truncate(c_uint, 10)));
|
||||
// try stdout.print("key:{s} -- hash:{u}\n", key, id);
|
||||
// i +%= 1;
|
||||
// }
|
||||
// c.cmph_destroy(hash);
|
||||
// c.cmph_io_vector_adapter_destroy(source);
|
||||
// _ = c.fclose(mphf_fd);
|
||||
// return 0;
|
||||
//}
|
||||
}
|
||||
|
||||
test "simple cmph usage" {
|
||||
var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
const arena = arena_instance.allocator();
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
const stderr = std.io.getStdErr().writer();
|
||||
|
||||
_ = stdout;
|
||||
var vector = ArrayList([*:0]const u8).init(arena);
|
||||
try vector.append("aaaaaaaaaa");
|
||||
try vector.append("bbbbbbbbbb");
|
||||
try vector.append("cccccccccc");
|
||||
try vector.append("dddddddddd");
|
||||
try vector.append("eeeeeeeeee");
|
||||
try vector.append("ffffffffff");
|
||||
try vector.append("gggggggggg");
|
||||
try vector.append("hhhhhhhhhh");
|
||||
try vector.append("iiiiiiiiii");
|
||||
try vector.append("jjjjjjjjjj");
|
||||
|
||||
var mphf_fh = try std.fs.cwd().createFile(tempMph, .{});
|
||||
defer {
|
||||
std.fs.cwd().deleteFile(tempMph) catch |err| {
|
||||
stderr.print("error removing file: {s}\n", .{@errorName(err)}) catch {};
|
||||
};
|
||||
}
|
||||
var mphf_fd = c.fdopen(mphf_fh.handle, "w");
|
||||
var vector_len = @truncate(c_uint, vector.items.len);
|
||||
var cvector = @ptrCast([*c][*c]u8, vector.toOwnedSlice().ptr);
|
||||
var source = c.cmph_io_vector_adapter(cvector, vector_len);
|
||||
var config: *c.cmph_config_t = c.cmph_config_new(source) orelse return error.OutOfMemory;
|
||||
c.cmph_config_set_algo(config, c.CMPH_BRZ);
|
||||
c.cmph_config_set_mphf_fd(config, mphf_fd);
|
||||
var hash: *c.cmph_t = c.cmph_new(config) orelse return error.OutOfMemory;
|
||||
c.cmph_config_destroy(config);
|
||||
_ = c.cmph_dump(hash, mphf_fd);
|
||||
c.cmph_destroy(hash);
|
||||
mphf_fh.close();
|
||||
|
||||
mphf_fh = try std.fs.cwd().openFile(tempMph, .{ .mode = .read_only });
|
||||
mphf_fd = c.fdopen(mphf_fh.handle, "r");
|
||||
defer mphf_fh.close();
|
||||
|
||||
var got_hash: ?*c.cmph_t = c.cmph_load(mphf_fd);
|
||||
if (got_hash) |real_hash| {
|
||||
hash = real_hash;
|
||||
} else {
|
||||
try stderr.print("failed to unwrap hash\n", .{});
|
||||
}
|
||||
}
|
||||
|
||||
const expectEqual = std.testing.expectEqual;
|
||||
|
||||
test "char**" {
|
||||
var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
const arena = arena_instance.allocator();
|
||||
|
||||
var arrl = ArrayList([*:0]const u8).init(arena);
|
||||
try arrl.append("foo");
|
||||
try arrl.append("bar");
|
||||
|
||||
var arr: [*][*:0]const u8 = arrl.toOwnedSlice().ptr;
|
||||
|
||||
try expectEqual(@TypeOf(arr[0]), [*:0]const u8);
|
||||
try expectEqual(arr[0][2], 'o');
|
||||
try expectEqual(arr[0][3], 0);
|
||||
}
|
Loading…
Reference in New Issue
Block a user