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

refactoring: rename slice_parallel to slice_concurrent

This commit is contained in:
dudaodong
2024-08-14 10:52:36 +08:00
parent 5c53cb5867
commit 7f78a6b11e
5 changed files with 21 additions and 18 deletions

View File

@@ -6,7 +6,8 @@ slice 包包含操作切片的方法集合。
## 源码: ## 源码:
- [https://github.com/duke-git/lancet/blob/main/slice/slice.go](https://github.com/duke-git/lancet/blob/main/slice/slice.go) - [https://github.com/duke-git/lancet/blob/main/slice/slice.go](https://github.com/duke-git/lancet/blob/main/slice/slice.go)
- [https://github.com/duke-git/lancet/blob/main/slice/slice_concurrent.go](https://github.com/duke-git/lancet/blob/main/slice/slice_concurrent.go)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -61,6 +62,7 @@ import (
- [IndexOf](#IndexOf) - [IndexOf](#IndexOf)
- [LastIndexOf](#LastIndexOf) - [LastIndexOf](#LastIndexOf)
- [Map](#Map) - [Map](#Map)
- [MapConcurrent](#MapConcurrent)
- [FilterMap](#FilterMap) - [FilterMap](#FilterMap)
- [FlatMap](#FlatMap) - [FlatMap](#FlatMap)
- [Merge](#Merge) - [Merge](#Merge)
@@ -88,7 +90,7 @@ import (
- [UniqueBy](#UniqueBy) - [UniqueBy](#UniqueBy)
- [UniqueByComparator](#UniqueByComparator) - [UniqueByComparator](#UniqueByComparator)
- [UniqueByField](#UniqueByField) - [UniqueByField](#UniqueByField)
- [UniqueByParallel](#UniqueByParallel) - [UniqueByConcurrent](#UniqueByConcurrent)
- [Union](#Union) - [Union](#Union)
- [UnionBy](#UnionBy) - [UnionBy](#UnionBy)
- [UpdateAt](#UpdateAt) - [UpdateAt](#UpdateAt)
@@ -1507,7 +1509,7 @@ func main() {
result := slice.MapConcurrent(nums, 4, func(_, n int) int { return n * n }) result := slice.MapConcurrent(nums, 4, func(_, n int) int { return n * n })
fmt.Println(result) fmt.Println(result)
// Output: // Output:
// [1 4 9 16 25 36] // [1 4 9 16 25 36]
} }
@@ -2395,14 +2397,14 @@ func main() {
} }
``` ```
### <span id="UniqueByParallel">UniqueByParallel</span> ### <span id="UniqueByConcurrent">UniqueByConcurrent</span>
<p>并发的从输入切片中移除重复元素,结果保持元素的顺序。</p> <p>并发的从输入切片中移除重复元素,结果保持元素的顺序。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func UniqueByParallel[T comparable](slice []T, numOfThreads int, comparator func(item T, other T) bool) []T func UniqueByConcurrent[T comparable](slice []T, numOfThreads int, comparator func(item T, other T) bool) []T
``` ```
<b>示例:</b> <b>示例:</b>
@@ -2418,7 +2420,7 @@ func main() {
numOfThreads := 4 numOfThreads := 4
comparator := func(item int, other int) bool { return item == other } comparator := func(item int, other int) bool { return item == other }
result := slice.UniqueByParallel(nums, numOfThreads, comparator) result := slice.UniqueByConcurrent(nums, numOfThreads, comparator)
fmt.Println(result) fmt.Println(result)
// Output: // Output:

View File

@@ -6,7 +6,8 @@ Package slice implements some functions to manipulate slice.
## Source: ## Source:
- [https://github.com/duke-git/lancet/blob/main/slice/slice.go](https://github.com/duke-git/lancet/blob/main/slice/slice.go) - [https://github.com/duke-git/lancet/blob/main/slice/slice.go](https://github.com/duke-git/lancet/blob/main/slice/slice.go)
- [https://github.com/duke-git/lancet/blob/main/slice/slice_concurrent.go](https://github.com/duke-git/lancet/blob/main/slice/slice_concurrent.go)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -89,7 +90,7 @@ import (
- [UniqueBy](#UniqueBy) - [UniqueBy](#UniqueBy)
- [UniqueByComparator](#UniqueByComparator) - [UniqueByComparator](#UniqueByComparator)
- [UniqueByField](#UniqueByField) - [UniqueByField](#UniqueByField)
- [UniqueByParallel](#UniqueByParallel) - [UniqueByConcurrent](#UniqueByConcurrent)
- [Union](#Union) - [Union](#Union)
- [UnionBy](#UnionBy) - [UnionBy](#UnionBy)
- [UpdateAt](#UpdateAt) - [UpdateAt](#UpdateAt)
@@ -1506,7 +1507,7 @@ func main() {
result := slice.MapConcurrent(nums, 4, func(_, n int) int { return n * n }) result := slice.MapConcurrent(nums, 4, func(_, n int) int { return n * n })
fmt.Println(result) fmt.Println(result)
// Output: // Output:
// [1 4 9 16 25 36] // [1 4 9 16 25 36]
} }
@@ -2394,14 +2395,14 @@ func main() {
} }
``` ```
### <span id="UniqueByParallel">UniqueByParallel</span> ### <span id="UniqueByConcurrent">UniqueByConcurrent</span>
<p>Removes duplicate elements from the slice by parallel.</p> <p>Removes duplicate elements from the slice by parallel.</p>
<b>Signature:</b> <b>Signature:</b>
```go ```go
func UniqueByParallel[T comparable](slice []T, numOfThreads int, comparator func(item T, other T) bool) []T func UniqueByConcurrent[T comparable](slice []T, numOfThreads int, comparator func(item T, other T) bool) []T
``` ```
<b>Example:</b> <b>Example:</b>
@@ -2417,7 +2418,7 @@ func main() {
numOfThreads := 4 numOfThreads := 4
comparator := func(item int, other int) bool { return item == other } comparator := func(item int, other int) bool { return item == other }
result := slice.UniqueByParallel(nums, numOfThreads, comparator) result := slice.UniqueByConcurrent(nums, numOfThreads, comparator)
fmt.Println(result) fmt.Println(result)
// Output: // Output:

View File

@@ -41,7 +41,7 @@ func MapConcurrent[T any, U any](slice []T, numOfThreads int, iteratee func(inde
// If numOfThreads is less than or equal to 0, it will be set to 1 // If numOfThreads 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 UniqueByParallel[T comparable](slice []T, numOfThreads int, comparator func(item T, other T) bool) []T { func UniqueByConcurrent[T comparable](slice []T, numOfThreads int, comparator func(item T, other T) bool) []T {
if numOfThreads <= 0 { if numOfThreads <= 0 {
numOfThreads = 1 numOfThreads = 1
} else if numOfThreads > len(slice) { } else if numOfThreads > len(slice) {

View File

@@ -1186,12 +1186,12 @@ func ExampleLeftPadding() {
// [0 0 0 1 2 3 4 5] // [0 0 0 1 2 3 4 5]
} }
func ExampleUniqueByParallel() { 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 numOfThreads := 4
comparator := func(item int, other int) bool { return item == other } comparator := func(item int, other int) bool { return item == other }
result := UniqueByParallel(nums, numOfThreads, comparator) result := UniqueByConcurrent(nums, numOfThreads, comparator)
fmt.Println(result) fmt.Println(result)

View File

@@ -1503,16 +1503,16 @@ func TestRightPaddingAndLeftPadding(t *testing.T) {
assert.Equal([]int{0, 0, 0, 1, 2, 3, 4, 5, 0, 0, 0}, padded) assert.Equal([]int{0, 0, 0, 1, 2, 3, 4, 5, 0, 0, 0}, padded)
} }
func TestUniqueByParallel(t *testing.T) { func TestUniqueByConcurrent(t *testing.T) {
t.Parallel() t.Parallel()
assert := internal.NewAssert(t, "TestUniqueByParallel") 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 numOfThreads := 4
comparator := func(item int, other int) bool { return item == other } comparator := func(item int, other int) bool { return item == other }
result := UniqueByParallel(nums, numOfThreads, comparator) result := UniqueByConcurrent(nums, numOfThreads, comparator)
assert.Equal([]int{1, 2, 3, 4, 5, 6, 7}, result) assert.Equal([]int{1, 2, 3, 4, 5, 6, 7}, result)
} }