chore: fix formatting

This commit is contained in:
Rekai Nyangadzayi Musuka 2023-04-10 23:57:10 -05:00
parent 5e630120f2
commit 4a983bd1e6
1 changed files with 212 additions and 210 deletions

View File

@ -1,210 +1,212 @@
//! Hash Array Mapped Trie //! Hash Array Mapped Trie
//! https://idea.popcount.org/2012-07-25-introduction-to-hamt/ //! https://idea.popcount.org/2012-07-25-introduction-to-hamt/
const std = @import("std"); const std = @import("std");
// const Token = @import("Token.zig"); // const Token = @import("Token.zig");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const HashArrayMappedTrie = @This(); const HashArrayMappedTrie = @This();
const t = 5; const t = 5;
const table_size = std.math.powi(u32, 2, t) catch unreachable; const table_size = std.math.powi(u32, 2, t) catch unreachable;
root: [table_size]?*Node, root: [table_size]?*Node,
allocator: Allocator, allocator: Allocator,
const Node = union(enum) { kv: Pair, table: Table }; const Node = union(enum) { kv: Pair, table: Table };
const Pair = struct { key: []const u8, value: void }; const Pair = struct { key: []const u8, value: void };
const Table = struct { map: u32 = 0, base: [*]Node }; const Table = struct { map: u32 = 0, base: [*]Node };
pub fn init(allocator: Allocator) !HashArrayMappedTrie { pub fn init(allocator: Allocator) !HashArrayMappedTrie {
return .{ return .{
.root = [_]?*Node{null} ** table_size, .root = [_]?*Node{null} ** table_size,
.allocator = allocator, .allocator = allocator,
}; };
} }
pub fn deinit(self: *HashArrayMappedTrie) void { pub fn deinit(self: *HashArrayMappedTrie) void {
for (self.root) |node| { for (self.root) |node| {
if (node == null) continue; if (node == null) continue;
deinitRecurse(self.allocator, node.?); deinitRecurse(self.allocator, node.?);
} }
} }
fn deinitRecurse(allocator: Allocator, node: *Node) void { fn deinitRecurse(allocator: Allocator, node: *Node) void {
switch (node.*) { switch (node.*) {
.kv => allocator.destroy(node), .kv => allocator.destroy(node),
else => {}, else => {},
} }
} }
fn amtIdx(comptime T: type, bitset: T, offset: u16) std.math.Log2Int(T) { fn amtIdx(comptime T: type, bitset: T, offset: u16) std.math.Log2Int(T) {
const L2I = std.math.Log2Int(T); const L2I = std.math.Log2Int(T);
const shift_amt = @intCast(L2I, @typeInfo(T).Int.bits - offset); const shift_amt = @intCast(L2I, @typeInfo(T).Int.bits - offset);
return @truncate(L2I, bitset >> shift_amt); return @truncate(L2I, bitset >> shift_amt);
} }
pub fn search(self: *HashArrayMappedTrie, key: []const u8) ?Pair { pub fn search(self: *HashArrayMappedTrie, key: []const u8) ?Pair {
const bitset = hash(key); const bitset = hash(key);
// most siginificant t bits from hash // most siginificant t bits from hash
var hash_offset: u5 = t; var hash_offset: u5 = t;
var current: *Node = self.root[amtIdx(u32, bitset, hash_offset)] orelse return null; var current: *Node = self.root[amtIdx(u32, bitset, hash_offset)] orelse return null;
while (true) { while (true) {
switch (current.*) { switch (current.*) {
.table => |table| { .table => |table| {
hash_offset += t; 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];
} else return null; // hash table entry is empty } else return null; // hash table entry is empty
}, },
.kv => |pair| { .kv => |pair| {
if (!std.mem.eql(u8, pair.key, key)) return null; if (!std.mem.eql(u8, pair.key, key)) return null;
return pair; return pair;
}, },
} }
} }
} }
pub fn insert(self: *HashArrayMappedTrie, comptime key: []const u8, value: void) !void { pub fn insert(self: *HashArrayMappedTrie, comptime key: []const u8, value: void) !void {
const bitset = hash(key); const bitset = hash(key);
// most siginificant t bits from hash // most siginificant t bits from hash
var hash_offset: u5 = t; var hash_offset: u5 = t;
const root_idx = amtIdx(u32, bitset, hash_offset); const root_idx = amtIdx(u32, bitset, hash_offset);
var current: *Node = self.root[root_idx] orelse { var current: *Node = self.root[root_idx] orelse {
// node in root table is empty, place the KV here // node in root table is empty, place the KV here
const node = try self.allocator.create(Node); const node = try self.allocator.create(Node);
node.* = .{ .kv = .{ .key = key, .value = value } }; node.* = .{ .kv = .{ .key = key, .value = value } };
self.root[root_idx] = node; self.root[root_idx] = node;
return; return;
}; };
while (true) { while (true) {
const mask = @as(u32, 1) << amtIdx(u32, bitset, hash_offset); const mask = @as(u32, 1) << amtIdx(u32, bitset, hash_offset);
switch (current.*) { switch (current.*) {
.table => |*table| { .table => |*table| {
if (table.map & mask == 0) { if (table.map & mask == 0) {
// Empty // Empty
const old_len = @popCount(table.map); const old_len = @popCount(table.map);
const new_base = try self.allocator.alloc(Node, old_len + 1); const new_base = try self.allocator.alloc(Node, old_len + 1);
var i: u5 = 0; var i: u5 = 0;
for (0..@typeInfo(u32).Int.bits) |shift| { for (0..@typeInfo(u32).Int.bits) |shift| {
const mask_loop = @as(u32, 1) << @intCast(u5, shift); const mask_loop = @as(u32, 1) << @intCast(u5, shift);
if (table.map & mask_loop != 0) { if (table.map & mask_loop != 0) {
defer i += 1; defer i += 1;
const idx = @popCount(table.map & (mask_loop - 1)); const idx = @popCount(table.map & (mask_loop - 1));
const copy = if (mask == mask_loop) Node{ .kv = Pair{ .key = key, .value = value } } else table.base[idx]; const copy = if (mask == mask_loop) Node{ .kv = Pair{ .key = key, .value = value } } else table.base[idx];
new_base[i] = copy; new_base[i] = copy;
} }
} }
self.allocator.free(table.base[0..old_len]); self.allocator.free(table.base[0..old_len]);
table.base = new_base.ptr; table.base = new_base.ptr;
table.map |= mask; table.map |= mask;
return; // inserted an elemnt into the Trie return; // inserted an elemnt into the Trie
} else { } else {
// Found an entry in the array, continue loop (?) // Found an entry in the array, continue loop (?)
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; // Go one layer deper hash_offset += t; // Go one layer deper
} }
}, },
.kv => |prev_pair| { .kv => |prev_pair| {
const prev_bitset = hash(prev_pair.key); const prev_bitset = hash(prev_pair.key);
const prev_mask = @as(u32, 1) << amtIdx(u32, prev_bitset, hash_offset); const prev_mask = @as(u32, 1) << amtIdx(u32, prev_bitset, hash_offset);
const table = switch (std.math.order(mask, prev_mask)) { const table = switch (std.math.order(mask, prev_mask)) {
.lt => blk: { .lt => blk: {
// there are no collisions between the two hash subsets. // there are no collisions between the two hash subsets.
const pairs = try self.allocator.alloc(Node, 2); const pairs = try self.allocator.alloc(Node, 2);
pairs[0] = .{ .kv = .{ .key = key, .value = value } }; pairs[0] = .{ .kv = .{ .key = key, .value = value } };
pairs[1] = .{ .kv = prev_pair }; pairs[1] = .{ .kv = prev_pair };
break :blk .{ .table = .{ .map = mask | prev_mask, .base = pairs.ptr } }; break :blk .{ .table = .{ .map = mask | prev_mask, .base = pairs.ptr } };
}, },
.gt => blk: { .gt => blk: {
// there are no collisions between the two hash subsets. // there are no collisions between the two hash subsets.
const pairs = try self.allocator.alloc(Node, 2); const pairs = try self.allocator.alloc(Node, 2);
pairs[0] = .{ .kv = prev_pair }; pairs[0] = .{ .kv = prev_pair };
pairs[1] = .{ .kv = .{ .key = key, .value = value } }; pairs[1] = .{ .kv = .{ .key = key, .value = value } };
break :blk .{ .table = .{ .map = mask | prev_mask, .base = pairs.ptr } }; break :blk .{ .table = .{ .map = mask | prev_mask, .base = pairs.ptr } };
}, },
.eq => blk: { .eq => blk: {
const copied_pair = try self.allocator.alloc(Node, 1); const copied_pair = try self.allocator.alloc(Node, 1);
copied_pair[0] = .{ .kv = prev_pair }; copied_pair[0] = .{ .kv = prev_pair };
break :blk .{ .table = .{ .map = mask, .base = copied_pair.ptr } }; break :blk .{ .table = .{ .map = mask, .base = copied_pair.ptr } };
}, },
}; };
current.* = table; current.* = table;
}, },
} }
} }
} }
fn walk(node: *const Node, indent: u8) void { fn walk(node: *const Node, indent: u8) void {
switch (node.*) { switch (node.*) {
.kv => |pair| std.debug.print("{}: {any}\n", .{ indent, pair }), .kv => |pair| std.debug.print("{}: {any}\n", .{ indent, pair }),
.table => |table| { .table => |table| {
const len = @popCount(table.map); const len = @popCount(table.map);
for (0..len) |i| { for (0..len) |i| {
walk(&table.base[i], indent + 1); walk(&table.base[i], indent + 1);
} }
}, },
} }
} }
fn hash(key: []const u8) u32 { fn hash(key: []const u8) u32 {
var result: u32 = 0; var result: u32 = 0;
for (key) |c| result |= @as(u32, 1) << @intCast(u5, c - 'a');
// 6 because we're working with 'a' -> 'z'
return result; for (key) |c| result |= @as(u32, 1) << 6 + @intCast(u5, c - 'a');
}
return result;
test "insert doesn't panic" { }
var trie = try HashArrayMappedTrie.init(std.testing.allocator);
defer trie.deinit(); test "insert doesn't panic" {
var trie = try HashArrayMappedTrie.init(std.testing.allocator);
try trie.insert("hello", {}); defer trie.deinit();
}
try trie.insert("hello", {});
test "search doesn't panic" { }
var trie = try HashArrayMappedTrie.init(std.testing.allocator);
defer trie.deinit(); test "search doesn't panic" {
var trie = try HashArrayMappedTrie.init(std.testing.allocator);
std.debug.assert(trie.search("hello") == null); defer trie.deinit();
}
std.debug.assert(trie.search("hello") == null);
test "insert then search" { }
var trie = try HashArrayMappedTrie.init(std.heap.page_allocator);
defer trie.deinit(); test "insert then search" {
var trie = try HashArrayMappedTrie.init(std.heap.page_allocator);
// Basic Usage defer trie.deinit();
try trie.insert("hello", {});
const test1 = trie.search("hello").?; // Basic Usage
try std.testing.expectEqual(Pair{ .key = "hello", .value = {} }, test1); try trie.insert("hello", {});
const test1 = trie.search("hello").?;
// Collision in Root Node try std.testing.expectEqual(Pair{ .key = "hello", .value = {} }, test1);
try trie.insert("helloworld", {});
const test2 = trie.search("helloworld").?; // Collision in Root Node
try std.testing.expectEqual(Pair{ .key = "helloworld", .value = {} }, test2); try trie.insert("helloworld", {});
} const test2 = trie.search("helloworld").?;
try std.testing.expectEqual(Pair{ .key = "helloworld", .value = {} }, test2);
}