Documentation ¶
Overview ¶
Package tsmap provides a collection of generic thread-safe map utility functions that can be safely used between multiple goroutines.
The provided functions are intended to simplify the process of working with maps in a thread-safe manner.
Index ¶
- func Delete[M ~map[K]V, K comparable, V any](mux threadsafe.Locker, m M, k K)
- func Filter[M ~map[K]V, K comparable, V any](mux threadsafe.RLocker, m M, f func(K, V) bool) M
- func Get[M ~map[K]V, K comparable, V any](mux threadsafe.RLocker, m M, k K) V
- func GetOK[M ~map[K]V, K comparable, V any](mux threadsafe.RLocker, m M, k K) (V, bool)
- func Invert[M ~map[K]V, K, V comparable](mux threadsafe.RLocker, m M) map[V]K
- func Len[M ~map[K]V, K comparable, V any](mux threadsafe.RLocker, m M) int
- func Map[M ~map[K]V, K, J comparable, V, U any](mux threadsafe.RLocker, m M, f func(K, V) (J, U)) map[J]U
- func Reduce[M ~map[K]V, K comparable, V, U any](mux threadsafe.RLocker, m M, init U, f func(K, V, U) U) U
- func Set[M ~map[K]V, K comparable, V any](mux threadsafe.Locker, m M, k K, v V)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Delete ¶ added in v1.82.0
func Delete[M ~map[K]V, K comparable, V any](mux threadsafe.Locker, m M, k K)
Delete is a thread-safe function to delete the key-value pair with the specified key from the given map.
func Filter ¶ added in v1.78.0
func Filter[M ~map[K]V, K comparable, V any](mux threadsafe.RLocker, m M, f func(K, V) bool) M
Filter is a thread-safe function that returns a new map containing only the elements in the input map m for which the specified function f is true.
func Get ¶
func Get[M ~map[K]V, K comparable, V any](mux threadsafe.RLocker, m M, k K) V
Get is a thread-safe function to get a value by key k in a map m. See also GetOK.
func GetOK ¶ added in v1.82.0
func GetOK[M ~map[K]V, K comparable, V any](mux threadsafe.RLocker, m M, k K) (V, bool)
GetOK is a thread-safe function to get a value by key k in a map m. The second return value is a boolean that indicates whether the key was present in the map.
func Invert ¶ added in v1.79.0
func Invert[M ~map[K]V, K, V comparable](mux threadsafe.RLocker, m M) map[V]K
Invert is a thread-safe function that returns a new map were keys and values are swapped.
func Len ¶
func Len[M ~map[K]V, K comparable, V any](mux threadsafe.RLocker, m M) int
Len is a thread-safe function to get the length of a map m.
func Map ¶ added in v1.78.0
func Map[M ~map[K]V, K, J comparable, V, U any](mux threadsafe.RLocker, m M, f func(K, V) (J, U)) map[J]U
Map is a thread-safe function that returns a new map that contains each of the elements of the input map m mutated by the specified function. This function can be used to invert a map.
func Reduce ¶ added in v1.78.0
func Reduce[M ~map[K]V, K comparable, V, U any](mux threadsafe.RLocker, m M, init U, f func(K, V, U) U) U
Reduce is a thread-safe function that applies the reducing function f to each element of the input map m, and returns the value of the last call to f. The first parameter of the reducing function f is initialized with init.
func Set ¶
func Set[M ~map[K]V, K comparable, V any](mux threadsafe.Locker, m M, k K, v V)
Set is a thread-safe function to assign a value v to a key k in a map m.
Types ¶
This section is empty.