-
Notifications
You must be signed in to change notification settings - Fork 9
/
stack.go
154 lines (137 loc) · 2.38 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
package compiler
type (
stack struct {
*stackEntry
}
stackEntry struct {
str string
prev *stackEntry
}
)
func (stk stack) isEmpty() bool {
return stk.stackEntry == nil
}
func (stk stack) top() string {
if stk.isEmpty() {
return ""
}
return stk.str
}
func (stk stack) add(str string) stack {
e := &stackEntry{
str: str,
prev: stk.stackEntry,
}
return stack{e}
}
func (stk stack) addFromStack(other stack) stack {
if other.isEmpty() {
return stk
}
res := stk.addFromStack(other.drop())
return res.add(other.top())
}
func (stk stack) drop() stack {
if !stk.isEmpty() {
stk = stack{stk.prev}
}
return stk
}
func (stk stack) dropN(n int) stack {
for n > 0 {
stk = stk.drop()
n--
}
return stk
}
func (stk stack) recurFind(str string) int {
if stk.isEmpty() {
return -1
}
if stk.str == str {
return 0
}
res := stk.drop().recurFind(str)
if res < 0 {
return res
}
return res + 1
}
func (stk stack) count() map[string]int {
if stk.isEmpty() {
return nil
}
stackCounts := make(map[string]int)
for {
stackCounts[stk.str]++
stk = stack{stk.prev}
if stk.stackEntry == nil {
break
}
}
return stackCounts
}
func (stk stack) find(str string) int {
stackCounts := stk.count()
if stk.isEmpty() || stackCounts[str] == 0 {
return -1
}
var pos int
for {
if stk.str == str {
if stackCounts[str] == 1 {
break
}
stackCounts[str]--
}
stk = stack{stk.prev}
if stk.stackEntry == nil {
break
}
pos++
}
return pos
}
func (stk stack) roll(n int) stack {
var x func(stack, int) (stack, string)
x = func(stk stack, n int) (stack, string) {
if n == 0 {
return stk.drop(), stk.top()
}
stk2, entry := x(stk.drop(), n-1)
return stk2.add(stk.top()), entry
}
stk, entry := x(stk, n)
return stk.add(entry)
}
func (stk stack) swap() stack {
a := stk.top()
stk = stk.drop()
b := stk.top()
stk = stk.drop()
return stk.add(a).add(b)
}
func (stk stack) dup() stack {
return stk.add(stk.top())
}
func (stk stack) over() stack {
t := stk.drop().top()
return stk.add(t)
}
func (stk stack) pick(n int) stack {
t := stk.dropN(n).top()
return stk.add(t)
}
func (stk stack) String() string {
if stk.stackEntry == nil {
return "[]"
}
var x func(stk stack) string
x = func(stk stack) string {
if stk.stackEntry == nil {
return ""
}
return x(stk.drop()) + " " + stk.stackEntry.str
}
return "[..." + x(stk) + "]"
}