-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstack.go
44 lines (36 loc) · 837 Bytes
/
stack.go
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
package edb
// IMPORTANT:
// Use Pointer as value type
// because slice allocates and copies when it excceeds the max capacity
type Stack[T any] struct {
Data []T
}
func (st *Stack[T]) Push(d T) {
st.Data = append(st.Data, d)
}
func (st *Stack[T]) PushN(ds ...T) {
st.Data = append(st.Data, ds...)
}
func (st *Stack[T]) Pop() (ret T) {
ret = st.Data[len(st.Data)-1]
st.Data = st.Data[:len(st.Data)-1]
return
}
func (st *Stack[T]) Len() int {
return len(st.Data)
}
func (st *Stack[T]) Swap(n int) {
st.Data[st.Len()-n], st.Data[st.Len()-1] = st.Data[st.Len()-1], st.Data[st.Len()-n]
}
func (st *Stack[T]) Dup(n int) {
st.Push(st.Data[st.Len()-n])
}
func (st *Stack[T]) PeekI(n int) *T {
return &st.Data[st.Len()-1-n]
}
func (st *Stack[T]) Peek() *T {
return st.PeekI(0)
}
func (st *Stack[T]) Clear() {
st.Data = nil
}