tsslice

package
v1.88.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package tsslice provides a collection of generic thread-safe slice utility functions that can be safely used between multiple goroutines.

The provided functions are intended to simplify the process of working with slices in a thread-safe manner.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append[S ~[]E, E any](mux threadsafe.Locker, s *S, v ...E)

Append is a thread-safe version of the Go built-in append function. Appends the value v to the slice s.

Example (Concurrent)
package main

import (
	"fmt"
	"sort"
	"sync"

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

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

	max := 5
	s := make([]int, 0, max)

	for i := 0; i < max; i++ {
		wg.Add(1)

		go func(item int) {
			defer wg.Done()

			tsslice.Append(mux, &s, item)
		}(i)
	}

	wg.Wait()

	sort.Ints(s)
	fmt.Println(s)

}
Output:

[0 1 2 3 4]
Example (Multiple)
package main

import (
	"fmt"
	"sync"

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

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

	s := make([]string, 0, 2)
	tsslice.Append(mux, &s, "Hello", "World")

	fmt.Println(s)

}
Output:

[Hello World]
Example (Simple)
package main

import (
	"fmt"
	"sync"

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

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

	s := make([]string, 0, 2)
	tsslice.Append(mux, &s, "Hello")
	tsslice.Append(mux, &s, "World")

	fmt.Println(s)

}
Output:

[Hello World]
Example (Slice)
package main

import (
	"fmt"
	"sync"

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

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

	s := make([]string, 0, 2)
	tsslice.Append(mux, &s, []string{"Hello", "World"}...)

	fmt.Println(s)

}
Output:

[Hello World]

func Filter added in v1.78.0

func Filter[S ~[]E, E any](mux threadsafe.RLocker, s S, f func(int, E) bool) S

Filter is a thread-safe function that returns a new slice containing only the elements in the input slice s for which the specified function f is true.

Example
package main

import (
	"fmt"
	"sync"

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

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

	s := []string{"Hello", "World", "Extra"}

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

	s2 := tsslice.Filter(mux, s, filterFn)

	fmt.Println(s2)

}
Output:

[World]

func Get

func Get[S ~[]E, E any](mux threadsafe.RLocker, s S, k int) E

Get is a thread-safe function to get a value by key k in a slice.

Example
package main

import (
	"fmt"
	"sync"

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

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

	s := []string{"Hello", "World"}
	fmt.Println(tsslice.Get(mux, s, 0))
	fmt.Println(tsslice.Get(mux, s, 1))

}
Output:

Hello
World

func Len

func Len[S ~[]E, E any](mux threadsafe.RLocker, s S) int

Len is a thread-safe function to get the length of a slice.

Example
package main

import (
	"fmt"
	"sync"

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

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

	s := []string{"Hello", "World"}
	fmt.Println(tsslice.Len(mux, s))

}
Output:

2

func Map added in v1.78.0

func Map[S ~[]E, E any, U any](mux threadsafe.RLocker, s S, f func(int, E) U) []U

Map is a thread-safe function that returns a new slice that contains each of the elements of the input slice s mutated by the specified function.

Example
package main

import (
	"fmt"
	"sync"

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

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

	s := []string{"Hello", "World", "Extra"}

	mapFn := func(k int, v string) int { return k + len(v) }

	s2 := tsslice.Map(mux, s, mapFn)

	fmt.Println(s2)

}
Output:

[5 6 7]

func Reduce added in v1.78.0

func Reduce[S ~[]E, E any, U any](mux threadsafe.RLocker, s S, init U, f func(int, E, U) U) U

Reduce is a thread-safe function that applies the reducing function f to each element of the input slice s, 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/tsslice"
)

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

	s := []int{2, 3, 5, 7, 11}

	init := 97
	reduceFn := func(k, v, r int) int { return k + v + r }

	r := tsslice.Reduce(mux, s, init, reduceFn)

	fmt.Println(r)

}
Output:

135

func Set

func Set[S ~[]E, E any](mux threadsafe.Locker, s S, k int, v E)

Set is a thread-safe function to assign a value v to a key k in a slice s.

Example
package main

import (
	"fmt"
	"sync"

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

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

	s := make([]string, 2)
	tsslice.Set(mux, s, 0, "Hello")
	tsslice.Set(mux, s, 1, "World")

	fmt.Println(s)

}
Output:

[Hello World]

Types

This section is empty.

Jump to

Keyboard shortcuts

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