Compare commits

...

3 Commits

4 changed files with 54 additions and 14 deletions

40
.github/workflows/main.yml vendored Normal file
View File

@ -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

View File

@ -18,7 +18,7 @@ const StringHashMap = std.hash_map.StringHashMap(void);
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer std.debug.assert(!gpa.deinit());
defer std.debug.assert(gpa.deinit() == .ok);
const allocator = gpa.allocator();
const elem_count = 1000;

View File

@ -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 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,
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 {
switch (len) {
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]),
}
}
@ -45,7 +45,7 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
pub fn init(allocator: Allocator) !FreeList {
const list = try allocator.create([table_size]?FreeList.Node);
std.mem.set(?FreeList.Node, list, null);
@memset(list, null);
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 {
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
@ -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 {
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 {
// TODO: Add ability to have a larger root node (for quicker lookup times)
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) };
}
@ -182,9 +182,9 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
}
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 {
@ -243,7 +243,7 @@ pub fn HashArrayMappedTrie(comptime K: type, comptime V: type, comptime Context:
var i: Log2Int(Digest) = 0;
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) {
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);
copied_pair.* = .{ .kv = prev_pair };
current.* = .{ .table = .{ .map = mask, .base = @ptrCast([*]Node, copied_pair) } };
current.* = .{ .table = .{ .map = mask, .base = @as([*]Node, @ptrCast(copied_pair)) } };
},
}
},