Compare commits
3 Commits
fa97344b24
...
f8153f6569
Author | SHA1 | Date |
---|---|---|
Rekai Nyangadzayi Musuka | f8153f6569 | |
Rekai Nyangadzayi Musuka | 722427b693 | |
Rekai Nyangadzayi Musuka | 56c5f254ee |
|
@ -0,0 +1,40 @@
|
||||||
|
name: Nightly
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
paths:
|
||||||
|
- "**.zig"
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
schedule:
|
||||||
|
- cron: "0 0 * * *"
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
lint:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: goto-bus-stop/setup-zig@v2
|
||||||
|
with:
|
||||||
|
version: master
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- run: zig fmt src/*.zig
|
||||||
|
test:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||||
|
runs-on: ${{matrix.os}}
|
||||||
|
steps:
|
||||||
|
- uses: goto-bus-stop/setup-zig@v2
|
||||||
|
with:
|
||||||
|
version: master
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- run: zig build test
|
||||||
|
bench:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: goto-bus-stop/setup-zig@v2
|
||||||
|
with:
|
||||||
|
version: master
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- run: zig build -Doptimize=ReleaseFast -Dcpu=baseline bench
|
|
@ -1,5 +1,5 @@
|
||||||
.{
|
.{
|
||||||
.name = "hamt",
|
.name = "hamt",
|
||||||
.version = "0.1.0",
|
.version = "0.1.0",
|
||||||
.dependencies = .{},
|
.dependencies = .{},
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ const StringHashMap = std.hash_map.StringHashMap(void);
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
defer std.debug.assert(!gpa.deinit());
|
defer std.debug.assert(gpa.deinit() == .ok);
|
||||||
|
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
const elem_count = 1000;
|
const elem_count = 1000;
|
||||||
|
|
20
src/trie.zig
20
src/trie.zig
|
@ -15,7 +15,7 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
||||||
|
|
||||||
const Digest = Context.Digest; // as in Hash Code or Hash Digest
|
const Digest = Context.Digest; // as in Hash Code or Hash Digest
|
||||||
const table_size = @typeInfo(Digest).Int.bits;
|
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,
|
free_list: FreeList,
|
||||||
root: []?*Node,
|
root: []?*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 {
|
pub fn deinit(self: *const FreeList.Node, allocator: Allocator, len: usize) void {
|
||||||
switch (len) {
|
switch (len) {
|
||||||
0 => unreachable,
|
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]),
|
else => allocator.free(self.inner[0..len]),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
||||||
|
|
||||||
pub fn init(allocator: Allocator) !FreeList {
|
pub fn init(allocator: Allocator) !FreeList {
|
||||||
const list = try allocator.create([table_size]?FreeList.Node);
|
const list = try allocator.create([table_size]?FreeList.Node);
|
||||||
std.mem.set(?FreeList.Node, list, null);
|
@memset(list, null);
|
||||||
|
|
||||||
return .{ .list = list };
|
return .{ .list = list };
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
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
|
/// Free'd nodes aren't deallocated, but instead are tracked by a free list where they
|
||||||
|
@ -140,14 +140,14 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn destroy(self: *FreeList, allocator: Allocator, node: *Self.Node) !void {
|
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 {
|
pub fn init(allocator: Allocator) !Self {
|
||||||
// TODO: Add ability to have a larger root node (for quicker lookup times)
|
// 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.alloc(?*Node, table_size);
|
||||||
std.mem.set(?*Node, root, null);
|
@memset(root, null);
|
||||||
|
|
||||||
return Self{ .root = root, .free_list = try FreeList.init(allocator) };
|
return Self{ .root = root, .free_list = try FreeList.init(allocator) };
|
||||||
}
|
}
|
||||||
|
@ -182,9 +182,9 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tableIdx(hash: Digest, offset: u16) Log2Int(Digest) {
|
fn tableIdx(hash: Digest, offset: u16) Log2Int(Digest) {
|
||||||
const shift_amt = @intCast(Log2Int(Digest), table_size - offset);
|
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 {
|
pub fn search(self: *Self, key: K) ?Pair {
|
||||||
|
@ -243,7 +243,7 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
||||||
|
|
||||||
var i: Log2Int(Digest) = 0;
|
var i: Log2Int(Digest) = 0;
|
||||||
for (0..table_size) |shift| {
|
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) {
|
if (new_map & mask_loop != 0) {
|
||||||
defer i += 1;
|
defer i += 1;
|
||||||
|
@ -287,7 +287,7 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
|
||||||
const copied_pair = try self.free_list.create(allocator, Node);
|
const copied_pair = try self.free_list.create(allocator, Node);
|
||||||
copied_pair.* = .{ .kv = prev_pair };
|
copied_pair.* = .{ .kv = prev_pair };
|
||||||
|
|
||||||
current.* = .{ .table = .{ .map = mask, .base = @ptrCast([*]Node, copied_pair) } };
|
current.* = .{ .table = .{ .map = mask, .base = @as([*]Node, @ptrCast(copied_pair)) } };
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue