diff --git a/build.zig b/build.zig index ce9f85c..b8263c9 100644 --- a/build.zig +++ b/build.zig @@ -15,55 +15,33 @@ pub fn build(b: *std.Build) void { // set a preferred release mode, allowing the user to decide how to optimize. const optimize = b.standardOptimizeOption(.{}); - const exe = b.addExecutable(.{ + const lib = b.addStaticLibrary(.{ .name = "hamt", // In this case the main source file is merely a path, however, in more // complicated build scripts, this could be a generated file. - .root_source_file = .{ .path = "src/main.zig" }, + .root_source_file = .{ .path = "src/lib.zig" }, .target = target, .optimize = optimize, }); - // This declares intent for the executable to be installed into the - // standard location when the user invokes the "install" step (the default - // step when running `zig build`). - b.installArtifact(exe); + // This declares intent for the library to be installed into the standard + // location when the user invokes the "install" step (the default step when + // running `zig build`). + b.installArtifact(lib); - // This *creates* a RunStep in the build graph, to be executed when another - // step is evaluated that depends on it. The next line below will establish - // such a dependency. - const run_cmd = b.addRunArtifact(exe); - - // By making the run step depend on the install step, it will be run from the - // installation directory rather than directly from within the cache directory. - // This is not necessary, however, if the application depends on other installed - // files, this ensures they will be present and in the expected location. - run_cmd.step.dependOn(b.getInstallStep()); - - // This allows the user to pass arguments to the application in the build - // command itself, like this: `zig build run -- arg1 arg2 etc` - if (b.args) |args| { - run_cmd.addArgs(args); - } - - // This creates a build step. It will be visible in the `zig build --help` menu, - // and can be selected like this: `zig build run` - // This will evaluate the `run` step rather than the default, which is "install". - const run_step = b.step("run", "Run the app"); - run_step.dependOn(&run_cmd.step); - - // Creates a step for unit testing. - const unit_tests = b.addTest(.{ + // Creates a step for unit testing. This only builds the test executable + // but does not run it. + const main_tests = b.addTest(.{ .root_source_file = .{ .path = "src/tests.zig" }, .target = target, .optimize = optimize, }); - const run_unit_tests = b.addRunArtifact(unit_tests); + const run_main_tests = b.addRunArtifact(main_tests); - // Similar to creating the run step earlier, this exposes a `test` step to - // the `zig build --help` menu, providing a way for the user to request - // running the unit tests. - const test_step = b.step("test", "Run unit tests"); - test_step.dependOn(&run_unit_tests.step); + // This creates a build step. It will be visible in the `zig build --help` menu, + // and can be selected like this: `zig build test` + // This will evaluate the `test` step rather than the default, which is "install". + const test_step = b.step("test", "Run library tests"); + test_step.dependOn(&run_main_tests.step); } diff --git a/src/lib.zig b/src/lib.zig new file mode 100644 index 0000000..ccc89e9 --- /dev/null +++ b/src/lib.zig @@ -0,0 +1 @@ +pub const HashArrayMappedTrie = @import("trie.zig").HashArrayMappedTrie; diff --git a/src/main.zig b/src/main.zig deleted file mode 100644 index e2157a4..0000000 --- a/src/main.zig +++ /dev/null @@ -1,53 +0,0 @@ -const std = @import("std"); -const HashArrayMappedTrie = @import("trie.zig").HashArrayMappedTrie; - -pub fn main() !void { - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; - defer std.debug.assert(!gpa.deinit()); - - const allocator = gpa.allocator(); - - var trie = HashArrayMappedTrie([]const u8, void, Context(u32)).init(); - defer trie.deinit(allocator); - - try trie.insert(allocator, "and", {}); - try trie.insert(allocator, "class", {}); - try trie.insert(allocator, "else", {}); - try trie.insert(allocator, "false", {}); - try trie.insert(allocator, "for", {}); - try trie.insert(allocator, "fun", {}); - try trie.insert(allocator, "if", {}); - try trie.insert(allocator, "nil", {}); - try trie.insert(allocator, "or", {}); - try trie.insert(allocator, "print", {}); - try trie.insert(allocator, "return", {}); - try trie.insert(allocator, "super", {}); - try trie.insert(allocator, "this", {}); - try trie.insert(allocator, "true", {}); - try trie.insert(allocator, "var", {}); - try trie.insert(allocator, "while", {}); - - try trie.print(); -} - -pub fn Context(comptime HashCode: type) type { - const Log2Int = std.math.Log2Int; - - return struct { - pub const Digest = HashCode; - - pub inline fn hash(key: []const u8) Digest { - // the MSB will represent 'z' - const offset = @typeInfo(Digest).Int.bits - 26; - - var result: Digest = 0; - for (key) |c| result |= @as(Digest, 1) << @intCast(Log2Int(Digest), offset + c - 'a'); - - return result; - } - - pub inline fn eql(left: []const u8, right: []const u8) bool { - return std.mem.eql(u8, left, right); - } - }; -} diff --git a/src/tests.zig b/src/tests.zig index f6acc18..354c095 100644 --- a/src/tests.zig +++ b/src/tests.zig @@ -1,4 +1,5 @@ comptime { + _ = @import("lib.zig"); _ = @import("trie.zig"); }