Go to file
Rekai Nyangadzayi Musuka 56c5f254ee chore: get project building on Zig v0.11.0-dev.3910+689f3163a 2023-07-02 21:06:41 -05:00
src chore: get project building on Zig v0.11.0-dev.3910+689f3163a 2023-07-02 21:06:41 -05:00
.gitignore chore: initial commit 2023-04-10 23:32:06 -05:00
README.md fix: allocate root table on heap 2023-04-20 23:12:52 -05:00
build.zig feat: add build command for benchmark tests 2023-04-20 15:42:26 -05:00
build.zig.zon chore: get project building on Zig v0.11.0-dev.3910+689f3163a 2023-07-02 21:06:41 -05:00

README.md

Hash Array Mapped Trie

A barebones implementation of this paper by Phil Bagwell.

Usage

As an example:

const std = @import("std");
const expectEqual = std.testing.expectEqual;
const HashArrayMappedTrie = @import("hamt").HashArrayMappedTrie;

const StringTrie = HashArrayMappedTrie([]const u8, void, StringContext);

const StringContext = struct {
    // Note: This definition is *required*
    // TODO: I could just grab the @typeInfo(HashFn).Fn.return_type right?
    pub const Digest = u64;

    pub inline fn hash(key: []const u8) Digest {
        return std.hash.Wyhash.hash(0, key);
    }

    pub inline fn eql(left: []const u8, right: []const u8) bool {
        return std.mem.eql(u8, left, right);
    }
};

test {
    const Pair = StringTrie.Pair;
    const allocator = std.testing.allocator;

    var trie = try StringTrie.init(allocator);
    defer trie.deinit(allocator);

    try trie.insert(allocator, "hello", {});

    try expectEqual(@as(?Pair, .{ .key = "hello", .value = {} }), trie.search("hello"));
    try expectEqual(@as(?Pair, null), trie.search("world"));
}

Building

Build in release mode with zig build -Doptimzie=ReleaseSafe;