Add initialization functions for stack data structures

This commit is contained in:
kayomn 2022-09-22 21:49:06 +01:00
parent 0bd2c6a638
commit 26ccb4cbe0
1 changed files with 22 additions and 2 deletions

24
src/stack.zig Normal file → Executable file
View File

@ -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.