From 26ccb4cbe0e6178329356105a16538eca3aa6ba8 Mon Sep 17 00:00:00 2001 From: kayomn Date: Thu, 22 Sep 2022 21:49:06 +0100 Subject: [PATCH] Add initialization functions for stack data structures --- src/stack.zig | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) mode change 100644 => 100755 src/stack.zig diff --git a/src/stack.zig b/src/stack.zig old mode 100644 new mode 100755 index 2335331..507bfe8 --- a/src/stack.zig +++ b/src/stack.zig @@ -18,8 +18,8 @@ pub const FinitePushError = error { /// pub fn Fixed(comptime Element: type, comptime capacity: usize) type { return struct { - filled: usize = 0, - buffer: [capacity]Element = undefined, + filled: usize, + buffer: [capacity]Element, const Self = @This(); @@ -57,6 +57,16 @@ pub fn Fixed(comptime Element: type, comptime capacity: usize) type { return self.filled; } + /// + /// Creates and returns a [Self] value. + /// + pub fn init() Self { + return Self{ + .filled = 0, + .buffer = undefined, + }; + } + /// /// Attempts to pop the tail-end of `self`, returning the element value or `null` if the /// stack is empty. @@ -137,6 +147,16 @@ pub fn Unmanaged(comptime Element: type) type { return self.filled; } + /// + /// Creates and returns a [Self] value wrapping `buffer` as its writable memory buffer. + /// + pub fn init(buffer: []Element) Self { + return Self{ + .filled = 0, + .buffer = buffer, + }; + } + /// /// Attempts to pop the tail-end of `self`, returning the element value or `null` if the /// stack is empty.