Compare commits
9 Commits
2b9f82a509
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 8e89cc5f4f | |||
| 052dd57cdd | |||
| 3e82fde54a | |||
| 7bdcbfd6f5 | |||
| 7a95a1fac0 | |||
| b0389bb1a4 | |||
| f8153f6569 | |||
| 722427b693 | |||
| 56c5f254ee |
8
.github/workflows/main.yml
vendored
8
.github/workflows/main.yml
vendored
@@ -16,9 +16,9 @@ jobs:
|
||||
steps:
|
||||
- uses: goto-bus-stop/setup-zig@v2
|
||||
with:
|
||||
version: master
|
||||
version: 0.12.0-dev.2063+804cee3b9
|
||||
- uses: actions/checkout@v3
|
||||
- run: zig fmt src/**/*.zig
|
||||
- run: zig fmt src/*.zig
|
||||
test:
|
||||
strategy:
|
||||
matrix:
|
||||
@@ -27,7 +27,7 @@ jobs:
|
||||
steps:
|
||||
- uses: goto-bus-stop/setup-zig@v2
|
||||
with:
|
||||
version: master
|
||||
version: 0.12.0-dev.2063+804cee3b9
|
||||
- uses: actions/checkout@v3
|
||||
- run: zig build test
|
||||
bench:
|
||||
@@ -35,6 +35,6 @@ jobs:
|
||||
steps:
|
||||
- uses: goto-bus-stop/setup-zig@v2
|
||||
with:
|
||||
version: master
|
||||
version: 0.12.0-dev.2063+804cee3b9
|
||||
- uses: actions/checkout@v3
|
||||
- run: zig build -Doptimize=ReleaseFast -Dcpu=baseline bench
|
||||
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
.zig-cache/
|
||||
zig-cache/
|
||||
zig-out/
|
||||
.vscode/
|
||||
.vscode/
|
||||
|
||||
30
build.zig
30
build.zig
@@ -15,13 +15,13 @@ 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 hamt_module = b.addModule("hamt", .{ .source_file = .{ .path = "src/lib.zig" } });
|
||||
const mod = b.addModule("hamt", .{ .root_source_file = b.path("src/lib.zig") });
|
||||
|
||||
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/lib.zig" },
|
||||
.root_source_file = b.path("src/lib.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
@@ -33,30 +33,32 @@ pub fn build(b: *std.Build) void {
|
||||
|
||||
// Benchmark
|
||||
const bench = b.addExecutable(.{
|
||||
.name = "hamt-benchmark",
|
||||
.root_source_file = .{ .path = "src/bench.zig" },
|
||||
.name = "hamt-bench",
|
||||
.root_source_file = b.path("src/bench.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
.optimize = .ReleaseFast,
|
||||
.link_libc = true,
|
||||
});
|
||||
bench.addModule("hamt", hamt_module);
|
||||
bench.root_module.addImport("hamt", mod);
|
||||
|
||||
const bench_cmd = b.addRunArtifact(bench);
|
||||
|
||||
const bench_step = b.step("bench", "Run benchmark");
|
||||
bench_step.dependOn(&bench_cmd.step);
|
||||
|
||||
// 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" },
|
||||
const lib_unit_tests = b.addTest(.{
|
||||
.root_source_file = b.path("src/tests.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
const run_main_tests = b.addRunArtifact(main_tests);
|
||||
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
|
||||
|
||||
// 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);
|
||||
// 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_lib_unit_tests.step);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,72 @@
|
||||
.{
|
||||
.name = "hamt",
|
||||
.version = "0.1.0",
|
||||
.dependencies = .{},
|
||||
// This is the default name used by packages depending on this one. For
|
||||
// example, when a user runs `zig fetch --save <url>`, this field is used
|
||||
// as the key in the `dependencies` table. Although the user can choose a
|
||||
// different name, most users will stick with this provided value.
|
||||
//
|
||||
// It is redundant to include "zig" in this name because it is already
|
||||
// within the Zig package namespace.
|
||||
.name = "hamt",
|
||||
|
||||
// This is a [Semantic Version](https://semver.org/).
|
||||
// In a future version of Zig it will be used for package deduplication.
|
||||
.version = "0.1.0",
|
||||
|
||||
// This field is optional.
|
||||
// This is currently advisory only; Zig does not yet do anything
|
||||
// with this value.
|
||||
//.minimum_zig_version = "0.11.0",
|
||||
|
||||
// This field is optional.
|
||||
// Each dependency must either provide a `url` and `hash`, or a `path`.
|
||||
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
|
||||
// Once all dependencies are fetched, `zig build` no longer requires
|
||||
// internet connectivity.
|
||||
.dependencies = .{
|
||||
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
|
||||
//.example = .{
|
||||
// // When updating this field to a new URL, be sure to delete the corresponding
|
||||
// // `hash`, otherwise you are communicating that you expect to find the old hash at
|
||||
// // the new URL.
|
||||
// .url = "https://example.com/foo.tar.gz",
|
||||
//
|
||||
// // This is computed from the file contents of the directory of files that is
|
||||
// // obtained after fetching `url` and applying the inclusion rules given by
|
||||
// // `paths`.
|
||||
// //
|
||||
// // This field is the source of truth; packages do not come from a `url`; they
|
||||
// // come from a `hash`. `url` is just one of many possible mirrors for how to
|
||||
// // obtain a package matching this `hash`.
|
||||
// //
|
||||
// // Uses the [multihash](https://multiformats.io/multihash/) format.
|
||||
// .hash = "...",
|
||||
//
|
||||
// // When this is provided, the package is found in a directory relative to the
|
||||
// // build root. In this case the package's hash is irrelevant and therefore not
|
||||
// // computed. This field and `url` are mutually exclusive.
|
||||
// .path = "foo",
|
||||
|
||||
// // When this is set to `true`, a package is declared to be lazily
|
||||
// // fetched. This makes the dependency only get fetched if it is
|
||||
// // actually used.
|
||||
// .lazy = false,
|
||||
//},
|
||||
},
|
||||
|
||||
// Specifies the set of files and directories that are included in this package.
|
||||
// Only files and directories listed here are included in the `hash` that
|
||||
// is computed for this package. Only files listed here will remain on disk
|
||||
// when using the zig package manager. As a rule of thumb, one should list
|
||||
// files required for compilation plus any license(s).
|
||||
// Paths are relative to the build root. Use the empty string (`""`) to refer to
|
||||
// the build root itself.
|
||||
// A directory listed here means that all files within, recursively, are included.
|
||||
.paths = .{
|
||||
"build.zig",
|
||||
"build.zig.zon",
|
||||
"src",
|
||||
// For example...
|
||||
//"LICENSE",
|
||||
//"README.md",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -2,10 +2,10 @@ const std = @import("std");
|
||||
const HashArrayMappedTrie = @import("hamt").HashArrayMappedTrie;
|
||||
|
||||
const StringContext = struct {
|
||||
pub const Digest = u64;
|
||||
pub const Digest = u32;
|
||||
|
||||
pub fn hash(input: []const u8) Digest {
|
||||
return std.hash.Wyhash.hash(0, input);
|
||||
return @truncate(std.hash.Wyhash.hash(0, input));
|
||||
}
|
||||
|
||||
pub fn eql(left: []const u8, right: []const u8) bool {
|
||||
@@ -17,16 +17,13 @@ const StringArrayHashMap = std.array_hash_map.StringArrayHashMap(void);
|
||||
const StringHashMap = std.hash_map.StringHashMap(void);
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer std.debug.assert(gpa.deinit() == .ok);
|
||||
|
||||
const allocator = gpa.allocator();
|
||||
const allocator = std.heap.c_allocator;
|
||||
const elem_count = 1000;
|
||||
|
||||
const keys = try allocator.alloc([32]u8, elem_count);
|
||||
defer allocator.free(keys);
|
||||
|
||||
var rand = std.rand.DefaultPrng.init(1337);
|
||||
var rand = std.rand.DefaultPrng.init(0);
|
||||
for (keys) |*key| rand.fill(key);
|
||||
|
||||
var trie = try HashArrayMappedTrie([]const u8, void, StringContext).init(allocator);
|
||||
|
||||
31
src/trie.zig
31
src/trie.zig
@@ -15,10 +15,10 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
||||
|
||||
const Digest = Context.Digest; // as in Hash Code or Hash Digest
|
||||
const table_size = @typeInfo(Digest).Int.bits;
|
||||
const t = @intCast(Log2Int(Digest), @typeInfo(Log2Int(Digest)).Int.bits);
|
||||
const t: Log2Int(Digest) = @intCast(@typeInfo(Log2Int(Digest)).Int.bits);
|
||||
|
||||
free_list: FreeList,
|
||||
root: []?*Node,
|
||||
root: *[table_size]?*Node,
|
||||
|
||||
const Node = union(enum) { kv: Pair, table: Table };
|
||||
const Table = struct { map: Digest = 0, base: [*]Node };
|
||||
@@ -37,7 +37,7 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
||||
pub fn deinit(self: *const FreeList.Node, allocator: Allocator, len: usize) void {
|
||||
switch (len) {
|
||||
0 => unreachable,
|
||||
1 => allocator.destroy(@ptrCast(*Self.Node, self.inner)),
|
||||
1 => allocator.destroy(@as(*Self.Node, @ptrCast(self.inner))),
|
||||
else => allocator.free(self.inner[0..len]),
|
||||
}
|
||||
}
|
||||
@@ -114,7 +114,7 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
||||
}
|
||||
|
||||
pub fn create(self: *FreeList, allocator: Allocator, comptime T: type) !*T {
|
||||
return @ptrCast(*T, try self.alloc(allocator, T, 1));
|
||||
return @ptrCast(try self.alloc(allocator, T, 1));
|
||||
}
|
||||
|
||||
/// Free'd nodes aren't deallocated, but instead are tracked by a free list where they
|
||||
@@ -140,13 +140,13 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
||||
}
|
||||
|
||||
pub fn destroy(self: *FreeList, allocator: Allocator, node: *Self.Node) !void {
|
||||
self.free(allocator, @ptrCast([*]Self.Node, node)[0..1]);
|
||||
self.free(allocator, @as([*]Self.Node, @ptrCast(node))[0..1]);
|
||||
}
|
||||
};
|
||||
|
||||
pub fn init(allocator: Allocator) !Self {
|
||||
// TODO: Add ability to have a larger root node (for quicker lookup times)
|
||||
const root = try allocator.alloc(?*Node, table_size);
|
||||
const root = try allocator.create([table_size]?*Node);
|
||||
@memset(root, null);
|
||||
|
||||
return Self{ .root = root, .free_list = try FreeList.init(allocator) };
|
||||
@@ -181,10 +181,10 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
||||
}
|
||||
}
|
||||
|
||||
fn tableIdx(hash: Digest, offset: u16) Log2Int(Digest) {
|
||||
const shift_amt = @intCast(Log2Int(Digest), table_size - offset);
|
||||
inline fn tableIdx(hash: Digest, offset: u16) Log2Int(Digest) {
|
||||
const shift_amt: Log2Int(Digest) = @intCast(table_size - offset);
|
||||
|
||||
return @truncate(Log2Int(Digest), hash >> shift_amt);
|
||||
return @truncate(hash >> shift_amt);
|
||||
}
|
||||
|
||||
pub fn search(self: *Self, key: K) ?Pair {
|
||||
@@ -198,13 +198,12 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
||||
switch (current.*) {
|
||||
.table => |table| {
|
||||
const mask = @as(Digest, 1) << tableIdx(hash, hash_offset);
|
||||
if (table.map & mask == 0) return null; // empty table
|
||||
|
||||
if (table.map & mask != 0) {
|
||||
const idx = @popCount(table.map & (mask - 1));
|
||||
current = &table.base[idx];
|
||||
const idx = @popCount(table.map & (mask - 1));
|
||||
current = &table.base[idx];
|
||||
|
||||
hash_offset += t;
|
||||
} else return null; // hash table entry is empty
|
||||
hash_offset += t;
|
||||
},
|
||||
.kv => |pair| {
|
||||
if (!Context.eql(pair.key, key)) return null;
|
||||
@@ -243,7 +242,7 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
||||
|
||||
var i: Log2Int(Digest) = 0;
|
||||
for (0..table_size) |shift| {
|
||||
const mask_loop = @as(Digest, 1) << @intCast(Log2Int(Digest), shift);
|
||||
const mask_loop = @as(Digest, 1) << @as(Log2Int(Digest), @intCast(shift));
|
||||
|
||||
if (new_map & mask_loop != 0) {
|
||||
defer i += 1;
|
||||
@@ -287,7 +286,7 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
||||
const copied_pair = try self.free_list.create(allocator, Node);
|
||||
copied_pair.* = .{ .kv = prev_pair };
|
||||
|
||||
current.* = .{ .table = .{ .map = mask, .base = @ptrCast([*]Node, copied_pair) } };
|
||||
current.* = .{ .table = .{ .map = mask, .base = @as([*]Node, @ptrCast(copied_pair)) } };
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user