-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.go
186 lines (149 loc) · 2.78 KB
/
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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package goforth
import (
"log"
)
type Stack[T comparable] struct {
data []T
}
func NewStack[T comparable]() *Stack[T] {
stack := new(Stack[T])
stack.data = make([]T, 0, 100)
return stack
}
func (s *Stack[T]) Len() int {
return len(s.data)
}
func (s *Stack[T]) IsEmpty() bool {
return s.Len() == 0
}
func (s *Stack[T]) Push(val T) {
s.data = append(s.data, val)
}
func (s *Stack[T]) Pop() (T, bool) {
var zero T
if s.IsEmpty() {
return zero, false
}
index := s.Len() - 1
element := s.data[index]
s.data[index] = zero
s.data = s.data[:index]
return element, true
}
func (s *Stack[T]) ExPop() T {
value, ok := s.Pop()
if !ok {
log.Fatal("Error: Pop() from empty Stack")
}
return value
}
func (s *Stack[T]) Fetch() (T, bool) {
if s.IsEmpty() {
var zero T
return zero, false
}
index := s.Len() - 1
element := s.data[index]
return element, true
}
func (s *Stack[T]) ExFetch() T {
value, ok := s.Fetch()
if !ok {
log.Fatal("Error: Fetch() from empty Stack")
}
return value
}
func (s *Stack[T]) Reverse() *Stack[T] {
result := NewStack[T]()
for s.Len() > 0 {
if value, ok := s.Pop(); ok {
result.Push(value)
}
}
return result
}
/*
func (s *Stack) Append(stk *Stack) {
nstk := stk.Reverse()
for nstk.Len() > 0 {
if value, ok := nstk.Pop(); ok {
s.Push(value)
}
}
}
*/
type StackIter[T comparable] struct {
stack *Stack[T]
len int
index int
}
func (s *Stack[T]) Iter() *StackIter[T] {
return &StackIter[T]{stack: s, len: s.Len(), index: -1}
}
func (s *StackIter[T]) Next() bool {
if s.index < s.len-1 {
s.index++
return true
}
return false
}
func (s *StackIter[T]) Get() T {
return s.stack.data[s.index]
}
func (s *Stack[T]) Contains(val T) bool {
for _, i := range s.data {
if i == val {
return true
}
}
return false
}
func (s *Stack[T]) GetIndex(val T) int {
for pos, i := range s.data {
if i == val {
return pos
}
}
return -1
}
func (s *Stack[T]) Each(f func(value T)) {
for iter := s.Iter(); iter.Next(); {
f(iter.Get())
}
}
// -------------------- SliceStack ----------------------------
type SliceStack[T comparable] []*Stack[T]
func (ss *SliceStack[T]) Contains(val T) bool {
for _, i := range *ss {
if i.Contains(val) {
return true
}
}
return false
}
func (ss *SliceStack[T]) Len() int {
return len(*ss)
}
func (ss *SliceStack[T]) IsEmpty() bool {
return ss.Len() == 0
}
func (ss *SliceStack[T]) Push(stk *Stack[T]) {
*ss = append(*ss, stk)
}
func (ss *SliceStack[T]) Pop() (*Stack[T], bool) {
if ss.IsEmpty() {
return nil, false
}
index := ss.Len() - 1
element := (*ss)[index]
(*ss)[index] = nil
*ss = (*ss)[:index]
return element, true
}
func (ss *SliceStack[T]) ExPop() *Stack[T] {
value, ok := ss.Pop()
if !ok {
log.Fatal("Error: Pop() from empty SliceStack")
}
return value
}