tsmap

package
v1.90.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 3, 2024 License: MIT Imports: 2 Imported by: 0

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

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.

Example
package main

import (
	"fmt"
	"sync"

	"github.com/Vonage/gosrvlib/pkg/threadsafe/tsmap"
)

func main() {
	mux := &sync.Mutex{}

	m := map[int]string{0: "Hello", 1: "World"}

	tsmap.Delete(mux, m, 0)

	fmt.Println(m)

}
Output:

map[1:World]

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.

Example
package main

import (
	"fmt"
	"sync"

	"github.com/Vonage/gosrvlib/pkg/threadsafe/tsmap"
)

func main() {
	mux := &sync.RWMutex{}

	m := map[int]string{0: "Hello", 1: "World"}

	filterFn := func(_ int, v string) bool { return v == "World" }

	s2 := tsmap.Filter(mux, m, filterFn)

	fmt.Println(s2)

}
Output:

map[1:World]

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.

Example
package main

import (
	"fmt"
	"sync"

	"github.com/Vonage/gosrvlib/pkg/threadsafe/tsmap"
)

func main() {
	mux := &sync.RWMutex{}

	m := map[int]string{0: "Hello", 1: "World"}
	fmt.Println(tsmap.Get(mux, m, 0))
	fmt.Println(tsmap.Get(mux, m, 1))

}
Output:

Hello
World

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.

Example
package main

import (
	"fmt"
	"sync"

	"github.com/Vonage/gosrvlib/pkg/threadsafe/tsmap"
)

func main() {
	mux := &sync.RWMutex{}

	m := map[int]string{0: "Hello", 1: "World"}

	v, ok := tsmap.GetOK(mux, m, 0)
	fmt.Println(v, ok)

	v, ok = tsmap.GetOK(mux, m, 3)
	fmt.Println(v, ok)

}
Output:

Hello true
 false

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.

Example
package main

import (
	"fmt"
	"sync"

	"github.com/Vonage/gosrvlib/pkg/threadsafe/tsmap"
)

func main() {
	mux := &sync.RWMutex{}

	m := map[int]int{1: 10, 2: 20}

	s2 := tsmap.Invert(mux, m)

	fmt.Println(s2)

}
Output:

map[10:1 20:2]

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.

Example
package main

import (
	"fmt"
	"sync"

	"github.com/Vonage/gosrvlib/pkg/threadsafe/tsmap"
)

func main() {
	mux := &sync.RWMutex{}

	m := map[int]string{0: "Hello", 1: "World"}
	fmt.Println(tsmap.Len(mux, m))

}
Output:

2

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.

Example
package main

import (
	"fmt"
	"sync"

	"github.com/Vonage/gosrvlib/pkg/threadsafe/tsmap"
)

func main() {
	mux := &sync.RWMutex{}

	m := map[int]string{0: "Hello", 1: "World"}

	mapFn := func(k int, v string) (string, int) { return "_" + v, k + 1 }

	s2 := tsmap.Map(mux, m, mapFn)

	fmt.Println(s2)

}
Output:

map[_Hello:1 _World:2]

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.

Example
package main

import (
	"fmt"
	"sync"

	"github.com/Vonage/gosrvlib/pkg/threadsafe/tsmap"
)

func main() {
	mux := &sync.RWMutex{}

	m := map[int]int{0: 2, 1: 3, 2: 5, 3: 7, 4: 11}
	init := 97
	reduceFn := func(k, v, r int) int { return k + v + r }

	r := tsmap.Reduce(mux, m, init, reduceFn)

	fmt.Println(r)

}
Output:

135

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.

Example
package main

import (
	"fmt"
	"sync"

	"github.com/Vonage/gosrvlib/pkg/threadsafe/tsmap"
)

func main() {
	mux := &sync.Mutex{}

	m := make(map[int]string, 2)
	tsmap.Set(mux, m, 0, "Hello")
	tsmap.Set(mux, m, 1, "World")

	fmt.Println(m)

}
Output:

map[0:Hello 1:World]

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL