1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-03-01 00:35:28 +08:00

refactoring: rename param and change its order

This commit is contained in:
dudaodong
2024-08-15 15:50:48 +08:00
parent c2a5335bc6
commit f5d70728c3
5 changed files with 40 additions and 42 deletions
+8 -7
View File
@@ -909,7 +909,7 @@ func main() {
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func FilterConcurrent[T any](slice []T, numOfThreads int, predicate func(index int, item T) bool) []T func FilterConcurrent[T any](slice []T, predicate func(index int, item T) bool, numThreads int) []T
``` ```
<b>示例:</b> <b>示例:</b>
@@ -927,7 +927,7 @@ func main() {
return num%2 == 0 return num%2 == 0
} }
result := slice.FilterConcurrent(nums, 2, isEven) result := slice.FilterConcurrent(nums, isEven, 2)
fmt.Println(result) fmt.Println(result)
@@ -1527,7 +1527,7 @@ func main() {
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func MapConcurrent[T any, U any](slice []T, numOfThreads int, iteratee func(index int, item T) U) []U func MapConcurrent[T any, U any](slice []T, iteratee func(index int, item T) U, numThreads int) []U
``` ```
<b>示例:</b> <b>示例:</b>
@@ -1541,7 +1541,9 @@ import (
func main() { func main() {
nums := []int{1, 2, 3, 4, 5, 6} nums := []int{1, 2, 3, 4, 5, 6}
result := slice.MapConcurrent(nums, 4, func(_, n int) int { return n * n }) result := slice.MapConcurrent(nums, func(_, n int) int {
return n * n
}, 4)
fmt.Println(result) fmt.Println(result)
@@ -2439,7 +2441,7 @@ func main() {
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func UniqueByConcurrent[T comparable](slice []T, numOfThreads int, comparator func(item T, other T) bool) []T func UniqueByConcurrent[T comparable](slice []T, comparator func(item T, other T) bool, numThreads int) []T
``` ```
<b>示例:</b> <b>示例:</b>
@@ -2452,10 +2454,9 @@ import (
func main() { func main() {
nums := []int{1, 2, 3, 1, 2, 4, 5, 6, 4, 7} nums := []int{1, 2, 3, 1, 2, 4, 5, 6, 4, 7}
numOfThreads := 4
comparator := func(item int, other int) bool { return item == other } comparator := func(item int, other int) bool { return item == other }
result := slice.UniqueByConcurrent(nums, numOfThreads, comparator) result := slice.UniqueByConcurrent(nums, comparator, 4)
fmt.Println(result) fmt.Println(result)
// Output: // Output:
+6 -7
View File
@@ -907,7 +907,7 @@ func main() {
<b>Signature:</b> <b>Signature:</b>
```go ```go
func FilterConcurrent[T any](slice []T, numOfThreads int, predicate func(index int, item T) bool) []T func FilterConcurrent[T any](slice []T, predicate func(index int, item T) bool, numThreads int) []T
``` ```
<b>Example:</b> <b>Example:</b>
@@ -925,7 +925,7 @@ func main() {
return num%2 == 0 return num%2 == 0
} }
result := slice.FilterConcurrent(nums, 2, isEven) result := slice.FilterConcurrent(nums, isEven, 2)
fmt.Println(result) fmt.Println(result)
@@ -1525,7 +1525,7 @@ func main() {
<b>Signature:</b> <b>Signature:</b>
```go ```go
func MapConcurrent[T any, U any](slice []T, numOfThreads int, iteratee func(index int, item T) U) []U func MapConcurrent[T any, U any](slice []T, iteratee func(index int, item T) U, numThreads int) []U
``` ```
<b>Example:</b> <b>Example:</b>
@@ -1539,7 +1539,7 @@ import (
func main() { func main() {
nums := []int{1, 2, 3, 4, 5, 6} nums := []int{1, 2, 3, 4, 5, 6}
result := slice.MapConcurrent(nums, 4, func(_, n int) int { return n * n }) result := slice.MapConcurrent(nums, func(_, n int) int { return n * n }, 4)
fmt.Println(result) fmt.Println(result)
@@ -2437,7 +2437,7 @@ func main() {
<b>Signature:</b> <b>Signature:</b>
```go ```go
func UniqueByConcurrent[T comparable](slice []T, numOfThreads int, comparator func(item T, other T) bool) []T func UniqueByConcurrent[T comparable](slice []T, comparator func(item T, other T) bool, numThreads int) []T
``` ```
<b>Example:</b> <b>Example:</b>
@@ -2450,10 +2450,9 @@ import (
func main() { func main() {
nums := []int{1, 2, 3, 1, 2, 4, 5, 6, 4, 7} nums := []int{1, 2, 3, 1, 2, 4, 5, 6, 4, 7}
numOfThreads := 4
comparator := func(item int, other int) bool { return item == other } comparator := func(item int, other int) bool { return item == other }
result := slice.UniqueByConcurrent(nums, numOfThreads, comparator) result := slice.UniqueByConcurrent(nums,comparator, 4)
fmt.Println(result) fmt.Println(result)
// Output: // Output:
+16 -16
View File
@@ -10,11 +10,11 @@ import (
// MapConcurrent applies the iteratee function to each item in the slice concurrently. // MapConcurrent applies the iteratee function to each item in the slice concurrently.
// Play: todo // Play: todo
func MapConcurrent[T any, U any](slice []T, numOfThreads int, iteratee func(index int, item T) U) []U { func MapConcurrent[T any, U any](slice []T, iteratee func(index int, item T) U, numThreads int) []U {
result := make([]U, len(slice)) result := make([]U, len(slice))
var wg sync.WaitGroup var wg sync.WaitGroup
workerChan := make(chan struct{}, numOfThreads) workerChan := make(chan struct{}, numThreads)
for index, item := range slice { for index, item := range slice {
wg.Add(1) wg.Add(1)
@@ -37,11 +37,11 @@ func MapConcurrent[T any, U any](slice []T, numOfThreads int, iteratee func(inde
// FilterConcurrent applies the provided filter function `predicate` to each element of the input slice concurrently. // FilterConcurrent applies the provided filter function `predicate` to each element of the input slice concurrently.
// Play: todo // Play: todo
func FilterConcurrent[T any](slice []T, numOfThreads int, predicate func(index int, item T) bool) []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
workerChan := make(chan struct{}, numOfThreads) workerChan := make(chan struct{}, numThreads)
for index, item := range slice { for index, item := range slice {
wg.Add(1) wg.Add(1)
@@ -66,20 +66,20 @@ func FilterConcurrent[T any](slice []T, numOfThreads int, predicate func(index i
// UniqueByParallel removes duplicate elements from the slice by parallel // UniqueByParallel removes duplicate elements from the slice by parallel
// The comparator function is used to compare the elements // The comparator function is used to compare the elements
// The numOfThreads parameter specifies the number of threads to use // The numThreads parameter specifies the number of threads to use
// If numOfThreads is less than or equal to 0, it will be set to 1 // If numThreads is less than or equal to 0, it will be set to 1
// The comparator function should return true if the two elements are equal // The comparator function should return true if the two elements are equal
// Play: todo // Play: todo
func UniqueByConcurrent[T comparable](slice []T, numOfThreads int, comparator func(item T, other T) bool) []T { func UniqueByConcurrent[T comparable](slice []T, comparator func(item T, other T) bool, numThreads int) []T {
if numOfThreads <= 0 { if numThreads <= 0 {
numOfThreads = 1 numThreads = 1
} else if numOfThreads > len(slice) { } else if numThreads > len(slice) {
numOfThreads = len(slice) numThreads = len(slice)
} }
maxThreads := runtime.NumCPU() maxThreads := runtime.NumCPU()
if numOfThreads > maxThreads { if numThreads > maxThreads {
numOfThreads = maxThreads numThreads = maxThreads
} }
removeDuplicate := func(items []T, comparator func(item T, other T) bool) []T { removeDuplicate := func(items []T, comparator func(item T, other T) bool) []T {
@@ -99,9 +99,9 @@ func UniqueByConcurrent[T comparable](slice []T, numOfThreads int, comparator fu
return result return result
} }
chunkSize := (len(slice) + numOfThreads - 1) / numOfThreads chunkSize := (len(slice) + numThreads - 1) / numThreads
chunks := make([][]T, 0, numOfThreads) chunks := make([][]T, 0, numThreads)
for i := 0; i < len(slice); i += chunkSize { for i := 0; i < len(slice); i += chunkSize {
end := i + chunkSize end := i + chunkSize
if end > len(slice) { if end > len(slice) {
@@ -114,7 +114,7 @@ func UniqueByConcurrent[T comparable](slice []T, numOfThreads int, comparator fu
index int index int
data []T data []T
} }
resultCh := make(chan resultChunk, numOfThreads) resultCh := make(chan resultChunk, numThreads)
var wg sync.WaitGroup var wg sync.WaitGroup
for i, chunk := range chunks { for i, chunk := range chunks {
+3 -4
View File
@@ -254,7 +254,7 @@ func ExampleFilterConcurrent() {
return num%2 == 0 return num%2 == 0
} }
result := FilterConcurrent(nums, 2, isEven) result := FilterConcurrent(nums, isEven, 2)
fmt.Println(result) fmt.Println(result)
@@ -1203,10 +1203,9 @@ func ExampleLeftPadding() {
func ExampleUniqueByConcurrent() { func ExampleUniqueByConcurrent() {
nums := []int{1, 2, 3, 1, 2, 4, 5, 6, 4, 7} nums := []int{1, 2, 3, 1, 2, 4, 5, 6, 4, 7}
numOfThreads := 4
comparator := func(item int, other int) bool { return item == other } comparator := func(item int, other int) bool { return item == other }
result := UniqueByConcurrent(nums, numOfThreads, comparator) result := UniqueByConcurrent(nums, comparator, 4)
fmt.Println(result) fmt.Println(result)
@@ -1216,7 +1215,7 @@ func ExampleUniqueByConcurrent() {
func ExampleMapConcurrent() { func ExampleMapConcurrent() {
nums := []int{1, 2, 3, 4, 5, 6} nums := []int{1, 2, 3, 4, 5, 6}
result := MapConcurrent(nums, 4, func(_, n int) int { return n * n }) result := MapConcurrent(nums, func(_, n int) int { return n * n }, 4)
fmt.Println(result) fmt.Println(result)
+7 -8
View File
@@ -1509,10 +1509,9 @@ func TestUniqueByConcurrent(t *testing.T) {
assert := internal.NewAssert(t, "TestUniqueByConcurrent") assert := internal.NewAssert(t, "TestUniqueByConcurrent")
nums := []int{1, 2, 3, 1, 2, 4, 5, 6, 4, 7} nums := []int{1, 2, 3, 1, 2, 4, 5, 6, 4, 7}
numOfThreads := 4
comparator := func(item int, other int) bool { return item == other } comparator := func(item int, other int) bool { return item == other }
result := UniqueByConcurrent(nums, numOfThreads, comparator) result := UniqueByConcurrent(nums, comparator, 4)
assert.Equal([]int{1, 2, 3, 4, 5, 6, 7}, result) assert.Equal([]int{1, 2, 3, 4, 5, 6, 7}, result)
} }
@@ -1523,21 +1522,21 @@ func TestMapConcurrent(t *testing.T) {
assert := internal.NewAssert(t, "TestMapConcurrent") assert := internal.NewAssert(t, "TestMapConcurrent")
t.Run("empty slice", func(t *testing.T) { t.Run("empty slice", func(t *testing.T) {
actual := MapConcurrent([]int{}, 4, func(_, n int) int { return n * n }) actual := MapConcurrent([]int{}, func(_, n int) int { return n * n }, 4)
assert.Equal([]int{}, actual) assert.Equal([]int{}, actual)
}) })
t.Run("single thread", func(t *testing.T) { t.Run("single thread", func(t *testing.T) {
nums := []int{1, 2, 3, 4, 5, 6} nums := []int{1, 2, 3, 4, 5, 6}
expected := []int{1, 4, 9, 16, 25, 36} expected := []int{1, 4, 9, 16, 25, 36}
actual := MapConcurrent(nums, 1, func(_, n int) int { return n * n }) actual := MapConcurrent(nums, func(_, n int) int { return n * n }, 1)
assert.Equal(expected, actual) assert.Equal(expected, actual)
}) })
t.Run("multiple threads", func(t *testing.T) { t.Run("multiple threads", func(t *testing.T) {
nums := []int{1, 2, 3, 4, 5, 6} nums := []int{1, 2, 3, 4, 5, 6}
expected := []int{1, 4, 9, 16, 25, 36} expected := []int{1, 4, 9, 16, 25, 36}
actual := MapConcurrent(nums, 4, func(_, n int) int { return n * n }) actual := MapConcurrent(nums, func(_, n int) int { return n * n }, 4)
assert.Equal(expected, actual) assert.Equal(expected, actual)
}) })
@@ -1549,21 +1548,21 @@ func TestFilterConcurrent(t *testing.T) {
assert := internal.NewAssert(t, "TestFilterConcurrent") assert := internal.NewAssert(t, "TestFilterConcurrent")
t.Run("empty slice", func(t *testing.T) { t.Run("empty slice", func(t *testing.T) {
actual := FilterConcurrent([]int{}, 4, func(_, n int) bool { return n != 0 }) actual := FilterConcurrent([]int{}, func(_, n int) bool { return n != 0 }, 4)
assert.Equal([]int{}, actual) assert.Equal([]int{}, actual)
}) })
t.Run("single thread", func(t *testing.T) { t.Run("single thread", func(t *testing.T) {
nums := []int{1, 2, 3, 4, 5, 6} nums := []int{1, 2, 3, 4, 5, 6}
expected := []int{4, 5, 6} expected := []int{4, 5, 6}
actual := FilterConcurrent(nums, 1, func(_, n int) bool { return n > 3 }) actual := FilterConcurrent(nums, func(_, n int) bool { return n > 3 }, 1)
assert.Equal(expected, actual) assert.Equal(expected, actual)
}) })
t.Run("multiple threads", func(t *testing.T) { t.Run("multiple threads", func(t *testing.T) {
nums := []int{1, 2, 3, 4, 5, 6} nums := []int{1, 2, 3, 4, 5, 6}
expected := []int{4, 5, 6} expected := []int{4, 5, 6}
actual := FilterConcurrent(nums, 4, func(_, n int) bool { return n > 3 }) actual := FilterConcurrent(nums, func(_, n int) bool { return n > 3 }, 4)
assert.Equal(expected, actual) assert.Equal(expected, actual)
}) })