-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookup.go
110 lines (93 loc) · 1.91 KB
/
lookup.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
package goatlang
type lookup struct {
keyToIndex map[string]int
indexToKey []string
data []Value
cap int
}
func newLookup() *lookup {
return &lookup{
keyToIndex: map[string]int{},
}
}
func (l *lookup) Len() int {
return len(l.data)
}
func (l *lookup) Cap() int {
return l.cap
}
func (l *lookup) Read(index int) Value {
return l.data[index]
}
func (l *lookup) Write(index int, v Value) {
l.data[index] = v
}
func (l *lookup) Assign(index int, v Value) {
l.data[index] = v.assign(l.data[index].t)
}
func (l *lookup) Set(key string, v Value) {
n := l.Index(key)
l.data[n] = v
}
func (l *lookup) Get(key string) Value {
n := l.Index(key)
return l.data[n]
}
// Index returns the index of the key, creating it if required
func (l *lookup) Index(key string) int {
n, ok := l.keyToIndex[key]
if ok {
return n
}
n = int(len(l.data))
l.data = append(l.data, Value{})
l.indexToKey = append(l.indexToKey, key)
if len(l.data) > l.cap {
l.cap = len(l.data)
}
l.keyToIndex[key] = n
return n
}
func (l *lookup) shadow(key string) {
if n, ok := l.keyToIndex[key]; ok {
l.shadow("~" + key)
l.keyToIndex["~"+key] = n
delete(l.keyToIndex, key)
}
}
func (l *lookup) unshadow(key string) {
if n, ok := l.keyToIndex["~"+key]; ok {
l.keyToIndex[key] = n
l.unshadow("~" + key)
}
}
func (l *lookup) Shadow(key string) int {
l.shadow(key)
return l.Index(key)
}
// func (l *lookup) Pop() {
// n := len(l.data) - 1
// key := l.indexToKey[n]
// l.data = l.data[:n]
// l.indexToKey = l.indexToKey[:n]
// delete(l.keyToIndex, key)
// }
func (l *lookup) Drop(t int) {
for i := 1; i <= t; i++ {
n := len(l.data) - i
key := l.indexToKey[n]
if key == "" {
continue
}
delete(l.keyToIndex, key)
l.indexToKey[n] = ""
l.unshadow(key)
}
}
func (l *lookup) Exists(key string) bool {
_, ok := l.keyToIndex[key]
return ok
}
func (l *lookup) Key(index int) string {
return l.indexToKey[index]
}