/
stack.go
104 lines (90 loc) · 2.27 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
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/
package neovm
import (
"OntologyWithPOC/vm/neovm/types"
)
const initStackCap = 16 // to avoid reallocation
type RandomAccessStack struct {
e []types.StackItems
}
func NewRandAccessStack() *RandomAccessStack {
var ras RandomAccessStack
ras.e = make([]types.StackItems, 0, initStackCap)
return &ras
}
func (r *RandomAccessStack) Count() int {
return len(r.e)
}
func (r *RandomAccessStack) Insert(index int, t types.StackItems) {
if t == nil {
return
}
l := len(r.e)
if index > l {
return
}
index = l - index
r.e = append(r.e, t)
copy(r.e[index+1:], r.e[index:])
r.e[index] = t
}
func (r *RandomAccessStack) Peek(index int) types.StackItems {
l := len(r.e)
if index >= l {
return nil
}
index = l - index
return r.e[index-1]
}
func (r *RandomAccessStack) Remove(index int) types.StackItems {
l := len(r.e)
if index >= l {
return nil
}
index = l - index
e := r.e[index-1]
r.e = append(r.e[:index-1], r.e[index:]...)
return e
}
func (r *RandomAccessStack) Set(index int, t types.StackItems) {
l := len(r.e)
if index >= l {
return
}
r.e[index] = t
}
func (r *RandomAccessStack) Push(t types.StackItems) {
r.e = append(r.e, t)
}
func (r *RandomAccessStack) Pop() types.StackItems {
var res types.StackItems
num := len(r.e)
if num > 0 {
res = r.e[num-1]
r.e = r.e[:num-1]
}
return res
}
func (r *RandomAccessStack) Swap(i, j int) {
l := len(r.e)
r.e[l-i-1], r.e[l-j-1] = r.e[l-j-1], r.e[l-i-1]
}
func (r *RandomAccessStack) CopyTo(stack *RandomAccessStack) {
stack.e = append(stack.e, r.e...)
}