Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration. Range does not necessarily correspond to any consistent snapshot of the MapOf's contents: no key will be visited more than once, but if the value for any key is stored or deleted con
(f func(key K, value V) bool)
| 316 | // Range may be O(N) with the number of elements in the map even if f returns |
| 317 | // false after a constant number of calls. |
| 318 | func (m *MapOf[K, V]) Range(f func(key K, value V) bool) { |
| 319 | // We need to be able to iterate over all of the keys that were already |
| 320 | // present at the start of the call to Range. |
| 321 | // If read.amended is false, then read.m satisfies that property without |
| 322 | // requiring us to hold m.mu for a long time. |
| 323 | read, _ := m.read.Load().(readOnly[K, V]) |
| 324 | if read.amended { |
| 325 | // m.dirty contains keys not in read.m. Fortunately, Range is already O(N) |
| 326 | // (assuming the caller does not break out early), so a call to Range |
| 327 | // amortizes an entire copy of the map: we can promote the dirty copy |
| 328 | // immediately! |
| 329 | m.mu.Lock() |
| 330 | read, _ = m.read.Load().(readOnly[K, V]) |
| 331 | if read.amended { |
| 332 | read = readOnly[K, V]{m: m.dirty} |
| 333 | m.read.Store(read) |
| 334 | m.dirty = nil |
| 335 | m.misses = 0 |
| 336 | } |
| 337 | m.mu.Unlock() |
| 338 | } |
| 339 | |
| 340 | for k, e := range read.m { |
| 341 | v, ok := e.load() |
| 342 | if !ok { |
| 343 | continue |
| 344 | } |
| 345 | if !f(k, v) { |
| 346 | break |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | // Values returns a slice of the values in the map. |
| 352 | func (m *MapOf[K, V]) Values() []V { |