-
Notifications
You must be signed in to change notification settings - Fork 67
/
typevectortable.go
65 lines (53 loc) · 1.21 KB
/
typevectortable.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
package super
import "slices"
type TypeVectorTable struct {
types []typeVector
}
func NewTypeVectorTable() *TypeVectorTable {
return &TypeVectorTable{}
}
func (t *TypeVectorTable) Lookup(types []Type) int {
for k, typ := range t.types {
if typ.equal(types) {
return k
}
}
k := len(t.types)
t.types = append(t.types, newTypeVector(types))
return k
}
func (t *TypeVectorTable) LookupByValues(vals []Value) int {
for k, typ := range t.types {
if typ.equalToValues(vals) {
return k
}
}
k := len(t.types)
t.types = append(t.types, newTypeVectorFromValues(vals))
return k
}
func (t *TypeVectorTable) Types(id int) []Type {
return t.types[id]
}
func (t *TypeVectorTable) Length() int {
return len(t.types)
}
type typeVector []Type
func newTypeVector(in []Type) typeVector {
return slices.Clone(in)
}
func newTypeVectorFromValues(vals []Value) typeVector {
out := make(typeVector, 0, len(vals))
for _, val := range vals {
out = append(out, val.Type())
}
return out
}
func (t typeVector) equal(to []Type) bool {
return slices.Equal(t, to)
}
func (t typeVector) equalToValues(vals []Value) bool {
return slices.EqualFunc(t, vals, func(typ Type, val Value) bool {
return typ == val.Type()
})
}