forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
169 lines (145 loc) · 2.95 KB
/
map.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
package goja
import (
"hash/maphash"
)
type mapEntry struct {
key, value Value
iterPrev, iterNext *mapEntry
hNext *mapEntry
}
type orderedMap struct {
hash *maphash.Hash
hashTable map[uint64]*mapEntry
iterFirst, iterLast *mapEntry
size int
}
type orderedMapIter struct {
m *orderedMap
cur *mapEntry
}
func (m *orderedMap) lookup(key Value) (h uint64, entry, hPrev *mapEntry) {
if key == _negativeZero {
key = intToValue(0)
}
h = key.hash(m.hash)
for entry = m.hashTable[h]; entry != nil && !entry.key.SameAs(key); hPrev, entry = entry, entry.hNext {
}
return
}
func (m *orderedMap) set(key, value Value) {
h, entry, hPrev := m.lookup(key)
if entry != nil {
entry.value = value
} else {
if key == _negativeZero {
key = intToValue(0)
}
entry = &mapEntry{key: key, value: value}
if hPrev == nil {
m.hashTable[h] = entry
} else {
hPrev.hNext = entry
}
if m.iterLast != nil {
entry.iterPrev = m.iterLast
m.iterLast.iterNext = entry
} else {
m.iterFirst = entry
}
m.iterLast = entry
m.size++
}
}
func (m *orderedMap) get(key Value) Value {
_, entry, _ := m.lookup(key)
if entry != nil {
return entry.value
}
return nil
}
func (m *orderedMap) remove(key Value) bool {
h, entry, hPrev := m.lookup(key)
if entry != nil {
entry.key = nil
entry.value = nil
// remove from the doubly-linked list
if entry.iterPrev != nil {
entry.iterPrev.iterNext = entry.iterNext
} else {
m.iterFirst = entry.iterNext
}
if entry.iterNext != nil {
entry.iterNext.iterPrev = entry.iterPrev
} else {
m.iterLast = entry.iterPrev
}
// remove from the hashTable
if hPrev == nil {
if entry.hNext == nil {
delete(m.hashTable, h)
} else {
m.hashTable[h] = entry.hNext
}
} else {
hPrev.hNext = entry.hNext
}
m.size--
return true
}
return false
}
func (m *orderedMap) has(key Value) bool {
_, entry, _ := m.lookup(key)
return entry != nil
}
func (iter *orderedMapIter) next() *mapEntry {
if iter.m == nil {
// closed iterator
return nil
}
cur := iter.cur
// if the current item was deleted, track back to find the latest that wasn't
for cur != nil && cur.key == nil {
cur = cur.iterPrev
}
if cur != nil {
cur = cur.iterNext
} else {
cur = iter.m.iterFirst
}
if cur == nil {
iter.close()
} else {
iter.cur = cur
}
return cur
}
func (iter *orderedMapIter) close() {
iter.m = nil
iter.cur = nil
}
func newOrderedMap(h *maphash.Hash) *orderedMap {
return &orderedMap{
hash: h,
hashTable: make(map[uint64]*mapEntry),
}
}
func (m *orderedMap) newIter() *orderedMapIter {
iter := &orderedMapIter{
m: m,
}
return iter
}
func (m *orderedMap) clear() {
for item := m.iterFirst; item != nil; item = item.iterNext {
item.key = nil
item.value = nil
if item.iterPrev != nil {
item.iterPrev.iterNext = nil
}
}
m.iterFirst = nil
m.iterLast = nil
m.hashTable = make(map[uint64]*mapEntry)
m.size = 0
}