feat: add README.md
This commit is contained in:
parent
c8152f1343
commit
7882924179
|
@ -0,0 +1,46 @@
|
||||||
|
# Hash Array Mapped Trie
|
||||||
|
|
||||||
|
A barebones implementation of [this paper](https://infoscience.epfl.ch/record/64398) by Phil Bagwell.
|
||||||
|
|
||||||
|
### Usage
|
||||||
|
|
||||||
|
As an example:
|
||||||
|
```zig
|
||||||
|
const std = @import("std");
|
||||||
|
const expectEqual = std.testing.expectEqual;
|
||||||
|
const HashArrayMappedTrie = @import("hamt");
|
||||||
|
|
||||||
|
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 = StringTrie.init();
|
||||||
|
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`;
|
||||||
|
|
25
src/trie.zig
25
src/trie.zig
|
@ -291,23 +291,23 @@ const StringContext = struct {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const TestHamt = HashArrayMappedTrie([]const u8, void, StringContext);
|
const StringTrie = HashArrayMappedTrie([]const u8, void, StringContext);
|
||||||
|
|
||||||
test "trie init" {
|
test "trie init" {
|
||||||
_ = TestHamt.init();
|
_ = StringTrie.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
test "init and deinit" {
|
test "init and deinit" {
|
||||||
const allocator = std.testing.allocator;
|
const allocator = std.testing.allocator;
|
||||||
|
|
||||||
var trie = TestHamt.init();
|
var trie = StringTrie.init();
|
||||||
defer trie.deinit(allocator);
|
defer trie.deinit(allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
test "trie insert" {
|
test "trie insert" {
|
||||||
const allocator = std.testing.allocator;
|
const allocator = std.testing.allocator;
|
||||||
|
|
||||||
var trie = TestHamt.init();
|
var trie = StringTrie.init();
|
||||||
defer trie.deinit(allocator);
|
defer trie.deinit(allocator);
|
||||||
|
|
||||||
try trie.insert(allocator, "hello", {});
|
try trie.insert(allocator, "hello", {});
|
||||||
|
@ -315,10 +315,10 @@ test "trie insert" {
|
||||||
}
|
}
|
||||||
|
|
||||||
test "trie search" {
|
test "trie search" {
|
||||||
const Pair = TestHamt.Pair;
|
const Pair = StringTrie.Pair;
|
||||||
const allocator = std.testing.allocator;
|
const allocator = std.testing.allocator;
|
||||||
|
|
||||||
var trie = TestHamt.init();
|
var trie = StringTrie.init();
|
||||||
defer trie.deinit(allocator);
|
defer trie.deinit(allocator);
|
||||||
|
|
||||||
try std.testing.expectEqual(@as(?Pair, null), trie.search("sdvx"));
|
try std.testing.expectEqual(@as(?Pair, null), trie.search("sdvx"));
|
||||||
|
@ -331,3 +331,16 @@ test "trie search" {
|
||||||
try trie.insert(allocator, "", {});
|
try trie.insert(allocator, "", {});
|
||||||
try std.testing.expectEqual(@as(?Pair, .{ .key = "", .value = {} }), trie.search(""));
|
try std.testing.expectEqual(@as(?Pair, .{ .key = "", .value = {} }), trie.search(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "README.md example" {
|
||||||
|
const Pair = StringTrie.Pair;
|
||||||
|
const allocator = std.testing.allocator;
|
||||||
|
|
||||||
|
var trie = StringTrie.init();
|
||||||
|
defer trie.deinit(allocator);
|
||||||
|
|
||||||
|
try trie.insert(allocator, "hello", {});
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(?Pair, .{ .key = "hello", .value = {} }), trie.search("hello"));
|
||||||
|
try std.testing.expectEqual(@as(?Pair, null), trie.search("world"));
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue