feat(hamt): properly implement search
This commit is contained in:
parent
6f6b1d8889
commit
12dc90a687
|
@ -66,14 +66,13 @@ pub fn search(self: *HashArrayMappedTrie, key: []const u8) ?Pair {
|
||||||
while (true) {
|
while (true) {
|
||||||
switch (current.*) {
|
switch (current.*) {
|
||||||
.table => |table| {
|
.table => |table| {
|
||||||
hash_offset += t;
|
|
||||||
|
|
||||||
const mask = @as(u32, 1) << amtIdx(u32, bitset, hash_offset);
|
const mask = @as(u32, 1) << amtIdx(u32, bitset, hash_offset);
|
||||||
|
|
||||||
if (table.map & mask != 0) {
|
if (table.map & mask != 0) {
|
||||||
const idx = @popCount(table.map & (mask - 1));
|
const idx = @popCount(table.map & (mask - 1));
|
||||||
|
|
||||||
current = &table.base[idx];
|
current = &table.base[idx];
|
||||||
|
|
||||||
|
hash_offset += t;
|
||||||
} else return null; // hash table entry is empty
|
} else return null; // hash table entry is empty
|
||||||
},
|
},
|
||||||
.kv => |pair| {
|
.kv => |pair| {
|
||||||
|
@ -218,17 +217,22 @@ test "search doesn't panic" {
|
||||||
std.debug.assert(trie.search("hello") == null);
|
std.debug.assert(trie.search("hello") == null);
|
||||||
}
|
}
|
||||||
|
|
||||||
test "insert then search" {
|
test "insert edge cases" {
|
||||||
var trie = try HashArrayMappedTrie.init(std.heap.page_allocator);
|
var trie = try HashArrayMappedTrie.init(std.heap.page_allocator);
|
||||||
defer trie.deinit();
|
defer trie.deinit();
|
||||||
|
|
||||||
// Basic Usage
|
// Basic Usage
|
||||||
try trie.insert("hello", {});
|
try trie.insert("hello", {});
|
||||||
const test1 = trie.search("hello").?;
|
try trie.insert("world", {});
|
||||||
try std.testing.expectEqual(Pair{ .key = "hello", .value = {} }, test1);
|
try trie.insert("zywxv", {});
|
||||||
|
|
||||||
// Collision in Root Node
|
// Colliding Keys
|
||||||
try trie.insert("helloworld", {});
|
try trie.insert("abcde", {});
|
||||||
const test2 = trie.search("helloworld").?;
|
try trie.insert("abcdef", {});
|
||||||
try std.testing.expectEqual(Pair{ .key = "helloworld", .value = {} }, test2);
|
|
||||||
|
trie.walk();
|
||||||
|
|
||||||
|
try std.testing.expectEqual(Pair{ .key = "hello", .value = {} }, trie.search("hello").?);
|
||||||
|
|
||||||
|
// try std.testing.expectEqual(Pair{ .key = "abcde", .value = {} }, trie.search("abcde").?);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue