Implement more hash table logic
continuous-integration/drone/pr Build is passing Details
continuous-integration/drone/push Build is passing Details

This commit is contained in:
kayomn 2022-10-15 11:38:22 +01:00
parent 01e6a7cb56
commit 5f4e4cc811
3 changed files with 46 additions and 18 deletions

View File

@ -11,9 +11,7 @@ const sys = @import("./sys.zig");
/// Entry point. /// Entry point.
/// ///
pub fn main() anyerror!void { pub fn main() anyerror!void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; return nosuspend await async sys.runGraphics(anyerror, run);
return nosuspend await async sys.runGraphics(gpa.allocator(), anyerror, run);
} }
test { test {

View File

@ -819,7 +819,7 @@ pub const RunError = error {
/// Should an error from `run` occur, an `Error` is returned, otherwise a [RunError] is returned if /// Should an error from `run` occur, an `Error` is returned, otherwise a [RunError] is returned if
/// the underlying runtime fails and is logged. /// 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 { comptime run: GraphicsRunner(Error)) (RunError || Error)!void {
if (ext.SDL_Init(ext.SDL_INIT_EVERYTHING) != 0) { 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); defer ext.SDL_DestroyRenderer(renderer);
var cwd_file_system = FileSystem{.native =.{.path_prefix = "./"}}; 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. defer data_access.close();
joinedPath(&.{"./data.oar"})).open(.readonly);
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) { ext.SDL_LogCritical(ext.SDL_LOG_CATEGORY_APPLICATION, switch (err) {
error.OutOfMemory => "Failed to allocate necessary memory", error.OutOfMemory => "Failed to allocate necessary memory",
error.OutOfMutexes => "Failed to create file-system work lock", error.OutOfMutexes => "Failed to create file-system work lock",

View File

@ -32,6 +32,29 @@ pub fn Hashed(comptime Key: type, comptime Value: type,
/// ///
const Self = @This(); 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 /// Searches for `key` to delete it, returning the deleted value or `null` if no matching
/// key was found. /// key was found.
@ -62,15 +85,6 @@ pub fn Hashed(comptime Key: type, comptime Value: type,
return null; 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 /// Attempts to insert the value at `key` to be `value` in `self`, returning an
/// [InsertError] if it fails. /// [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 { pub const InsertError = std.mem.Allocator.Error || error {
KeyExists, 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 { pub fn KeyContext(comptime Key: type) type {
return struct { return struct {
hash: fn (Key) usize, 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 { fn equalsString(this_string: []const u8, that_string: []const u8) bool {
return std.mem.eql(u8, this_string, that_string); return std.mem.eql(u8, this_string, that_string);
} }
///
/// Hashes `string` into a hash value of `usize`.
///
fn hashString(string: []const u8) usize { fn hashString(string: []const u8) usize {
var hash = @as(usize, 5381); var hash = @as(usize, 5381);
@ -162,6 +187,9 @@ fn hashString(string: []const u8) usize {
return hash; return hash;
} }
///
/// A [KeyContext] for handling `[]const u8` types.
///
pub const string_context = KeyContext([]const u8){ pub const string_context = KeyContext([]const u8){
.hash = hashString, .hash = hashString,
.equals = equalsString, .equals = equalsString,