Add initialization functions for stack data structures
This commit is contained in:
parent
0bd2c6a638
commit
26ccb4cbe0
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue