mirror of
https://github.com/duke-git/lancet.git
synced 2025-09-26 19:41:20 +08:00
Merge branch 'rc' of github.com:duke-git/lancet into rc
This commit is contained in:
@@ -125,6 +125,8 @@ func ReduceConcurrent[T any](slice []T, initial T, reducer func(index int, item
|
|||||||
func FilterConcurrent[T any](slice []T, predicate func(index int, item T) bool, numThreads int) []T {
|
func FilterConcurrent[T any](slice []T, predicate func(index int, item T) bool, numThreads int) []T {
|
||||||
result := make([]T, 0)
|
result := make([]T, 0)
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
var mu sync.Mutex
|
||||||
|
|
||||||
|
|
||||||
workerChan := make(chan struct{}, numThreads)
|
workerChan := make(chan struct{}, numThreads)
|
||||||
|
|
||||||
@@ -137,7 +139,9 @@ func FilterConcurrent[T any](slice []T, predicate func(index int, item T) bool,
|
|||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
if predicate(i, v) {
|
if predicate(i, v) {
|
||||||
|
mu.Lock()
|
||||||
result = append(result, v)
|
result = append(result, v)
|
||||||
|
mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
<-workerChan
|
<-workerChan
|
||||||
|
Reference in New Issue
Block a user