Put inserts a new key-value pair into the hashmap
(key, value any)
| 48 | |
| 49 | // Put inserts a new key-value pair into the hashmap |
| 50 | func (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 |
| 75 | func (hm *HashMap) Contains(key any) bool { |