MCPcopy Index your code
hub / github.com/TheAlgorithms/Go / Put

Method Put

structure/hashmap/hashmap.go:50–72  ·  view source on GitHub ↗

Put inserts a new key-value pair into the hashmap

(key, value any)

Source from the content-addressed store, hash-verified

48
49// Put inserts a new key-value pair into the hashmap
50func (hm *HashMap) Put(key, value any) {
51 index := hm.hash(key)
52 if hm.table[index] == nil {
53 hm.table[index] = &node{key: key, value: value}
54 } else {
55 current := hm.table[index]
56 for {
57 if current.key == key {
58 current.value = value
59 return
60 }
61 if current.next == nil {
62 break
63 }
64 current = current.next
65 }
66 current.next = &node{key: key, value: value}
67 }
68 hm.size++
69 if float64(hm.size)/float64(hm.capacity) > 0.75 {
70 hm.resize()
71 }
72}
73
74// Contains checks if the given key is stored in the hashmap
75func (hm *HashMap) Contains(key any) bool {

Callers 2

resizeMethod · 0.95
TestHashMapFunction · 0.45

Calls 2

hashMethod · 0.95
resizeMethod · 0.95

Tested by 1

TestHashMapFunction · 0.36