MCPcopy
hub / github.com/cornelk/hashmap / Set

Method Set

hashmap.go:164–187  ·  view source on GitHub ↗

Set sets the value under the specified key to the map. An existing item for this key will be overwritten. If a resizing operation is happening concurrently while calling Set, the item might show up in the map after the resize operation is finished.

(key Key, value Value)

Source from the content-addressed store, hash-verified

162// If a resizing operation is happening concurrently while calling Set, the item might show up in the map
163// after the resize operation is finished.
164func (m *Map[Key, Value]) Set(key Key, value Value) {
165 hash := m.hasher(key)
166
167 for {
168 store := m.store.Load()
169 searchStart := store.item(hash)
170
171 element, added := m.linkedList.AddOrUpdate(searchStart, hash, key, value)
172 if !added {
173 continue // a concurrent add did interfere, try again
174 }
175
176 count := store.addItem(element)
177 currentStore := m.store.Load()
178 if store != currentStore { // retry insert in case of insert during grow
179 continue
180 }
181
182 if m.isResizeNeeded(store, count) && m.resizing.CompareAndSwap(0, 1) {
183 go m.grow(0, true)
184 }
185 return
186 }
187}
188
189// Grow resizes the map to a new size, the size gets rounded up to next power of 2.
190// To double the size of the map use newSize 0.

Callers 15

TestSetStringFunction · 0.80
TestSetUint8Function · 0.80
TestSetInt16Function · 0.80
TestSetFloat32Function · 0.80
TestSetFloat64Function · 0.80
TestSetInt64Function · 0.80
TestResizeFunction · 0.80
TestStringerFunction · 0.80
TestDeleteFunction · 0.80
TestRangeFunction · 0.80
TestHashMap_parallelFunction · 0.80

Calls 5

isResizeNeededMethod · 0.95
growMethod · 0.95
itemMethod · 0.80
AddOrUpdateMethod · 0.80
addItemMethod · 0.80

Tested by 15

TestSetStringFunction · 0.64
TestSetUint8Function · 0.64
TestSetInt16Function · 0.64
TestSetFloat32Function · 0.64
TestSetFloat64Function · 0.64
TestSetInt64Function · 0.64
TestResizeFunction · 0.64
TestStringerFunction · 0.64
TestDeleteFunction · 0.64
TestRangeFunction · 0.64
TestHashMap_parallelFunction · 0.64