Compare commits
No commits in common. "d578bb422e5018c7ec9d1b984232365fbdf3c784" and "da7d9cfcc09c560bc4c61861d97e4bce833cfa67" have entirely different histories.
d578bb422e
...
da7d9cfcc0
@ -3,9 +3,9 @@ const stack = @import("./stack.zig");
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
///
|
///
|
||||||
|
/// Closure for allocating, reallocating, and deallocating dynamic memory resources through itself.
|
||||||
///
|
///
|
||||||
///
|
pub const Allocator = meta.BiFunction(56, ?[]u8, usize, AllocationError![]u8);
|
||||||
pub const Allocator = std.mem.Allocator;
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// File-system agnostic abstraction for manipulating a file.
|
/// File-system agnostic abstraction for manipulating a file.
|
||||||
@ -203,7 +203,7 @@ pub fn equals(comptime Element: type, this: []const Element, that: []const Eleme
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
test "Memory buffers equal" {
|
test "Equivalence of bytes" {
|
||||||
const bytes_sequence = &.{69, 42, 0};
|
const bytes_sequence = &.{69, 42, 0};
|
||||||
const testing = std.testing;
|
const testing = std.testing;
|
||||||
|
|
||||||
@ -255,3 +255,11 @@ test "Null writing" {
|
|||||||
try testing.expectEqual(nullWriter().apply(sequence), sequence.len);
|
try testing.expectEqual(nullWriter().apply(sequence), sequence.len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Applies the singular `byte` to `writer`, returning `true` if it was successfully written,
|
||||||
|
/// otherwise `false`.
|
||||||
|
///
|
||||||
|
pub fn writeByte(writer: *Writer, byte: u8) bool {
|
||||||
|
return (writer.apply(std.mem.asBytes(&byte)) != 0);
|
||||||
|
}
|
||||||
|
@ -48,20 +48,20 @@ pub fn BiFunction(comptime captures_size: usize, comptime A: type,
|
|||||||
@compileError("`captures` must be smaller than or equal to " ++
|
@compileError("`captures` must be smaller than or equal to " ++
|
||||||
std.fmt.comptimePrint("{d}", .{captures_size}) ++ " bytes");
|
std.fmt.comptimePrint("{d}", .{captures_size}) ++ " bytes");
|
||||||
|
|
||||||
|
const captures_align = @alignOf(Captures);
|
||||||
|
|
||||||
var function = Self{
|
var function = Self{
|
||||||
.context = undefined,
|
.context = undefined,
|
||||||
|
|
||||||
.applyErased = struct {
|
.applyErased = struct {
|
||||||
fn applyErased(erased: *anyopaque, a: A, b: B) Out {
|
fn do(erased: *anyopaque, a: A, b: B) Out {
|
||||||
return call(if (Captures == void) {} else @ptrCast(*Captures,
|
return call(@ptrCast(*Captures, @alignCast(
|
||||||
@alignCast(@alignOf(Captures), erased)).*, a, b);
|
captures_align, erased)).*, a, b);
|
||||||
}
|
}
|
||||||
}.applyErased,
|
}.do,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (captures != {}) {
|
@ptrCast(*Captures, @alignCast(captures_align, &function.context)).* = captures;
|
||||||
@ptrCast(*Captures, @alignCast(@alignOf(Captures), &function.context)).* = captures;
|
|
||||||
}
|
|
||||||
|
|
||||||
return function;
|
return function;
|
||||||
}
|
}
|
||||||
@ -108,11 +108,11 @@ pub fn Function(comptime captures_size: usize, comptime In: type, comptime Out:
|
|||||||
.context = undefined,
|
.context = undefined,
|
||||||
|
|
||||||
.applyErased = struct {
|
.applyErased = struct {
|
||||||
fn applyErased(erased: *anyopaque, input: In) Out {
|
fn do(erased: *anyopaque, input: In) Out {
|
||||||
return call(if (Captures == void) {} else @ptrCast(*Captures,
|
return call(@ptrCast(*Captures, @alignCast(
|
||||||
@alignCast(@alignOf(Captures), erased)).*, input);
|
captures_align, erased)).*, input);
|
||||||
}
|
}
|
||||||
}.applyErased,
|
}.do,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ptrCast(*Captures, @alignCast(captures_align, &function.context)).* = captures;
|
@ptrCast(*Captures, @alignCast(captures_align, &function.context)).* = captures;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
const io = @import("./io.zig");
|
const std = @import("std");
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Returns a hash-backed table type of `Value`s indexed by `Key` and using `key_context` as the key
|
/// Returns a hash-backed table type of `Value`s indexed by `Key` and using `key_context` as the key
|
||||||
@ -7,7 +7,7 @@ const io = @import("./io.zig");
|
|||||||
pub fn Hashed(comptime Key: type, comptime Value: type,
|
pub fn Hashed(comptime Key: type, comptime Value: type,
|
||||||
comptime key_context: KeyContext(Key)) type {
|
comptime key_context: KeyContext(Key)) type {
|
||||||
|
|
||||||
const Allocator = io.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
|
|
||||||
return struct {
|
return struct {
|
||||||
allocator: Allocator,
|
allocator: Allocator,
|
||||||
@ -44,13 +44,11 @@ pub fn Hashed(comptime Key: type, comptime Value: type,
|
|||||||
///
|
///
|
||||||
/// Initializes a [Self] using `allocator` as the memory allocation strategy.
|
/// Initializes a [Self] using `allocator` as the memory allocation strategy.
|
||||||
///
|
///
|
||||||
/// Returns a new [Self] value or an [io.Allocator.Error] if initializing failed.
|
/// Returns a new [Self] value or an [Allocator.Error] if initializing failed.
|
||||||
///
|
///
|
||||||
pub fn init(allocator: Allocator) Allocator.Error!Self {
|
pub fn init(allocator: Allocator) Allocator.Error!Self {
|
||||||
const initial_capacity = 4;
|
|
||||||
|
|
||||||
return Self{
|
return Self{
|
||||||
.buckets = try allocator.alloc(Bucket, initial_capacity),
|
.buckets = try allocator.alloc(Bucket, 4),
|
||||||
.filled = 0,
|
.filled = 0,
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
.load_limit = 0.75,
|
.load_limit = 0.75,
|
||||||
@ -160,7 +158,7 @@ pub fn Hashed(comptime Key: type, comptime Value: type,
|
|||||||
/// [InsertError.KeyExists] occurs when an insertion was attempted on a table with a matching key
|
/// [InsertError.KeyExists] occurs when an insertion was attempted on a table with a matching key
|
||||||
/// already present.
|
/// already present.
|
||||||
///
|
///
|
||||||
pub const InsertError = io.Allocator.Error || error {
|
pub const InsertError = std.mem.Allocator.Error || error {
|
||||||
KeyExists,
|
KeyExists,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -175,27 +173,14 @@ pub fn KeyContext(comptime Key: type) type {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
test "Hashed table manipulation with bytes context" {
|
||||||
/// A [KeyContext] for dealing with string literal (i.e. []const u8) values.
|
const testing = std.testing;
|
||||||
///
|
const io = @import("./io.zig");
|
||||||
/// **Note** that, while lightweight, this context should only be considered safe to use with string
|
|
||||||
/// literals or variables pointing to string literals - as the [KeyContext] does not take ownership
|
|
||||||
/// of its keys beyond copying the reference.
|
|
||||||
///
|
|
||||||
pub const string_literal_context = KeyContext([]const u8){
|
|
||||||
.hash = io.hashBytes,
|
|
||||||
|
|
||||||
.equals = struct {
|
var table = try Hashed([]const u8, u32, .{
|
||||||
fn stringsEqual(this: []const u8, that: []const u8) bool {
|
.equals = io.equalsBytes,
|
||||||
return io.equals(u8, this, that);
|
.hash = io.hashBytes,
|
||||||
}
|
}).init(testing.allocator);
|
||||||
}.stringsEqual,
|
|
||||||
};
|
|
||||||
|
|
||||||
test "Hash table manipulation with string literal context" {
|
|
||||||
const testing = @import("std").testing;
|
|
||||||
|
|
||||||
var table = try Hashed([]const u8, u32, string_literal_context).init(testing.allocator);
|
|
||||||
|
|
||||||
defer table.deinit();
|
defer table.deinit();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user