/
lastack.go
173 lines (142 loc) · 2.75 KB
/
lastack.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
package lastack
import (
"github.com/474420502/focus/stack"
"github.com/davecgh/go-spew/spew"
)
type Node struct {
elements []interface{}
cur int
down *Node
}
type Stack struct {
top *Node
cache *Node
size uint
}
func assertImplementation() {
var _ stack.IStack = (*Stack)(nil)
}
func (as *Stack) grow() bool {
if as.top.cur+1 == cap(as.top.elements) {
var grownode *Node
if as.cache != nil {
grownode = as.cache
grownode.cur = -1
as.cache = nil
} else {
var growsize uint
if as.size <= 256 {
growsize = as.size << 1
} else {
growsize = 256 + as.size>>2
}
grownode = &Node{elements: make([]interface{}, growsize, growsize), cur: -1}
}
grownode.down = as.top
as.top = grownode
return true
}
return false
}
func (as *Stack) cacheRemove() bool {
if as.top.cur == 0 && as.top.down != nil {
as.cache = as.top
as.top = as.top.down
as.cache.down = nil
return true
}
return false
}
func New() *Stack {
s := &Stack{}
s.size = 0
s.top = &Node{elements: make([]interface{}, 8, 8), cur: -1}
return s
}
func NewWithCap(cap int) *Stack {
s := &Stack{}
s.size = 0
s.top = &Node{elements: make([]interface{}, cap, cap), cur: -1}
return s
}
func (as *Stack) Clear() {
as.size = 0
as.top.down = nil
as.top.cur = -1
}
func (as *Stack) Empty() bool {
return as.size == 0
}
func (as *Stack) Size() uint {
return as.size
}
// String 左为Top
func (as *Stack) String() string {
content := ""
cur := as.top
for ; cur != nil; cur = cur.down {
for i, _ := range cur.elements {
if cur.cur >= i {
content += spew.Sprint(cur.elements[cur.cur-i]) + " "
}
}
}
if len(content) > 0 {
content = content[0 : len(content)-1]
} else {
content = ""
}
return content
}
func (as *Stack) Values() []interface{} {
result := make([]interface{}, as.size, as.size)
cur := as.top
n := 0
for ; cur != nil; cur = cur.down {
for i, _ := range cur.elements {
if cur.cur >= i {
result[n] = cur.elements[cur.cur-i]
n++
}
}
}
return result
}
func (as *Stack) Index(idx int) (interface{}, bool) {
if idx < 0 {
return nil, false
}
cur := as.top
for cur != nil && idx-cur.cur > 0 {
idx = idx - cur.cur - 1
cur = cur.down
}
if cur == nil {
return nil, false
}
return cur.elements[cur.cur-idx], true
}
func (as *Stack) Push(v interface{}) {
as.grow()
as.top.cur++
as.top.elements[as.top.cur] = v
as.size++
}
func (as *Stack) Pop() (interface{}, bool) {
if as.size <= 0 {
return nil, false
}
as.size--
if as.cacheRemove() {
return as.cache.elements[as.cache.cur], true
}
epop := as.top.elements[as.top.cur]
as.top.cur--
return epop, true
}
func (as *Stack) Peek() (interface{}, bool) {
if as.size <= 0 {
return nil, false
}
return as.top.elements[as.top.cur], true
}