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)
| 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. |
| 164 | func (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. |