-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtsslice.go
More file actions
76 lines (59 loc) · 2.17 KB
/
tsslice.go
File metadata and controls
76 lines (59 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Package tsslice provides a collection of generic thread-safe Go slice utility
functions that can be safely used across multiple goroutines.
The provided functions are intended to simplify the process of working with
slices in a thread-safe manner.
See also: github.com/Vonage/gosrvlib/pkg/threadsafe
*/
package tsslice
import (
"github.com/Vonage/gosrvlib/pkg/sliceutil"
"github.com/Vonage/gosrvlib/pkg/threadsafe"
)
// Set is a thread-safe function to assign a value v to a key k in a slice s.
func Set[S ~[]E, E any](mux threadsafe.Locker, s S, k int, v E) {
mux.Lock()
defer mux.Unlock()
s[k] = v
}
// Get is a thread-safe function to get a value by key k in a slice.
func Get[S ~[]E, E any](mux threadsafe.RLocker, s S, k int) E {
mux.RLock()
defer mux.RUnlock()
return s[k]
}
// Len is a thread-safe function to get the length of a slice.
func Len[S ~[]E, E any](mux threadsafe.RLocker, s S) int {
mux.RLock()
defer mux.RUnlock()
return len(s)
}
// Append is a thread-safe version of the Go built-in append function.
// Appends the value v to the slice s.
func Append[S ~[]E, E any](mux threadsafe.Locker, s *S, v ...E) {
mux.Lock()
defer mux.Unlock()
*s = append(*s, v...)
}
// 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.
func Filter[S ~[]E, E any](mux threadsafe.RLocker, s S, f func(int, E) bool) S {
mux.RLock()
defer mux.RUnlock()
return sliceutil.Filter(s, f)
}
// 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.
func Map[S ~[]E, E any, U any](mux threadsafe.RLocker, s S, f func(int, E) U) []U {
mux.RLock()
defer mux.RUnlock()
return sliceutil.Map(s, f)
}
// 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.
func Reduce[S ~[]E, E any, U any](mux threadsafe.RLocker, s S, init U, f func(int, E, U) U) U {
mux.RLock()
defer mux.RUnlock()
return sliceutil.Reduce(s, init, f)
}