separate core from cli into submodules, add support for boolean and and or operations, make more fields public, fix passing arguments to functions
This commit is contained in:
parent
7498c85424
commit
62656c6dff
19 changed files with 202 additions and 99 deletions
55
core/stack.go
Normal file
55
core/stack.go
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
package core
|
||||
|
||||
type Stack[T any] struct {
|
||||
Current Pos
|
||||
Size Pos
|
||||
|
||||
items []T
|
||||
}
|
||||
|
||||
func NewStack[T any](size Pos) *Stack[T] {
|
||||
return &Stack[T]{
|
||||
items: make([]T, size),
|
||||
Size: size,
|
||||
Current: 0,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Stack[T]) Push(items ...T) {
|
||||
for _, item := range items {
|
||||
if s.Current >= s.Size {
|
||||
panic("stack overflow")
|
||||
}
|
||||
|
||||
s.items[s.Current] = item
|
||||
s.Current++
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Stack[T]) Pop() T {
|
||||
if s.Current <= 0 {
|
||||
panic("stack underflow")
|
||||
}
|
||||
|
||||
s.Current--
|
||||
return s.items[s.Current]
|
||||
}
|
||||
|
||||
func (s *Stack[T]) Peek() T {
|
||||
if s.Current <= 0 {
|
||||
panic("stack underflow")
|
||||
}
|
||||
|
||||
return s.items[s.Current-1]
|
||||
}
|
||||
|
||||
// check whether the stack is invalid (stack over-/underflow)
|
||||
func (s *Stack[T]) check() {
|
||||
if s.Current >= s.Size {
|
||||
panic("stack underflow")
|
||||
}
|
||||
|
||||
if s.Current < 0 {
|
||||
panic("stack underflow")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue