mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-12 16:52:29 +08:00
@@ -195,18 +195,14 @@ func UniqueByConcurrent[T comparable](slice []T, comparator func(item T, other T
|
|||||||
chunks = append(chunks, slice[i:end])
|
chunks = append(chunks, slice[i:end])
|
||||||
}
|
}
|
||||||
|
|
||||||
type resultChunk struct {
|
resultCh := make(chan resultChunk[T], numThreads)
|
||||||
index int
|
|
||||||
data []T
|
|
||||||
}
|
|
||||||
resultCh := make(chan resultChunk, numThreads)
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
for i, chunk := range chunks {
|
for i, chunk := range chunks {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(index int, chunk []T) {
|
go func(index int, chunk []T) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
resultCh <- resultChunk{index, removeDuplicate(chunk, comparator)}
|
resultCh <- resultChunk[T]{index, removeDuplicate(chunk, comparator)}
|
||||||
}(i, chunk)
|
}(i, chunk)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,13 @@ import (
|
|||||||
"golang.org/x/exp/constraints"
|
"golang.org/x/exp/constraints"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// resultChunk is used to store the intermediate results of UniqueByConcurrent.
|
||||||
|
// It is defined separately to be compatible with versions of go up to 1.20.
|
||||||
|
type resultChunk[T comparable] struct {
|
||||||
|
index int
|
||||||
|
data []T
|
||||||
|
}
|
||||||
|
|
||||||
// sliceValue return the reflect value of a slice
|
// sliceValue return the reflect value of a slice
|
||||||
func sliceValue(slice any) reflect.Value {
|
func sliceValue(slice any) reflect.Value {
|
||||||
v := reflect.ValueOf(slice)
|
v := reflect.ValueOf(slice)
|
||||||
|
|||||||
Reference in New Issue
Block a user