forked from cornelk/hashmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashmap_get.go
178 lines (151 loc) · 3.77 KB
/
hashmap_get.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
package hashmap
import (
"bytes"
"reflect"
"unsafe"
"github.com/dchest/siphash"
)
// Get retrieves an element from the map under given hash key.
// Using interface{} adds a performance penalty.
// Please consider using GetUintKey or GetStringKey instead.
func (m *HashMap) Get(key interface{}) (value interface{}, ok bool) {
h := getKeyHash(key)
data, element := m.indexElement(h)
if data == nil {
return nil, false
}
// inline HashMap.searchItem()
for element != nil {
if element.keyHash == h {
switch key.(type) {
case []byte:
if bytes.Compare(element.key.([]byte), key.([]byte)) == 0 {
return element.Value(), true
}
default:
if element.key == key {
return element.Value(), true
}
}
}
if element.keyHash > h {
return nil, false
}
element = element.Next()
}
return nil, false
}
// GetUintKey retrieves an element from the map under given integer key.
func (m *HashMap) GetUintKey(key uintptr) (value interface{}, ok bool) {
// inline getUintptrHash()
bh := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&key)),
Len: intSizeBytes,
Cap: intSizeBytes,
}
buf := *(*[]byte)(unsafe.Pointer(&bh))
h := uintptr(siphash.Hash(sipHashKey1, sipHashKey2, buf))
data, element := m.indexElement(h)
if data == nil {
return nil, false
}
// inline HashMap.searchItem()
for element != nil {
if element.keyHash == h && element.key == key {
return element.Value(), true
}
if element.keyHash > h {
return nil, false
}
element = element.Next()
}
return nil, false
}
// GetStringKey retrieves an element from the map under given string key.
func (m *HashMap) GetStringKey(key string) (value interface{}, ok bool) {
// inline getStringHash()
sh := (*reflect.StringHeader)(unsafe.Pointer(&key))
bh := reflect.SliceHeader{
Data: sh.Data,
Len: sh.Len,
Cap: sh.Len,
}
buf := *(*[]byte)(unsafe.Pointer(&bh))
h := uintptr(siphash.Hash(sipHashKey1, sipHashKey2, buf))
data, element := m.indexElement(h)
if data == nil {
return nil, false
}
// inline HashMap.searchItem()
for element != nil {
if element.keyHash == h && element.key == key {
return element.Value(), true
}
if element.keyHash > h {
return nil, false
}
element = element.Next()
}
return nil, false
}
// GetHashedKey retrieves an element from the map under given hashed key.
func (m *HashMap) GetHashedKey(hashedKey uintptr) (value interface{}, ok bool) {
data, element := m.indexElement(hashedKey)
if data == nil {
return nil, false
}
// inline HashMap.searchItem()
for element != nil {
if element.keyHash == hashedKey {
return element.Value(), true
}
if element.keyHash > hashedKey {
return nil, false
}
element = element.Next()
}
return nil, false
}
// GetOrInsert returns the existing value for the key if present.
// Otherwise, it stores and returns the given value.
// The loaded result is true if the value was loaded, false if stored.
func (m *HashMap) GetOrInsert(key interface{}, value interface{}) (actual interface{}, loaded bool) {
h := getKeyHash(key)
var newelement *ListElement
for {
data, element := m.indexElement(h)
if data == nil {
m.allocate(DefaultSize)
continue
}
for element != nil {
if element.keyHash == h {
switch key.(type) {
case []byte:
if bytes.Compare(element.key.([]byte), key.([]byte)) == 0 {
return element.Value(), true
}
default:
if element.key == key {
actual = element.Value()
return actual, true
}
}
}
if element.keyHash > h {
break
}
element = element.Next()
}
if newelement == nil { // allocate only once
newelement = &ListElement{
key: key,
keyHash: h,
value: unsafe.Pointer(&value),
}
}
if m.insertListElement(newelement, false) {
return value, false
}
}
}