Created
February 4, 2025 11:32
-
-
Save lmariscal/4ee86264a5f03e85d46ed606df6c58fb to your computer and use it in GitHub Desktop.
Stack data structure for Jai
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Basic"; | |
Stack :: struct ($T: Type) { | |
data: [..] T; | |
} | |
free :: inline (stack: Stack) { | |
array_free(stack.data); | |
} | |
is_empty :: (stack: Stack) -> bool { | |
return stack.data.count == 0; | |
} | |
push :: (stack: *Stack($T), v: T) { | |
array_add(*stack.data, v); | |
} | |
pop :: (stack: *Stack($T)) -> T { | |
assert(stack.data.count > 0); | |
defer stack.data.count -= 1; | |
return stack.data[stack.data.count - 1]; | |
} | |
peek :: (stack: *Stack($T)) -> *T { | |
assert(stack.data.count > 0); | |
return *stack.data[stack.data.count - 1]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment