-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_set.go
181 lines (159 loc) · 3.71 KB
/
value_set.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package runtime
import (
"container/list"
"fmt"
"reflect"
"sync"
)
type valueSet struct {
*ValueBase
rw sync.RWMutex
ma map[int64]*list.List
}
type ValueSet = *valueSet
func NewSet() ValueMap {
return &valueMap{
ValueBase: &ValueBase{},
ma: make(map[int64]*list.List),
}
}
func (m ValueSet) getHash(c *Context, key Value) (int64, bool) {
if h, ok := key.(CanHash); ok {
return h.Hash(), true
}
if h, ok := c.GetCallable(key.GetMember("hash", c)); ok {
c.Invoke(h, nil, NoArgs)
if r, ok := c.RetVal.(ValueInt); !ok {
c.RaiseRuntimeError("hash method returns an non-integer value!")
} else {
return r.Value(), true
}
}
return 0, false
}
func (m ValueSet) get(c *Context, key Value) (Value, bool) {
hash, ok := m.getHash(c, key)
if !ok {
c.RaiseRuntimeError("key %s is not hashable!", key.ToString(c))
}
m.rw.RLock()
defer m.rw.RUnlock()
if l, found := m.ma[hash]; found {
for p := l.Front(); p != nil; p = p.Next() {
pe := p.Value.(Value)
if c.ValuesEqual(key, pe) {
return key, true
}
}
}
return constUndefined, false
}
func (m ValueSet) set(c *Context, key Value, isDelete bool) {
hash, ok := m.getHash(c, key)
if !ok {
c.RaiseRuntimeError("key %s is not hashable!", key.ToString(c))
}
m.rw.Lock()
defer m.rw.Unlock()
if l, found := m.ma[hash]; found {
for p := l.Front(); p != nil; p = p.Next() {
pe := p.Value.(Value)
if c.ValuesEqual(key, pe) {
if isDelete {
l.Remove(p)
}
return
}
}
if !isDelete {
l.PushFront(key)
}
} else if !isDelete {
l = list.New()
l.PushBack(key)
m.ma[hash] = l
}
return
}
func (m ValueSet) Each(handle func(key, value Value) bool) {
m.rw.RLock()
defer m.rw.RUnlock()
for _, v := range m.ma {
for p := v.Front(); p != nil; p = p.Next() {
e := p.Value.(mapElem)
if !handle(e.key, e.value) {
return
}
}
}
}
// Implements Value
func (m ValueSet) CompareTo(other Value, c *Context) CompareResult {
return CompareResultNotEqual
}
func (m ValueSet) GetMember(name string, c *Context) Value {
return getMemberByType(c, m, name)
}
func (m ValueSet) GetIndex(int, *Context) Value {
return constUndefined
}
func (m ValueSet) Type() ValueType {
return TypeMap
}
func (m ValueSet) GoType() reflect.Type {
var vv map[interface{}]interface{}
return reflect.TypeOf(vv)
}
func (m ValueSet) ToGoValue(c *Context) interface{} {
vv := make(map[interface{}]interface{}, len(m.ma))
m.rw.RLock()
defer m.rw.RUnlock()
for _, v := range m.ma {
for p := v.Front(); p != nil; p = p.Next() {
e := p.Value.(mapElem)
vv[e.key.ToGoValue(c)] = e.key.ToGoValue(c)
}
}
return vv
}
func (m ValueSet) ToString(c *Context) string {
return fmt.Sprint(m.ToGoValue(c))
}
func (m ValueSet) IsTrue() bool {
return len(m.ma) > 0
}
// Implements CanLen
func (m ValueSet) Len() int { return len(m.ma) }
var builtinSetMethods = map[string]ValueCallable{
"load": NewNativeFunction("load", func(c *Context, this Value, args []Value) Value {
var key Value
EnsureFuncParams(c, "Map.load", args, ArgRuleRequired("key", TypeAny, &key))
m := this.(ValueMap)
if rv, found := m.get(c, key); found {
return rv
} else {
return constUndefined
}
}, "key"),
"store": NewNativeFunction("store", func(c *Context, this Value, args []Value) Value {
var key, value Value
EnsureFuncParams(c, "Map.store", args,
ArgRuleRequired("key", TypeAny, &key),
ArgRuleRequired("value", TypeAny, &value),
)
m := this.(ValueMap)
m.set(c, key, value)
return constUndefined
}, "key", "value"),
}
func init() {
addMembersAndStatics(TypeMap, builtinMapMethods)
}
// check builtin hashable types
var (
_ CanHash = NewInt(0)
_ CanHash = NewFloat(0)
_ CanHash = NewStr("")
_ CanHash = NewBool(false)
_ CanHash = TypeStr
)