Documentation ¶
Index ¶
- func DeleteWithLock[K comparable, V any](l sync.Locker, m map[K]V, key K)
- func KeysSorted[M ~map[K]V, K constraints.Ordered, V any](m M) []K
- func MaxKey[M ~map[K]V, K constraints.Ordered, V any](m M) K
- func MinKey[M ~map[K]V, K constraints.Ordered, V any](m M) K
- func MutexMapCompareAndDelete[K, V comparable](m *MutexMap[K, V], key K, old V) (deleted bool)
- func MutexMapCompareAndSwap[K, V comparable](m *MutexMap[K, V], key K, old, new V) bool
- func StoreWithLock[K comparable, V any](l sync.Locker, m map[K]V, key K, value V)
- func UnsyncedMapCompareAndDelete[K, V comparable](m *UnsyncedMap[K, V], key K, old V) (deleted bool)
- func UnsyncedMapCompareAndSwap[K, V comparable](m *UnsyncedMap[K, V], key K, old, new V) bool
- func ValuesSorted[M ~map[K]V, K comparable, V constraints.Ordered](m M) []V
- func ValuesSortedByKey[M ~map[K]V, K constraints.Ordered, V any](m M) []V
- type AppendMap
- func (m *AppendMap[K, V]) Len() int
- func (m *AppendMap[K, V]) Load(key K) (V, bool)
- func (m *AppendMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)
- func (m *AppendMap[K, V]) LoadOrZero(key K) V
- func (m *AppendMap[K, V]) Range(f func(key K, value V) bool)
- func (m *AppendMap[K, V]) Snapshot() map[K]V
- type MutexMap
- func (m *MutexMap[K, V]) Delete(key K)
- func (m *MutexMap[K, V]) Len() int
- func (m *MutexMap[K, V]) Load(key K) (V, bool)
- func (m *MutexMap[K, V]) LoadAndDelete(key K) (V, bool)
- func (m *MutexMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)
- func (m *MutexMap[K, V]) LoadOrZero(key K) V
- func (m *MutexMap[K, V]) Range(f func(key K, value V) bool)
- func (m *MutexMap[K, V]) Store(key K, value V)
- func (m *MutexMap[K, V]) Swap(key K, value V) (previous V, loaded bool)
- func (m *MutexMap[K, V]) WithLock(f func(m map[K]V))
- type SyncMap
- func (m *SyncMap[K, V]) CompareAndDelete(key K, old V) (deleted bool)
- func (m *SyncMap[K, V]) CompareAndSwap(key K, old, new V) bool
- func (m *SyncMap[K, V]) Delete(key K)
- func (m *SyncMap[K, V]) Load(key K) (V, bool)
- func (m *SyncMap[K, V]) LoadAndDelete(key K) (V, bool)
- func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)
- func (m *SyncMap[K, V]) LoadOrZero(key K) V
- func (m *SyncMap[K, V]) Range(f func(key K, value V) bool)
- func (m *SyncMap[K, V]) Store(key K, value V)
- func (m *SyncMap[K, V]) Swap(key K, value V) (previous V, loaded bool)
- type UnsyncedMap
- func (m *UnsyncedMap[K, V]) Delete(key K)
- func (m *UnsyncedMap[K, V]) Len() int
- func (m *UnsyncedMap[K, V]) Load(key K) (V, bool)
- func (m *UnsyncedMap[K, V]) LoadAndDelete(key K) (V, bool)
- func (m *UnsyncedMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)
- func (m *UnsyncedMap[K, V]) LoadOrZero(key K) V
- func (m *UnsyncedMap[K, V]) Range(f func(key K, value V) bool)
- func (m *UnsyncedMap[K, V]) Store(key K, value V)
- func (m *UnsyncedMap[K, V]) Swap(key K, value V) (previous V, loaded bool)
- func (m *UnsyncedMap[K, V]) WithLock(f func(m map[K]V))
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeleteWithLock ¶ added in v0.4.0
func DeleteWithLock[K comparable, V any](l sync.Locker, m map[K]V, key K)
DeleteWithLock grabs l and then calls delete(m, key). Useful one-liner for in a defer.
func KeysSorted ¶ added in v0.2.0
func KeysSorted[M ~map[K]V, K constraints.Ordered, V any](m M) []K
KeysSorted gets the keys of the given map, sorts them and returns them. KeysSorted may fail to sort correctly when sorting slices of floating-point numbers containing Not-a-number (NaN) values.
func MaxKey ¶
func MaxKey[M ~map[K]V, K constraints.Ordered, V any](m M) K
MaxKey returns the highest key of the map m. It panics when m is empty.
func MinKey ¶
func MinKey[M ~map[K]V, K constraints.Ordered, V any](m M) K
MinKey returns the lowest key of the map m. It panics when m is empty.
func MutexMapCompareAndDelete ¶
func MutexMapCompareAndDelete[K, V comparable](m *MutexMap[K, V], key K, old V) (deleted bool)
CompareAndDelete deletes the entry for key if its value is equal to old.
If there is no current value for key in the map, CompareAndDelete returns false.
This is a function rather than a method because Go 1.18 doesn't allow restricting a method's type parameters more than the base type (yet?).
func MutexMapCompareAndSwap ¶
func MutexMapCompareAndSwap[K, V comparable](m *MutexMap[K, V], key K, old, new V) bool
CompareAndSwap swaps the old and new values for key if the value stored in the map is equal to old. The old value must be of a comparable type.
This is a function rather than a method because Go 1.18 doesn't allow restricting a method's type parameters more than the base type (yet?).
func StoreWithLock ¶ added in v0.4.0
func StoreWithLock[K comparable, V any](l sync.Locker, m map[K]V, key K, value V)
StoreWithLock grabs l and then does m[key] = value. Useful one-liner for in a defer.
func UnsyncedMapCompareAndDelete ¶
func UnsyncedMapCompareAndDelete[K, V comparable](m *UnsyncedMap[K, V], key K, old V) (deleted bool)
CompareAndDelete deletes the entry for key if its value is equal to old.
If there is no current value for key in the map, CompareAndDelete returns false.
This is a function rather than a method because Go 1.18 doesn't allow restricting a method's type parameters more than the base type (yet?).
func UnsyncedMapCompareAndSwap ¶
func UnsyncedMapCompareAndSwap[K, V comparable](m *UnsyncedMap[K, V], key K, old, new V) bool
CompareAndSwap swaps the old and new values for key if the value stored in the map is equal to old. The old value must be of a comparable type.
This is a function rather than a method because Go 1.18 doesn't allow restricting a method's type parameters more than the base type (yet?).
func ValuesSorted ¶ added in v0.2.0
func ValuesSorted[M ~map[K]V, K comparable, V constraints.Ordered](m M) []V
ValuesSorted gets the values of the given map, sorts them and returns them. ValuesSorted may fail to sort correctly when sorting slices of floating-point numbers containing Not-a-number (NaN) values.
func ValuesSortedByKey ¶ added in v0.12.0
func ValuesSortedByKey[M ~map[K]V, K constraints.Ordered, V any](m M) []V
ValuesSortedByKey gets the values of the given map, sorts them by their key and returns them. ValuesSortedByKey may fail to sort correctly when sorting slices of floating-point numbers containing Not-a-number (NaN) values.
Types ¶
type AppendMap ¶ added in v0.10.0
type AppendMap[K comparable, V any] struct { // contains filtered or unexported fields }
AppendMap is a concurrency-safe append-only map. Its interface resembles a subset of sync.Map. Loads on existing values only need an atomic read. Loads of non-existent and recently stored values pick up a mutex. Stores pick up a mutex. The zero value is valid.
func (*AppendMap[K, V]) Load ¶ added in v0.10.0
Load returns the value stored in the map for a key. The ok result indicates whether value was found in the map.
func (*AppendMap[K, V]) LoadOrStore ¶ added in v0.10.0
LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.
func (*AppendMap[K, V]) LoadOrZero ¶ added in v0.10.0
func (m *AppendMap[K, V]) LoadOrZero(key K) V
LoadOrZero returns the value stored in the map for a key, or zero if no value is present. This is the same as Load() but ignoring the second result.
func (*AppendMap[K, V]) Range ¶ added in v0.10.0
Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.
Range does not block other methods on the receiver; even f itself may call any method on m.
Range iterates over a consistent copy of the map, snapshotted before the first callback.
func (*AppendMap[K, V]) Snapshot ¶ added in v0.11.0
func (m *AppendMap[K, V]) Snapshot() map[K]V
Snapshot returns a read-only map[K]V. Any writes that have happened before are reflected. It should not be modified if the AppendMap could be used in the future. Future calls to the AppendMap will not modify the returned map.
type MutexMap ¶
type MutexMap[K comparable, V any] struct { L sync.Mutex M map[K]V }
MutexMap is a map protected with a mutex. Its interface closely resembles sync.Map. The zero value is valid.
func (*MutexMap[K, V]) Delete ¶
func (m *MutexMap[K, V]) Delete(key K)
Delete deletes the value for a key.
func (*MutexMap[K, V]) Load ¶
Load returns the value stored in the map for a key. The ok result indicates whether value was found in the map.
func (*MutexMap[K, V]) LoadAndDelete ¶
LoadAndDelete deletes the value for a key, returning the previous value if any. The second result reports whether the key was present.
func (*MutexMap[K, V]) LoadOrStore ¶
LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.
func (*MutexMap[K, V]) LoadOrZero ¶
func (m *MutexMap[K, V]) LoadOrZero(key K) V
LoadOrZero returns the value stored in the map for a key, or zero if no value is present. This is the same as Load() but ignoring the second result.
func (*MutexMap[K, V]) Range ¶
Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.
Range does not block other methods on the receiver; even f itself may call any method on m.
Range repeatedly picks up and drops the mutex so f() won't be called with the mutex held. Use WithLock if you need more performance at the cost of blocking other users.
func (*MutexMap[K, V]) Store ¶
func (m *MutexMap[K, V]) Store(key K, value V)
Store sets the value for a key.
type SyncMap ¶
type SyncMap[K comparable, V any] struct { // contains filtered or unexported fields }
func (*SyncMap[K, V]) CompareAndDelete ¶ added in v0.3.0
CompareAndDelete deletes the entry for key if its value is equal to old. The old value must be of a comparable type.
If there is no current value for key in the map, CompareAndDelete returns false (even if the old value is the nil interface value).
CompareAndDelete is only available from Go 1.20 as that is when Go added the sync.Map.CompareAndDelete method.
func (*SyncMap[K, V]) CompareAndSwap ¶ added in v0.3.0
CompareAndSwap swaps the old and new values for key if the value stored in the map is equal to old. The old value must be of a comparable type.
CompareAndSwap is only available from Go 1.20 as that is when Go added the sync.Map.CompareAndSwap method.
func (*SyncMap[K, V]) Delete ¶
func (m *SyncMap[K, V]) Delete(key K)
Delete deletes the value for a key.
func (*SyncMap[K, V]) Load ¶
Load returns the value stored in the map for a key. The ok result indicates whether value was found in the map.
func (*SyncMap[K, V]) LoadAndDelete ¶
LoadAndDelete deletes the value for a key, returning the previous value if any. The second result reports whether the key was present.
func (*SyncMap[K, V]) LoadOrStore ¶
LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.
func (*SyncMap[K, V]) LoadOrZero ¶
func (m *SyncMap[K, V]) LoadOrZero(key K) V
Load returns the value stored in the map for a key, or zero if no value is present. This is the same as Load() but ignoring the second result.
func (*SyncMap[K, V]) Range ¶
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 Map's contents: no key will be visited more than once, but if the value for any key is stored or deleted concurrently (including by f), Range may reflect any mapping for that key from any point during the Range call. Range does not block other methods on the receiver; even f itself may call any method on m.
Range may be O(N) with the number of elements in the map even if f returns false after a constant number of calls.
func (*SyncMap[K, V]) Store ¶
func (m *SyncMap[K, V]) Store(key K, value V)
Store sets the value for a key.
type UnsyncedMap ¶
type UnsyncedMap[K comparable, V any] struct { M map[K]V }
UnsyncedMap is a map protected with a mutex. Its interface closely resembles sync.Map. It's indended as a drop-in replacement for MutexMap and SyncMap when synchronization is no longer needed (but might become so in the future). The zero value is valid.
func (*UnsyncedMap[K, V]) Delete ¶
func (m *UnsyncedMap[K, V]) Delete(key K)
Delete deletes the value for a key.
func (*UnsyncedMap[K, V]) Len ¶
func (m *UnsyncedMap[K, V]) Len() int
Len returns the number of elements in the map.
func (*UnsyncedMap[K, V]) Load ¶
func (m *UnsyncedMap[K, V]) Load(key K) (V, bool)
Load returns the value stored in the map for a key. The ok result indicates whether value was found in the map.
func (*UnsyncedMap[K, V]) LoadAndDelete ¶
func (m *UnsyncedMap[K, V]) LoadAndDelete(key K) (V, bool)
LoadAndDelete deletes the value for a key, returning the previous value if any. The second result reports whether the key was present.
func (*UnsyncedMap[K, V]) LoadOrStore ¶
func (m *UnsyncedMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)
LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.
func (*UnsyncedMap[K, V]) LoadOrZero ¶
func (m *UnsyncedMap[K, V]) LoadOrZero(key K) V
Load returns the value stored in the map for a key, or zero if no value is present. This is the same as Load() but ignoring the second result.
func (*UnsyncedMap[K, V]) Range ¶
func (m *UnsyncedMap[K, V]) Range(f func(key K, value V) bool)
Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.
func (*UnsyncedMap[K, V]) Store ¶
func (m *UnsyncedMap[K, V]) Store(key K, value V)
Store sets the value for a key.
func (*UnsyncedMap[K, V]) Swap ¶
func (m *UnsyncedMap[K, V]) Swap(key K, value V) (previous V, loaded bool)
Swap swaps the value for a key and returns the previous value if any. The loaded result reports whether the key was present.
func (*UnsyncedMap[K, V]) WithLock ¶
func (m *UnsyncedMap[K, V]) WithLock(f func(m map[K]V))
WithLock calls f. The name is based on the MutexMap method, but UnsyncedMap has no mutex.