github.com/fufuok/utils@v1.0.10/xsync/mapof_helper.go (about) 1 //go:build go1.18 2 // +build go1.18 3 4 package xsync 5 6 type HashMapOf[K comparable, V any] interface { 7 // Load returns the value stored in the map for a key, or nil if no 8 // value is present. 9 // The ok result indicates whether value was found in the map. 10 Load(key K) (value V, ok bool) 11 12 // Store sets the value for a key. 13 Store(key K, value V) 14 15 // LoadOrStore returns the existing value for the key if present. 16 // Otherwise, it stores and returns the given value. 17 // The loaded result is true if the value was loaded, false if stored. 18 LoadOrStore(key K, value V) (actual V, loaded bool) 19 20 // LoadAndStore returns the existing value for the key if present, 21 // while setting the new value for the key. 22 // It stores the new value and returns the existing one, if present. 23 // The loaded result is true if the existing value was loaded, 24 // false otherwise. 25 LoadAndStore(key K, value V) (actual V, loaded bool) 26 27 // LoadOrCompute returns the existing value for the key if present. 28 // Otherwise, it computes the value using the provided function and 29 // returns the computed value. The loaded result is true if the value 30 // was loaded, false if stored. 31 LoadOrCompute(key K, valueFn func() V) (actual V, loaded bool) 32 33 // Compute either sets the computed new value for the key or deletes 34 // the value for the key. When the delete result of the valueFn function 35 // is set to true, the value will be deleted, if it exists. When delete 36 // is set to false, the value is updated to the newValue. 37 // The ok result indicates whether value was computed and stored, thus, is 38 // present in the map. The actual result contains the new value in cases where 39 // the value was computed and stored. See the example for a few use cases. 40 Compute( 41 key K, 42 valueFn func(oldValue V, loaded bool) (newValue V, delete bool), 43 ) (actual V, ok bool) 44 45 // LoadAndDelete deletes the value for a key, returning the previous 46 // value if any. The loaded result reports whether the key was 47 // present. 48 LoadAndDelete(key K) (value V, loaded bool) 49 50 // Delete deletes the value for a key. 51 Delete(key K) 52 53 // Range calls f sequentially for each key and value present in the 54 // map. If f returns false, range stops the iteration. 55 // 56 // Range does not necessarily correspond to any consistent snapshot 57 // of the Map's contents: no key will be visited more than once, but 58 // if the value for any key is stored or deleted concurrently, Range 59 // may reflect any mapping for that key from any point during the 60 // Range call. 61 // 62 // It is safe to modify the map while iterating it. However, the 63 // concurrent modification rule apply, i.e. the changes may be not 64 // reflected in the subsequently iterated entries. 65 Range(f func(key K, value V) bool) 66 67 // Clear deletes all keys and values currently stored in the map. 68 Clear() 69 70 // Size returns current size of the map. 71 Size() int 72 }