1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-07 22:22:29 +08:00

Compare commits

...

18 Commits

Author SHA1 Message Date
dudaodong
f24ad26692 release: v2.0.6 2022-05-12 10:13:13 +08:00
dudaodong
88de9bfac2 docs: add doc for SplitEx function 2022-05-12 10:12:19 +08:00
dudaodong
e4777a0986 docs: add doc for SplitEx function 2022-05-12 10:11:12 +08:00
franktz
84a69d1fa5 feat: strutil.SplitEx. Split a given string whether the result contains empty string (#31)
Co-authored-by: Frank.Town <tangzhi21@sfmail.sf-express.com>
2022-05-11 15:19:53 +08:00
dudaodong
5eb056277d update readme file 2022-04-27 11:51:02 +08:00
dudaodong
4e6d586251 update readme file 2022-04-27 11:42:06 +08:00
dudaodong
61c7012e17 test: add unit test TestBSTree_IsSubTree 2022-04-27 11:36:51 +08:00
dudaodong
0d48778886 feat: add HasSubTree func for BSTree 2022-04-27 10:43:01 +08:00
dudaodong
e98c46c903 feat: add IsSubTree func for BSTree 2022-04-27 10:36:11 +08:00
dudaodong
155f287ab0 refactor: add param comparator in NewBSTree func 2022-04-26 11:04:04 +08:00
dudaodong
652916b7d7 test: add unit test for PriorityQueue Dequeue function 2022-04-26 10:56:24 +08:00
dudaodong
955de2bdbf feat: add Dequeue function 2022-04-26 10:50:27 +08:00
dudaodong
a4c1d40faa test: add unit test for PriorityQueue Enqueue function 2022-04-26 10:45:19 +08:00
dudaodong
f155e0caa6 test: add unit test for PriorityQueue Enqueue function 2022-04-26 10:40:48 +08:00
dudaodong
fb0332449c feat: add IsEmpty and IsFull for PriorityQueue 2022-04-26 10:24:10 +08:00
dudaodong
68f0fd1d4c feat: add PriorityQueue 2022-04-26 10:19:31 +08:00
dudaodong
70995c5098 feat: add PriorityQueue 2022-04-26 10:18:43 +08:00
dudaodong
55e62ed8ca feat: add PriorityQueue 2022-04-26 10:18:06 +08:00
12 changed files with 461 additions and 102 deletions

View File

@@ -4,7 +4,7 @@
<br/> <br/>
![Go version](https://img.shields.io/badge/go-%3E%3D1.16<recommend>-9cf) ![Go version](https://img.shields.io/badge/go-%3E%3D1.16<recommend>-9cf)
[![Release](https://img.shields.io/badge/release-2.0.5-green.svg)](https://github.com/duke-git/lancet/releases) [![Release](https://img.shields.io/badge/release-2.0.6-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2) [![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
@@ -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"
@@ -90,7 +90,7 @@ import "github.com/duke-git/lancet/v2/algorithm"
### Concurrency package contain some functions to support concurrent programming. eg, goroutine, channel, async. ### 2. Concurrency package contain some functions to support concurrent programming. eg, goroutine, channel, async.
```go ```go
import "github.com/duke-git/lancet/v2/concurrency" import "github.com/duke-git/lancet/v2/concurrency"
@@ -107,7 +107,7 @@ import "github.com/duke-git/lancet/v2/concurrency"
- [Take](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Take) - [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) - [Tee](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Tee)
### Convertor package contains some functions for data convertion. ### 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"
@@ -123,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"
@@ -163,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
@@ -201,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"
@@ -224,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"
@@ -232,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"
@@ -248,7 +248,7 @@ 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)
### Maputil package includes some functions to manipulate map. ### 9. Maputil package includes some functions to manipulate map.
```go ```go
import "github.com/duke-git/lancet/v2/maputil" import "github.com/duke-git/lancet/v2/maputil"
@@ -264,7 +264,7 @@ import "github.com/duke-git/lancet/v2/maputil"
- [Values](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Values) - [Values](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Values)
### Mathutil package implements some functions for math calculation. ### 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"
@@ -283,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"
@@ -303,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"
@@ -315,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"
@@ -328,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"
@@ -371,7 +371,7 @@ import "github.com/duke-git/lancet/v2/slice"
- [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"
``` ```
@@ -392,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"
@@ -410,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"
@@ -444,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"

View File

@@ -4,7 +4,7 @@
<br/> <br/>
![Go version](https://img.shields.io/badge/go-%3E%3D1.16<recommend>-9cf) ![Go version](https://img.shields.io/badge/go-%3E%3D1.16<recommend>-9cf)
[![Release](https://img.shields.io/badge/release-2.0.5-green.svg)](https://github.com/duke-git/lancet/releases) [![Release](https://img.shields.io/badge/release-2.0.6-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2) [![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
@@ -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"
@@ -89,7 +89,7 @@ import "github.com/duke-git/lancet/v2/algorithm"
- [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)
### 并发包包含一些支持并发编程的功能。例如goroutine, channel, async等。 ### 2. 并发包包含一些支持并发编程的功能。例如goroutine, channel, async等。
```go ```go
import "github.com/duke-git/lancet/v2/concurrency" import "github.com/duke-git/lancet/v2/concurrency"
@@ -106,7 +106,7 @@ import "github.com/duke-git/lancet/v2/concurrency"
- [Take](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Take) - [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) - [Tee](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Tee)
### convertor转换器包支持一些常见的数据类型转换。 ### 3. convertor转换器包支持一些常见的数据类型转换。
```go ```go
import "github.com/duke-git/lancet/v2/convertor" import "github.com/duke-git/lancet/v2/convertor"
@@ -122,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加密包支持数据加密和解密获取md5hash值。支持base64, md5, hmac, aes, des, rsa。 ### 4. cryptor加密包支持数据加密和解密获取md5hash值。支持base64, md5, hmac, aes, des, rsa。
```go ```go
import "github.com/duke-git/lancet/v2/cryptor" import "github.com/duke-git/lancet/v2/cryptor"
@@ -162,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
@@ -200,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"
@@ -223,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"
@@ -232,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"
@@ -248,7 +248,7 @@ import "github.com/duke-git/lancet/v2/function"
- [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)
### maputil包包括一些操作map的函数. ### 9. maputil包包括一些操作map的函数.
```go ```go
import "github.com/duke-git/lancet/v2/maputil" import "github.com/duke-git/lancet/v2/maputil"
@@ -263,7 +263,7 @@ import "github.com/duke-git/lancet/v2/maputil"
- [Minus](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Minus) - [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) - [Values](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Values)
### mathutil包实现了一些数学计算的函数。 ### 10. mathutil包实现了一些数学计算的函数。
```go ```go
import "github.com/duke-git/lancet/v2/mathutil" import "github.com/duke-git/lancet/v2/mathutil"
@@ -281,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"
@@ -301,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"
@@ -312,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"
@@ -326,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"
@@ -370,7 +370,7 @@ import "github.com/duke-git/lancet/v2/slice"
- [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"
@@ -392,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"
@@ -412,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"
@@ -447,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"

View File

@@ -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}
} }

View 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]
}

View 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())
}

View File

@@ -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)

View File

@@ -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))
}

View File

@@ -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

View File

@@ -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>

View File

@@ -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>

View File

@@ -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
}

View File

@@ -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))
}