From 5f4e4cc81178dc7cd3d2f5cda6c1c362b9a9321b Mon Sep 17 00:00:00 2001 From: kayomn Date: Sat, 15 Oct 2022 11:38:22 +0100 Subject: [PATCH] Implement more hash table logic --- src/main.zig | 4 +--- src/sys.zig | 12 +++++++----- src/table.zig | 48 ++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/main.zig b/src/main.zig index 9b95b9d..2f85626 100644 --- a/src/main.zig +++ b/src/main.zig @@ -11,9 +11,7 @@ const sys = @import("./sys.zig"); /// Entry point. /// pub fn main() anyerror!void { - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; - - return nosuspend await async sys.runGraphics(gpa.allocator(), anyerror, run); + return nosuspend await async sys.runGraphics(anyerror, run); } test { diff --git a/src/sys.zig b/src/sys.zig index cbfe2a8..e431003 100644 --- a/src/sys.zig +++ b/src/sys.zig @@ -819,7 +819,7 @@ pub const RunError = error { /// Should an error from `run` occur, an `Error` is returned, otherwise a [RunError] is returned if /// the underlying runtime fails and is logged. /// -pub fn runGraphics(allocator: std.mem.Allocator, comptime Error: anytype, +pub fn runGraphics(comptime Error: anytype, comptime run: GraphicsRunner(Error)) (RunError || Error)!void { if (ext.SDL_Init(ext.SDL_INIT_EVERYTHING) != 0) { @@ -856,13 +856,15 @@ pub fn runGraphics(allocator: std.mem.Allocator, comptime Error: anytype, defer ext.SDL_DestroyRenderer(renderer); var cwd_file_system = FileSystem{.native =.{.path_prefix = "./"}}; + var data_access = try (try cwd_file_system.joinedPath(&.{"./data.oar"})).open(.readonly); - var data_archive_file_access = try (try cwd_file_system. - joinedPath(&.{"./data.oar"})).open(.readonly); + defer data_access.close(); - defer data_archive_file_access.close(); + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; - var app_context = AppContext.Implementation.init(allocator, data_archive_file_access) catch |err| { + defer _ = gpa.deinit(); + + var app_context = AppContext.Implementation.init(gpa.allocator(), data_access) catch |err| { ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, switch (err) { error.OutOfMemory => "Failed to allocate necessary memory", error.OutOfMutexes => "Failed to create file-system work lock", diff --git a/src/table.zig b/src/table.zig index c89439c..aecd12d 100644 --- a/src/table.zig +++ b/src/table.zig @@ -32,6 +32,29 @@ pub fn Hashed(comptime Key: type, comptime Value: type, /// const Self = @This(); + /// + /// Deinitializes `self`, preventing any further use. + /// + pub fn deinit(self: *Self) void { + self.allocator.free(self.buckets); + + self.buckets = &.{}; + } + + /// + /// Initializes a [Self] using `allocator` as the memory allocation strategy. + /// + /// Returns a new [Self] value or an [Allocator.Error] if initializing failed. + /// + pub fn init(allocator: Allocator) Allocator.Error!Self { + return Self{ + .buckets = try allocator.alloc(Bucket, 4), + .filled = 0, + .allocator = allocator, + .load_limit = 0.75, + }; + } + /// /// Searches for `key` to delete it, returning the deleted value or `null` if no matching /// key was found. @@ -62,15 +85,6 @@ pub fn Hashed(comptime Key: type, comptime Value: type, return null; } - pub fn init(allocator: Allocator) Allocator.Error!Self { - return Self{ - .buckets = try allocator.alloc(Bucket, 4), - .filled = 0, - .allocator = allocator, - .load_limit = 0.75, - }; - } - /// /// Attempts to insert the value at `key` to be `value` in `self`, returning an /// [InsertError] if it fails. @@ -137,12 +151,17 @@ pub fn Hashed(comptime Key: type, comptime Value: type, } /// -/// +/// [InsertError.KeyExists] occurs when an insertion was attempted on a table with a matching key +/// already present. /// pub const InsertError = std.mem.Allocator.Error || error { KeyExists, }; +/// +/// Returns a context type for handling `Key` as a key in a table, associating hashing and equality +/// behaviors to it. +/// pub fn KeyContext(comptime Key: type) type { return struct { hash: fn (Key) usize, @@ -150,10 +169,16 @@ pub fn KeyContext(comptime Key: type) type { }; } +/// +/// Tests if the contents of `this_string` lexically equals the contents of `that_string`. +/// fn equalsString(this_string: []const u8, that_string: []const u8) bool { return std.mem.eql(u8, this_string, that_string); } +/// +/// Hashes `string` into a hash value of `usize`. +/// fn hashString(string: []const u8) usize { var hash = @as(usize, 5381); @@ -162,6 +187,9 @@ fn hashString(string: []const u8) usize { return hash; } +/// +/// A [KeyContext] for handling `[]const u8` types. +/// pub const string_context = KeyContext([]const u8){ .hash = hashString, .equals = equalsString,