-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
stack.go
52 lines (43 loc) · 786 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
45
46
47
48
49
50
51
52
package gojq
type stack struct {
data []block
index int
limit int
}
type block struct {
value any
next int
}
func newStack() *stack {
return &stack{index: -1, limit: -1}
}
func (s *stack) push(v any) {
b := block{v, s.index}
s.index = max(s.index, s.limit) + 1
if s.index < len(s.data) {
s.data[s.index] = b
} else {
s.data = append(s.data, b)
}
}
func (s *stack) pop() any {
b := s.data[s.index]
s.index = b.next
return b.value
}
func (s *stack) top() any {
return s.data[s.index].value
}
func (s *stack) empty() bool {
return s.index < 0
}
func (s *stack) save() (index, limit int) {
index, limit = s.index, s.limit
if s.index > s.limit {
s.limit = s.index
}
return
}
func (s *stack) restore(index, limit int) {
s.index, s.limit = index, limit
}