-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.go
More file actions
86 lines (69 loc) · 1.89 KB
/
stack.go
File metadata and controls
86 lines (69 loc) · 1.89 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package stack
import (
"slices"
"github.com/sdsc-ordes/quitsh/pkg/debug"
)
type Stack[T any] struct {
stack []T
}
// NewStack creates a new stack with passing `capacity` argument
// to `make(T[],...)`.
func NewStack[T any]() Stack[T] {
return Stack[T]{}
}
// NewStack creates a new stack with passing `capacity` argument
// to `make(T[],...)`.
func NewStackWithCap[T any](capacity int) Stack[T] {
return Stack[T]{stack: make([]T, 0, capacity)}
}
// Pop pops the top element on the stack.
// The stack size needs to be greater > 0.
func (s *Stack[T]) Pop() T {
res := s.Top()
s.stack = s.stack[:len(s.stack)-1]
return res
}
// Returns the top element on the stack.
func (s *Stack[T]) Top() T {
debug.Assert(s.Len() != 0, "the stack size is not > 0")
return s.stack[len(s.stack)-1]
}
// PopFront pops the bottom level on the stack.
// This method is useful to do Breath-First-Traversal.
// instead of Depth-First-Traversal when using `Pop`.
func (s *Stack[T]) PopBottom() T {
res := s.Bottom()
s.stack = s.stack[1:]
return res
}
// Returns the bottom element.
func (s *Stack[T]) Bottom() T {
debug.Assert(s.Len() != 0, "the stack size is not > 0")
return s.stack[0]
}
// Visit travers the stack from top to bottom and applies a function.
// If the visitor returns `false` the iteration is aborted.
func (s *Stack[T]) Visit(visitor func(int, T) bool) {
for i, el := range slices.Backward(s.stack) {
if !visitor(i, el) {
return
}
}
}
// Visit travers the stack from bottom to top and applies a function.
// If the visitor returns `false` the iteration is aborted.
func (s *Stack[T]) VisitUpward(visitor func(int, T) bool) {
for i, el := range s.stack {
if !visitor(i, el) {
return
}
}
}
// Push appends an element to the stack.
func (s *Stack[T]) Push(t ...T) {
s.stack = append(s.stack, t...)
}
// Len returns the length of the stack.
func (s *Stack[T]) Len() int {
return len(s.stack)
}