mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
Compare commits
61 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f24ad26692 | ||
|
|
88de9bfac2 | ||
|
|
e4777a0986 | ||
|
|
84a69d1fa5 | ||
|
|
5eb056277d | ||
|
|
4e6d586251 | ||
|
|
61c7012e17 | ||
|
|
0d48778886 | ||
|
|
e98c46c903 | ||
|
|
155f287ab0 | ||
|
|
652916b7d7 | ||
|
|
955de2bdbf | ||
|
|
a4c1d40faa | ||
|
|
f155e0caa6 | ||
|
|
fb0332449c | ||
|
|
68f0fd1d4c | ||
|
|
70995c5098 | ||
|
|
55e62ed8ca | ||
|
|
e614274f07 | ||
|
|
c524eb04a1 | ||
|
|
985b3cddd8 | ||
|
|
abadeec007 | ||
|
|
3ab05154aa | ||
|
|
9f1c89bf0e | ||
|
|
9444582e44 | ||
|
|
c27ccad2b9 | ||
|
|
d1c6c57700 | ||
|
|
922999037f | ||
|
|
046e90486d | ||
|
|
fc6dee9e77 | ||
|
|
980ff2c363 | ||
|
|
763aa8e10d | ||
|
|
83c0d1d6e6 | ||
|
|
dfc6b868fb | ||
|
|
f66c0938e5 | ||
|
|
a4900fecb4 | ||
|
|
dc25bdab2f | ||
|
|
bb23c9eef8 | ||
|
|
b4cd0750e4 | ||
|
|
c960841491 | ||
|
|
5d6f9443fd | ||
|
|
5483380066 | ||
|
|
20b9e5353e | ||
|
|
1ec3a5af87 | ||
|
|
f31dde97b1 | ||
|
|
1718fd1cf1 | ||
|
|
513c0f829c | ||
|
|
c51a806aff | ||
|
|
3c16d50c4b | ||
|
|
21dd6ab8aa | ||
|
|
f5bf5183cc | ||
|
|
f28b5b2f92 | ||
|
|
79867e8a63 | ||
|
|
19939c2b03 | ||
|
|
bf7ffbfa8d | ||
|
|
cbb46f9cb4 | ||
|
|
3ad142d5d7 | ||
|
|
1a436aeb41 | ||
|
|
1af8fe8daf | ||
|
|
ae54c8db6f | ||
|
|
be942ec33e |
76
README.md
76
README.md
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
@@ -23,7 +23,7 @@ English | [简体中文](./README_zh-CN.md)
|
|||||||
## Feature
|
## Feature
|
||||||
|
|
||||||
- 👏 Comprehensive, efficient and reusable.
|
- 👏 Comprehensive, efficient and reusable.
|
||||||
- 💪 250+ go util functions, support string, slice, datetime, net, crypt...
|
- 💪 300+ go util functions, support string, slice, datetime, net, crypt...
|
||||||
- 💅 Only depend on the go standard library.
|
- 💅 Only depend on the go standard library.
|
||||||
- 🌍 Unit test for every exported function.
|
- 🌍 Unit test for every exported function.
|
||||||
|
|
||||||
@@ -35,9 +35,9 @@ English | [简体中文](./README_zh-CN.md)
|
|||||||
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
|
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
2. <b>For users who use version below go1.18, you should install v1.x.x. now latest v1 is v1.2.7. </b>
|
2. <b>For users who use version below go1.18, you should install v1.x.x. now latest v1 is v1.2.9. </b>
|
||||||
```go
|
```go
|
||||||
go get github.com/duke-git/lancet@v1.2.7 // below go1.18, install latest version of v1.x.x
|
go get github.com/duke-git/lancet@v1.2.9 // below go1.18, install latest version of v1.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
@@ -69,7 +69,7 @@ func main() {
|
|||||||
|
|
||||||
## API Documentation
|
## API Documentation
|
||||||
|
|
||||||
### Algorithm package implements some basic algorithm. eg. sort, search.
|
### 1. Algorithm package implements some basic algorithm. eg. sort, search.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/algorithm"
|
import "github.com/duke-git/lancet/v2/algorithm"
|
||||||
@@ -89,7 +89,25 @@ import "github.com/duke-git/lancet/v2/algorithm"
|
|||||||
- [LRUCache](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LRUCache)
|
- [LRUCache](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LRUCache)
|
||||||
|
|
||||||
|
|
||||||
### Convertor package contains some functions for data convertion.
|
|
||||||
|
### 2. Concurrency package contain some functions to support concurrent programming. eg, goroutine, channel, async.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
```
|
||||||
|
#### Function list:
|
||||||
|
- [NewChannel](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#NewChannel)
|
||||||
|
- [Bridge](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Bridge)
|
||||||
|
- [FanIn](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#FanIn)
|
||||||
|
- [Generate](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Generate)
|
||||||
|
- [Or](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Or)
|
||||||
|
- [OrDone](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#OrDone)
|
||||||
|
- [Repeat](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Repeat)
|
||||||
|
- [RepeatFn](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#RepeatFn)
|
||||||
|
- [Take](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Take)
|
||||||
|
- [Tee](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Tee)
|
||||||
|
|
||||||
|
### 3. Convertor package contains some functions for data convertion.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/convertor"
|
import "github.com/duke-git/lancet/v2/convertor"
|
||||||
@@ -105,7 +123,7 @@ import "github.com/duke-git/lancet/v2/convertor"
|
|||||||
- [ToString](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ToString)
|
- [ToString](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ToString)
|
||||||
- [StructToMap](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#StructToMap)
|
- [StructToMap](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#StructToMap)
|
||||||
|
|
||||||
### Cryptor package is for data encryption and decryption.
|
### 4. Cryptor package is for data encryption and decryption.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/cryptor"
|
import "github.com/duke-git/lancet/v2/cryptor"
|
||||||
@@ -145,7 +163,7 @@ import "github.com/duke-git/lancet/v2/cryptor"
|
|||||||
- [RsaEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#RsaEncrypt)
|
- [RsaEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#RsaEncrypt)
|
||||||
- [RsaDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#RsaDecrypt)
|
- [RsaDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#RsaDecrypt)
|
||||||
|
|
||||||
### Datetime package supports date and time format and compare.
|
### 5. Datetime package supports date and time format and compare.
|
||||||
|
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -183,7 +201,7 @@ import "github.com/duke-git/lancet/v2/datetime"
|
|||||||
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToFormatForTpl)
|
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToFormatForTpl)
|
||||||
- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToIso8601)
|
- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToIso8601)
|
||||||
|
|
||||||
### Fileutil package implements some basic functions for file operations.
|
### 6. Fileutil package implements some basic functions for file operations.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/fileutil"
|
import "github.com/duke-git/lancet/v2/fileutil"
|
||||||
@@ -206,7 +224,7 @@ import "github.com/duke-git/lancet/v2/fileutil"
|
|||||||
- [Zip](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#Zip)
|
- [Zip](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#Zip)
|
||||||
- [UnZip](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#UnZip)
|
- [UnZip](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#UnZip)
|
||||||
|
|
||||||
### Formatter contains some functions for data formatting.
|
### 7. Formatter contains some functions for data formatting.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/formatter"
|
import "github.com/duke-git/lancet/v2/formatter"
|
||||||
@@ -214,7 +232,7 @@ import "github.com/duke-git/lancet/v2/formatter"
|
|||||||
#### Function list:
|
#### Function list:
|
||||||
- [Comma](https://github.com/duke-git/lancet/blob/main/docs/formatter.md#Comma)
|
- [Comma](https://github.com/duke-git/lancet/blob/main/docs/formatter.md#Comma)
|
||||||
|
|
||||||
### Function package can control the flow of function execution and support part of functional programming
|
### 8. Function package can control the flow of function execution and support part of functional programming
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/function"
|
import "github.com/duke-git/lancet/v2/function"
|
||||||
@@ -230,7 +248,23 @@ import "github.com/duke-git/lancet/v2/function"
|
|||||||
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function.md#Watcher)
|
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function.md#Watcher)
|
||||||
|
|
||||||
|
|
||||||
### Mathutil package implements some functions for math calculation.
|
### 9. Maputil package includes some functions to manipulate map.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "github.com/duke-git/lancet/v2/maputil"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Function list:
|
||||||
|
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ForEach)
|
||||||
|
- [Filter](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Filter)
|
||||||
|
- [Intersect](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Intersect)
|
||||||
|
- [Keys](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Keys)
|
||||||
|
- [Merge](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Merge)
|
||||||
|
- [Minus](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Minus)
|
||||||
|
- [Values](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Values)
|
||||||
|
|
||||||
|
|
||||||
|
### 10. Mathutil package implements some functions for math calculation.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/mathutil"
|
import "github.com/duke-git/lancet/v2/mathutil"
|
||||||
@@ -249,7 +283,7 @@ import "github.com/duke-git/lancet/v2/mathutil"
|
|||||||
- [TruncRound](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#TruncRound)
|
- [TruncRound](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#TruncRound)
|
||||||
|
|
||||||
|
|
||||||
### Netutil package contains functions to get net information and send http request.
|
### 11. Netutil package contains functions to get net information and send http request.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/netutil"
|
import "github.com/duke-git/lancet/v2/netutil"
|
||||||
@@ -269,7 +303,7 @@ import "github.com/duke-git/lancet/v2/netutil"
|
|||||||
- [HttpPatch](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPatch)
|
- [HttpPatch](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPatch)
|
||||||
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#ParseHttpResponse)
|
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#ParseHttpResponse)
|
||||||
|
|
||||||
### Random package implements some basic functions to generate random int and string.
|
### 12. Random package implements some basic functions to generate random int and string.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/random"
|
import "github.com/duke-git/lancet/v2/random"
|
||||||
@@ -281,7 +315,7 @@ import "github.com/duke-git/lancet/v2/random"
|
|||||||
- [RandString](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandString)
|
- [RandString](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandString)
|
||||||
- [UUIdV4](https://github.com/duke-git/lancet/blob/main/docs/random.md#UUIdV4)
|
- [UUIdV4](https://github.com/duke-git/lancet/blob/main/docs/random.md#UUIdV4)
|
||||||
|
|
||||||
### Retry package is for executing a function repeatedly until it was successful or canceled by the context.
|
### 13. Retry package is for executing a function repeatedly until it was successful or canceled by the context.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/retry"
|
import "github.com/duke-git/lancet/v2/retry"
|
||||||
@@ -294,7 +328,7 @@ import "github.com/duke-git/lancet/v2/retry"
|
|||||||
- [RetryDuration](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryDuration)
|
- [RetryDuration](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryDuration)
|
||||||
- [RetryTimes](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryTimes)
|
- [RetryTimes](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryTimes)
|
||||||
|
|
||||||
### Slice contains some functions to manipulate slice.
|
### 14. Slice contains some functions to manipulate slice.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/slice"
|
import "github.com/duke-git/lancet/v2/slice"
|
||||||
@@ -331,12 +365,13 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
- [SortByField](https://github.com/duke-git/lancet/blob/main/docs/slice.md#SortByField)
|
- [SortByField](https://github.com/duke-git/lancet/blob/main/docs/slice.md#SortByField)
|
||||||
- [Some](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Some)
|
- [Some](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Some)
|
||||||
- [StringSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#StringSlice)
|
- [StringSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#StringSlice)
|
||||||
|
- [SymmetricDifference](https://github.com/duke-git/lancet/blob/main/docs/slice.md#SymmetricDifference)
|
||||||
- [Unique](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Unique)
|
- [Unique](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Unique)
|
||||||
- [Union](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Union)
|
- [Union](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Union)
|
||||||
- [UpdateAt](https://github.com/duke-git/lancet/blob/main/docs/slice.md#UpdateAt)
|
- [UpdateAt](https://github.com/duke-git/lancet/blob/main/docs/slice.md#UpdateAt)
|
||||||
- [Without](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Without)
|
- [Without](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Without)
|
||||||
|
|
||||||
### Strutil package contains some functions to manipulate string.
|
### 15. Strutil package contains some functions to manipulate string.
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/strutil"
|
import "github.com/duke-git/lancet/v2/strutil"
|
||||||
```
|
```
|
||||||
@@ -357,10 +392,11 @@ import "github.com/duke-git/lancet/v2/strutil"
|
|||||||
- [PadStart](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadStart)
|
- [PadStart](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadStart)
|
||||||
- [ReverseStr](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#ReverseStr)
|
- [ReverseStr](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#ReverseStr)
|
||||||
- [SnakeCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SnakeCase)
|
- [SnakeCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SnakeCase)
|
||||||
|
- [SplitEx](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SplitEx)
|
||||||
- [Wrap](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Wrap)
|
- [Wrap](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Wrap)
|
||||||
- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Unwrap)
|
- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Unwrap)
|
||||||
|
|
||||||
### System package contain some functions about os, runtime, shell command.
|
### 16. System package contain some functions about os, runtime, shell command.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/system"
|
import "github.com/duke-git/lancet/v2/system"
|
||||||
@@ -375,7 +411,7 @@ import "github.com/duke-git/lancet/v2/system"
|
|||||||
- [CompareOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system.md#CompareOsEnv)
|
- [CompareOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system.md#CompareOsEnv)
|
||||||
- [ExecCommand](https://github.com/duke-git/lancet/blob/main/docs/system.md#ExecCommand)
|
- [ExecCommand](https://github.com/duke-git/lancet/blob/main/docs/system.md#ExecCommand)
|
||||||
|
|
||||||
### Validator package contains some functions for data validation.
|
### 17. Validator package contains some functions for data validation.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/validator"
|
import "github.com/duke-git/lancet/v2/validator"
|
||||||
@@ -409,7 +445,7 @@ import "github.com/duke-git/lancet/v2/validator"
|
|||||||
- [IsStrongPassword](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsStrongPassword)
|
- [IsStrongPassword](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsStrongPassword)
|
||||||
- [IsUrl](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsUrl)
|
- [IsUrl](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsUrl)
|
||||||
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsWeakPassword)
|
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsWeakPassword)
|
||||||
### xerror package implements helpers for errors.
|
### 18. xerror package implements helpers for errors.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/xerror"
|
import "github.com/duke-git/lancet/v2/xerror"
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
## 特性
|
## 特性
|
||||||
|
|
||||||
- 👏 全面、高效、可复用
|
- 👏 全面、高效、可复用
|
||||||
- 💪 250+常用go工具函数,支持string、slice、datetime、net、crypt...
|
- 💪 300+常用go工具函数,支持string、slice、datetime、net、crypt...
|
||||||
- 💅 只依赖go标准库
|
- 💅 只依赖go标准库
|
||||||
- 🌍 所有导出函数单元测试覆盖率100%
|
- 🌍 所有导出函数单元测试覆盖率100%
|
||||||
|
|
||||||
@@ -35,9 +35,9 @@
|
|||||||
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
|
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
2. <b>使用go1.18以下版本的用户,必须安装v1.x.x。目前最新的v1版本是v1.2.7。</b>
|
2. <b>使用go1.18以下版本的用户,必须安装v1.x.x。目前最新的v1版本是v1.2.9。</b>
|
||||||
```go
|
```go
|
||||||
go get github.com/duke-git/lancet@v1.2.7 // 使用go1.18以下版本, 必须安装v1.x.x版本
|
go get github.com/duke-git/lancet@v1.2.9 // 使用go1.18以下版本, 必须安装v1.x.x版本
|
||||||
```
|
```
|
||||||
|
|
||||||
## 用法
|
## 用法
|
||||||
@@ -69,7 +69,7 @@ func main() {
|
|||||||
|
|
||||||
## API文档
|
## API文档
|
||||||
|
|
||||||
### algorithm算法包实现一些基本算法。eg. sort, search.
|
### 1. algorithm算法包实现一些基本算法。eg. sort, search.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/algorithm"
|
import "github.com/duke-git/lancet/v2/algorithm"
|
||||||
@@ -88,7 +88,25 @@ import "github.com/duke-git/lancet/v2/algorithm"
|
|||||||
- [LinearSearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LinearSearch)
|
- [LinearSearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LinearSearch)
|
||||||
- [LRUCache](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LRUCache)
|
- [LRUCache](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LRUCache)
|
||||||
|
|
||||||
### convertor转换器包支持一些常见的数据类型转换。
|
|
||||||
|
### 2. 并发包包含一些支持并发编程的功能。例如:goroutine, channel, async等。
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
```
|
||||||
|
#### Function list:
|
||||||
|
- [NewChannel](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#NewChannel)
|
||||||
|
- [Bridge](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Bridge)
|
||||||
|
- [FanIn](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#FanIn)
|
||||||
|
- [Generate](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Generate)
|
||||||
|
- [Or](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Or)
|
||||||
|
- [OrDone](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#OrDone)
|
||||||
|
- [Repeat](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Repeat)
|
||||||
|
- [RepeatFn](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#RepeatFn)
|
||||||
|
- [Take](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Take)
|
||||||
|
- [Tee](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Tee)
|
||||||
|
|
||||||
|
### 3. convertor转换器包支持一些常见的数据类型转换。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/convertor"
|
import "github.com/duke-git/lancet/v2/convertor"
|
||||||
@@ -104,7 +122,7 @@ import "github.com/duke-git/lancet/v2/convertor"
|
|||||||
- [ToString](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ToString)
|
- [ToString](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ToString)
|
||||||
- [StructToMap](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#StructToMap)
|
- [StructToMap](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#StructToMap)
|
||||||
|
|
||||||
### cryptor加密包支持数据加密和解密,获取md5,hash值。支持base64, md5, hmac, aes, des, rsa。
|
### 4. cryptor加密包支持数据加密和解密,获取md5,hash值。支持base64, md5, hmac, aes, des, rsa。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/cryptor"
|
import "github.com/duke-git/lancet/v2/cryptor"
|
||||||
@@ -144,7 +162,7 @@ import "github.com/duke-git/lancet/v2/cryptor"
|
|||||||
- [RsaEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#RsaEncrypt)
|
- [RsaEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#RsaEncrypt)
|
||||||
- [RsaDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#RsaDecrypt)
|
- [RsaDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#RsaDecrypt)
|
||||||
|
|
||||||
### datetime日期时间处理包,格式化日期,比较日期。
|
### 5. datetime日期时间处理包,格式化日期,比较日期。
|
||||||
|
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -182,7 +200,7 @@ import "github.com/duke-git/lancet/v2/datetime"
|
|||||||
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormatForTpl)
|
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormatForTpl)
|
||||||
- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToIso8601)
|
- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToIso8601)
|
||||||
|
|
||||||
### fileutil包支持文件基本操作。
|
### 6. fileutil包支持文件基本操作。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/fileutil"
|
import "github.com/duke-git/lancet/v2/fileutil"
|
||||||
@@ -205,7 +223,7 @@ import "github.com/duke-git/lancet/v2/fileutil"
|
|||||||
- [Zip](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#Zip)
|
- [Zip](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#Zip)
|
||||||
- [UnZip](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#UnZip)
|
- [UnZip](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#UnZip)
|
||||||
|
|
||||||
### formatter格式化器包含一些数据格式化处理方法。
|
### 7. formatter格式化器包含一些数据格式化处理方法。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/formatter"
|
import "github.com/duke-git/lancet/v2/formatter"
|
||||||
@@ -214,7 +232,7 @@ import "github.com/duke-git/lancet/v2/formatter"
|
|||||||
- [Comma](https://github.com/duke-git/lancet/blob/main/docs/formatter_zh-CN.md#Comma)
|
- [Comma](https://github.com/duke-git/lancet/blob/main/docs/formatter_zh-CN.md#Comma)
|
||||||
|
|
||||||
|
|
||||||
### function函数包控制函数执行流程,包含部分函数式编程。
|
### 8. function函数包控制函数执行流程,包含部分函数式编程。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/function"
|
import "github.com/duke-git/lancet/v2/function"
|
||||||
@@ -229,7 +247,23 @@ import "github.com/duke-git/lancet/v2/function"
|
|||||||
- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Delay)
|
- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Delay)
|
||||||
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher)
|
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher)
|
||||||
|
|
||||||
### mathutil包实现了一些数学计算的函数。
|
|
||||||
|
### 9. maputil包包括一些操作map的函数.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "github.com/duke-git/lancet/v2/maputil"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 函数列表:
|
||||||
|
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ForEach)
|
||||||
|
- [Filter](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Filter)
|
||||||
|
- [Intersect](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Intersect)
|
||||||
|
- [Keys](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Keys)
|
||||||
|
- [Merge](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Merge)
|
||||||
|
- [Minus](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Minus)
|
||||||
|
- [Values](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Values)
|
||||||
|
|
||||||
|
### 10. mathutil包实现了一些数学计算的函数。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/mathutil"
|
import "github.com/duke-git/lancet/v2/mathutil"
|
||||||
@@ -247,7 +281,7 @@ import "github.com/duke-git/lancet/v2/mathutil"
|
|||||||
- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToString)
|
- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToString)
|
||||||
- [TruncRound](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#TruncRound)
|
- [TruncRound](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#TruncRound)
|
||||||
|
|
||||||
### netutil网络包支持获取ip地址,发送http请求。
|
### 11. netutil网络包支持获取ip地址,发送http请求。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/netutil"
|
import "github.com/duke-git/lancet/v2/netutil"
|
||||||
@@ -267,7 +301,7 @@ import "github.com/duke-git/lancet/v2/netutil"
|
|||||||
- [HttpPatch](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPatch)
|
- [HttpPatch](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPatch)
|
||||||
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ParseHttpResponse)
|
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ParseHttpResponse)
|
||||||
|
|
||||||
### random随机数生成器包,可以生成随机[]bytes, int, string。
|
### 12. random随机数生成器包,可以生成随机[]bytes, int, string。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/random"
|
import "github.com/duke-git/lancet/v2/random"
|
||||||
@@ -278,7 +312,7 @@ import "github.com/duke-git/lancet/v2/random"
|
|||||||
- [RandInt](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandInt)
|
- [RandInt](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandInt)
|
||||||
- [RandString](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandString)
|
- [RandString](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandString)
|
||||||
- [UUIdV4](https://github.com/duke-git/lancet/blob/main/docs/random.md#UUIdV4)
|
- [UUIdV4](https://github.com/duke-git/lancet/blob/main/docs/random.md#UUIdV4)
|
||||||
### retry重试执行函数直到函数运行成功或被context cancel。
|
### 13. retry重试执行函数直到函数运行成功或被context cancel。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/retry"
|
import "github.com/duke-git/lancet/v2/retry"
|
||||||
@@ -292,7 +326,7 @@ import "github.com/duke-git/lancet/v2/retry"
|
|||||||
- [RetryTimes](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryTimes)
|
- [RetryTimes](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryTimes)
|
||||||
|
|
||||||
|
|
||||||
### slice包包含操作切片的方法集合。
|
### 14. slice包包含操作切片的方法集合。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/slice"
|
import "github.com/duke-git/lancet/v2/slice"
|
||||||
@@ -329,13 +363,14 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
- [SortByField](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#SortByField)
|
- [SortByField](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#SortByField)
|
||||||
- [Some](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Some)
|
- [Some](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Some)
|
||||||
- [StringSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#StringSlice)
|
- [StringSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#StringSlice)
|
||||||
|
- [SymmetricDifference](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#SymmetricDifference)
|
||||||
- [Unique](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Unique)
|
- [Unique](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Unique)
|
||||||
- [Union](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Union)
|
- [Union](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Union)
|
||||||
- [UpdateAt](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#UpdateAt)
|
- [UpdateAt](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#UpdateAt)
|
||||||
- [Without](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Without)
|
- [Without](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Without)
|
||||||
|
|
||||||
|
|
||||||
### strutil包含处理字符串的相关函数。
|
### 15. strutil包含处理字符串的相关函数。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/strutil"
|
import "github.com/duke-git/lancet/v2/strutil"
|
||||||
@@ -357,11 +392,12 @@ import "github.com/duke-git/lancet/v2/strutil"
|
|||||||
- [PadStart](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#PadStart)
|
- [PadStart](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#PadStart)
|
||||||
- [ReverseStr](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#ReverseStr)
|
- [ReverseStr](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#ReverseStr)
|
||||||
- [SnakeCase](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SnakeCase)
|
- [SnakeCase](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SnakeCase)
|
||||||
|
- [SplitEx](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SplitEx)
|
||||||
- [Wrap](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Wrap)
|
- [Wrap](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Wrap)
|
||||||
- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Unwrap)
|
- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Unwrap)
|
||||||
|
|
||||||
|
|
||||||
### system包含os, runtime, shell command相关函数。
|
### 16. system包含os, runtime, shell command相关函数。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/system"
|
import "github.com/duke-git/lancet/v2/system"
|
||||||
@@ -377,7 +413,7 @@ import "github.com/duke-git/lancet/v2/system"
|
|||||||
- [CompareOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#CompareOsEnv)
|
- [CompareOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#CompareOsEnv)
|
||||||
- [ExecCommand](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#ExecCommand)
|
- [ExecCommand](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#ExecCommand)
|
||||||
|
|
||||||
### validator验证器包,包含常用字符串格式验证函数。
|
### 17. validator验证器包,包含常用字符串格式验证函数。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/validator"
|
import "github.com/duke-git/lancet/v2/validator"
|
||||||
@@ -412,7 +448,7 @@ import "github.com/duke-git/lancet/v2/validator"
|
|||||||
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsWeakPassword)
|
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsWeakPassword)
|
||||||
|
|
||||||
validator.md#IsWeakPassword)
|
validator.md#IsWeakPassword)
|
||||||
### xerror包实现一些错误处理函数
|
### 18. xerror包实现一些错误处理函数
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/duke-git/lancet/v2/xerror"
|
import "github.com/duke-git/lancet/v2/xerror"
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ package algorithm
|
|||||||
import "github.com/duke-git/lancet/v2/lancetconstraints"
|
import "github.com/duke-git/lancet/v2/lancetconstraints"
|
||||||
|
|
||||||
// BubbleSort use bubble to sort slice.
|
// BubbleSort use bubble to sort slice.
|
||||||
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
|
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
||||||
for i := 0; i < len(slice); i++ {
|
for i := 0; i < len(slice); i++ {
|
||||||
for j := 0; j < len(slice)-1-i; j++ {
|
for j := 0; j < len(slice)-1-i; j++ {
|
||||||
isCurrGreatThanNext := comparator.Compare(slice[j], slice[j+1]) == 1
|
isCurrGreatThanNext := comparator.Compare(slice[j], slice[j+1]) == 1
|
||||||
@@ -16,38 +16,26 @@ func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return slice
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// InsertionSort use insertion to sort slice.
|
// InsertionSort use insertion to sort slice.
|
||||||
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
|
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
||||||
size := len(slice)
|
for i := 0; i < len(slice); i++ {
|
||||||
if size <= 1 {
|
for j := i; j > 0; j-- {
|
||||||
return slice
|
isPreLessThanCurrent := comparator.Compare(slice[j], slice[j-1]) == -1
|
||||||
}
|
if isPreLessThanCurrent {
|
||||||
|
swap(slice, j, j-1)
|
||||||
for i := 1; i < size; i++ {
|
} else {
|
||||||
currentItem := slice[i]
|
break
|
||||||
preIndex := i - 1
|
}
|
||||||
preItem := slice[preIndex]
|
|
||||||
|
|
||||||
isPreLessThanCurrent := comparator.Compare(preItem, currentItem) == -1
|
|
||||||
for preIndex >= 0 && isPreLessThanCurrent {
|
|
||||||
slice[preIndex+1] = slice[preIndex]
|
|
||||||
preIndex--
|
|
||||||
}
|
}
|
||||||
|
|
||||||
slice[preIndex+1] = currentItem
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return slice
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SelectionSort use selection to sort slice.
|
// SelectionSort use selection to sort slice.
|
||||||
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
|
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
||||||
for i := 0; i < len(slice); i++ {
|
for i := 0; i < len(slice); i++ {
|
||||||
min := i
|
min := i
|
||||||
|
|
||||||
for j := i + 1; j < len(slice); j++ {
|
for j := i + 1; j < len(slice); j++ {
|
||||||
if comparator.Compare(slice[j], slice[min]) == -1 {
|
if comparator.Compare(slice[j], slice[min]) == -1 {
|
||||||
min = j
|
min = j
|
||||||
@@ -55,15 +43,11 @@ func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) []
|
|||||||
}
|
}
|
||||||
swap(slice, i, min)
|
swap(slice, i, min)
|
||||||
}
|
}
|
||||||
return slice
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShellSort shell sort slice.
|
// ShellSort shell sort slice.
|
||||||
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
|
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
||||||
size := len(slice)
|
size := len(slice)
|
||||||
if size <= 1 {
|
|
||||||
return slice
|
|
||||||
}
|
|
||||||
|
|
||||||
gap := 1
|
gap := 1
|
||||||
for gap < size/3 {
|
for gap < size/3 {
|
||||||
@@ -76,21 +60,17 @@ func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
|
|||||||
swap(slice, j, j-gap)
|
swap(slice, j, j-gap)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
gap /= 3
|
gap = gap / 3
|
||||||
}
|
}
|
||||||
|
|
||||||
return slice
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// QuickSort quick sorting for slice, lowIndex is 0 and highIndex is len(slice)-1
|
// QuickSort quick sorting for slice, lowIndex is 0 and highIndex is len(slice)-1
|
||||||
func QuickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) []T {
|
func QuickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) {
|
||||||
if lowIndex < highIndex {
|
if lowIndex < highIndex {
|
||||||
p := partition(slice, lowIndex, highIndex, comparator)
|
p := partition(slice, lowIndex, highIndex, comparator)
|
||||||
QuickSort(slice, lowIndex, p-1, comparator)
|
QuickSort(slice, lowIndex, p-1, comparator)
|
||||||
QuickSort(slice, p+1, highIndex, comparator)
|
QuickSort(slice, p+1, highIndex, comparator)
|
||||||
}
|
}
|
||||||
|
|
||||||
return slice
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// partition split slice into two parts
|
// partition split slice into two parts
|
||||||
@@ -110,7 +90,7 @@ func partition[T any](slice []T, lowIndex, highIndex int, comparator lancetconst
|
|||||||
}
|
}
|
||||||
|
|
||||||
// HeapSort use heap to sort slice
|
// HeapSort use heap to sort slice
|
||||||
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
|
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
||||||
size := len(slice)
|
size := len(slice)
|
||||||
|
|
||||||
for i := size/2 - 1; i >= 0; i-- {
|
for i := size/2 - 1; i >= 0; i-- {
|
||||||
@@ -120,8 +100,6 @@ func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
|
|||||||
swap(slice, 0, j)
|
swap(slice, 0, j)
|
||||||
sift(slice, 0, j-1, comparator)
|
sift(slice, 0, j-1, comparator)
|
||||||
}
|
}
|
||||||
|
|
||||||
return slice
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func sift[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) {
|
func sift[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) {
|
||||||
@@ -145,15 +123,17 @@ func sift[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraint
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MergeSort merge sorting for slice
|
// MergeSort merge sorting for slice
|
||||||
func MergeSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) []T {
|
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
||||||
|
mergeSort(slice, 0, len(slice)-1, comparator)
|
||||||
|
}
|
||||||
|
|
||||||
|
func mergeSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) {
|
||||||
if lowIndex < highIndex {
|
if lowIndex < highIndex {
|
||||||
mid := (lowIndex + highIndex) / 2
|
mid := (lowIndex + highIndex) / 2
|
||||||
MergeSort(slice, lowIndex, mid, comparator)
|
mergeSort(slice, lowIndex, mid, comparator)
|
||||||
MergeSort(slice, mid+1, highIndex, comparator)
|
mergeSort(slice, mid+1, highIndex, comparator)
|
||||||
merge(slice, lowIndex, mid, highIndex, comparator)
|
merge(slice, lowIndex, mid, highIndex, comparator)
|
||||||
}
|
}
|
||||||
|
|
||||||
return slice
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func merge[T any](slice []T, lowIndex, midIndex, highIndex int, comparator lancetconstraints.Comparator) {
|
func merge[T any](slice []T, lowIndex, midIndex, highIndex int, comparator lancetconstraints.Comparator) {
|
||||||
|
|||||||
@@ -28,22 +28,15 @@ func (pc *peopleAgeComparator) Compare(v1 any, v2 any) int {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
//decending order
|
|
||||||
// if p1.Age > p2.Age {
|
|
||||||
// return -1
|
|
||||||
// } else if p1.Age < p2.Age {
|
|
||||||
// return 1
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var peoples = []people{
|
// var peoples = []people{
|
||||||
{Name: "a", Age: 20},
|
// {Name: "a", Age: 20},
|
||||||
{Name: "b", Age: 10},
|
// {Name: "b", Age: 10},
|
||||||
{Name: "c", Age: 17},
|
// {Name: "c", Age: 17},
|
||||||
{Name: "d", Age: 8},
|
// {Name: "d", Age: 8},
|
||||||
{Name: "e", Age: 28},
|
// {Name: "e", Age: 28},
|
||||||
}
|
// }
|
||||||
|
|
||||||
type intComparator struct{}
|
type intComparator struct{}
|
||||||
|
|
||||||
@@ -60,40 +53,51 @@ func (c *intComparator) Compare(v1 any, v2 any) int {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
var intSlice = []int{2, 1, 5, 3, 6, 4}
|
// var intSlice = []int{2, 1, 5, 3, 6, 4}
|
||||||
|
|
||||||
func TestBubbleSortForStructSlice(t *testing.T) {
|
func TestBubbleSortForStructSlice(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestBubbleSortForStructSlice")
|
asssert := internal.NewAssert(t, "TestBubbleSortForStructSlice")
|
||||||
|
|
||||||
|
peoples := []people{
|
||||||
|
{Name: "a", Age: 20},
|
||||||
|
{Name: "b", Age: 10},
|
||||||
|
{Name: "c", Age: 17},
|
||||||
|
{Name: "d", Age: 8},
|
||||||
|
{Name: "e", Age: 28},
|
||||||
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := BubbleSort(peoples, comparator)
|
BubbleSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
|
||||||
|
|
||||||
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
||||||
actual := fmt.Sprintf("%v", sortedPeopleByAge)
|
actual := fmt.Sprintf("%v", peoples)
|
||||||
|
|
||||||
asssert.Equal(expected, actual)
|
asssert.Equal(expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBubbleSortForIntSlice(t *testing.T) {
|
func TestBubbleSortForIntSlice(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestBubbleSortForIntSlice")
|
asssert := internal.NewAssert(t, "TestBubbleSortForIntSlice")
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedInt := BubbleSort(intSlice, comparator)
|
BubbleSort(numbers, comparator)
|
||||||
expected := []int{1, 2, 3, 4, 5, 6}
|
|
||||||
|
|
||||||
asssert.Equal(expected, sortedInt)
|
asssert.Equal([]int{1, 2, 3, 4, 5, 6}, numbers)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInsertionSort(t *testing.T) {
|
func TestInsertionSort(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestInsertionSort")
|
asssert := internal.NewAssert(t, "TestInsertionSort")
|
||||||
|
|
||||||
|
peoples := []people{
|
||||||
|
{Name: "a", Age: 20},
|
||||||
|
{Name: "b", Age: 10},
|
||||||
|
{Name: "c", Age: 17},
|
||||||
|
{Name: "d", Age: 8},
|
||||||
|
{Name: "e", Age: 28},
|
||||||
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := InsertionSort(peoples, comparator)
|
InsertionSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
|
||||||
|
|
||||||
expected := "[{e 28} {a 20} {c 17} {b 10} {d 8}]"
|
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
||||||
actual := fmt.Sprintf("%v", sortedPeopleByAge)
|
actual := fmt.Sprintf("%v", peoples)
|
||||||
|
|
||||||
asssert.Equal(expected, actual)
|
asssert.Equal(expected, actual)
|
||||||
}
|
}
|
||||||
@@ -101,12 +105,18 @@ func TestInsertionSort(t *testing.T) {
|
|||||||
func TestSelectionSort(t *testing.T) {
|
func TestSelectionSort(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestSelectionSort")
|
asssert := internal.NewAssert(t, "TestSelectionSort")
|
||||||
|
|
||||||
|
peoples := []people{
|
||||||
|
{Name: "a", Age: 20},
|
||||||
|
{Name: "b", Age: 10},
|
||||||
|
{Name: "c", Age: 17},
|
||||||
|
{Name: "d", Age: 8},
|
||||||
|
{Name: "e", Age: 28},
|
||||||
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := SelectionSort(peoples, comparator)
|
SelectionSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
|
||||||
|
|
||||||
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
||||||
actual := fmt.Sprintf("%v", sortedPeopleByAge)
|
actual := fmt.Sprintf("%v", peoples)
|
||||||
|
|
||||||
asssert.Equal(expected, actual)
|
asssert.Equal(expected, actual)
|
||||||
}
|
}
|
||||||
@@ -114,12 +124,18 @@ func TestSelectionSort(t *testing.T) {
|
|||||||
func TestShellSort(t *testing.T) {
|
func TestShellSort(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestShellSort")
|
asssert := internal.NewAssert(t, "TestShellSort")
|
||||||
|
|
||||||
|
peoples := []people{
|
||||||
|
{Name: "a", Age: 20},
|
||||||
|
{Name: "b", Age: 10},
|
||||||
|
{Name: "c", Age: 17},
|
||||||
|
{Name: "d", Age: 8},
|
||||||
|
{Name: "e", Age: 28},
|
||||||
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := ShellSort(peoples, comparator)
|
ShellSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
|
||||||
|
|
||||||
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
||||||
actual := fmt.Sprintf("%v", sortedPeopleByAge)
|
actual := fmt.Sprintf("%v", peoples)
|
||||||
|
|
||||||
asssert.Equal(expected, actual)
|
asssert.Equal(expected, actual)
|
||||||
}
|
}
|
||||||
@@ -127,12 +143,18 @@ func TestShellSort(t *testing.T) {
|
|||||||
func TestQuickSort(t *testing.T) {
|
func TestQuickSort(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestQuickSort")
|
asssert := internal.NewAssert(t, "TestQuickSort")
|
||||||
|
|
||||||
|
peoples := []people{
|
||||||
|
{Name: "a", Age: 20},
|
||||||
|
{Name: "b", Age: 10},
|
||||||
|
{Name: "c", Age: 17},
|
||||||
|
{Name: "d", Age: 8},
|
||||||
|
{Name: "e", Age: 28},
|
||||||
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := QuickSort(peoples, 0, len(peoples)-1, comparator)
|
QuickSort(peoples, 0, len(peoples)-1, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
|
||||||
|
|
||||||
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
||||||
actual := fmt.Sprintf("%v", sortedPeopleByAge)
|
actual := fmt.Sprintf("%v", peoples)
|
||||||
|
|
||||||
asssert.Equal(expected, actual)
|
asssert.Equal(expected, actual)
|
||||||
}
|
}
|
||||||
@@ -140,12 +162,18 @@ func TestQuickSort(t *testing.T) {
|
|||||||
func TestHeapSort(t *testing.T) {
|
func TestHeapSort(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestHeapSort")
|
asssert := internal.NewAssert(t, "TestHeapSort")
|
||||||
|
|
||||||
|
peoples := []people{
|
||||||
|
{Name: "a", Age: 20},
|
||||||
|
{Name: "b", Age: 10},
|
||||||
|
{Name: "c", Age: 17},
|
||||||
|
{Name: "d", Age: 8},
|
||||||
|
{Name: "e", Age: 28},
|
||||||
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := HeapSort(peoples, comparator)
|
HeapSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
|
||||||
|
|
||||||
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
||||||
actual := fmt.Sprintf("%v", sortedPeopleByAge)
|
actual := fmt.Sprintf("%v", peoples)
|
||||||
|
|
||||||
asssert.Equal(expected, actual)
|
asssert.Equal(expected, actual)
|
||||||
}
|
}
|
||||||
@@ -153,12 +181,18 @@ func TestHeapSort(t *testing.T) {
|
|||||||
func TestMergeSort(t *testing.T) {
|
func TestMergeSort(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestMergeSort")
|
asssert := internal.NewAssert(t, "TestMergeSort")
|
||||||
|
|
||||||
|
peoples := []people{
|
||||||
|
{Name: "a", Age: 20},
|
||||||
|
{Name: "b", Age: 10},
|
||||||
|
{Name: "c", Age: 17},
|
||||||
|
{Name: "d", Age: 8},
|
||||||
|
{Name: "e", Age: 28},
|
||||||
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := MergeSort(peoples, 0, len(peoples)-1, comparator)
|
MergeSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
|
||||||
|
|
||||||
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
||||||
actual := fmt.Sprintf("%v", sortedPeopleByAge)
|
actual := fmt.Sprintf("%v", peoples)
|
||||||
|
|
||||||
asssert.Equal(expected, actual)
|
asssert.Equal(expected, actual)
|
||||||
}
|
}
|
||||||
@@ -166,6 +200,13 @@ func TestMergeSort(t *testing.T) {
|
|||||||
func TestCountSort(t *testing.T) {
|
func TestCountSort(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestCountSort")
|
asssert := internal.NewAssert(t, "TestCountSort")
|
||||||
|
|
||||||
|
peoples := []people{
|
||||||
|
{Name: "a", Age: 20},
|
||||||
|
{Name: "b", Age: 10},
|
||||||
|
{Name: "c", Age: 17},
|
||||||
|
{Name: "d", Age: 8},
|
||||||
|
{Name: "e", Age: 28},
|
||||||
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := CountSort(peoples, comparator)
|
sortedPeopleByAge := CountSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
t.Log(sortedPeopleByAge)
|
||||||
|
|||||||
243
concurrency/channel.go
Normal file
243
concurrency/channel.go
Normal file
@@ -0,0 +1,243 @@
|
|||||||
|
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
|
||||||
|
// Use of this source code is governed by MIT license
|
||||||
|
|
||||||
|
// Package concurrency contain some functions to support concurrent programming. eg, goroutine, channel, async.
|
||||||
|
package concurrency
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Channel is a logic object which can generate or manipulate go channel
|
||||||
|
// all methods of Channel are in the book tilted《Concurrency in Go》
|
||||||
|
type Channel struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewChannel return a Channel instance
|
||||||
|
func NewChannel() *Channel {
|
||||||
|
return &Channel{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate a data of type any chan, put param `values` into the chan
|
||||||
|
func (c *Channel) Generate(ctx context.Context, values ...any) <-chan any {
|
||||||
|
dataStream := make(chan any)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer close(dataStream)
|
||||||
|
|
||||||
|
for _, v := range values {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case dataStream <- v:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return dataStream
|
||||||
|
}
|
||||||
|
|
||||||
|
// Repeat return a data of type any chan, put param `values` into the chan repeatly until cancel the context.
|
||||||
|
func (c *Channel) Repeat(ctx context.Context, values ...any) <-chan any {
|
||||||
|
dataStream := make(chan any)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer close(dataStream)
|
||||||
|
for {
|
||||||
|
for _, v := range values {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case dataStream <- v:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return dataStream
|
||||||
|
}
|
||||||
|
|
||||||
|
// RepeatFn return a chan, excutes fn repeatly, and put the result into retruned chan
|
||||||
|
// until close the `done` channel
|
||||||
|
func (c *Channel) RepeatFn(ctx context.Context, fn func() any) <-chan any {
|
||||||
|
dataStream := make(chan any)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer close(dataStream)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case dataStream <- fn():
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return dataStream
|
||||||
|
}
|
||||||
|
|
||||||
|
// Take return a chan whose values are tahken from another chan
|
||||||
|
func (c *Channel) Take(ctx context.Context, valueStream <-chan any, number int) <-chan any {
|
||||||
|
takeStream := make(chan any)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer close(takeStream)
|
||||||
|
|
||||||
|
for i := 0; i < number; i++ {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case takeStream <- <-valueStream:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return takeStream
|
||||||
|
}
|
||||||
|
|
||||||
|
// FanIn merge multiple channels into one channel
|
||||||
|
func (c *Channel) FanIn(ctx context.Context, channels ...<-chan any) <-chan any {
|
||||||
|
out := make(chan any)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(len(channels))
|
||||||
|
|
||||||
|
for _, c := range channels {
|
||||||
|
go func(c <-chan any) {
|
||||||
|
defer wg.Done()
|
||||||
|
for v := range c {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case out <- v:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}(c)
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
close(out)
|
||||||
|
}()
|
||||||
|
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tee split one chanel into two channels
|
||||||
|
func (c *Channel) Tee(ctx context.Context, in <-chan any) (<-chan any, <-chan any) {
|
||||||
|
out1 := make(chan any)
|
||||||
|
out2 := make(chan any)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer close(out1)
|
||||||
|
defer close(out2)
|
||||||
|
|
||||||
|
for val := range c.OrDone(ctx, in) {
|
||||||
|
var out1, out2 = out1, out2
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
case out1 <- val:
|
||||||
|
out1 = nil
|
||||||
|
case out2 <- val:
|
||||||
|
out2 = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return out1, out2
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bridge link multiply channels into one channel
|
||||||
|
func (c *Channel) Bridge(ctx context.Context, chanStream <-chan <-chan any) <-chan any {
|
||||||
|
valStream := make(chan any)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer close(valStream)
|
||||||
|
|
||||||
|
for {
|
||||||
|
var stream <-chan any
|
||||||
|
select {
|
||||||
|
case maybeStream, ok := <-chanStream:
|
||||||
|
if ok == false {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
stream = maybeStream
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for val := range c.OrDone(ctx, stream) {
|
||||||
|
select {
|
||||||
|
case valStream <- val:
|
||||||
|
case <-ctx.Done():
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return valStream
|
||||||
|
}
|
||||||
|
|
||||||
|
// Or read one or more channels into one channel, will close when any readin channel is closed
|
||||||
|
func (c *Channel) Or(channels ...<-chan any) <-chan any {
|
||||||
|
switch len(channels) {
|
||||||
|
case 0:
|
||||||
|
return nil
|
||||||
|
case 1:
|
||||||
|
return channels[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
orDone := make(chan any)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer close(orDone)
|
||||||
|
|
||||||
|
switch len(channels) {
|
||||||
|
case 2:
|
||||||
|
select {
|
||||||
|
case <-channels[0]:
|
||||||
|
case <-channels[1]:
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
m := len(channels) / 2
|
||||||
|
select {
|
||||||
|
case <-c.Or(channels[:m]...):
|
||||||
|
case <-c.Or(channels[m:]...):
|
||||||
|
}
|
||||||
|
// select {
|
||||||
|
// case <-channels[0]:
|
||||||
|
// case <-channels[1]:
|
||||||
|
// case <-channels[2]:
|
||||||
|
// case <-c.Or(append(channels[3:], orDone)...):
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return orDone
|
||||||
|
}
|
||||||
|
|
||||||
|
// OrDone read a channel into another channel, will close until cancel context.
|
||||||
|
func (c *Channel) OrDone(ctx context.Context, channel <-chan any) <-chan any {
|
||||||
|
valStream := make(chan any)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer close(valStream)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case v, ok := <-channel:
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case valStream <- v:
|
||||||
|
case <-ctx.Done():
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}()
|
||||||
|
|
||||||
|
return valStream
|
||||||
|
}
|
||||||
206
concurrency/channel_test.go
Normal file
206
concurrency/channel_test.go
Normal file
@@ -0,0 +1,206 @@
|
|||||||
|
package concurrency
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGenerate(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestGenerate")
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
c := NewChannel()
|
||||||
|
intStream := c.Generate(ctx, 1, 2, 3)
|
||||||
|
|
||||||
|
// for v := range intStream {
|
||||||
|
// t.Log(v) //1, 2, 3
|
||||||
|
// }
|
||||||
|
assert.Equal(1, <-intStream)
|
||||||
|
assert.Equal(2, <-intStream)
|
||||||
|
assert.Equal(3, <-intStream)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRepeat(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestRepeat")
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
c := NewChannel()
|
||||||
|
intStream := c.Take(ctx, c.Repeat(ctx, 1, 2), 5)
|
||||||
|
|
||||||
|
// for v := range intStream {
|
||||||
|
// t.Log(v) //1, 2, 1, 2, 1
|
||||||
|
// }
|
||||||
|
assert.Equal(1, <-intStream)
|
||||||
|
assert.Equal(2, <-intStream)
|
||||||
|
assert.Equal(1, <-intStream)
|
||||||
|
assert.Equal(2, <-intStream)
|
||||||
|
assert.Equal(1, <-intStream)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRepeatFn(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestRepeatFn")
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
fn := func() any {
|
||||||
|
s := "a"
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
c := NewChannel()
|
||||||
|
dataStream := c.Take(ctx, c.RepeatFn(ctx, fn), 3)
|
||||||
|
|
||||||
|
// for v := range dataStream {
|
||||||
|
// t.Log(v) //a, a, a
|
||||||
|
// }
|
||||||
|
|
||||||
|
assert.Equal("a", <-dataStream)
|
||||||
|
assert.Equal("a", <-dataStream)
|
||||||
|
assert.Equal("a", <-dataStream)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTake(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestTake")
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
numbers := make(chan any, 5)
|
||||||
|
numbers <- 1
|
||||||
|
numbers <- 2
|
||||||
|
numbers <- 3
|
||||||
|
numbers <- 4
|
||||||
|
numbers <- 5
|
||||||
|
defer close(numbers)
|
||||||
|
|
||||||
|
c := NewChannel()
|
||||||
|
intStream := c.Take(ctx, numbers, 3)
|
||||||
|
|
||||||
|
assert.Equal(1, <-intStream)
|
||||||
|
assert.Equal(2, <-intStream)
|
||||||
|
assert.Equal(3, <-intStream)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFanIn(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestFanIn")
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
c := NewChannel()
|
||||||
|
channels := make([]<-chan any, 3)
|
||||||
|
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
channels[i] = c.Take(ctx, c.Repeat(ctx, i), 3)
|
||||||
|
}
|
||||||
|
|
||||||
|
mergedChannel := c.FanIn(ctx, channels...)
|
||||||
|
|
||||||
|
for val := range mergedChannel {
|
||||||
|
t.Logf("\t%d\n", val)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(1, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOr(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestOr")
|
||||||
|
|
||||||
|
sig := func(after time.Duration) <-chan any {
|
||||||
|
c := make(chan interface{})
|
||||||
|
go func() {
|
||||||
|
defer close(c)
|
||||||
|
time.Sleep(after)
|
||||||
|
}()
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
|
c := NewChannel()
|
||||||
|
<-c.Or(
|
||||||
|
sig(1*time.Second),
|
||||||
|
sig(2*time.Second),
|
||||||
|
sig(3*time.Second),
|
||||||
|
sig(4*time.Second),
|
||||||
|
sig(5*time.Second),
|
||||||
|
)
|
||||||
|
|
||||||
|
t.Logf("done after %v", time.Since(start))
|
||||||
|
|
||||||
|
assert.Equal(1, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOrDone(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestOrDone")
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
c := NewChannel()
|
||||||
|
intStream := c.Take(ctx, c.Repeat(ctx, 1), 3)
|
||||||
|
|
||||||
|
var res any
|
||||||
|
for val := range c.OrDone(ctx, intStream) {
|
||||||
|
t.Logf("%v", val)
|
||||||
|
res = val
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(1, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTee(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestTee")
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
c := NewChannel()
|
||||||
|
inStream := c.Take(ctx, c.Repeat(ctx, 1), 4)
|
||||||
|
|
||||||
|
out1, out2 := c.Tee(ctx, inStream)
|
||||||
|
for val := range out1 {
|
||||||
|
val1 := val
|
||||||
|
val2 := <-out2
|
||||||
|
// t.Log("val1 is", val1)
|
||||||
|
// t.Log("val2 is", val2)
|
||||||
|
assert.Equal(1, val1)
|
||||||
|
assert.Equal(1, val2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBridge(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestBridge")
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
c := NewChannel()
|
||||||
|
genVals := func() <-chan <-chan any {
|
||||||
|
chanStream := make(chan (<-chan any))
|
||||||
|
go func() {
|
||||||
|
defer close(chanStream)
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
stream := make(chan any, 1)
|
||||||
|
stream <- i
|
||||||
|
close(stream)
|
||||||
|
chanStream <- stream
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return chanStream
|
||||||
|
}
|
||||||
|
|
||||||
|
index := 0
|
||||||
|
for val := range c.Bridge(ctx, genVals()) {
|
||||||
|
// t.Logf("%v ", val) //0 1 2 3 4 5 6 7 8 9
|
||||||
|
assert.Equal(index, val)
|
||||||
|
index++
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -40,12 +40,12 @@ func NewQueueNode[T any](value T) *QueueNode[T] {
|
|||||||
|
|
||||||
// TreeNode is node of tree
|
// TreeNode is node of tree
|
||||||
type TreeNode[T any] struct {
|
type TreeNode[T any] struct {
|
||||||
Data T
|
Value T
|
||||||
Left *TreeNode[T]
|
Left *TreeNode[T]
|
||||||
Right *TreeNode[T]
|
Right *TreeNode[T]
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTreeNode return a TreeNode pointer
|
// NewTreeNode return a TreeNode pointer
|
||||||
func NewTreeNode[T any](data T) *TreeNode[T] {
|
func NewTreeNode[T any](val T) *TreeNode[T] {
|
||||||
return &TreeNode[T]{data, nil, nil}
|
return &TreeNode[T]{val, nil, nil}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,79 +1,101 @@
|
|||||||
package datastructure
|
package datastructure
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ArrayQueue implements queue with slice
|
// ArrayQueue implements queue with slice
|
||||||
type ArrayQueue[T any] struct {
|
type ArrayQueue[T any] struct {
|
||||||
data []T
|
items []T
|
||||||
length int
|
head int
|
||||||
|
tail int
|
||||||
|
capacity int
|
||||||
|
size int
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewArrayQueue return a empty ArrayQueue pointer
|
func NewArrayQueue[T any](capacity int) *ArrayQueue[T] {
|
||||||
func NewArrayQueue[T any](values ...T) *ArrayQueue[T] {
|
return &ArrayQueue[T]{
|
||||||
data := make([]T, len(values))
|
items: make([]T, 0, capacity),
|
||||||
for i, v := range values {
|
head: 0,
|
||||||
data[i] = v
|
tail: 0,
|
||||||
|
capacity: capacity,
|
||||||
|
size: 0,
|
||||||
}
|
}
|
||||||
return &ArrayQueue[T]{data: data, length: len(values)}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Data return queue data
|
// Data return queue data
|
||||||
func (q *ArrayQueue[T]) Data() []T {
|
func (q *ArrayQueue[T]) Data() []T {
|
||||||
return q.data
|
items := []T{}
|
||||||
|
for i := q.head; i < q.tail; i++ {
|
||||||
|
items = append(items, q.items[i])
|
||||||
|
}
|
||||||
|
return items
|
||||||
}
|
}
|
||||||
|
|
||||||
// Size return length of queue data
|
// Size return length of queue data
|
||||||
func (q *ArrayQueue[T]) Size() int {
|
func (q *ArrayQueue[T]) Size() int {
|
||||||
return q.length
|
return q.size
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsEmpty checks if queue is empty or not
|
// IsEmpty checks if queue is empty or not
|
||||||
func (q *ArrayQueue[T]) IsEmpty() bool {
|
func (q *ArrayQueue[T]) IsEmpty() bool {
|
||||||
return q.length == 0
|
return q.size == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Front return front value of queue
|
// Front return front value of queue
|
||||||
func (q *ArrayQueue[T]) Front() T {
|
func (q *ArrayQueue[T]) Front() T {
|
||||||
return q.data[0]
|
return q.items[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Back return back value of queue
|
// Back return back value of queue
|
||||||
func (q *ArrayQueue[T]) Back() T {
|
func (q *ArrayQueue[T]) Back() T {
|
||||||
return q.data[q.length-1]
|
return q.items[q.size-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnQueue put element into queue
|
// EnQueue put element into queue
|
||||||
func (q *ArrayQueue[T]) EnQueue(value T) {
|
func (q *ArrayQueue[T]) Enqueue(item T) bool {
|
||||||
q.data = append(q.data, value)
|
if q.head == 0 && q.tail == q.capacity {
|
||||||
q.length++
|
return false
|
||||||
|
} else if q.head != 0 && q.tail == q.capacity {
|
||||||
|
for i := q.head; i < q.tail; i++ {
|
||||||
|
q.items[i-q.head] = q.items[i]
|
||||||
|
}
|
||||||
|
q.tail = q.tail - q.head
|
||||||
|
q.head = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
q.items = append(q.items, item)
|
||||||
|
q.tail++
|
||||||
|
q.size++
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeQueue remove head element of queue and return it, if queue is empty, return nil and error
|
// DeQueue remove head element of queue and return it, if queue is empty, return nil and error
|
||||||
func (q *ArrayQueue[T]) DeQueue() (*T, error) {
|
func (q *ArrayQueue[T]) Dequeue() (T, bool) {
|
||||||
if q.IsEmpty() {
|
var item T
|
||||||
return nil, errors.New("queue is empty")
|
if q.head == q.tail {
|
||||||
|
return item, false
|
||||||
}
|
}
|
||||||
|
item = q.items[q.head]
|
||||||
headItem := q.data[0]
|
q.head++
|
||||||
q.data = q.data[1:]
|
q.size--
|
||||||
q.length--
|
return item, true
|
||||||
|
|
||||||
return &headItem, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear the queue data
|
// Clear the queue data
|
||||||
func (q *ArrayQueue[T]) Clear() {
|
func (q *ArrayQueue[T]) Clear() {
|
||||||
q.data = []T{}
|
capacity := q.capacity
|
||||||
q.length = 0
|
q.items = make([]T, 0, capacity)
|
||||||
|
q.head = 0
|
||||||
|
q.tail = 0
|
||||||
|
q.size = 0
|
||||||
|
q.capacity = capacity
|
||||||
}
|
}
|
||||||
|
|
||||||
// Contain checks if the value is in queue or not
|
// Contain checks if the value is in queue or not
|
||||||
func (q *ArrayQueue[T]) Contain(value T) bool {
|
func (q *ArrayQueue[T]) Contain(value T) bool {
|
||||||
for _, v := range q.data {
|
for _, v := range q.items {
|
||||||
if reflect.DeepEqual(v, value) {
|
if reflect.DeepEqual(v, value) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -84,8 +106,8 @@ func (q *ArrayQueue[T]) Contain(value T) bool {
|
|||||||
// Print queue data
|
// Print queue data
|
||||||
func (q *ArrayQueue[T]) Print() {
|
func (q *ArrayQueue[T]) Print() {
|
||||||
info := "["
|
info := "["
|
||||||
for _, v := range q.data {
|
for i := q.head; i < q.tail; i++ {
|
||||||
info += fmt.Sprintf("%+v, ", v)
|
info += fmt.Sprintf("%+v, ", q.items[i])
|
||||||
}
|
}
|
||||||
info += "]"
|
info += "]"
|
||||||
fmt.Println(info)
|
fmt.Println(info)
|
||||||
|
|||||||
@@ -6,13 +6,13 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/internal"
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestArrayQueue_EnQueue(t *testing.T) {
|
func TestArrayQueue_Enqueue(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestArrayQueue_EnQueue")
|
assert := internal.NewAssert(t, "TestArrayQueue_Enqueue")
|
||||||
|
|
||||||
queue := NewArrayQueue[int]()
|
queue := NewArrayQueue[int](5)
|
||||||
queue.EnQueue(1)
|
queue.Enqueue(1)
|
||||||
queue.EnQueue(2)
|
queue.Enqueue(2)
|
||||||
queue.EnQueue(3)
|
queue.Enqueue(3)
|
||||||
|
|
||||||
expected := []int{1, 2, 3}
|
expected := []int{1, 2, 3}
|
||||||
data := queue.Data()
|
data := queue.Data()
|
||||||
@@ -24,26 +24,30 @@ func TestArrayQueue_EnQueue(t *testing.T) {
|
|||||||
assert.Equal(3, size)
|
assert.Equal(3, size)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestArrayQueue_DeQueue(t *testing.T) {
|
func TestArrayQueue_Dequeue(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestArrayQueue_DeQueue")
|
assert := internal.NewAssert(t, "TestArrayQueue_Dequeue")
|
||||||
|
|
||||||
queue := NewArrayQueue(1, 2, 3)
|
queue := NewArrayQueue[int](4)
|
||||||
|
queue.Enqueue(1)
|
||||||
|
queue.Enqueue(2)
|
||||||
|
queue.Enqueue(3)
|
||||||
|
|
||||||
val, err := queue.DeQueue()
|
val, ok := queue.Dequeue()
|
||||||
if err != nil {
|
assert.Equal(true, ok)
|
||||||
t.Fail()
|
|
||||||
}
|
|
||||||
|
|
||||||
queue.Print()
|
queue.Print()
|
||||||
assert.Equal(1, *val)
|
assert.Equal(1, val)
|
||||||
|
|
||||||
assert.Equal([]int{2, 3}, queue.Data())
|
assert.Equal([]int{2, 3}, queue.Data())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestArrayQueue_Front(t *testing.T) {
|
func TestArrayQueue_Front(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestArrayQueue_Front")
|
assert := internal.NewAssert(t, "TestArrayQueue_Front")
|
||||||
|
|
||||||
queue := NewArrayQueue(1, 2, 3)
|
queue := NewArrayQueue[int](4)
|
||||||
|
queue.Enqueue(1)
|
||||||
|
queue.Enqueue(2)
|
||||||
|
queue.Enqueue(3)
|
||||||
|
|
||||||
val := queue.Front()
|
val := queue.Front()
|
||||||
|
|
||||||
queue.Print()
|
queue.Print()
|
||||||
@@ -55,7 +59,11 @@ func TestArrayQueue_Front(t *testing.T) {
|
|||||||
func TestArrayQueue_Back(t *testing.T) {
|
func TestArrayQueue_Back(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestArrayQueue_Back")
|
assert := internal.NewAssert(t, "TestArrayQueue_Back")
|
||||||
|
|
||||||
queue := NewArrayQueue(1, 2, 3)
|
queue := NewArrayQueue[int](4)
|
||||||
|
queue.Enqueue(1)
|
||||||
|
queue.Enqueue(2)
|
||||||
|
queue.Enqueue(3)
|
||||||
|
|
||||||
val := queue.Back()
|
val := queue.Back()
|
||||||
|
|
||||||
queue.Print()
|
queue.Print()
|
||||||
@@ -67,7 +75,10 @@ func TestArrayQueue_Back(t *testing.T) {
|
|||||||
func TestArrayQueue_Contain(t *testing.T) {
|
func TestArrayQueue_Contain(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestArrayQueue_Contain")
|
assert := internal.NewAssert(t, "TestArrayQueue_Contain")
|
||||||
|
|
||||||
queue := NewArrayQueue(1, 2, 3)
|
queue := NewArrayQueue[int](4)
|
||||||
|
queue.Enqueue(1)
|
||||||
|
queue.Enqueue(2)
|
||||||
|
queue.Enqueue(3)
|
||||||
|
|
||||||
assert.Equal(true, queue.Contain(1))
|
assert.Equal(true, queue.Contain(1))
|
||||||
assert.Equal(false, queue.Contain(4))
|
assert.Equal(false, queue.Contain(4))
|
||||||
@@ -76,11 +87,12 @@ func TestArrayQueue_Contain(t *testing.T) {
|
|||||||
func TestArrayQueue_Clear(t *testing.T) {
|
func TestArrayQueue_Clear(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestArrayQueue_Clear")
|
assert := internal.NewAssert(t, "TestArrayQueue_Clear")
|
||||||
|
|
||||||
queue := NewArrayQueue[int]()
|
queue := NewArrayQueue[int](4)
|
||||||
|
|
||||||
assert.Equal(true, queue.IsEmpty())
|
assert.Equal(true, queue.IsEmpty())
|
||||||
assert.Equal(0, queue.Size())
|
assert.Equal(0, queue.Size())
|
||||||
|
|
||||||
queue.EnQueue(1)
|
queue.Enqueue(1)
|
||||||
assert.Equal(false, queue.IsEmpty())
|
assert.Equal(false, queue.IsEmpty())
|
||||||
assert.Equal(1, queue.Size())
|
assert.Equal(1, queue.Size())
|
||||||
|
|
||||||
|
|||||||
108
datastructure/queue/priorityqueue.go
Normal file
108
datastructure/queue/priorityqueue.go
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
package datastructure
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/v2/lancetconstraints"
|
||||||
|
)
|
||||||
|
|
||||||
|
// PriorityQueue is a priority queue implemented by binary heap tree
|
||||||
|
// type T should implements Compare function in lancetconstraints.Comparator interface.
|
||||||
|
type PriorityQueue[T any] struct {
|
||||||
|
items []T
|
||||||
|
size int
|
||||||
|
comparator lancetconstraints.Comparator
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewPriorityQueue return a pointer of PriorityQueue
|
||||||
|
// param `comparator` is used to compare values in the queue
|
||||||
|
func NewPriorityQueue[T any](capacity int, comparator lancetconstraints.Comparator) *PriorityQueue[T] {
|
||||||
|
return &PriorityQueue[T]{
|
||||||
|
items: make([]T, capacity+1),
|
||||||
|
size: 0,
|
||||||
|
comparator: comparator,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsEmpty checks if the queue is empty or not
|
||||||
|
func (q *PriorityQueue[T]) IsEmpty() bool {
|
||||||
|
return q.size == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsFull checks if the queue capacity is full or not
|
||||||
|
func (q *PriorityQueue[T]) IsFull() bool {
|
||||||
|
return q.size == len(q.items)-1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Data return data slice in the queue
|
||||||
|
func (q *PriorityQueue[T]) Data() []T {
|
||||||
|
data := make([]T, q.size)
|
||||||
|
for i := 1; i < q.size+1; i++ {
|
||||||
|
data[i-1] = q.items[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enqueue insert value into queue
|
||||||
|
func (q *PriorityQueue[T]) Enqueue(val T) error {
|
||||||
|
if q.IsFull() {
|
||||||
|
return errors.New("queue is already full.")
|
||||||
|
}
|
||||||
|
q.size++
|
||||||
|
q.items[q.size] = val
|
||||||
|
q.swim(q.size)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dequeue delete and return max value in queue
|
||||||
|
func (q *PriorityQueue[T]) Dequeue() (T, bool) {
|
||||||
|
var val T
|
||||||
|
if q.IsEmpty() {
|
||||||
|
return val, false
|
||||||
|
}
|
||||||
|
|
||||||
|
max := q.items[1]
|
||||||
|
|
||||||
|
q.swap(1, q.size)
|
||||||
|
q.size--
|
||||||
|
q.sink(1)
|
||||||
|
|
||||||
|
//set zero value for rest values of the queue
|
||||||
|
q.items[q.size+1] = val
|
||||||
|
|
||||||
|
return max, true
|
||||||
|
}
|
||||||
|
|
||||||
|
// swim when child's key is larger than parent's key, exchange them.
|
||||||
|
func (q *PriorityQueue[T]) swim(index int) {
|
||||||
|
for index > 1 && q.comparator.Compare(q.items[index/2], q.items[index]) < 0 {
|
||||||
|
q.swap(index, index/2)
|
||||||
|
index = index / 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// sink when parent's key smaller than child's key, exchange parent's key with larger child's key.
|
||||||
|
func (q *PriorityQueue[T]) sink(index int) {
|
||||||
|
|
||||||
|
for 2*index <= q.size {
|
||||||
|
j := 2 * index
|
||||||
|
|
||||||
|
// get larger child node index
|
||||||
|
if j < q.size && q.comparator.Compare(q.items[j], q.items[j+1]) < 0 {
|
||||||
|
j++
|
||||||
|
}
|
||||||
|
// if parent larger than child, stop
|
||||||
|
if !(q.comparator.Compare(q.items[index], q.items[j]) < 0) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
q.swap(index, j)
|
||||||
|
index = j
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// swap the two values at index i and j
|
||||||
|
func (q *PriorityQueue[T]) swap(i, j int) {
|
||||||
|
q.items[i], q.items[j] = q.items[j], q.items[i]
|
||||||
|
}
|
||||||
60
datastructure/queue/priorityqueue_test.go
Normal file
60
datastructure/queue/priorityqueue_test.go
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package datastructure
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
type intComparator struct{}
|
||||||
|
|
||||||
|
func (c *intComparator) Compare(v1, v2 any) int {
|
||||||
|
val1, _ := v1.(int)
|
||||||
|
val2, _ := v2.(int)
|
||||||
|
|
||||||
|
if val1 < val2 {
|
||||||
|
return -1
|
||||||
|
} else if val1 > val2 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
func TestPriorityQueue_Enqueue(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestPriorityQueue_Enqueue")
|
||||||
|
|
||||||
|
comparator := &intComparator{}
|
||||||
|
pq := NewPriorityQueue[int](10, comparator)
|
||||||
|
|
||||||
|
assert.Equal(true, pq.IsEmpty())
|
||||||
|
assert.Equal(false, pq.IsFull())
|
||||||
|
|
||||||
|
for i := 1; i < 11; i++ {
|
||||||
|
pq.Enqueue(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(true, pq.IsFull())
|
||||||
|
|
||||||
|
queueData := pq.Data()
|
||||||
|
assert.Equal([]int{10, 9, 6, 7, 8, 2, 5, 1, 4, 3}, queueData)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPriorityQueue_Dequeue(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestPriorityQueue_Dequeue")
|
||||||
|
|
||||||
|
comparator := &intComparator{}
|
||||||
|
pq := NewPriorityQueue[int](10, comparator)
|
||||||
|
|
||||||
|
_, ok := pq.Dequeue()
|
||||||
|
assert.Equal(false, ok)
|
||||||
|
|
||||||
|
for i := 1; i < 11; i++ {
|
||||||
|
pq.Enqueue(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
val, ok := pq.Dequeue()
|
||||||
|
assert.Equal(true, ok)
|
||||||
|
assert.Equal(10, val)
|
||||||
|
|
||||||
|
assert.Equal([]int{9, 8, 6, 7, 3, 2, 5, 1, 4}, pq.Data())
|
||||||
|
}
|
||||||
@@ -103,3 +103,34 @@ func (s Set[T]) Intersection(other Set[T]) Set[T] {
|
|||||||
|
|
||||||
return set
|
return set
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SymmetricDifference creates a new set whose element is in set1 or set2, but not in both sets
|
||||||
|
func (s Set[T]) SymmetricDifference(other Set[T]) Set[T] {
|
||||||
|
set := NewSet[T]()
|
||||||
|
s.Iterate(func(value T) {
|
||||||
|
if !other.Contain(value) {
|
||||||
|
set.Add(value)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
other.Iterate(func(value T) {
|
||||||
|
if !s.Contain(value) {
|
||||||
|
set.Add(value)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return set
|
||||||
|
}
|
||||||
|
|
||||||
|
// Minus creates an set of whose element in origin set but not in compared set
|
||||||
|
func (s Set[T]) Minus(comparedSet Set[T]) Set[T] {
|
||||||
|
set := NewSet[T]()
|
||||||
|
|
||||||
|
s.Iterate(func(value T) {
|
||||||
|
if !comparedSet.Contain(value) {
|
||||||
|
set.Add(value)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return set
|
||||||
|
}
|
||||||
|
|||||||
@@ -127,3 +127,23 @@ func TestSet_Intersection(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(expected, intersectionSet)
|
assert.Equal(expected, intersectionSet)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSet_SymmetricDifference(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSet_SymmetricDifference")
|
||||||
|
|
||||||
|
set1 := NewSet(1, 2, 3)
|
||||||
|
set2 := NewSet(2, 3, 4, 5)
|
||||||
|
|
||||||
|
assert.Equal(NewSet(1, 4, 5), set1.SymmetricDifference(set2))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSet_Minus(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSet_Minus")
|
||||||
|
|
||||||
|
set1 := NewSet(1, 2, 3)
|
||||||
|
set2 := NewSet(2, 3, 4, 5)
|
||||||
|
set3 := NewSet(2, 3)
|
||||||
|
|
||||||
|
assert.Equal(NewSet(1), set1.Minus(set2))
|
||||||
|
assert.Equal(NewSet(4, 5), set2.Minus(set3))
|
||||||
|
}
|
||||||
|
|||||||
@@ -12,29 +12,31 @@ import (
|
|||||||
// In BSTree: leftNode < rootNode < rightNode
|
// In BSTree: leftNode < rootNode < rightNode
|
||||||
// type T should implements Compare function in lancetconstraints.Comparator interface.
|
// type T should implements Compare function in lancetconstraints.Comparator interface.
|
||||||
type BSTree[T any] struct {
|
type BSTree[T any] struct {
|
||||||
root *datastructure.TreeNode[T]
|
root *datastructure.TreeNode[T]
|
||||||
|
comparator lancetconstraints.Comparator
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBSTree create a BSTree pointer
|
// NewBSTree create a BSTree pointer
|
||||||
func NewBSTree[T any](rootData T) *BSTree[T] {
|
// param `comparator` is used to compare values in the tree
|
||||||
|
func NewBSTree[T any](rootData T, comparator lancetconstraints.Comparator) *BSTree[T] {
|
||||||
root := datastructure.NewTreeNode(rootData)
|
root := datastructure.NewTreeNode(rootData)
|
||||||
return &BSTree[T]{root}
|
return &BSTree[T]{root, comparator}
|
||||||
}
|
}
|
||||||
|
|
||||||
// InsertNode insert data into BSTree
|
// InsertNode insert data into BSTree
|
||||||
func (t *BSTree[T]) InsertNode(data T, comparator lancetconstraints.Comparator) {
|
func (t *BSTree[T]) InsertNode(data T) {
|
||||||
root := t.root
|
root := t.root
|
||||||
newNode := datastructure.NewTreeNode(data)
|
newNode := datastructure.NewTreeNode(data)
|
||||||
if root == nil {
|
if root == nil {
|
||||||
t.root = newNode
|
t.root = newNode
|
||||||
} else {
|
} else {
|
||||||
insertTreeNode(root, newNode, comparator)
|
insertTreeNode(root, newNode, t.comparator)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeletetNode delete data into BSTree
|
// DeletetNode delete data into BSTree
|
||||||
func (t *BSTree[T]) DeletetNode(data T, comparator lancetconstraints.Comparator) {
|
func (t *BSTree[T]) DeletetNode(data T) {
|
||||||
deleteTreeNode(t.root, data, comparator)
|
deleteTreeNode(t.root, data, t.comparator)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NodeLevel get node level in BSTree
|
// NodeLevel get node level in BSTree
|
||||||
@@ -75,6 +77,29 @@ func (t *BSTree[T]) Depth() int {
|
|||||||
return calculateDepth(t.root, 0)
|
return calculateDepth(t.root, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsSubTree checks if the tree `t` has `subTree` or not
|
||||||
|
func (t *BSTree[T]) HasSubTree(subTree *BSTree[T]) bool {
|
||||||
|
return hasSubTree(t.root, subTree.root, t.comparator)
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T],
|
||||||
|
comparator lancetconstraints.Comparator) bool {
|
||||||
|
res := false
|
||||||
|
|
||||||
|
if superTreeRoot != nil && subTreeRoot != nil {
|
||||||
|
if comparator.Compare(superTreeRoot.Value, subTreeRoot.Value) == 0 {
|
||||||
|
res = isSubTree(superTreeRoot, subTreeRoot, comparator)
|
||||||
|
}
|
||||||
|
if !res {
|
||||||
|
res = hasSubTree(superTreeRoot.Left, subTreeRoot, comparator)
|
||||||
|
}
|
||||||
|
if !res {
|
||||||
|
res = hasSubTree(superTreeRoot.Right, subTreeRoot, comparator)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
// Print the bstree structure
|
// Print the bstree structure
|
||||||
func (t *BSTree[T]) Print() {
|
func (t *BSTree[T]) Print() {
|
||||||
maxLevel := t.NodeLevel(t.root)
|
maxLevel := t.NodeLevel(t.root)
|
||||||
|
|||||||
@@ -21,13 +21,12 @@ func (c *intComparator) Compare(v1, v2 any) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestBSTree_InsertNode(t *testing.T) {
|
func TestBSTree_InsertNode(t *testing.T) {
|
||||||
bstree := NewBSTree(6)
|
bstree := NewBSTree(6, &intComparator{})
|
||||||
|
|
||||||
comparator := &intComparator{}
|
bstree.InsertNode(7)
|
||||||
bstree.InsertNode(7, comparator)
|
bstree.InsertNode(5)
|
||||||
bstree.InsertNode(5, comparator)
|
bstree.InsertNode(2)
|
||||||
bstree.InsertNode(2, comparator)
|
bstree.InsertNode(4)
|
||||||
bstree.InsertNode(4, comparator)
|
|
||||||
|
|
||||||
bstree.Print()
|
bstree.Print()
|
||||||
}
|
}
|
||||||
@@ -35,13 +34,12 @@ func TestBSTree_InsertNode(t *testing.T) {
|
|||||||
func TestBSTree_PreOrderTraverse(t *testing.T) {
|
func TestBSTree_PreOrderTraverse(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestBSTree_PreOrderTraverse")
|
assert := internal.NewAssert(t, "TestBSTree_PreOrderTraverse")
|
||||||
|
|
||||||
bstree := NewBSTree(6)
|
bstree := NewBSTree(6, &intComparator{})
|
||||||
|
|
||||||
comparator := &intComparator{}
|
bstree.InsertNode(7)
|
||||||
bstree.InsertNode(7, comparator)
|
bstree.InsertNode(5)
|
||||||
bstree.InsertNode(5, comparator)
|
bstree.InsertNode(2)
|
||||||
bstree.InsertNode(2, comparator)
|
bstree.InsertNode(4)
|
||||||
bstree.InsertNode(4, comparator)
|
|
||||||
|
|
||||||
acturl := bstree.PreOrderTraverse()
|
acturl := bstree.PreOrderTraverse()
|
||||||
t.Log(acturl)
|
t.Log(acturl)
|
||||||
@@ -51,13 +49,12 @@ func TestBSTree_PreOrderTraverse(t *testing.T) {
|
|||||||
func TestBSTree_PostOrderTraverse(t *testing.T) {
|
func TestBSTree_PostOrderTraverse(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestBSTree_PostOrderTraverse")
|
assert := internal.NewAssert(t, "TestBSTree_PostOrderTraverse")
|
||||||
|
|
||||||
bstree := NewBSTree(6)
|
bstree := NewBSTree(6, &intComparator{})
|
||||||
|
|
||||||
comparator := &intComparator{}
|
bstree.InsertNode(7)
|
||||||
bstree.InsertNode(7, comparator)
|
bstree.InsertNode(5)
|
||||||
bstree.InsertNode(5, comparator)
|
bstree.InsertNode(2)
|
||||||
bstree.InsertNode(2, comparator)
|
bstree.InsertNode(4)
|
||||||
bstree.InsertNode(4, comparator)
|
|
||||||
|
|
||||||
acturl := bstree.PostOrderTraverse()
|
acturl := bstree.PostOrderTraverse()
|
||||||
t.Log(acturl)
|
t.Log(acturl)
|
||||||
@@ -67,13 +64,12 @@ func TestBSTree_PostOrderTraverse(t *testing.T) {
|
|||||||
func TestBSTree_InOrderTraverse(t *testing.T) {
|
func TestBSTree_InOrderTraverse(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestBSTree_InOrderTraverse")
|
assert := internal.NewAssert(t, "TestBSTree_InOrderTraverse")
|
||||||
|
|
||||||
bstree := NewBSTree(6)
|
bstree := NewBSTree(6, &intComparator{})
|
||||||
|
|
||||||
comparator := &intComparator{}
|
bstree.InsertNode(7)
|
||||||
bstree.InsertNode(7, comparator)
|
bstree.InsertNode(5)
|
||||||
bstree.InsertNode(5, comparator)
|
bstree.InsertNode(2)
|
||||||
bstree.InsertNode(2, comparator)
|
bstree.InsertNode(4)
|
||||||
bstree.InsertNode(4, comparator)
|
|
||||||
|
|
||||||
acturl := bstree.InOrderTraverse()
|
acturl := bstree.InOrderTraverse()
|
||||||
t.Log(acturl)
|
t.Log(acturl)
|
||||||
@@ -83,13 +79,12 @@ func TestBSTree_InOrderTraverse(t *testing.T) {
|
|||||||
func TestBSTree_LevelOrderTraverse(t *testing.T) {
|
func TestBSTree_LevelOrderTraverse(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestBSTree_LevelOrderTraverse")
|
assert := internal.NewAssert(t, "TestBSTree_LevelOrderTraverse")
|
||||||
|
|
||||||
bstree := NewBSTree(6)
|
bstree := NewBSTree(6, &intComparator{})
|
||||||
|
|
||||||
comparator := &intComparator{}
|
bstree.InsertNode(7)
|
||||||
bstree.InsertNode(7, comparator)
|
bstree.InsertNode(5)
|
||||||
bstree.InsertNode(5, comparator)
|
bstree.InsertNode(2)
|
||||||
bstree.InsertNode(2, comparator)
|
bstree.InsertNode(4)
|
||||||
bstree.InsertNode(4, comparator)
|
|
||||||
|
|
||||||
bstree.Print()
|
bstree.Print()
|
||||||
|
|
||||||
@@ -101,17 +96,16 @@ func TestBSTree_LevelOrderTraverse(t *testing.T) {
|
|||||||
func TestBSTree_DeletetNode(t *testing.T) {
|
func TestBSTree_DeletetNode(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestBSTree_DeletetNode")
|
assert := internal.NewAssert(t, "TestBSTree_DeletetNode")
|
||||||
|
|
||||||
bstree := NewBSTree(6)
|
bstree := NewBSTree(6, &intComparator{})
|
||||||
|
|
||||||
comparator := &intComparator{}
|
bstree.InsertNode(7)
|
||||||
bstree.InsertNode(7, comparator)
|
bstree.InsertNode(5)
|
||||||
bstree.InsertNode(5, comparator)
|
bstree.InsertNode(2)
|
||||||
bstree.InsertNode(2, comparator)
|
bstree.InsertNode(4)
|
||||||
bstree.InsertNode(4, comparator)
|
|
||||||
|
|
||||||
bstree.Print()
|
bstree.Print()
|
||||||
|
|
||||||
bstree.DeletetNode(4, comparator)
|
bstree.DeletetNode(4)
|
||||||
bstree.Print()
|
bstree.Print()
|
||||||
acturl1 := bstree.InOrderTraverse()
|
acturl1 := bstree.InOrderTraverse()
|
||||||
t.Log(acturl1)
|
t.Log(acturl1)
|
||||||
@@ -128,15 +122,36 @@ func TestBSTree_DeletetNode(t *testing.T) {
|
|||||||
func TestBSTree_Depth(t *testing.T) {
|
func TestBSTree_Depth(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestBSTree_Depth")
|
assert := internal.NewAssert(t, "TestBSTree_Depth")
|
||||||
|
|
||||||
bstree := NewBSTree(6)
|
bstree := NewBSTree(6, &intComparator{})
|
||||||
|
|
||||||
comparator := &intComparator{}
|
bstree.InsertNode(7)
|
||||||
bstree.InsertNode(7, comparator)
|
bstree.InsertNode(5)
|
||||||
bstree.InsertNode(5, comparator)
|
bstree.InsertNode(2)
|
||||||
bstree.InsertNode(2, comparator)
|
bstree.InsertNode(4)
|
||||||
bstree.InsertNode(4, comparator)
|
|
||||||
|
|
||||||
bstree.Print()
|
bstree.Print()
|
||||||
|
|
||||||
assert.Equal(bstree.Depth(), 4)
|
assert.Equal(bstree.Depth(), 4)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBSTree_IsSubTree(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestBSTree_IsSubTree")
|
||||||
|
|
||||||
|
superTree := NewBSTree(8, &intComparator{})
|
||||||
|
superTree.InsertNode(4)
|
||||||
|
superTree.InsertNode(5)
|
||||||
|
superTree.InsertNode(6)
|
||||||
|
superTree.InsertNode(9)
|
||||||
|
superTree.InsertNode(4)
|
||||||
|
|
||||||
|
superTree.Print()
|
||||||
|
|
||||||
|
subTree := NewBSTree(5, &intComparator{})
|
||||||
|
subTree.InsertNode(4)
|
||||||
|
subTree.InsertNode(6)
|
||||||
|
|
||||||
|
subTree.Print()
|
||||||
|
|
||||||
|
assert.Equal(true, superTree.HasSubTree(subTree))
|
||||||
|
assert.Equal(false, subTree.HasSubTree(superTree))
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
func preOrderTraverse[T any](node *datastructure.TreeNode[T]) []T {
|
func preOrderTraverse[T any](node *datastructure.TreeNode[T]) []T {
|
||||||
data := []T{}
|
data := []T{}
|
||||||
if node != nil {
|
if node != nil {
|
||||||
data = append(data, node.Data)
|
data = append(data, node.Value)
|
||||||
data = append(data, preOrderTraverse(node.Left)...)
|
data = append(data, preOrderTraverse(node.Left)...)
|
||||||
data = append(data, preOrderTraverse(node.Right)...)
|
data = append(data, preOrderTraverse(node.Right)...)
|
||||||
}
|
}
|
||||||
@@ -23,7 +23,7 @@ func postOrderTraverse[T any](node *datastructure.TreeNode[T]) []T {
|
|||||||
if node != nil {
|
if node != nil {
|
||||||
data = append(data, preOrderTraverse(node.Left)...)
|
data = append(data, preOrderTraverse(node.Left)...)
|
||||||
data = append(data, preOrderTraverse(node.Right)...)
|
data = append(data, preOrderTraverse(node.Right)...)
|
||||||
data = append(data, node.Data)
|
data = append(data, node.Value)
|
||||||
}
|
}
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
@@ -32,7 +32,7 @@ func inOrderTraverse[T any](node *datastructure.TreeNode[T]) []T {
|
|||||||
data := []T{}
|
data := []T{}
|
||||||
if node != nil {
|
if node != nil {
|
||||||
data = append(data, inOrderTraverse(node.Left)...)
|
data = append(data, inOrderTraverse(node.Left)...)
|
||||||
data = append(data, node.Data)
|
data = append(data, node.Value)
|
||||||
data = append(data, inOrderTraverse(node.Right)...)
|
data = append(data, inOrderTraverse(node.Right)...)
|
||||||
}
|
}
|
||||||
return data
|
return data
|
||||||
@@ -43,7 +43,7 @@ func preOrderPrint[T any](node *datastructure.TreeNode[T]) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("%v, ", node.Data)
|
fmt.Printf("%v, ", node.Value)
|
||||||
preOrderPrint(node.Left)
|
preOrderPrint(node.Left)
|
||||||
preOrderPrint(node.Right)
|
preOrderPrint(node.Right)
|
||||||
}
|
}
|
||||||
@@ -55,7 +55,7 @@ func postOrderPrint[T any](node *datastructure.TreeNode[T]) {
|
|||||||
|
|
||||||
preOrderPrint(node.Left)
|
preOrderPrint(node.Left)
|
||||||
preOrderPrint(node.Right)
|
preOrderPrint(node.Right)
|
||||||
fmt.Printf("%v, ", node.Data)
|
fmt.Printf("%v, ", node.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func inOrderPrint[T any](node *datastructure.TreeNode[T]) {
|
func inOrderPrint[T any](node *datastructure.TreeNode[T]) {
|
||||||
@@ -64,7 +64,7 @@ func inOrderPrint[T any](node *datastructure.TreeNode[T]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inOrderPrint(node.Left)
|
inOrderPrint(node.Left)
|
||||||
fmt.Printf("%v, ", node.Data)
|
fmt.Printf("%v, ", node.Value)
|
||||||
inOrderPrint(node.Right)
|
inOrderPrint(node.Right)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,7 +76,7 @@ func levelOrderTraverse[T any](root *datastructure.TreeNode[T], traversal *[]T)
|
|||||||
|
|
||||||
for len(q) != 0 {
|
for len(q) != 0 {
|
||||||
n, q = q[0], q[1:]
|
n, q = q[0], q[1:]
|
||||||
*traversal = append(*traversal, n.Data)
|
*traversal = append(*traversal, n.Value)
|
||||||
if n.Left != nil {
|
if n.Left != nil {
|
||||||
q = append(q, n.Left)
|
q = append(q, n.Left)
|
||||||
}
|
}
|
||||||
@@ -87,7 +87,7 @@ func levelOrderTraverse[T any](root *datastructure.TreeNode[T], traversal *[]T)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func insertTreeNode[T any](rootNode, newNode *datastructure.TreeNode[T], comparator lancetconstraints.Comparator) {
|
func insertTreeNode[T any](rootNode, newNode *datastructure.TreeNode[T], comparator lancetconstraints.Comparator) {
|
||||||
if comparator.Compare(newNode.Data, rootNode.Data) == -1 {
|
if comparator.Compare(newNode.Value, rootNode.Value) == -1 {
|
||||||
if rootNode.Left == nil {
|
if rootNode.Left == nil {
|
||||||
rootNode.Left = newNode
|
rootNode.Left = newNode
|
||||||
} else {
|
} else {
|
||||||
@@ -107,9 +107,9 @@ func deleteTreeNode[T any](node *datastructure.TreeNode[T], data T, comparator l
|
|||||||
if node == nil {
|
if node == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if comparator.Compare(data, node.Data) == -1 {
|
if comparator.Compare(data, node.Value) == -1 {
|
||||||
node.Left = deleteTreeNode(node.Left, data, comparator)
|
node.Left = deleteTreeNode(node.Left, data, comparator)
|
||||||
} else if comparator.Compare(data, node.Data) == 1 {
|
} else if comparator.Compare(data, node.Value) == 1 {
|
||||||
node.Right = deleteTreeNode(node.Right, data, comparator)
|
node.Right = deleteTreeNode(node.Right, data, comparator)
|
||||||
} else {
|
} else {
|
||||||
if node.Left == nil {
|
if node.Left == nil {
|
||||||
@@ -150,7 +150,7 @@ func printTreeNodes[T any](nodes []*datastructure.TreeNode[T], level, maxLevel i
|
|||||||
newNodes := []*datastructure.TreeNode[T]{}
|
newNodes := []*datastructure.TreeNode[T]{}
|
||||||
for _, node := range nodes {
|
for _, node := range nodes {
|
||||||
if node != nil {
|
if node != nil {
|
||||||
fmt.Printf("%v", node.Data)
|
fmt.Printf("%v", node.Value)
|
||||||
newNodes = append(newNodes, node.Left)
|
newNodes = append(newNodes, node.Left)
|
||||||
newNodes = append(newNodes, node.Right)
|
newNodes = append(newNodes, node.Right)
|
||||||
} else {
|
} else {
|
||||||
@@ -216,6 +216,20 @@ func calculateDepth[T any](node *datastructure.TreeNode[T], depth int) int {
|
|||||||
return max(calculateDepth(node.Left, depth+1), calculateDepth(node.Right, depth+1))
|
return max(calculateDepth(node.Left, depth+1), calculateDepth(node.Right, depth+1))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T], comparator lancetconstraints.Comparator) bool {
|
||||||
|
if subTreeRoot == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if superTreeRoot == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if comparator.Compare(superTreeRoot.Value, subTreeRoot.Value) != 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
res := isSubTree(superTreeRoot.Left, subTreeRoot.Left, comparator) && isSubTree(superTreeRoot.Right, subTreeRoot.Right, comparator)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
func max(a, b int) int {
|
func max(a, b int) int {
|
||||||
if a > b {
|
if a > b {
|
||||||
return a
|
return a
|
||||||
|
|||||||
@@ -21,18 +21,23 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
- [BubbleSort](#BubbleSort)
|
- [Algorithm](#algorithm)
|
||||||
- [CountSort](#CountSort)
|
- [Source](#source)
|
||||||
- [HeapSort](#HeapSort)
|
- [Usage](#usage)
|
||||||
- [InsertionSort](#InsertionSort)
|
- [Index](#index)
|
||||||
- [MergeSort](#MergeSort)
|
- [Documentation](#documentation)
|
||||||
- [QuickSort](#QuickSort)
|
- [<span id="BubbleSort">BubbleSort</span>](#bubblesort)
|
||||||
- [SelectionSort](#SelectionSort)
|
- [<span id="InsertionSort">InsertionSort</span>](#insertionsort)
|
||||||
- [ShellSort](#ShellSort)
|
- [<span id="SelectionSort">SelectionSort</span>](#selectionsort)
|
||||||
- [BinarySearch](#BinarySearch)
|
- [<span id="ShellSort">ShellSort</span>](#shellsort)
|
||||||
- [BinaryIterativeSearch](#BinaryIterativeSearch)
|
- [<span id="QuickSort">QuickSort</span>](#quicksort)
|
||||||
- [LinearSearch](#LinearSearch)
|
- [<span id="HeapSort">HeapSort</span>](#heapsort)
|
||||||
- [LRUCache](#LRUCache)
|
- [<span id="MergeSort">MergeSort</span>](#mergesort)
|
||||||
|
- [<span id="CountSort">CountSort</span>](#countsort)
|
||||||
|
- [<span id="BinarySearch">BinarySearch</span>](#binarysearch)
|
||||||
|
- [<span id="BinaryIterativeSearch">BinaryIterativeSearch</span>](#binaryiterativesearch)
|
||||||
|
- [<span id="LinearSearch">LinearSearch</span>](#linearsearch)
|
||||||
|
- [<span id="LRUCache">LRUCache</span>](#lrucache)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -46,7 +51,7 @@ import (
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -76,9 +81,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.BubbleSort(intSlice, comparator)
|
algorithm.BubbleSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -91,7 +96,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -141,9 +146,9 @@ func main() {
|
|||||||
{Name: "e", Age: 28},
|
{Name: "e", Age: 28},
|
||||||
}
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeople := algorithm.InsertionSort(peoples, comparator)
|
algorithm.InsertionSort(peoples, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[{d 8} {b 10} {c 17} {a 20} {e 28}]
|
fmt.Println(peoples) //[{d 8} {b 10} {c 17} {a 20} {e 28}]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -156,7 +161,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -186,9 +191,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.SelectionSort(intSlice, comparator)
|
algorithm.SelectionSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -201,7 +206,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -231,9 +236,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.ShellSort(intSlice, comparator)
|
algorithm.ShellSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -246,7 +251,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func QuickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) []T
|
func QuickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -276,9 +281,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.QuickSort(intSlice, 0, len(intSlice)-1, comparator)
|
algorithm.QuickSort(intSlice, 0, len(intSlice)-1, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -291,7 +296,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -321,9 +326,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.HeapSort(intSlice, comparator)
|
algorithm.HeapSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -336,7 +341,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func MergeSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) []T
|
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -366,9 +371,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.MergeSort(intSlice, 0, len(intSlice)-1, comparator)
|
algorithm.MergeSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -21,18 +21,23 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
- [BubbleSort](#BubbleSort)
|
- [Algorithm](#algorithm)
|
||||||
- [CountSort](#CountSort)
|
- [源码](#源码)
|
||||||
- [HeapSort](#HeapSort)
|
- [用法](#用法)
|
||||||
- [InsertionSort](#InsertionSort)
|
- [目录](#目录)
|
||||||
- [MergeSort](#MergeSort)
|
- [文档](#文档)
|
||||||
- [QuickSort](#QuickSort)
|
- [<span id="BubbleSort">BubbleSort</span>](#bubblesort)
|
||||||
- [SelectionSort](#SelectionSort)
|
- [<span id="InsertionSort">InsertionSort</span>](#insertionsort)
|
||||||
- [ShellSort](#ShellSort)
|
- [<span id="SelectionSort">SelectionSort</span>](#selectionsort)
|
||||||
- [BinarySearch](#BinarySearch)
|
- [<span id="ShellSort">ShellSort</span>](#shellsort)
|
||||||
- [BinaryIterativeSearch](#BinaryIterativeSearch)
|
- [<span id="QuickSort">QuickSort</span>](#quicksort)
|
||||||
- [LinearSearch](#LinearSearch)
|
- [<span id="HeapSort">HeapSort</span>](#heapsort)
|
||||||
- [LRUCache](#LRUCache)
|
- [<span id="MergeSort">MergeSort</span>](#mergesort)
|
||||||
|
- [<span id="CountSort">CountSort</span>](#countsort)
|
||||||
|
- [<span id="BinarySearch">BinarySearch</span>](#binarysearch)
|
||||||
|
- [<span id="BinaryIterativeSearch">BinaryIterativeSearch</span>](#binaryiterativesearch)
|
||||||
|
- [<span id="LinearSearch">LinearSearch</span>](#linearsearch)
|
||||||
|
- [<span id="LRUCache">LRUCache</span>](#lrucache)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -46,7 +51,7 @@ import (
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -76,9 +81,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.BubbleSort(intSlice, comparator)
|
algorithm.BubbleSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -91,7 +96,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -141,9 +146,9 @@ func main() {
|
|||||||
{Name: "e", Age: 28},
|
{Name: "e", Age: 28},
|
||||||
}
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeople := algorithm.InsertionSort(peoples, comparator)
|
algorithm.InsertionSort(peoples, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[{d 8} {b 10} {c 17} {a 20} {e 28}]
|
fmt.Println(intSlice) //[{d 8} {b 10} {c 17} {a 20} {e 28}]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -156,7 +161,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -186,9 +191,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.SelectionSort(intSlice, comparator)
|
algorithm.SelectionSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -201,7 +206,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -231,9 +236,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.ShellSort(intSlice, comparator)
|
algorithm.ShellSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -276,9 +281,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.QuickSort(intSlice, 0, len(intSlice)-1, comparator)
|
algorithm.QuickSort(intSlice, 0, len(intSlice)-1, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -321,9 +326,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.HeapSort(intSlice, comparator)
|
algorithm.HeapSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -336,7 +341,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func MergeSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) []T
|
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -366,9 +371,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.MergeSort(intSlice, 0, len(intSlice)-1, comparator)
|
algorithm.MergeSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
390
docs/concurrency.md
Normal file
390
docs/concurrency.md
Normal file
@@ -0,0 +1,390 @@
|
|||||||
|
# Concurrency
|
||||||
|
Package concurrency contain some functions to support concurrent programming. eg, goroutine, channel, async.
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Source:
|
||||||
|
|
||||||
|
- [https://github.com/duke-git/lancet/blob/main/concurrency/channel.go](https://github.com/duke-git/lancet/blob/main/concurrency/channel.go)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Usage:
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Index
|
||||||
|
### Channel
|
||||||
|
- [NewChannel](#NewChannel)
|
||||||
|
- [Bridge](#Bridge)
|
||||||
|
- [FanIn](#FanIn)
|
||||||
|
- [Generate](#Generate)
|
||||||
|
- [Or](#Or)
|
||||||
|
- [OrDone](#OrDone)
|
||||||
|
- [Repeat](#Repeat)
|
||||||
|
- [RepeatFn](#RepeatFn)
|
||||||
|
- [Take](#Take)
|
||||||
|
- [Tee](#Tee)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
## Channel
|
||||||
|
### <span id="NewChannel">NewChannel</span>
|
||||||
|
<p>return a Channel pointer instance.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type Channel struct {}
|
||||||
|
func NewChannel() *Channel
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
c := concurrency.NewChannel()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Bridge">Bridge</span>
|
||||||
|
|
||||||
|
<p>Link multiple channels into one channel until cancel the context.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (c *Channel) Bridge(ctx context.Context, chanStream <-chan <-chan any) <-chan any
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
c := concurrency.NewChannel()
|
||||||
|
genVals := func() <-chan <-chan any {
|
||||||
|
chanStream := make(chan (<-chan any))
|
||||||
|
go func() {
|
||||||
|
defer close(chanStream)
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
stream := make(chan any, 1)
|
||||||
|
stream <- i
|
||||||
|
close(stream)
|
||||||
|
chanStream <- stream
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return chanStream
|
||||||
|
}
|
||||||
|
|
||||||
|
index := 0
|
||||||
|
for val := range c.Bridge(ctx, genVals()) {
|
||||||
|
fmt.Printf("%v ", val) //0 1 2 3 4 5 6 7 8 9
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="FanIn">FanIn</span>
|
||||||
|
|
||||||
|
<p>merge multiple channels into one channel until cancel the context.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (c *Channel) FanIn(ctx context.Context, channels ...<-chan any) <-chan any
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
c := concurrency.NewChannel()
|
||||||
|
channels := make([]<-chan any, 3)
|
||||||
|
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
channels[i] = c.Take(ctx, c.Repeat(ctx, i), 3)
|
||||||
|
}
|
||||||
|
|
||||||
|
mergedChannel := c.FanIn(ctx, channels...)
|
||||||
|
|
||||||
|
for val := range mergedChannel {
|
||||||
|
fmt.Println("\t%d\n", val) //1,2,1,0,0,1,0,2,2 (order not for sure)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Repeat">Repeat</span>
|
||||||
|
|
||||||
|
<p>Return a chan, put param `values` into the chan repeatly until cancel the context.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (c *Channel) Repeat(ctx context.Context, values ...any) <-chan any
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
c := concurrency.NewChannel()
|
||||||
|
intStream := c.Take(ctx, c.Repeat(ctx, 1, 2), 5)
|
||||||
|
|
||||||
|
for v := range intStream {
|
||||||
|
fmt.Println(v) //1, 2, 1, 2, 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="RepeatFn">RepeatFn</span>
|
||||||
|
|
||||||
|
<p>Return a chan, excutes fn repeatly, and put the result into retruned chan until cancel context.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (c *Channel) RepeatFn(ctx context.Context, fn func() any) <-chan any
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
fn := func() any {
|
||||||
|
s := "a"
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
c := concurrency.NewChannel()
|
||||||
|
dataStream := c.Take(ctx, c.RepeatFn(ctx, fn), 3)
|
||||||
|
|
||||||
|
for v := range dataStream {
|
||||||
|
fmt.Println(v) //a, a, a
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Or">Or</span>
|
||||||
|
|
||||||
|
<p>Read one or more channels into one channel, will close when any readin channel is closed.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (c *Channel) Or(channels ...<-chan any) <-chan any
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
sig := func(after time.Duration) <-chan any {
|
||||||
|
c := make(chan interface{})
|
||||||
|
go func() {
|
||||||
|
defer close(c)
|
||||||
|
time.Sleep(after)
|
||||||
|
}()
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
|
c := concurrency.NewChannel()
|
||||||
|
<-c.Or(
|
||||||
|
sig(1*time.Second),
|
||||||
|
sig(2*time.Second),
|
||||||
|
sig(3*time.Second),
|
||||||
|
sig(4*time.Second),
|
||||||
|
sig(5*time.Second),
|
||||||
|
)
|
||||||
|
|
||||||
|
fmt.Println("done after %v", time.Since(start)) //1.003s
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="OrDone">OrDone</span>
|
||||||
|
|
||||||
|
<p>Read a channel into another channel, will close until cancel context.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (c *Channel) OrDone(ctx context.Context, channel <-chan any) <-chan any
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
c := concurrency.NewChannel()
|
||||||
|
intStream := c.Take(ctx, c.Repeat(ctx, 1), 3)
|
||||||
|
|
||||||
|
for val := range c.OrDone(ctx, intStream) {
|
||||||
|
fmt.Println(val) //1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Take">Take</span>
|
||||||
|
|
||||||
|
<p>Return a chan whose values are tahken from another chan until cancel context.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (c *Channel) Take(ctx context.Context, valueStream <-chan any, number int) <-chan any
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
numbers := make(chan any, 5)
|
||||||
|
numbers <- 1
|
||||||
|
numbers <- 2
|
||||||
|
numbers <- 3
|
||||||
|
numbers <- 4
|
||||||
|
numbers <- 5
|
||||||
|
defer close(numbers)
|
||||||
|
|
||||||
|
c := concurrency.NewChannel()
|
||||||
|
intStream := c.Take(ctx, numbers, 3)
|
||||||
|
|
||||||
|
for val := range intStream {
|
||||||
|
fmt.Println(val) //1, 2, 3
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Tee">Tee</span>
|
||||||
|
|
||||||
|
<p>Split one chanel into two channels until cancel context.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (c *Channel) Tee(ctx context.Context, in <-chan any) (<-chan any, <-chan any)
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
c := concurrency.NewChannel()
|
||||||
|
inStream := c.Take(ctx, c.Repeat(ctx, 1, 2), 4)
|
||||||
|
|
||||||
|
out1, out2 := c.Tee(ctx, inStream)
|
||||||
|
for val := range out1 {
|
||||||
|
fmt.Println(val) //1
|
||||||
|
fmt.Println(<-out2) //1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
390
docs/concurrency_zh-CN.md
Normal file
390
docs/concurrency_zh-CN.md
Normal file
@@ -0,0 +1,390 @@
|
|||||||
|
# Concurrency
|
||||||
|
并发包包含一些支持并发编程的功能。例如:goroutine, channel, async等。
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 源码:
|
||||||
|
|
||||||
|
- [https://github.com/duke-git/lancet/blob/main/concurrency/channel.go](https://github.com/duke-git/lancet/blob/main/concurrency/channel.go)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 用法:
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 目录
|
||||||
|
### Channel
|
||||||
|
- [NewChannel](#NewChannel)
|
||||||
|
- [Bridge](#Bridge)
|
||||||
|
- [FanIn](#FanIn)
|
||||||
|
- [Generate](#Generate)
|
||||||
|
- [Or](#Or)
|
||||||
|
- [OrDone](#OrDone)
|
||||||
|
- [Repeat](#Repeat)
|
||||||
|
- [RepeatFn](#RepeatFn)
|
||||||
|
- [Take](#Take)
|
||||||
|
- [Tee](#Tee)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 文档
|
||||||
|
|
||||||
|
|
||||||
|
### Channel
|
||||||
|
### <span id="NewChannel">NewChannel</span>
|
||||||
|
<p>返回一个 Channel 指针实例</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type Channel struct {}
|
||||||
|
func NewChannel() *Channel
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
c := concurrency.NewChannel()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Bridge">Bridge</span>
|
||||||
|
|
||||||
|
<p>将多个通道链接到一个通道,直到取消上下文。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (c *Channel) Bridge(ctx context.Context, chanStream <-chan <-chan any) <-chan any
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
c := concurrency.NewChannel()
|
||||||
|
genVals := func() <-chan <-chan any {
|
||||||
|
chanStream := make(chan (<-chan any))
|
||||||
|
go func() {
|
||||||
|
defer close(chanStream)
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
stream := make(chan any, 1)
|
||||||
|
stream <- i
|
||||||
|
close(stream)
|
||||||
|
chanStream <- stream
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return chanStream
|
||||||
|
}
|
||||||
|
|
||||||
|
index := 0
|
||||||
|
for val := range c.Bridge(ctx, genVals()) {
|
||||||
|
fmt.Printf("%v ", val) //0 1 2 3 4 5 6 7 8 9
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="FanIn">FanIn</span>
|
||||||
|
|
||||||
|
<p>将多个通道合并为一个通道,直到取消上下文</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (c *Channel) FanIn(ctx context.Context, channels ...<-chan any) <-chan any
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
c := concurrency.NewChannel()
|
||||||
|
channels := make([]<-chan any, 3)
|
||||||
|
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
channels[i] = c.Take(ctx, c.Repeat(ctx, i), 3)
|
||||||
|
}
|
||||||
|
|
||||||
|
mergedChannel := c.FanIn(ctx, channels...)
|
||||||
|
|
||||||
|
for val := range mergedChannel {
|
||||||
|
fmt.Println("\t%d\n", val) //1,2,1,0,0,1,0,2,2 (order not for sure)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Repeat">Repeat</span>
|
||||||
|
|
||||||
|
<p>返回一个chan,将参数`values`重复放入chan,直到取消上下文。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (c *Channel) Repeat(ctx context.Context, values ...any) <-chan any
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
c := concurrency.NewChannel()
|
||||||
|
intStream := c.Take(ctx, c.Repeat(ctx, 1, 2), 5)
|
||||||
|
|
||||||
|
for v := range intStream {
|
||||||
|
fmt.Println(v) //1, 2, 1, 2, 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="RepeatFn">RepeatFn</span>
|
||||||
|
|
||||||
|
<p>返回一个chan,重复执行函数fn,并将结果放入返回的chan,直到取消上下文。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (c *Channel) RepeatFn(ctx context.Context, fn func() any) <-chan any
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
fn := func() any {
|
||||||
|
s := "a"
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
c := concurrency.NewChannel()
|
||||||
|
dataStream := c.Take(ctx, c.RepeatFn(ctx, fn), 3)
|
||||||
|
|
||||||
|
for v := range dataStream {
|
||||||
|
fmt.Println(v) //a, a, a
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Or">Or</span>
|
||||||
|
|
||||||
|
<p>将一个或多个通道读取到一个通道中,当任何读取通道关闭时将结束读取。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (c *Channel) Or(channels ...<-chan any) <-chan any
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
sig := func(after time.Duration) <-chan any {
|
||||||
|
c := make(chan interface{})
|
||||||
|
go func() {
|
||||||
|
defer close(c)
|
||||||
|
time.Sleep(after)
|
||||||
|
}()
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
|
c := concurrency.NewChannel()
|
||||||
|
<-c.Or(
|
||||||
|
sig(1*time.Second),
|
||||||
|
sig(2*time.Second),
|
||||||
|
sig(3*time.Second),
|
||||||
|
sig(4*time.Second),
|
||||||
|
sig(5*time.Second),
|
||||||
|
)
|
||||||
|
|
||||||
|
fmt.Println("done after %v", time.Since(start)) //1.003s
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="OrDone">OrDone</span>
|
||||||
|
|
||||||
|
<p>将一个通道读入另一个通道,直到取消上下文。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (c *Channel) OrDone(ctx context.Context, channel <-chan any) <-chan any
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
c := concurrency.NewChannel()
|
||||||
|
intStream := c.Take(ctx, c.Repeat(ctx, 1), 3)
|
||||||
|
|
||||||
|
for val := range c.OrDone(ctx, intStream) {
|
||||||
|
fmt.Println(val) //1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Take">Take</span>
|
||||||
|
|
||||||
|
<p>返回一个chan,其值从另一个chan获取,直到取消上下文。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (c *Channel) Take(ctx context.Context, valueStream <-chan any, number int) <-chan any
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
numbers := make(chan any, 5)
|
||||||
|
numbers <- 1
|
||||||
|
numbers <- 2
|
||||||
|
numbers <- 3
|
||||||
|
numbers <- 4
|
||||||
|
numbers <- 5
|
||||||
|
defer close(numbers)
|
||||||
|
|
||||||
|
c := concurrency.NewChannel()
|
||||||
|
intStream := c.Take(ctx, numbers, 3)
|
||||||
|
|
||||||
|
for val := range intStream {
|
||||||
|
fmt.Println(val) //1, 2, 3
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Tee">Tee</span>
|
||||||
|
|
||||||
|
<p>将一个通道分成两个通道,直到取消上下文。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (c *Channel) Tee(ctx context.Context, in <-chan any) (<-chan any, <-chan any)
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/concurrency"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
c := concurrency.NewChannel()
|
||||||
|
inStream := c.Take(ctx, c.Repeat(ctx, 1, 2), 4)
|
||||||
|
|
||||||
|
out1, out2 := c.Tee(ctx, inStream)
|
||||||
|
for val := range out1 {
|
||||||
|
fmt.Println(val) //1
|
||||||
|
fmt.Println(<-out2) //1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
304
docs/maputil.md
Normal file
304
docs/maputil.md
Normal file
@@ -0,0 +1,304 @@
|
|||||||
|
# Maputil
|
||||||
|
Package maputil includes some functions to manipulate map.
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Source:
|
||||||
|
|
||||||
|
- [https://github.com/duke-git/lancet/blob/main/maputil/maputil.go](https://github.com/duke-git/lancet/blob/main/maputil/maputil.go)
|
||||||
|
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Example:
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Index
|
||||||
|
- [ForEach](#ForEach)
|
||||||
|
- [Filter](#Filter)
|
||||||
|
- [Intersect](#Intersect)
|
||||||
|
- [Keys](#Keys)
|
||||||
|
- [Merge](#Merge)
|
||||||
|
- [Minus](#Minus)
|
||||||
|
- [Values](#Values)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ForEach">ForEach</span>
|
||||||
|
<p>Executes iteratee funcation for every key and value pair in map.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V))
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
"d": 4,
|
||||||
|
}
|
||||||
|
|
||||||
|
var sum int
|
||||||
|
|
||||||
|
maputil.ForEach(m, func(_ string, value int) {
|
||||||
|
sum += value
|
||||||
|
})
|
||||||
|
fmt.Println(sum) // 10
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Filter">Filter</span>
|
||||||
|
<p>Iterates over map, return a new map contains all key and value pairs pass the predicate function.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
"d": 4,
|
||||||
|
"e": 5,
|
||||||
|
}
|
||||||
|
isEven := func(_ string, value int) bool {
|
||||||
|
return value%2 == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
maputil.Filter(m, func(_ string, value int) {
|
||||||
|
sum += value
|
||||||
|
})
|
||||||
|
res := maputil.Filter(m, isEven)
|
||||||
|
fmt.Println(res) // map[string]int{"b": 2, "d": 4,}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Intersect">Intersect</span>
|
||||||
|
<p>Iterates over maps, return a new map of key and value pairs in all given maps.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Intersect[K comparable, V any](maps ...map[K]V) map[K]V
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m1 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
m2 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 6,
|
||||||
|
"d": 7,
|
||||||
|
}
|
||||||
|
|
||||||
|
m3 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 9,
|
||||||
|
"e": 9,
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(maputil.Intersect(m1)) // map[string]int{"a": 1, "b": 2, "c": 3}
|
||||||
|
|
||||||
|
fmt.Println(maputil.Intersect(m1, m2)) // map[string]int{"a": 1, "b": 2}
|
||||||
|
|
||||||
|
fmt.Println(maputil.Intersect(m1, m2, m3)) // map[string]int{"a": 1}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Keys">Keys</span>
|
||||||
|
<p>Returns a slice of the map's keys.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Keys[K comparable, V any](m map[K]V) []K
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "a",
|
||||||
|
3: "b",
|
||||||
|
4: "c",
|
||||||
|
5: "d",
|
||||||
|
}
|
||||||
|
|
||||||
|
keys := maputil.Keys(m)
|
||||||
|
sort.Ints(keys)
|
||||||
|
fmt.Println(keys) // []int{1, 2, 3, 4, 5}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Merge">Merge</span>
|
||||||
|
<p>Merge maps, next key will overwrite previous key.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Merge[K comparable, V any](maps ...map[K]V) map[K]V
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m1 := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "b",
|
||||||
|
}
|
||||||
|
m2 := map[int]string{
|
||||||
|
1: "1",
|
||||||
|
3: "2",
|
||||||
|
}
|
||||||
|
fmt.Println(maputil.Merge(m1, m2)) // map[int]string{1:"1", 2:"b", 3:"2",}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Minus">Minus</span>
|
||||||
|
<p>Creates an map of whose key in mapA but not in mapB.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m1 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
m2 := map[string]int{
|
||||||
|
"a": 11,
|
||||||
|
"b": 22,
|
||||||
|
"d": 33,
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(maputil.Minus(m1, m2)) //map[string]int{"c": 3}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Values">Values</span>
|
||||||
|
<p>Returns a slice of the map's values.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Values[K comparable, V any](m map[K]V) []V
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "a",
|
||||||
|
3: "b",
|
||||||
|
4: "c",
|
||||||
|
5: "d",
|
||||||
|
}
|
||||||
|
|
||||||
|
values := maputil.Values(m)
|
||||||
|
sort.Strings(values)
|
||||||
|
|
||||||
|
fmt.Println(values) // []string{"a", "a", "b", "c", "d"}
|
||||||
|
}
|
||||||
|
```
|
||||||
304
docs/maputil_zh-CN.md
Normal file
304
docs/maputil_zh-CN.md
Normal file
@@ -0,0 +1,304 @@
|
|||||||
|
# Maputil
|
||||||
|
maputil包包括一些操作map的函数。
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 源码:
|
||||||
|
|
||||||
|
- [https://github.com/duke-git/lancet/blob/main/maputil/maputil.go](https://github.com/duke-git/lancet/blob/main/maputil/maputil.go)
|
||||||
|
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 用法:
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 目录:
|
||||||
|
- [ForEach](#ForEach)
|
||||||
|
- [Filter](#Filter)
|
||||||
|
- [Intersect](#Intersect)
|
||||||
|
- [Keys](#Keys)
|
||||||
|
- [Merge](#Merge)
|
||||||
|
- [Minus](#Minus)
|
||||||
|
- [Values](#Values)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## API文档:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ForEach">ForEach</span>
|
||||||
|
<p>对map中的每对key和value执行iteratee函数</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V))
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
"d": 4,
|
||||||
|
}
|
||||||
|
|
||||||
|
var sum int
|
||||||
|
|
||||||
|
maputil.ForEach(m, func(_ string, value int) {
|
||||||
|
sum += value
|
||||||
|
})
|
||||||
|
fmt.Println(sum) // 10
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Filter">Filter</span>
|
||||||
|
<p>迭代map中的每对key和value, 返回符合predicate函数的key, value</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
"d": 4,
|
||||||
|
"e": 5,
|
||||||
|
}
|
||||||
|
isEven := func(_ string, value int) bool {
|
||||||
|
return value%2 == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
maputil.Filter(m, func(_ string, value int) {
|
||||||
|
sum += value
|
||||||
|
})
|
||||||
|
res := maputil.Filter(m, isEven)
|
||||||
|
fmt.Println(res) // map[string]int{"b": 2, "d": 4,}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Intersect">Intersect</span>
|
||||||
|
<p>多个map的交集操作</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Intersect[K comparable, V any](maps ...map[K]V) map[K]V
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m1 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
m2 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 6,
|
||||||
|
"d": 7,
|
||||||
|
}
|
||||||
|
|
||||||
|
m3 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 9,
|
||||||
|
"e": 9,
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(maputil.Intersect(m1)) // map[string]int{"a": 1, "b": 2, "c": 3}
|
||||||
|
|
||||||
|
fmt.Println(maputil.Intersect(m1, m2)) // map[string]int{"a": 1, "b": 2}
|
||||||
|
|
||||||
|
fmt.Println(maputil.Intersect(m1, m2, m3)) // map[string]int{"a": 1}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Keys">Keys</span>
|
||||||
|
<p>返回map中所有key的切片</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Keys[K comparable, V any](m map[K]V) []K
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "a",
|
||||||
|
3: "b",
|
||||||
|
4: "c",
|
||||||
|
5: "d",
|
||||||
|
}
|
||||||
|
|
||||||
|
keys := maputil.Keys(m)
|
||||||
|
sort.Ints(keys)
|
||||||
|
fmt.Println(keys) // []int{1, 2, 3, 4, 5}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Merge">Merge</span>
|
||||||
|
<p>合并多个maps, 相同的key会被后来的key覆盖</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Merge[K comparable, V any](maps ...map[K]V) map[K]V
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m1 := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "b",
|
||||||
|
}
|
||||||
|
m2 := map[int]string{
|
||||||
|
1: "1",
|
||||||
|
3: "2",
|
||||||
|
}
|
||||||
|
fmt.Println(maputil.Merge(m1, m2)) // map[int]string{1:"1", 2:"b", 3:"2",}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Minus">Minus</span>
|
||||||
|
<p>返回一个map,其中的key存在于mapA,不存在于mapB.</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m1 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
m2 := map[string]int{
|
||||||
|
"a": 11,
|
||||||
|
"b": 22,
|
||||||
|
"d": 33,
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(maputil.Minus(m1, m2)) //map[string]int{"c": 3}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Values">Values</span>
|
||||||
|
<p>返回map中所有value的切片</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Values[K comparable, V any](m map[K]V) []V
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "a",
|
||||||
|
3: "b",
|
||||||
|
4: "c",
|
||||||
|
5: "d",
|
||||||
|
}
|
||||||
|
|
||||||
|
values := maputil.Values(m)
|
||||||
|
sort.Strings(values)
|
||||||
|
|
||||||
|
fmt.Println(values) // []string{"a", "a", "b", "c", "d"}
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -51,6 +51,7 @@ import (
|
|||||||
- [SortByField](#SortByField)
|
- [SortByField](#SortByField)
|
||||||
- [Some](#Some)
|
- [Some](#Some)
|
||||||
- [StringSlice](#StringSlice)
|
- [StringSlice](#StringSlice)
|
||||||
|
- [SymmetricDifference](#SymmetricDifference)
|
||||||
- [Unique](#Unique)
|
- [Unique](#Unique)
|
||||||
- [Union](#Union)
|
- [Union](#Union)
|
||||||
- [UpdateAt](#UpdateAt)
|
- [UpdateAt](#UpdateAt)
|
||||||
@@ -909,6 +910,35 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="SymmetricDifference">SymmetricDifference</span>
|
||||||
|
<p>Create a slice whose element is in given slices, but not in both slices.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func SymmetricDifference[T any](slices ...[]T) []T
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
s1 := []int{1, 2, 3}
|
||||||
|
s2 := []int{1, 2, 4}
|
||||||
|
s3 := []int{1, 2, 3, 5}
|
||||||
|
|
||||||
|
fmt.Println(slice.SymmetricDifference(s1)) //[]int{1, 2, 3}
|
||||||
|
fmt.Println(slice.SymmetricDifference(s1, s2)) //[]int{3, 4}
|
||||||
|
fmt.Println(slice.SymmetricDifference(s1, s2, s3)) //[]int{3, 4, 5}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Unique">Unique</span>
|
### <span id="Unique">Unique</span>
|
||||||
<p>Remove duplicate elements in slice.</p>
|
<p>Remove duplicate elements in slice.</p>
|
||||||
|
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ import (
|
|||||||
- [SortByField](#SortByField)
|
- [SortByField](#SortByField)
|
||||||
- [Some](#Some)
|
- [Some](#Some)
|
||||||
- [StringSlice](#StringSlice)
|
- [StringSlice](#StringSlice)
|
||||||
|
- [SymmetricDifference](#SymmetricDifference)
|
||||||
- [Unique](#Unique)
|
- [Unique](#Unique)
|
||||||
- [Union](#Union)
|
- [Union](#Union)
|
||||||
- [UpdateAt](#UpdateAt)
|
- [UpdateAt](#UpdateAt)
|
||||||
@@ -909,6 +910,34 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="SymmetricDifference">SymmetricDifference</span>
|
||||||
|
<p>返回一个切片,其中的元素存在于参数切片中,但不同时存储在于参数切片中(交集取反)</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func SymmetricDifference[T any](slices ...[]T) []T
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
s1 := []int{1, 2, 3}
|
||||||
|
s2 := []int{1, 2, 4}
|
||||||
|
s3 := []int{1, 2, 3, 5}
|
||||||
|
|
||||||
|
fmt.Println(slice.SymmetricDifference(s1)) //[]int{1, 2, 3}
|
||||||
|
fmt.Println(slice.SymmetricDifference(s1, s2)) //[]int{3, 4}
|
||||||
|
fmt.Println(slice.SymmetricDifference(s1, s2, s3)) //[]int{3, 4, 5}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Unique">Unique</span>
|
### <span id="Unique">Unique</span>
|
||||||
<p>删除切片中的重复元素</p>
|
<p>删除切片中的重复元素</p>
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ import (
|
|||||||
- [PadStart](#PadStart)
|
- [PadStart](#PadStart)
|
||||||
- [ReverseStr](#ReverseStr)
|
- [ReverseStr](#ReverseStr)
|
||||||
- [SnakeCase](#SnakeCase)
|
- [SnakeCase](#SnakeCase)
|
||||||
|
- [SplitEx](#SplitEx)
|
||||||
- [Wrap](#Wrap)
|
- [Wrap](#Wrap)
|
||||||
|
|
||||||
- [Unwrap](#Unwrap)
|
- [Unwrap](#Unwrap)
|
||||||
@@ -493,6 +494,43 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="SplitEx">SplitEx</span>
|
||||||
|
<p>Split a given string whether the result contains empty string.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func SplitEx(s, sep string, removeEmptyString bool) []string
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
arr1 := strutil.SplitEx(" a b c ", "", true)
|
||||||
|
fmt.Println(arr1) //[]string{}
|
||||||
|
|
||||||
|
arr2 := strutil.SplitEx(" a b c ", " ", false)
|
||||||
|
fmt.Println(arr2) //[]string{"", "a", "b", "c", ""}
|
||||||
|
|
||||||
|
arr3 := strutil.SplitEx(" a b c ", " ", true)
|
||||||
|
fmt.Println(arr3) //[]string{"a", "b", "c"}
|
||||||
|
|
||||||
|
arr4 := strutil.SplitEx(" a = b = c = ", " = ", false)
|
||||||
|
fmt.Println(arr4) //[]string{" a", "b", "c", ""}
|
||||||
|
|
||||||
|
arr5 := strutil.SplitEx(" a = b = c = ", " = ", true)
|
||||||
|
fmt.Println(arr5) //[]string{" a", "b", "c"}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Wrap">Wrap</span>
|
### <span id="Wrap">Wrap</span>
|
||||||
<p>Wrap a string with another string.</p>
|
<p>Wrap a string with another string.</p>
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ import (
|
|||||||
- [PadStart](#PadStart)
|
- [PadStart](#PadStart)
|
||||||
- [ReverseStr](#ReverseStr)
|
- [ReverseStr](#ReverseStr)
|
||||||
- [SnakeCase](#SnakeCase)
|
- [SnakeCase](#SnakeCase)
|
||||||
|
- [SplitEx](#SplitEx)
|
||||||
- [Wrap](#Wrap)
|
- [Wrap](#Wrap)
|
||||||
|
|
||||||
- [Unwrap](#Unwrap)
|
- [Unwrap](#Unwrap)
|
||||||
@@ -493,6 +494,41 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="SplitEx">SplitEx</span>
|
||||||
|
<p>分割字符串为切片,removeEmptyString参数指定是否去除空字符串</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func SplitEx(s, sep string, removeEmptyString bool) []string
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
arr1 := strutil.SplitEx(" a b c ", "", true)
|
||||||
|
fmt.Println(arr1) //[]string{}
|
||||||
|
|
||||||
|
arr2 := strutil.SplitEx(" a b c ", " ", false)
|
||||||
|
fmt.Println(arr2) //[]string{"", "a", "b", "c", ""}
|
||||||
|
|
||||||
|
arr3 := strutil.SplitEx(" a b c ", " ", true)
|
||||||
|
fmt.Println(arr3) //[]string{"a", "b", "c"}
|
||||||
|
|
||||||
|
arr4 := strutil.SplitEx(" a = b = c = ", " = ", false)
|
||||||
|
fmt.Println(arr4) //[]string{" a", "b", "c", ""}
|
||||||
|
|
||||||
|
arr5 := strutil.SplitEx(" a = b = c = ", " = ", true)
|
||||||
|
fmt.Println(arr5) //[]string{" a", "b", "c"}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Wrap">Wrap</span>
|
### <span id="Wrap">Wrap</span>
|
||||||
<p>用另一个字符串包裹一个字符串</p>
|
<p>用另一个字符串包裹一个字符串</p>
|
||||||
|
|||||||
106
maputil/map.go
Normal file
106
maputil/map.go
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
|
||||||
|
// Use of this source code is governed by MIT license
|
||||||
|
|
||||||
|
// Package maputil includes some functions to manipulate map.
|
||||||
|
package maputil
|
||||||
|
|
||||||
|
import "reflect"
|
||||||
|
|
||||||
|
// Keys returns a slice of the map's keys
|
||||||
|
func Keys[K comparable, V any](m map[K]V) []K {
|
||||||
|
keys := make([]K, 0, len(m))
|
||||||
|
|
||||||
|
for k := range m {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
|
||||||
|
// Values returns a slice of the map's values
|
||||||
|
func Values[K comparable, V any](m map[K]V) []V {
|
||||||
|
values := make([]V, 0, len(m))
|
||||||
|
|
||||||
|
for _, v := range m {
|
||||||
|
values = append(values, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return values
|
||||||
|
}
|
||||||
|
|
||||||
|
// Merge maps, next key will overwrite previous key
|
||||||
|
func Merge[K comparable, V any](maps ...map[K]V) map[K]V {
|
||||||
|
res := make(map[K]V, 0)
|
||||||
|
|
||||||
|
for _, m := range maps {
|
||||||
|
for k, v := range m {
|
||||||
|
res[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
// ForEach executes iteratee funcation for every key and value pair in map
|
||||||
|
func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V)) {
|
||||||
|
for k, v := range m {
|
||||||
|
iteratee(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter iterates over map, return a new map contains all key and value pairs pass the predicate function
|
||||||
|
func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V {
|
||||||
|
res := make(map[K]V)
|
||||||
|
|
||||||
|
for k, v := range m {
|
||||||
|
if predicate(k, v) {
|
||||||
|
res[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
// Intersect iterates over maps, return a new map of key and value pairs in all given maps
|
||||||
|
func Intersect[K comparable, V any](maps ...map[K]V) map[K]V {
|
||||||
|
if len(maps) == 0 {
|
||||||
|
return map[K]V{}
|
||||||
|
}
|
||||||
|
if len(maps) == 1 {
|
||||||
|
return maps[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
var res map[K]V
|
||||||
|
|
||||||
|
reducer := func(m1, m2 map[K]V) map[K]V {
|
||||||
|
m := make(map[K]V)
|
||||||
|
for k, v1 := range m1 {
|
||||||
|
if v2, ok := m2[k]; ok && reflect.DeepEqual(v1, v2) {
|
||||||
|
m[k] = v1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
reduceMaps := make([]map[K]V, 2, 2)
|
||||||
|
res = reducer(maps[0], maps[1])
|
||||||
|
|
||||||
|
for i := 2; i < len(maps); i++ {
|
||||||
|
reduceMaps[0] = res
|
||||||
|
reduceMaps[1] = maps[i]
|
||||||
|
res = reducer(reduceMaps[0], reduceMaps[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
// Minus creates an map of whose key in mapA but not in mapB
|
||||||
|
func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V {
|
||||||
|
res := make(map[K]V)
|
||||||
|
|
||||||
|
for k, v := range mapA {
|
||||||
|
if _, ok := mapB[k]; !ok {
|
||||||
|
res[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
150
maputil/map_test.go
Normal file
150
maputil/map_test.go
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
package maputil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestKeys(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestKeys")
|
||||||
|
|
||||||
|
m := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "a",
|
||||||
|
3: "b",
|
||||||
|
4: "c",
|
||||||
|
5: "d",
|
||||||
|
}
|
||||||
|
|
||||||
|
keys := Keys(m)
|
||||||
|
sort.Ints(keys)
|
||||||
|
|
||||||
|
assert.Equal([]int{1, 2, 3, 4, 5}, keys)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValues(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestValues")
|
||||||
|
|
||||||
|
m := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "a",
|
||||||
|
3: "b",
|
||||||
|
4: "c",
|
||||||
|
5: "d",
|
||||||
|
}
|
||||||
|
|
||||||
|
values := Values(m)
|
||||||
|
sort.Strings(values)
|
||||||
|
|
||||||
|
assert.Equal([]string{"a", "a", "b", "c", "d"}, values)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMerge(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestMerge")
|
||||||
|
|
||||||
|
m1 := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "b",
|
||||||
|
}
|
||||||
|
m2 := map[int]string{
|
||||||
|
1: "1",
|
||||||
|
3: "2",
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := map[int]string{
|
||||||
|
1: "1",
|
||||||
|
2: "b",
|
||||||
|
3: "2",
|
||||||
|
}
|
||||||
|
acturl := Merge(m1, m2)
|
||||||
|
|
||||||
|
assert.Equal(expected, acturl)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestForEach(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestForEach")
|
||||||
|
|
||||||
|
m := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
"d": 4,
|
||||||
|
}
|
||||||
|
|
||||||
|
var sum int
|
||||||
|
|
||||||
|
ForEach(m, func(_ string, value int) {
|
||||||
|
sum += value
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(10, sum)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFilter(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestFilter")
|
||||||
|
|
||||||
|
m := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
"d": 4,
|
||||||
|
"e": 5,
|
||||||
|
}
|
||||||
|
isEven := func(_ string, value int) bool {
|
||||||
|
return value%2 == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
acturl := Filter(m, isEven)
|
||||||
|
|
||||||
|
assert.Equal(map[string]int{
|
||||||
|
"b": 2,
|
||||||
|
"d": 4,
|
||||||
|
}, acturl)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIntersect(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIntersect")
|
||||||
|
|
||||||
|
m1 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
m2 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 6,
|
||||||
|
"d": 7,
|
||||||
|
}
|
||||||
|
|
||||||
|
m3 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 9,
|
||||||
|
"e": 9,
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(map[string]int{"a": 1, "b": 2, "c": 3}, Intersect(m1))
|
||||||
|
assert.Equal(map[string]int{"a": 1, "b": 2}, Intersect(m1, m2))
|
||||||
|
assert.Equal(map[string]int{"a": 1}, Intersect(m1, m2, m3))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMinus(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestMinus")
|
||||||
|
|
||||||
|
m1 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
m2 := map[string]int{
|
||||||
|
"a": 11,
|
||||||
|
"b": 22,
|
||||||
|
"d": 33,
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(map[string]int{"c": 3}, Minus(m1, m2))
|
||||||
|
}
|
||||||
@@ -6,7 +6,8 @@
|
|||||||
// HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `url` is required.
|
// HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `url` is required.
|
||||||
// HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `params` is variable, the order is:
|
// HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `params` is variable, the order is:
|
||||||
// params[0] is header which type should be http.Header or map[string]string,
|
// params[0] is header which type should be http.Header or map[string]string,
|
||||||
// params[1] is query param which type should be url.Values or map[string]any,
|
// params[1] is query param which type should be url.Values or map[string]any, when content-type header is
|
||||||
|
// multipart/form-data or application/x-www-form-urlencoded, params[1] should be url.Values
|
||||||
// params[2] is post body which type should be []byte.
|
// params[2] is post body which type should be []byte.
|
||||||
// params[3] is http client which type should be http.Client.
|
// params[3] is http client which type should be http.Client.
|
||||||
package netutil
|
package netutil
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"net/url"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/internal"
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
@@ -46,6 +47,29 @@ func TestHttpPost(t *testing.T) {
|
|||||||
t.Log("response: ", resp.StatusCode, string(body))
|
t.Log("response: ", resp.StatusCode, string(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHttpPostFormData(t *testing.T) {
|
||||||
|
apiUrl := "https://jsonplaceholder.typicode.com/todos"
|
||||||
|
header := map[string]string{
|
||||||
|
// "Content-Type": "application/x-www-form-urlencoded",
|
||||||
|
"Content-Type": "multipart/form-data",
|
||||||
|
}
|
||||||
|
type Todo struct {
|
||||||
|
UserId int `json:"userId"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
}
|
||||||
|
postData := url.Values{}
|
||||||
|
postData.Add("userId", "1")
|
||||||
|
postData.Add("title", "TestAddToDo")
|
||||||
|
|
||||||
|
resp, err := HttpPost(apiUrl, header, postData, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
body, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
t.Log("response: ", resp.StatusCode, string(body))
|
||||||
|
}
|
||||||
|
|
||||||
func TestHttpPut(t *testing.T) {
|
func TestHttpPut(t *testing.T) {
|
||||||
url := "https://jsonplaceholder.typicode.com/todos/1"
|
url := "https://jsonplaceholder.typicode.com/todos/1"
|
||||||
header := map[string]string{
|
header := map[string]string{
|
||||||
|
|||||||
@@ -81,10 +81,16 @@ func setHeaderAndQueryAndBody(req *http.Request, reqUrl string, header, queryPar
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = setBodyByte(req, body)
|
if req.Header.Get("Content-Type") == "multipart/form-data" || req.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {
|
||||||
|
formData := queryParam.(url.Values)
|
||||||
|
err = setBodyByte(req, []byte(formData.Encode()))
|
||||||
|
} else {
|
||||||
|
err = setBodyByte(req, body)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -571,7 +571,6 @@ func Union[T any](slices ...[]T) []T {
|
|||||||
|
|
||||||
// Intersection creates a slice of unique values that included by all slices.
|
// Intersection creates a slice of unique values that included by all slices.
|
||||||
func Intersection[T any](slices ...[]T) []T {
|
func Intersection[T any](slices ...[]T) []T {
|
||||||
var res []T
|
|
||||||
if len(slices) == 0 {
|
if len(slices) == 0 {
|
||||||
return []T{}
|
return []T{}
|
||||||
}
|
}
|
||||||
@@ -579,28 +578,51 @@ func Intersection[T any](slices ...[]T) []T {
|
|||||||
return Unique(slices[0])
|
return Unique(slices[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
//return elements both in slice1 and slice2
|
var res []T
|
||||||
reduceFunc := func(slice1, slice2 []T) []T {
|
|
||||||
|
reducer := func(s1, s2 []T) []T {
|
||||||
s := make([]T, 0, 0)
|
s := make([]T, 0, 0)
|
||||||
for _, v := range slice1 {
|
for _, v := range s1 {
|
||||||
if Contain(slice2, v) {
|
if Contain(s2, v) {
|
||||||
s = append(s, v)
|
s = append(s, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
res = reduceFunc(slices[0], slices[1])
|
res = reducer(slices[0], slices[1])
|
||||||
|
|
||||||
if len(slices) == 2 {
|
reduceSlice := make([][]T, 2, 2)
|
||||||
return Unique(res)
|
for i := 2; i < len(slices); i++ {
|
||||||
|
reduceSlice[0] = res
|
||||||
|
reduceSlice[1] = slices[i]
|
||||||
|
res = reducer(reduceSlice[0], reduceSlice[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp := make([][]T, 2, 2)
|
return Unique(res)
|
||||||
for i := 2; i < len(slices); i++ {
|
}
|
||||||
tmp[0] = res
|
|
||||||
tmp[1] = slices[i]
|
// SymmetricDifference oppoiste operation of intersection function
|
||||||
res = reduceFunc(tmp[0], tmp[1])
|
func SymmetricDifference[T any](slices ...[]T) []T {
|
||||||
|
if len(slices) == 0 {
|
||||||
|
return []T{}
|
||||||
|
}
|
||||||
|
if len(slices) == 1 {
|
||||||
|
return Unique(slices[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
res := make([]T, 0)
|
||||||
|
|
||||||
|
intersectSlice := Intersection(slices...)
|
||||||
|
|
||||||
|
for i := 0; i < len(slices); i++ {
|
||||||
|
slice := slices[i]
|
||||||
|
for _, v := range slice {
|
||||||
|
if !Contain(intersectSlice, v) {
|
||||||
|
res = append(res, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Unique(res)
|
return Unique(res)
|
||||||
|
|||||||
@@ -407,7 +407,18 @@ func TestIntersection(t *testing.T) {
|
|||||||
for i := 0; i < len(res); i++ {
|
for i := 0; i < len(res); i++ {
|
||||||
assert.Equal(expected[i], res[i])
|
assert.Equal(expected[i], res[i])
|
||||||
}
|
}
|
||||||
// assert.IsNil(Intersection())
|
}
|
||||||
|
|
||||||
|
func TestSymmetricDifference(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSymmetricDifference")
|
||||||
|
|
||||||
|
s1 := []int{1, 2, 3}
|
||||||
|
s2 := []int{1, 2, 4}
|
||||||
|
s3 := []int{1, 2, 3, 5}
|
||||||
|
|
||||||
|
assert.Equal([]int{1, 2, 3}, SymmetricDifference(s1))
|
||||||
|
assert.Equal([]int{3, 4}, SymmetricDifference(s1, s2))
|
||||||
|
assert.Equal([]int{3, 4, 5}, SymmetricDifference(s1, s2, s3))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReverse(t *testing.T) {
|
func TestReverse(t *testing.T) {
|
||||||
|
|||||||
@@ -247,3 +247,52 @@ func Unwrap(str string, wrapToken string) string {
|
|||||||
|
|
||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SplitEx split a given string whether the result contains empty string
|
||||||
|
func SplitEx(s, sep string, removeEmptyString bool) []string {
|
||||||
|
if sep == "" {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
n := strings.Count(s, sep) + 1
|
||||||
|
a := make([]string, n)
|
||||||
|
n--
|
||||||
|
i := 0
|
||||||
|
sepSave := 0
|
||||||
|
ignore := false
|
||||||
|
|
||||||
|
for i < n {
|
||||||
|
m := strings.Index(s, sep)
|
||||||
|
if m < 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
ignore = false
|
||||||
|
if removeEmptyString {
|
||||||
|
if s[:m+sepSave] == "" {
|
||||||
|
ignore = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !ignore {
|
||||||
|
a[i] = s[:m+sepSave]
|
||||||
|
s = s[m+len(sep):]
|
||||||
|
i++
|
||||||
|
} else {
|
||||||
|
s = s[m+len(sep):]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var ret []string
|
||||||
|
if removeEmptyString {
|
||||||
|
if s != "" {
|
||||||
|
a[i] = s
|
||||||
|
ret = a[:i+1]
|
||||||
|
} else {
|
||||||
|
ret = a[:i]
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
a[i] = s
|
||||||
|
ret = a[:i+1]
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|||||||
@@ -177,3 +177,15 @@ func TestUnwrap(t *testing.T) {
|
|||||||
assert.Equal("***", Unwrap("***", "**"))
|
assert.Equal("***", Unwrap("***", "**"))
|
||||||
assert.Equal("**", Unwrap("**", "**"))
|
assert.Equal("**", Unwrap("**", "**"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSplitEx(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSplitEx")
|
||||||
|
|
||||||
|
assert.Equal([]string{}, SplitEx(" a b c ", "", true))
|
||||||
|
|
||||||
|
assert.Equal([]string{"", "a", "b", "c", ""}, SplitEx(" a b c ", " ", false))
|
||||||
|
assert.Equal([]string{"a", "b", "c"}, SplitEx(" a b c ", " ", true))
|
||||||
|
|
||||||
|
assert.Equal([]string{" a", "b", "c", ""}, SplitEx(" a = b = c = ", " = ", false))
|
||||||
|
assert.Equal([]string{" a", "b", "c"}, SplitEx(" a = b = c = ", " = ", true))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user