tsmap

package
v1.80.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2023 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package tsmap provides a collection of thread-safe map functions that can be safely used between multiple goroutines.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

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.

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 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