-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.go
73 lines (62 loc) · 1.24 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
package main
type PositionStack struct {
Positions []Position
}
type Position struct {
Depth int
Row int
}
func (pStack *PositionStack) IsEmpty() bool {
return pStack.Size() == 0
}
func (pStack *PositionStack) Size() int {
if &pStack.Positions == nil {
return 0
}
return len(pStack.Positions)
}
func (pStack *PositionStack) GetLast() *Position {
if pStack.IsEmpty() {
return &Position{
Depth: 0,
Row: 0,
}
}
return pStack.GetPosition(pStack.Size() - 1)
}
func (pStack *PositionStack) GetPosition(depth int) *Position {
if pStack.IsEmpty() {
return &Position{
Depth: 0,
Row: 0,
}
}
return &pStack.Positions[depth]
}
func (pStack *PositionStack) GetRow(depth int) int {
if pStack.IsEmpty() {
return 0
}
return pStack.GetPosition(depth).Row
}
func (pStack *PositionStack) SetRow(depth, row int) {
if pStack.IsEmpty() {
return
}
pStack.GetPosition(depth).Row = row
}
func (pStack *PositionStack) AddPosition(depth, row int) {
pStack.Push(Position{
Depth: depth,
Row: row,
})
}
func (pStack *PositionStack) Push(p Position) {
pStack.Positions = append(pStack.Positions, p)
}
func (pStack *PositionStack) Pop() {
if pStack.IsEmpty() {
return
}
pStack.Positions = pStack.Positions[:pStack.Size()-1]
}