blob: a0af4943c8336009be9320090152feabd3632af3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Paul Buetow
package rpn
import (
"fmt"
"slices"
)
// Stack represents an RPN stack of values.
type Stack struct {
values []StackValue
}
// NewStack creates a new empty stack.
func NewStack() *Stack {
return &Stack{
values: make([]StackValue, 0),
}
}
// Push adds a value to the top of the stack.
func (s *Stack) Push(val StackValue) {
s.values = append(s.values, val)
}
// Pop removes and returns the top value from the stack.
// Returns an error if the stack is empty.
func (s *Stack) Pop() (StackValue, error) {
if len(s.values) == 0 {
return nil, fmt.Errorf("stack is empty")
}
val := s.values[len(s.values)-1]
s.values = s.values[:len(s.values)-1]
return val, nil
}
// Peek returns the top value without removing it.
// Returns an error if the stack is empty.
func (s *Stack) Peek() (StackValue, error) {
if len(s.values) == 0 {
return nil, fmt.Errorf("stack is empty")
}
return s.values[len(s.values)-1], nil
}
// Len returns the number of values on the stack.
func (s *Stack) Len() int {
return len(s.values)
}
// Values returns a copy of all stack values (bottom-to-top order).
func (s *Stack) Values() []StackValue {
return slices.Clone(s.values)
}
// Clear removes all values from the stack.
// Note: This resets the slice to nil, releasing the underlying memory.
func (s *Stack) Clear() {
s.values = nil
}
|