aboutsummaryrefslogtreecommitdiff
path: root/pkg/pointer/stack-stack.go
blob: 404542c99d6ee45cdebab7dcc81608e4c870cff4 (plain)
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
package pointer

type StackStack struct {
	head   *Stack
	height int
}

func NewStackStack() *StackStack {
	return &StackStack{
		head:   NewStack(),
		height: 1,
	}
}

func (ss *StackStack) Begin(p *Pointer) {
	ss.height++
	soss := ss.head
	n := soss.Pop()
	np := n
	if np < 0 {
		np = -np
	}
	toss := &Stack{
		size: np,
		data: make([]int, np),
		next: soss,
	}
	ss.head = toss
	if n > 0 {
		toss.height = n
		elts := soss.height - n
		if elts < 0 {
			elts = soss.height
		} else {
			elts = n
		}
		for i := 1; i <= elts; i++ {
			toss.data[toss.height-i] = soss.data[soss.height-i]
		}
		soss.height -= elts
	} else if n < 0 {
		for i := 0; i < np; i++ {
			soss.Push(0)
		}
	}
	x, y := p.GetStorageOffset()
	soss.Push(x)
	soss.Push(y)
	p.CalculateNewStorageOffset()
}

func (ss *StackStack) End(p *Pointer) (reflect bool) {
	if ss.height == 1 {
		return true
	}
	soss := ss.head.next
	n := ss.head.Pop()
	y := soss.Pop()
	x := soss.Pop()
	p.SetStorageOffset(x, y)
	if n > 0 {
		if n > ss.head.height {
			for i := n; i > ss.head.height; i-- {
				soss.Push(0)
			}
			n = ss.head.height
		}
		soss.height += n
		if soss.size < soss.height {
			soss.data = append(soss.data, make([]int, soss.height-soss.size)...)
			soss.size = soss.height
		}
		for i := n; i > 0; i-- {
			soss.data[soss.height-i] = ss.head.data[ss.head.height-i]
		}
	} else if n < 0 {
		soss.height += n
		if soss.height < 0 {
			soss.height = 0
		}
	}
	ss.height--
	ss.head = ss.head.next
	return false
}

func (ss *StackStack) Under() (reflect bool) {
	if ss.height == 1 {
		return true
	}
	n := ss.head.Pop()
	if n > 0 {
		for i := 0; i < n; i++ {
			ss.head.Push(ss.head.next.Pop())
		}
	} else {
		for i := 0; i < -n; i++ {
			ss.head.next.Push(ss.head.Pop())
		}
	}
	return false
}