Tidy up naming conventions in stack module

This commit is contained in:
kayomn 2022-10-15 01:08:03 +01:00
parent 53a369952e
commit b8517d3b22
1 changed files with 14 additions and 14 deletions

View File

@ -1,11 +1,17 @@
const io = @import("./io.zig"); const io = @import("./io.zig");
const std = @import("std"); const std = @import("std");
///
/// Returns a fixed-size stack type of `Element`s.
///
pub fn Fixed(comptime Element: type) type { pub fn Fixed(comptime Element: type) type {
return struct { return struct {
filled: usize = 0, filled: usize = 0,
buffer: []Element, buffer: []Element,
///
/// Stack type.
///
const Self = @This(); const Self = @This();
/// ///
@ -57,8 +63,8 @@ pub fn Fixed(comptime Element: type) type {
/// ///
/// Attempts to push `element` into `self`, returning a [FixedPushError] if it failed. /// Attempts to push `element` into `self`, returning a [FixedPushError] if it failed.
/// ///
pub fn push(self: *Self, element: Element) FixedPushError!void { pub fn push(self: *Self, element: Element) PushError!void {
if (self.filled == self.buffer.len) return error.Overflow; if (self.filled == self.buffer.len) return error.OutOfMemory;
self.buffer[self.filled] = element; self.buffer[self.filled] = element;
self.filled += 1; self.filled += 1;
@ -68,10 +74,10 @@ pub fn Fixed(comptime Element: type) type {
/// Attempts to push all of `elements` into `self`, returning a [FixedPushError] if it /// Attempts to push all of `elements` into `self`, returning a [FixedPushError] if it
/// failed. /// failed.
/// ///
pub fn pushAll(self: *Self, elements: []const u8) FixedPushError!void { pub fn pushAll(self: *Self, elements: []const u8) PushError!void {
const filled = (self.filled + elements.len); const filled = (self.filled + elements.len);
if (filled > self.buffer.len) return error.Overflow; if (filled > self.buffer.len) return error.OutOfMemory;
std.mem.copy(u8, self.buffer[self.filled ..], elements); std.mem.copy(u8, self.buffer[self.filled ..], elements);
@ -81,15 +87,9 @@ pub fn Fixed(comptime Element: type) type {
} }
/// ///
/// Potential errors that may occur while trying to push one or more elements into a stack of a /// Potential errors that may occur while trying to push one or more elements into a stack.
/// known maximum size.
/// ///
/// [FinitePushError.Overflow] is returned if the stack does not have sufficient capacity to hold a pub const PushError = std.mem.Allocator.Error;
/// given set of elements.
///
pub const FixedPushError = error {
Overflow,
};
test { test {
const testing = std.testing; const testing = std.testing;
@ -103,8 +103,8 @@ test {
try testing.expectEqual(stack.pop(), 69); try testing.expectEqual(stack.pop(), 69);
try stack.pushAll(&.{42, 10, 95, 0}); try stack.pushAll(&.{42, 10, 95, 0});
try testing.expectEqual(stack.count(), 4); try testing.expectEqual(stack.count(), 4);
try testing.expectError(FixedPushError.Overflow, stack.push(1)); try testing.expectError(PushError.OutOfMemory, stack.push(1));
try testing.expectError(FixedPushError.Overflow, stack.pushAll(&.{1, 11, 11})); try testing.expectError(PushError.OutOfMemory, stack.pushAll(&.{1, 11, 11}));
stack.clear(); stack.clear();