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

Method insertElement

hashmap.go:245–274  ·  view source on GitHub ↗
(element *ListElement[Key, Value], hash uintptr, key Key, value Value)

Source from the content-addressed store, hash-verified

243}
244
245func (m *Map[Key, Value]) insertElement(element *ListElement[Key, Value], hash uintptr, key Key, value Value) bool {
246 var existed, inserted bool
247
248 for {
249 store := m.store.Load()
250 searchStart := store.item(element.keyHash)
251
252 if !inserted { // if retrying after insert during grow, do not add to list again
253 _, existed, inserted = m.linkedList.Add(searchStart, hash, key, value)
254 if existed {
255 return false
256 }
257
258 if !inserted {
259 continue // a concurrent add did interfere, try again
260 }
261 }
262
263 count := store.addItem(element)
264 currentStore := m.store.Load()
265 if store != currentStore { // retry insert in case of insert during grow
266 continue
267 }
268
269 if m.isResizeNeeded(store, count) && m.resizing.CompareAndSwap(0, 1) {
270 go m.grow(0, true)
271 }
272 return true
273 }
274}
275
276// deleteElement deletes an element from index.
277func (m *Map[Key, Value]) deleteElement(element *ListElement[Key, Value]) {

Callers 1

GetOrInsertMethod · 0.95

Calls 5

isResizeNeededMethod · 0.95
growMethod · 0.95
itemMethod · 0.80
AddMethod · 0.80
addItemMethod · 0.80

Tested by

no test coverage detected