diff --git a/docs/.vitepress/en.ts b/docs/.vitepress/en.ts index d5c1b1b..498cb34 100644 --- a/docs/.vitepress/en.ts +++ b/docs/.vitepress/en.ts @@ -65,6 +65,8 @@ export const enConfig: LocaleSpecificConfig = { items: [ { text: 'algorithm', link: '/en/api/packages/algorithm' }, { text: 'compare', link: '/en/api/packages/compare' }, + { text: 'concurrency', link: '/en/api/packages/concurrency' }, + { text: 'condition', link: '/en/api/packages/condition' }, ], }, ], diff --git a/docs/.vitepress/zh.ts b/docs/.vitepress/zh.ts index f740d4d..6a8ea07 100644 --- a/docs/.vitepress/zh.ts +++ b/docs/.vitepress/zh.ts @@ -75,6 +75,8 @@ export const zhConfig: LocaleSpecificConfig = { items: [ { text: '算法', link: '/api/packages/algorithm' }, { text: '比较器', link: '/api/packages/compare' }, + { text: '并发处理', link: '/api/packages/concurrency' }, + { text: '条件判断', link: '/api/packages/condition' }, ], }, ], diff --git a/docs/api/api_doc.css b/docs/api/api_doc.css new file mode 100644 index 0000000..8c96556 --- /dev/null +++ b/docs/api/api_doc.css @@ -0,0 +1,4 @@ +.run-container { + float: right; + display: inline-block; +} \ No newline at end of file diff --git a/docs/api/packages/algorithm.md b/docs/api/packages/algorithm.md index 3f3b7de..d47386d 100644 --- a/docs/api/packages/algorithm.md +++ b/docs/api/packages/algorithm.md @@ -39,6 +39,8 @@ import (
+ + ## 文档 ### BubbleSort @@ -51,7 +53,7 @@ import ( func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` -示例:[运行](https://go.dev/play/p/GNdv7Jg2Taj) +示例:[运行](https://go.dev/play/p/GNdv7Jg2Taj) ```go package main @@ -99,7 +101,7 @@ func main() { func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` -示例:[运行](https://go.dev/play/p/G5LJiWgJJW6) +示例:[运行](https://go.dev/play/p/G5LJiWgJJW6) ```go package main @@ -162,7 +164,7 @@ func main() { func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` -示例:[运行](https://go.dev/play/p/oXovbkekayS) +示例:[运行](https://go.dev/play/p/oXovbkekayS) ```go package main @@ -210,7 +212,7 @@ func main() { func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` -示例:[运行](https://go.dev/play/p/3ibkszpJEu3) +示例:[运行](https://go.dev/play/p/3ibkszpJEu3) ```go package main @@ -258,7 +260,7 @@ func main() { func QuickSort[T any](slice []T comparator lancetconstraints.Comparator) ``` -示例:[运行](https://go.dev/play/p/7Y7c1Elk3ax) +示例:[运行](https://go.dev/play/p/7Y7c1Elk3ax) ```go package main @@ -306,7 +308,7 @@ func main() { func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` -示例:[运行](https://go.dev/play/p/u6Iwa1VZS_f) +示例:[运行](https://go.dev/play/p/u6Iwa1VZS_f) ```go package main @@ -354,7 +356,7 @@ func main() { func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` -示例:[运行](https://go.dev/play/p/ydinn9YzUJn) +示例:[运行](https://go.dev/play/p/ydinn9YzUJn) ```go package main @@ -402,7 +404,7 @@ func main() { func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T ``` -示例:[运行](https://go.dev/play/p/tB-Umgm0DrP) +示例:[运行](https://go.dev/play/p/tB-Umgm0DrP) ```go package main @@ -451,7 +453,7 @@ func main() { func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int ``` -示例: [运行](https://go.dev/play/p/t6MeGiUSN47) +示例: [运行](https://go.dev/play/p/t6MeGiUSN47) ```go package main @@ -502,7 +504,7 @@ func main() { func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int ``` -示例: [运行](https://go.dev/play/p/Anozfr8ZLH3) +示例: [运行](https://go.dev/play/p/Anozfr8ZLH3) ```go package main @@ -553,7 +555,7 @@ func main() { func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int ``` -示例: [运行](https://go.dev/play/p/IsS7rgn5s3x) +示例: [运行](https://go.dev/play/p/IsS7rgn5s3x) ```go package main @@ -596,7 +598,7 @@ func (l *LRUCache[K, V]) Delete(key K) bool func (l *LRUCache[K, V]) Len() int ``` -示例:[运行](https://go.dev/play/p/-EZjgOURufP) +示例:[运行](https://go.dev/play/p/-EZjgOURufP) ```go package main diff --git a/docs/api/packages/compare.md b/docs/api/packages/compare.md index aa15604..18495f9 100644 --- a/docs/api/packages/compare.md +++ b/docs/api/packages/compare.md @@ -35,6 +35,8 @@ import (
+ + ## 文档 ### Equal @@ -47,7 +49,7 @@ import ( func Equal(left, right any) bool ``` -示例: [运行](https://go.dev/play/p/wmVxR-to4lz) +示例: [运行](https://go.dev/play/p/wmVxR-to4lz) ```go package main @@ -96,7 +98,7 @@ func main() { func EqualValue(left, right any) bool ``` -示例: [运行](https://go.dev/play/p/fxnna_LLD9u) +示例: [运行](https://go.dev/play/p/fxnna_LLD9u) ```go package main @@ -135,7 +137,7 @@ func main() { func LessThan(left, right any) bool ``` -示例: [运行](https://go.dev/play/p/cYh7FQQj0ne) +示例: [运行](https://go.dev/play/p/cYh7FQQj0ne) ```go package main @@ -184,7 +186,7 @@ func main() { func GreaterThan(left, right any) bool ``` -示例: [运行](https://go.dev/play/p/9-NYDFZmIMp) +示例: [运行](https://go.dev/play/p/9-NYDFZmIMp) ```go package main @@ -236,7 +238,7 @@ func main() { func LessOrEqual(left, right any) bool ``` -示例: [运行](https://go.dev/play/p/e4T_scwoQzp) +示例: [运行](https://go.dev/play/p/e4T_scwoQzp) ```go package main @@ -285,7 +287,7 @@ func main() { func GreaterOrEqual(left, right any) bool ``` -示例: [运行](https://go.dev/play/p/vx8mP0U8DFk) +示例: [运行](https://go.dev/play/p/vx8mP0U8DFk) ```go package main @@ -337,7 +339,7 @@ func main() { func InDelta[T constraints.Integer | constraints.Float](left, right T, delta float64) bool ``` -示例: [运行](https://go.dev/play/p/TuDdcNtMkjo) +示例: [运行](https://go.dev/play/p/TuDdcNtMkjo) ```go package main diff --git a/docs/olddocs/concurrency_zh-CN.md b/docs/api/packages/concurrency.md similarity index 88% rename from docs/olddocs/concurrency_zh-CN.md rename to docs/api/packages/concurrency.md index 4fc41b6..6c5cc12 100644 --- a/docs/olddocs/concurrency_zh-CN.md +++ b/docs/api/packages/concurrency.md @@ -33,6 +33,8 @@ import (
+ + ## 文档 @@ -46,7 +48,7 @@ import ( type Channel[T any] struct func NewChannel[T any]() *Channel[T] ``` -示例: +示例:[运行](https://go.dev/play/p/7aB4KyMMp9A) ```go package main @@ -65,12 +67,12 @@ func main() {

将多个channel链接到一个channel,直到取消上下文。

-函数签名: +函数签名: ```go func (c *Channel[T]) Bridge(ctx context.Context, chanStream <-chan <-chan T) <-chan T ``` -示例: +示例:[运行](https://go.dev/play/p/qmWSy1NVF-Y) ```go package main @@ -122,7 +124,7 @@ func main() { ```go func (c *Channel[T]) FanIn(ctx context.Context, channels ...<-chan T) <-chan T ``` -示例: +示例:[运行](https://go.dev/play/p/2VYFMexEvTm) ```go package main @@ -161,7 +163,7 @@ func main() { ```go func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T ``` -示例: +示例:[运行](https://go.dev/play/p/7aB4KyMMp9A) ```go package main @@ -199,7 +201,7 @@ func main() { ```go func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T ``` -示例: +示例:[运行](https://go.dev/play/p/k5N_ALVmYjE) ```go package main @@ -238,7 +240,7 @@ func main() { ```go func (c *Channel[T]) RepeatFn(ctx context.Context, fn func() T) <-chan T ``` -示例: +示例:[运行](https://go.dev/play/p/4J1zAWttP85) ```go package main @@ -279,7 +281,7 @@ func main() { ```go func (c *Channel[T]) Or(channels ...<-chan T) <-chan T ``` -示例: +示例:[运行](https://go.dev/play/p/Wqz9rwioPww) ```go package main @@ -322,7 +324,7 @@ func main() { ```go func (c *Channel[T]) OrDone(ctx context.Context, channel <-chan T) <-chan T ``` -示例: +示例:[运行](https://go.dev/play/p/lm_GoS6aDjo) ```go package main @@ -360,7 +362,7 @@ func main() { ```go func (c *Channel[T]) Take(ctx context.Context, valueStream <-chan T, number int) <-chan T ``` -示例: +示例:[运行](https://go.dev/play/p/9Utt-1pDr2J) ```go package main @@ -406,7 +408,7 @@ func main() { ```go func (c *Channel[T]) Tee(ctx context.Context, in <-chan T) (<-chan T, <-chan T) ``` -示例: +示例:[运行](https://go.dev/play/p/3TQPKnCirrP) ```go package main diff --git a/docs/olddocs/condition_zh-CN.md b/docs/api/packages/condition.md similarity index 98% rename from docs/olddocs/condition_zh-CN.md rename to docs/api/packages/condition.md index cd27da4..c9a9e40 100644 --- a/docs/olddocs/condition_zh-CN.md +++ b/docs/api/packages/condition.md @@ -18,7 +18,7 @@ import (
-## Index +## 目录 - [Bool](#Bool) - [And](#And) @@ -31,7 +31,9 @@ import (
-## 目录 + + +## 文档 ### Bool

返回传入参数的bool值.
diff --git a/docs/en/api/packages/algorithm.md b/docs/en/api/packages/algorithm.md index 0a6437b..a61bfb9 100644 --- a/docs/en/api/packages/algorithm.md +++ b/docs/en/api/packages/algorithm.md @@ -12,6 +12,8 @@ Package algorithm implements some basic algorithm. eg. sort, search.

+ + ## Usage ```go @@ -51,7 +53,7 @@ import ( func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` -Example: [Run](https://go.dev/play/p/GNdv7Jg2Taj) +Example: [Run](https://go.dev/play/p/GNdv7Jg2Taj) ```go package main @@ -99,7 +101,7 @@ func main() { func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` -Example: [Run](https://go.dev/play/p/G5LJiWgJJW6) +Example: [Run](https://go.dev/play/p/G5LJiWgJJW6) ```go package main @@ -162,7 +164,7 @@ func main() { func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` -Example: [Run](https://go.dev/play/p/oXovbkekayS) +Example: [Run](https://go.dev/play/p/oXovbkekayS) ```go package main @@ -210,7 +212,7 @@ func main() { func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` -Example: [Run](https://go.dev/play/p/3ibkszpJEu3) +Example: [Run](https://go.dev/play/p/3ibkszpJEu3) ```go package main @@ -258,7 +260,7 @@ func main() { func QuickSort[T any](slice []T comparator lancetconstraints.Comparator) ``` -Example:[Run](https://go.dev/play/p/7Y7c1Elk3ax) +Example:[Run](https://go.dev/play/p/7Y7c1Elk3ax) ```go package main @@ -306,7 +308,7 @@ func main() { func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` -Example: [Run](https://go.dev/play/p/u6Iwa1VZS_f) +Example: [Run](https://go.dev/play/p/u6Iwa1VZS_f) ```go package main @@ -354,7 +356,7 @@ func main() { func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` -Example: [Run](https://go.dev/play/p/ydinn9YzUJn) +Example: [Run](https://go.dev/play/p/ydinn9YzUJn) ```go package main @@ -402,7 +404,7 @@ func main() { func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T ``` -Example: [Run](https://go.dev/play/p/tB-Umgm0DrP) +Example: [Run](https://go.dev/play/p/tB-Umgm0DrP) ```go package main @@ -451,7 +453,7 @@ func main() { func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int ``` -Example: [Run](https://go.dev/play/p/t6MeGiUSN47) +Example: [Run](https://go.dev/play/p/t6MeGiUSN47) ```go package main @@ -502,7 +504,7 @@ func main() { func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int ``` -Example: [Run](https://go.dev/play/p/Anozfr8ZLH3) +Example: [Run](https://go.dev/play/p/Anozfr8ZLH3) ```go package main @@ -553,7 +555,7 @@ func main() { func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int ``` -Example: [Run](https://go.dev/play/p/IsS7rgn5s3x) +Example: [Run](https://go.dev/play/p/IsS7rgn5s3x) ```go package main @@ -596,7 +598,7 @@ func (l *LRUCache[K, V]) Delete(key K) bool func (l *LRUCache[K, V]) Len() int ``` -Example: [Run](https://go.dev/play/p/IsS7rgn5s3x) +Example: [Run](https://go.dev/play/p/IsS7rgn5s3x) ```go package main diff --git a/docs/en/api/packages/compare.md b/docs/en/api/packages/compare.md index 026e008..3c4f4a1 100644 --- a/docs/en/api/packages/compare.md +++ b/docs/en/api/packages/compare.md @@ -34,13 +34,15 @@ import (
+ + ## Documentation ### Equal

Checks if two values are equal or not. (check both type and value)

-Signature: [Run](https://go.dev/play/p/wmVxR-to4lz) +Signature: [Run](https://go.dev/play/p/wmVxR-to4lz) ```go func Equal(left, right any) bool @@ -89,7 +91,7 @@ func main() {

Checks if two values are equal or not. (check value only)

-Signature: [Run](https://go.dev/play/p/fxnna_LLD9u) +Signature: [Run](https://go.dev/play/p/fxnna_LLD9u) ```go func EqualValue(left, right any) bool @@ -128,7 +130,7 @@ func main() {

Checks if value `left` less than value `right`.

-Signature: [Run](https://go.dev/play/p/cYh7FQQj0ne) +Signature: [Run](https://go.dev/play/p/cYh7FQQj0ne) ```go func LessThan(left, right any) bool @@ -183,7 +185,7 @@ func main() { func GreaterThan(left, right any) bool ``` -Example: [Run](https://go.dev/play/p/9-NYDFZmIMp) +Example: [Run](https://go.dev/play/p/9-NYDFZmIMp) ```go package main @@ -235,7 +237,7 @@ func main() { func LessOrEqual(left, right any) bool ``` -Example: [Run](https://go.dev/play/p/e4T_scwoQzp) +Example: [Run](https://go.dev/play/p/e4T_scwoQzp) ```go package main @@ -278,7 +280,7 @@ func main() {

Checks if value `left` less greater or equal than value `right`.

-Signature: [Run](https://go.dev/play/p/vx8mP0U8DFk) +Signature: [Run](https://go.dev/play/p/vx8mP0U8DFk) ```go func GreaterOrEqual(left, right any) bool @@ -330,7 +332,7 @@ func main() {

Checks if two values are equal or not within a delta.

-Signature: [Run](https://go.dev/play/p/TuDdcNtMkjo) +Signature: [Run](https://go.dev/play/p/TuDdcNtMkjo) ```go func InDelta[T constraints.Integer | constraints.Float](left, right T, delta float64) bool diff --git a/docs/olddocs/concurrency.md b/docs/en/api/packages/concurrency.md similarity index 88% rename from docs/olddocs/concurrency.md rename to docs/en/api/packages/concurrency.md index e71848c..81829c1 100644 --- a/docs/olddocs/concurrency.md +++ b/docs/en/api/packages/concurrency.md @@ -33,8 +33,9 @@ import (
-## Documentation + +## Documentation ## Channel ### NewChannel @@ -46,7 +47,7 @@ import ( type Channel[T any] struct func NewChannel[T any]() *Channel[T] ``` -Example: +Example: [运行](https://go.dev/play/p/7aB4KyMMp9A) ```go package main @@ -70,7 +71,7 @@ func main() { ```go func (c *Channel[T]) Bridge(ctx context.Context, chanStream <-chan <-chan T) <-chan T ``` -Example: +Example: [运行](https://go.dev/play/p/qmWSy1NVF-Y) ```go package main @@ -122,7 +123,7 @@ func main() { ```go func (c *Channel[T]) FanIn(ctx context.Context, channels ...<-chan T) <-chan T ``` -Example: +Example: [运行](https://go.dev/play/p/2VYFMexEvTm) ```go package main @@ -156,7 +157,7 @@ func main() {

Create channel, put values into the channel repeatly until cancel the context.

-Signature: +Signature: [运行](https://go.dev/play/p/k5N_ALVmYjE) ```go func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T @@ -200,7 +201,7 @@ func main() { ```go func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T ``` -Example: +Example: [运行](https://go.dev/play/p/7aB4KyMMp9A) ```go package main @@ -233,7 +234,7 @@ func main() {

Create a channel, excutes fn repeatly, and put the result into the channel, until close context.

-Signature: +Signature: [运行](https://go.dev/play/p/4J1zAWttP85) ```go func (c *Channel[T]) RepeatFn(ctx context.Context, fn func() T) <-chan T @@ -275,7 +276,7 @@ func main() {

Read one or more channels into one channel, will close when any readin channel is closed.

-Signature: +Signature: [运行](https://go.dev/play/p/Wqz9rwioPww) ```go func (c *Channel[T]) Or(channels ...<-chan T) <-chan T @@ -318,7 +319,7 @@ func main() {

Read a channel into another channel, will close until cancel context.

-Signature: +Signature: [运行](https://go.dev/play/p/lm_GoS6aDjo) ```go func (c *Channel[T]) OrDone(ctx context.Context, channel <-chan T) <-chan T @@ -356,7 +357,7 @@ func main() {

Create a channel whose values are taken from another channel with limit number.

-Signature: +Signature: [运行](https://go.dev/play/p/9Utt-1pDr2J) ```go func (c *Channel[T]) Take(ctx context.Context, valueStream <-chan T, number int) <-chan T @@ -402,7 +403,7 @@ func main() {

Split one chanel into two channels, until cancel the context.

-Signature: +Signature: [运行](https://go.dev/play/p/3TQPKnCirrP) ```go func (c *Channel[T]) Tee(ctx context.Context, in <-chan T) (<-chan T, <-chan T) diff --git a/docs/olddocs/condition.md b/docs/en/api/packages/condition.md similarity index 100% rename from docs/olddocs/condition.md rename to docs/en/api/packages/condition.md diff --git a/docs/en/index.md b/docs/en/index.md index 02b603e..54c2f15 100644 --- a/docs/en/index.md +++ b/docs/en/index.md @@ -34,3 +34,10 @@ features: icon: 👏 details: Well structure, test for every exported function. --- + +

+ + + + +

\ No newline at end of file diff --git a/docs/olddocs/algorithm.md b/docs/olddocs/algorithm.md deleted file mode 100644 index 0263872..0000000 --- a/docs/olddocs/algorithm.md +++ /dev/null @@ -1,636 +0,0 @@ -# Algorithm - -Package algorithm implements some basic algorithm. eg. sort, search. - -
- -## Source - -- [https://github.com/duke-git/lancet/blob/main/algorithm/sort.go](https://github.com/duke-git/lancet/blob/main/algorithm/sort.go) -- [https://github.com/duke-git/lancet/blob/main/algorithm/search.go](https://github.com/duke-git/lancet/blob/main/algorithm/search.go) -- [https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go](https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go) - -
- -## Usage - -```go -import ( - "github.com/duke-git/lancet/v2/algorithm" -) -``` - -
- -## Index - -- [BubbleSort](#BubbleSort) -- [InsertionSort](#InsertionSort) -- [SelectionSort](#SelectionSort) -- [ShellSort](#ShellSort) -- [QuickSort](#QuickSort) -- [HeapSort](#HeapSort) -- [MergeSort](#MergeSort) -- [CountSort](#CountSort) -- [BinarySearch](#BinarySearch) -- [BinaryIterativeSearch](#BinaryIterativeSearch) -- [LinearSearch](#LinearSearch) -- [LRUCache](#LRUCache) - -
- -## Documentation - -### BubbleSort - -

Sort slice with bubble sort algorithm. Param comparator should implements lancetconstraints.Comparator.

- -Signature: - -```go -func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -type intComparator struct{} - -func (c *intComparator) Compare(v1 any, v2 any) int { - val1, _ := v1.(int) - val2, _ := v2.(int) - - //ascending order - if val1 < val2 { - return -1 - } else if val1 > val2 { - return 1 - } - return 0 -} - -func main() { - numbers := []int{2, 1, 5, 3, 6, 4} - comparator := &intComparator{} - - algorithm.BubbleSort(numbers, comparator) - - fmt.Println(numbers) - - // Output: - // [1 2 3 4 5 6] -} -``` - -### InsertionSort - -

Sort slice with insertion sort algorithm. Param comparator should implements lancetconstraints.Comparator.

- -Signature: - -```go -func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -type people struct { - Name string - Age int -} - -// PeopleAageComparator sort people slice by age field -type peopleAgeComparator struct{} - -// Compare implements github.com/duke-git/lancet/lancetconstraints/constraints.go/Comparator -func (pc *peopleAgeComparator) Compare(v1 any, v2 any) int { - p1, _ := v1.(people) - p2, _ := v2.(people) - - //ascending order - if p1.Age < p2.Age { - return -1 - } else if p1.Age > p2.Age { - return 1 - } - - return 0 -} - -func main() { - peoples := []people{ - {Name: "a", Age: 20}, - {Name: "b", Age: 10}, - {Name: "c", Age: 17}, - {Name: "d", Age: 8}, - {Name: "e", Age: 28}, - } - - comparator := &peopleAgeComparator{} - - algorithm.InsertionSort(peoples, comparator) - - fmt.Println(peoples) - - // Output: - // [{d 8} {b 10} {c 17} {a 20} {e 28}] -} -``` - -### SelectionSort - -

Sort slice with selection sort algorithm. Param comparator should implements lancetconstraints.Comparator.

- -Signature: - -```go -func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -type intComparator struct{} - -func (c *intComparator) Compare(v1 any, v2 any) int { - val1, _ := v1.(int) - val2, _ := v2.(int) - - //ascending order - if val1 < val2 { - return -1 - } else if val1 > val2 { - return 1 - } - return 0 -} - -func main() { - numbers := []int{2, 1, 5, 3, 6, 4} - comparator := &intComparator{} - - algorithm.SelectionSort(numbers, comparator) - - fmt.Println(numbers) - - // Output: - // [1 2 3 4 5 6] -} -``` - -### ShellSort - -

Sort slice with shell sort algorithm. Param comparator should implements lancetconstraints.Comparator.

- -Signature: - -```go -func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -type intComparator struct{} - -func (c *intComparator) Compare(v1 any, v2 any) int { - val1, _ := v1.(int) - val2, _ := v2.(int) - - //ascending order - if val1 < val2 { - return -1 - } else if val1 > val2 { - return 1 - } - return 0 -} - -func main() { - numbers := []int{2, 1, 5, 3, 6, 4} - comparator := &intComparator{} - - algorithm.ShellSort(numbers, comparator) - - fmt.Println(numbers) - - // Output: - // [1 2 3 4 5 6] -} -``` - -### QuickSort - -

Sort slice with quick sort algorithm. Param comparator should implements lancetconstraints.Comparator.

- -Signature: - -```go -func QuickSort[T any](slice []T comparator lancetconstraints.Comparator) -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -type intComparator struct{} - -func (c *intComparator) Compare(v1 any, v2 any) int { - val1, _ := v1.(int) - val2, _ := v2.(int) - - //ascending order - if val1 < val2 { - return -1 - } else if val1 > val2 { - return 1 - } - return 0 -} - -func main() { - numbers := []int{2, 1, 5, 3, 6, 4} - comparator := &intComparator{} - - algorithm.QuickSort(numbers, comparator) - - fmt.Println(numbers) - - // Output: - // [1 2 3 4 5 6] -} -``` - -### HeapSort - -

Sort slice with heap sort algorithm. Param comparator should implements lancetconstraints.Comparator.

- -Signature: - -```go -func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -type intComparator struct{} - -func (c *intComparator) Compare(v1 any, v2 any) int { - val1, _ := v1.(int) - val2, _ := v2.(int) - - //ascending order - if val1 < val2 { - return -1 - } else if val1 > val2 { - return 1 - } - return 0 -} - -func main() { - numbers := []int{2, 1, 5, 3, 6, 4} - comparator := &intComparator{} - - algorithm.HeapSort(numbers, comparator) - - fmt.Println(numbers) - - // Output: - // [1 2 3 4 5 6] -} -``` - -### MergeSort - -

Sort slice with merge sort algorithm. Param comparator should implements lancetconstraints.Comparator.

- -Signature: - -```go -func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator) -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -type intComparator struct{} - -func (c *intComparator) Compare(v1 any, v2 any) int { - val1, _ := v1.(int) - val2, _ := v2.(int) - - //ascending order - if val1 < val2 { - return -1 - } else if val1 > val2 { - return 1 - } - return 0 -} - -func main() { - numbers := []int{2, 1, 5, 3, 6, 4} - comparator := &intComparator{} - - algorithm.MergeSort(numbers, comparator) - - fmt.Println(numbers) - - // Output: - // [1 2 3 4 5 6] -} -``` - -### CountSort - -

Sort slice with count sort algorithm. Param comparator should implements lancetconstraints.Comparator.

- -Signature: - -```go -func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - - -type intComparator struct{} - -func (c *intComparator) Compare(v1 any, v2 any) int { - val1, _ := v1.(int) - val2, _ := v2.(int) - - //ascending order - if val1 < val2 { - return -1 - } else if val1 > val2 { - return 1 - } - return 0 -} - -func main() { - numbers := []int{2, 1, 5, 3, 6, 4} - comparator := &intComparator{} - - sortedNums := algorithm.CountSort(numbers, comparator) - - fmt.Println(sortedNums) - - // Output: - // [1 2 3 4 5 6] -} -``` - -### BinarySearch - -

BinarySearch search for target within a sorted slice, recursive call itself. If a target is found, the index of the target is returned. Else the function return -1.

- -Signature: - -```go -func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -type intComparator struct{} - -func (c *intComparator) Compare(v1 any, v2 any) int { - val1, _ := v1.(int) - val2, _ := v2.(int) - - //ascending order - if val1 < val2 { - return -1 - } else if val1 > val2 { - return 1 - } - return 0 -} - -func main() { - numbers := []int{1, 2, 3, 4, 5, 6, 7, 8} - comparator := &intComparator{} - - result1 := algorithm.BinarySearch(numbers, 5, 0, len(numbers)-1, comparator) - result2 := algorithm.BinarySearch(numbers, 9, 0, len(numbers)-1, comparator) - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // 4 - // -1 -} -``` - -### BinaryIterativeSearch - -

BinaryIterativeSearch search for target within a sorted slice, recursive call itself. If a target is found, the index of the target is returned. Else the function return -1.

- -Signature: - -```go -func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -type intComparator struct{} - -func (c *intComparator) Compare(v1 any, v2 any) int { - val1, _ := v1.(int) - val2, _ := v2.(int) - - //ascending order - if val1 < val2 { - return -1 - } else if val1 > val2 { - return 1 - } - return 0 -} - -func main() { - numbers := []int{1, 2, 3, 4, 5, 6, 7, 8} - comparator := &intComparator{} - - result1 := algorithm.BinaryIterativeSearch(numbers, 5, 0, len(numbers)-1, comparator) - result2 := algorithm.BinaryIterativeSearch(numbers, 9, 0, len(numbers)-1, comparator) - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // 4 - // -1 -} -``` - -### LinearSearch - -

return the index of target in slice base on equal function.If a target is found, the index of the target is returned. Else the function return -1.

- -Signature: - -```go -func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -func main() { - numbers := []int{3, 4, 5, 3, 2, 1} - - equalFunc := func(a, b int) bool { - return a == b - } - - result1 := algorithm.LinearSearch(numbers, 3, equalFunc) - result2 := algorithm.LinearSearch(numbers, 6, equalFunc) - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // 0 - // -1 -} -``` - -### LRUCache - -

LRUCache implements mem cache with lru.

- -Signature: - -```go -func NewLRUCache[K comparable, V any](capacity int) *LRUCache[K, V] -func (l *LRUCache[K, V]) Get(key K) (V, bool) -func (l *LRUCache[K, V]) Put(key K, value V) -func (l *LRUCache[K, V]) Delete(key K) bool -func (l *LRUCache[K, V]) Len() int -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -func main() { - cache := algorithm.NewLRUCache[int, int](2) - - cache.Put(1, 1) - cache.Put(2, 2) - - result1, ok1 := cache.Get(1) - result2, ok2 := cache.Get(2) - result3, ok3 := cache.Get(3) - - fmt.Println(result1, ok1) - fmt.Println(result2, ok2) - fmt.Println(result3, ok3) - - fmt.Println(cache.Len()) - - ok := cache.Delete(2) - fmt.Println(ok) - - - // Output: - // 1 true - // 2 true - // 0 false - // 2 - // true -} -``` diff --git a/docs/olddocs/algorithm_zh-CN.md b/docs/olddocs/algorithm_zh-CN.md deleted file mode 100644 index 6627159..0000000 --- a/docs/olddocs/algorithm_zh-CN.md +++ /dev/null @@ -1,636 +0,0 @@ -# Algorithm - -algorithm 算法包实现一些基本算法,sort,search,lrucache。 - -
- -## 源码 - -- [https://github.com/duke-git/lancet/blob/main/algorithm/sort.go](https://github.com/duke-git/lancet/blob/main/algorithm/sort.go) -- [https://github.com/duke-git/lancet/blob/main/algorithm/search.go](https://github.com/duke-git/lancet/blob/main/algorithm/search.go) -- [https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go](https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go) - -
- -## 用法 - -```go -import ( - "github.com/duke-git/lancet/v2/algorithm" -) -``` - -
- -## 目录 - -- [BubbleSort](#BubbleSort) -- [InsertionSort](#InsertionSort) -- [SelectionSort](#SelectionSort) -- [ShellSort](#ShellSort) -- [QuickSort](#QuickSort) -- [HeapSort](#HeapSort) -- [MergeSort](#MergeSort) -- [CountSort](#CountSort) -- [BinarySearch](#BinarySearch) -- [BinaryIterativeSearch](#BinaryIterativeSearch) -- [LinearSearch](#LinearSearch) -- [LRUCache](#LRUCache) - -
- -## 文档 - -### BubbleSort - -

冒泡排序,参数comparator需要实现包lancetconstraints.Comparator。

- -函数签名: - -```go -func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -type intComparator struct{} - -func (c *intComparator) Compare(v1 any, v2 any) int { - val1, _ := v1.(int) - val2, _ := v2.(int) - - //ascending order - if val1 < val2 { - return -1 - } else if val1 > val2 { - return 1 - } - return 0 -} - -func main() { - numbers := []int{2, 1, 5, 3, 6, 4} - comparator := &intComparator{} - - algorithm.BubbleSort(numbers, comparator) - - fmt.Println(numbers) - - // Output: - // [1 2 3 4 5 6] -} -``` - -### InsertionSort - -

插入排序,参数comparator需要实现包lancetconstraints.Comparator。

- -函数签名: - -```go -func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -type people struct { - Name string - Age int -} - -// PeopleAageComparator sort people slice by age field -type peopleAgeComparator struct{} - -// Compare implements github.com/duke-git/lancet/lancetconstraints/constraints.go/Comparator -func (pc *peopleAgeComparator) Compare(v1 any, v2 any) int { - p1, _ := v1.(people) - p2, _ := v2.(people) - - //ascending order - if p1.Age < p2.Age { - return -1 - } else if p1.Age > p2.Age { - return 1 - } - - return 0 -} - -func main() { - peoples := []people{ - {Name: "a", Age: 20}, - {Name: "b", Age: 10}, - {Name: "c", Age: 17}, - {Name: "d", Age: 8}, - {Name: "e", Age: 28}, - } - - comparator := &peopleAgeComparator{} - - algorithm.InsertionSort(peoples, comparator) - - fmt.Println(peoples) - - // Output: - // [{d 8} {b 10} {c 17} {a 20} {e 28}] -} -``` - -### SelectionSort - -

选择排序,参数comparator需要实现包lancetconstraints.Comparator。

- -函数签名: - -```go -func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -type intComparator struct{} - -func (c *intComparator) Compare(v1 any, v2 any) int { - val1, _ := v1.(int) - val2, _ := v2.(int) - - //ascending order - if val1 < val2 { - return -1 - } else if val1 > val2 { - return 1 - } - return 0 -} - -func main() { - numbers := []int{2, 1, 5, 3, 6, 4} - comparator := &intComparator{} - - algorithm.SelectionSort(numbers, comparator) - - fmt.Println(numbers) - - // Output: - // [1 2 3 4 5 6] -} -``` - -### ShellSort - -

希尔排序,参数comparator需要实现包lancetconstraints.Comparator。

- -函数签名: - -```go -func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -type intComparator struct{} - -func (c *intComparator) Compare(v1 any, v2 any) int { - val1, _ := v1.(int) - val2, _ := v2.(int) - - //ascending order - if val1 < val2 { - return -1 - } else if val1 > val2 { - return 1 - } - return 0 -} - -func main() { - numbers := []int{2, 1, 5, 3, 6, 4} - comparator := &intComparator{} - - algorithm.ShellSort(numbers, comparator) - - fmt.Println(numbers) - - // Output: - // [1 2 3 4 5 6] -} -``` - -### QuickSort - -

快速排序,参数comparator需要实现包lancetconstraints.Comparator。

- -函数签名: - -```go -func QuickSort[T any](slice []T comparator lancetconstraints.Comparator) -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -type intComparator struct{} - -func (c *intComparator) Compare(v1 any, v2 any) int { - val1, _ := v1.(int) - val2, _ := v2.(int) - - //ascending order - if val1 < val2 { - return -1 - } else if val1 > val2 { - return 1 - } - return 0 -} - -func main() { - numbers := []int{2, 1, 5, 3, 6, 4} - comparator := &intComparator{} - - algorithm.QuickSort(numbers, comparator) - - fmt.Println(numbers) - - // Output: - // [1 2 3 4 5 6] -} -``` - -### HeapSort - -

堆排序,参数comparator需要实现包lancetconstraints.Comparator。

- -函数签名: - -```go -func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -type intComparator struct{} - -func (c *intComparator) Compare(v1 any, v2 any) int { - val1, _ := v1.(int) - val2, _ := v2.(int) - - //ascending order - if val1 < val2 { - return -1 - } else if val1 > val2 { - return 1 - } - return 0 -} - -func main() { - numbers := []int{2, 1, 5, 3, 6, 4} - comparator := &intComparator{} - - algorithm.HeapSort(numbers, comparator) - - fmt.Println(numbers) - - // Output: - // [1 2 3 4 5 6] -} -``` - -### MergeSort - -

归并排序,参数comparator需要实现包lancetconstraints.Comparator。

- -函数签名: - -```go -func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator) -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -type intComparator struct{} - -func (c *intComparator) Compare(v1 any, v2 any) int { - val1, _ := v1.(int) - val2, _ := v2.(int) - - //ascending order - if val1 < val2 { - return -1 - } else if val1 > val2 { - return 1 - } - return 0 -} - -func main() { - numbers := []int{2, 1, 5, 3, 6, 4} - comparator := &intComparator{} - - algorithm.MergeSort(numbers, comparator) - - fmt.Println(numbers) - - // Output: - // [1 2 3 4 5 6] -} -``` - -### CountSort - -

计数排序,参数comparator需要实现包lancetconstraints.Comparator。

- -函数签名: - -```go -func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - - -type intComparator struct{} - -func (c *intComparator) Compare(v1 any, v2 any) int { - val1, _ := v1.(int) - val2, _ := v2.(int) - - //ascending order - if val1 < val2 { - return -1 - } else if val1 > val2 { - return 1 - } - return 0 -} - -func main() { - numbers := []int{2, 1, 5, 3, 6, 4} - comparator := &intComparator{} - - sortedNums := algorithm.CountSort(numbers, comparator) - - fmt.Println(sortedNums) - - // Output: - // [1 2 3 4 5 6] -} -``` - -### BinarySearch - -

二分递归查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator。

- -函数签名: - -```go -func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -type intComparator struct{} - -func (c *intComparator) Compare(v1 any, v2 any) int { - val1, _ := v1.(int) - val2, _ := v2.(int) - - //ascending order - if val1 < val2 { - return -1 - } else if val1 > val2 { - return 1 - } - return 0 -} - -func main() { - numbers := []int{1, 2, 3, 4, 5, 6, 7, 8} - comparator := &intComparator{} - - result1 := algorithm.BinarySearch(numbers, 5, 0, len(numbers)-1, comparator) - result2 := algorithm.BinarySearch(numbers, 9, 0, len(numbers)-1, comparator) - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // 4 - // -1 -} -``` - -### BinaryIterativeSearch - -

二分迭代查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator。

- -函数签名: - -```go -func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -type intComparator struct{} - -func (c *intComparator) Compare(v1 any, v2 any) int { - val1, _ := v1.(int) - val2, _ := v2.(int) - - //ascending order - if val1 < val2 { - return -1 - } else if val1 > val2 { - return 1 - } - return 0 -} - -func main() { - numbers := []int{1, 2, 3, 4, 5, 6, 7, 8} - comparator := &intComparator{} - - result1 := algorithm.BinaryIterativeSearch(numbers, 5, 0, len(numbers)-1, comparator) - result2 := algorithm.BinaryIterativeSearch(numbers, 9, 0, len(numbers)-1, comparator) - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // 4 - // -1 -} -``` - -### LinearSearch - -

基于传入的相等函数线性查找元素,返回元素索引,未找到元素返回-1。

- -函数签名: - -```go -func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -func main() { - numbers := []int{3, 4, 5, 3, 2, 1} - - equalFunc := func(a, b int) bool { - return a == b - } - - result1 := algorithm.LinearSearch(numbers, 3, equalFunc) - result2 := algorithm.LinearSearch(numbers, 6, equalFunc) - - fmt.Println(result1) - fmt.Println(result2) - - // Output: - // 0 - // -1 -} -``` - -### LRUCache - -

lru算法实现缓存。

- -函数签名: - -```go -func NewLRUCache[K comparable, V any](capacity int) *LRUCache[K, V] -func (l *LRUCache[K, V]) Get(key K) (V, bool) -func (l *LRUCache[K, V]) Put(key K, value V) -func (l *LRUCache[K, V]) Delete(key K) bool -func (l *LRUCache[K, V]) Len() int -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/algorithm" -) - -func main() { - cache := algorithm.NewLRUCache[int, int](2) - - cache.Put(1, 1) - cache.Put(2, 2) - - result1, ok1 := cache.Get(1) - result2, ok2 := cache.Get(2) - result3, ok3 := cache.Get(3) - - fmt.Println(result1, ok1) - fmt.Println(result2, ok2) - fmt.Println(result3, ok3) - - fmt.Println(cache.Len()) - - ok := cache.Delete(2) - fmt.Println(ok) - - - // Output: - // 1 true - // 2 true - // 0 false - // 2 - // true -} -``` diff --git a/docs/olddocs/compare.md b/docs/olddocs/compare.md deleted file mode 100644 index e88befe..0000000 --- a/docs/olddocs/compare.md +++ /dev/null @@ -1,374 +0,0 @@ -# Compare - -Package compare provides a lightweight comparison function on any type. - -
- -## Source: - -- [https://github.com/duke-git/lancet/blob/main/compare/compare.go](https://github.com/duke-git/lancet/blob/main/compare/compare.go) - -- [https://github.com/duke-git/lancet/blob/main/compare/compare_internal.go](https://github.com/duke-git/lancet/blob/main/compare/compare_internal.go) - -
- -## Usage: - -```go -import ( - "github.com/duke-git/lancet/v2/condition" -) -``` - -
- -## Index - -- [Equal](#Equal) -- [EqualValue](#EqualValue) -- [LessThan](#LessThan) -- [GreaterThan](#GreaterThan) -- [LessOrEqual](#LessOrEqual) -- [GreaterOrEqual](#GreaterOrEqual) -- [InDelta](#InDelta) - -
- -## Documentation - -### Equal - -

Checks if two values are equal or not. (check both type and value)

- -Signature: - -```go -func Equal(left, right any) bool -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/compare" -) - -func main() { - result1 := compare.Equal(1, 1) - result2 := compare.Equal("1", "1") - result3 := compare.Equal([]int{1, 2, 3}, []int{1, 2, 3}) - result4 := compare.Equal(map[int]string{1: "a", 2: "b"}, map[int]string{1: "a", 2: "b"}) - - result5 := compare.Equal(1, "1") - result6 := compare.Equal(1, int64(1)) - result7 := compare.Equal([]int{1, 2}, []int{1, 2, 3}) - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - fmt.Println(result7) - - // Output: - // true - // true - // true - // true - // false - // false - // false -} -``` - -### EqualValue - -

Checks if two values are equal or not. (check value only)

- -Signature: - -```go -func EqualValue(left, right any) bool -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/compare" -) - -func main() { - result1 := compare.EqualValue(1, 1) - result2 := compare.EqualValue(int(1), int64(1)) - result3 := compare.EqualValue(1, "1") - result4 := compare.EqualValue(1, "2") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - - // Output: - // true - // true - // true - // false -} -``` - -### LessThan - -

Checks if value `left` less than value `right`.

- -Signature: - -```go -func LessThan(left, right any) bool -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/compare" -) - -func main() { - result1 := compare.LessThan(1, 2) - result2 := compare.LessThan(1.1, 2.2) - result3 := compare.LessThan("a", "b") - - time1 := time.Now() - time2 := time1.Add(time.Second) - result4 := compare.LessThan(time1, time2) - - result5 := compare.LessThan(2, 1) - result6 := compare.LessThan(1, int64(2)) - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - - // Output: - // true - // true - // true - // true - // false - // false -} -``` - -### GreaterThan - -

Checks if value `left` greater than value `right`.

- -Signature: - -```go -func GreaterThan(left, right any) bool -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/compare" -) - -func main() { - result1 := compare.GreaterThan(2, 1) - result2 := compare.GreaterThan(2.2, 1.1) - result3 := compare.GreaterThan("b", "a") - - time1 := time.Now() - time2 := time1.Add(time.Second) - result4 := compare.GreaterThan(time2, time1) - - result5 := compare.GreaterThan(1, 2) - result6 := compare.GreaterThan(int64(2), 1) - result7 := compare.GreaterThan("b", "c") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - fmt.Println(result7) - - // Output: - // true - // true - // true - // true - // false - // false - // false -} -``` - -### LessOrEqual - -

Checks if value `left` less than or equal than value `right`.

- -Signature: - -```go -func LessOrEqual(left, right any) bool -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/compare" -) - -func main() { - result1 := compare.LessOrEqual(1, 1) - result2 := compare.LessOrEqual(1.1, 2.2) - result3 := compare.LessOrEqual("a", "b") - - time1 := time.Now() - time2 := time1.Add(time.Second) - result4 := compare.LessOrEqual(time1, time2) - - result5 := compare.LessOrEqual(2, 1) - result6 := compare.LessOrEqual(1, int64(2)) - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - - // Output: - // true - // true - // true - // true - // false - // false -} -``` - -### GreaterOrEqual - -

Checks if value `left` less greater or equal than value `right`.

- -Signature: - -```go -func GreaterOrEqual(left, right any) bool -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/compare" -) - -func main() { - result1 := compare.GreaterOrEqual(1, 1) - result2 := compare.GreaterOrEqual(2.2, 1.1) - result3 := compare.GreaterOrEqual("b", "b") - - time1 := time.Now() - time2 := time1.Add(time.Second) - result4 := compare.GreaterOrEqual(time2, time1) - - result5 := compare.GreaterOrEqual(1, 2) - result6 := compare.GreaterOrEqual(int64(2), 1) - result7 := compare.GreaterOrEqual("b", "c") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - fmt.Println(result7) - - // Output: - // true - // true - // true - // true - // false - // false - // false -} -``` - -### InDelta - -

Checks if two values are equal or not within a delta.

- -Signature: - -```go -func InDelta[T constraints.Integer | constraints.Float](left, right T, delta float64) bool -``` - -Example: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/compare" -) - -func main() { - result1 := InDelta(1, 1, 0) - result2 := InDelta(1, 2, 0) - - result3 := InDelta(2.0/3.0, 0.66667, 0.001) - result4 := InDelta(2.0/3.0, 0.0, 0.001) - - result5 := InDelta(float64(74.96)-float64(20.48), 54.48, 0) - result6 := InDelta(float64(74.96)-float64(20.48), 54.48, 1e-14) - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - - // Output: - // true - // false - // true - // false - // false - // true -} -``` diff --git a/docs/olddocs/compare_zh-CN.md b/docs/olddocs/compare_zh-CN.md deleted file mode 100644 index 3cb2ef3..0000000 --- a/docs/olddocs/compare_zh-CN.md +++ /dev/null @@ -1,375 +0,0 @@ -# Compare - -compare包提供几个轻量级的类型比较函数。 - -
- -## 源码: - -- [https://github.com/duke-git/lancet/blob/main/compare/compare.go](https://github.com/duke-git/lancet/blob/main/compare/compare.go) - -- [https://github.com/duke-git/lancet/blob/main/compare/compare_internal.go](https://github.com/duke-git/lancet/blob/main/compare/compare_internal.go) - -
- -## 用法: - -```go -import ( - "github.com/duke-git/lancet/v2/condition" -) -``` - -
- -## 目录 - -- [Equal](#Equal) -- [EqualValue](#EqualValue) -- [LessThan](#LessThan) -- [GreaterThan](#GreaterThan) -- [LessOrEqual](#LessOrEqual) -- [GreaterOrEqual](#GreaterOrEqual) -- [InDelta](#InDelta) - - -
- -## Documentation - -### Equal - -

检查两个值是否相等(检查类型和值)

- -函数签名: - -```go -func Equal(left, right any) bool -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/compare" -) - -func main() { - result1 := compare.Equal(1, 1) - result2 := compare.Equal("1", "1") - result3 := compare.Equal([]int{1, 2, 3}, []int{1, 2, 3}) - result4 := compare.Equal(map[int]string{1: "a", 2: "b"}, map[int]string{1: "a", 2: "b"}) - - result5 := compare.Equal(1, "1") - result6 := compare.Equal(1, int64(1)) - result7 := compare.Equal([]int{1, 2}, []int{1, 2, 3}) - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - fmt.Println(result7) - - // Output: - // true - // true - // true - // true - // false - // false - // false -} -``` - -### EqualValue - -

检查两个值是否相等(只检查值)

- -函数签名: - -```go -func EqualValue(left, right any) bool -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/compare" -) - -func main() { - result1 := compare.EqualValue(1, 1) - result2 := compare.EqualValue(int(1), int64(1)) - result3 := compare.EqualValue(1, "1") - result4 := compare.EqualValue(1, "2") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - - // Output: - // true - // true - // true - // false -} -``` - -### LessThan - -

验证参数`left`的值是否小于参数`right`的值。

- -函数签名: - -```go -func LessThan(left, right any) bool -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/compare" -) - -func main() { - result1 := compare.LessThan(1, 2) - result2 := compare.LessThan(1.1, 2.2) - result3 := compare.LessThan("a", "b") - - time1 := time.Now() - time2 := time1.Add(time.Second) - result4 := compare.LessThan(time1, time2) - - result5 := compare.LessThan(2, 1) - result6 := compare.LessThan(1, int64(2)) - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - - // Output: - // true - // true - // true - // true - // false - // false -} -``` - -### GreaterThan - -

验证参数`left`的值是否大于参数`right`的值。

- -函数签名: - -```go -func GreaterThan(left, right any) bool -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/compare" -) - -func main() { - result1 := compare.GreaterThan(2, 1) - result2 := compare.GreaterThan(2.2, 1.1) - result3 := compare.GreaterThan("b", "a") - - time1 := time.Now() - time2 := time1.Add(time.Second) - result4 := compare.GreaterThan(time2, time1) - - result5 := compare.GreaterThan(1, 2) - result6 := compare.GreaterThan(int64(2), 1) - result7 := compare.GreaterThan("b", "c") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - fmt.Println(result7) - - // Output: - // true - // true - // true - // true - // false - // false - // false -} -``` - -### LessOrEqual - -

验证参数`left`的值是否小于或等于参数`right`的值。

- -函数签名: - -```go -func LessOrEqual(left, right any) bool -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/compare" -) - -func main() { - result1 := compare.LessOrEqual(1, 1) - result2 := compare.LessOrEqual(1.1, 2.2) - result3 := compare.LessOrEqual("a", "b") - - time1 := time.Now() - time2 := time1.Add(time.Second) - result4 := compare.LessOrEqual(time1, time2) - - result5 := compare.LessOrEqual(2, 1) - result6 := compare.LessOrEqual(1, int64(2)) - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - - // Output: - // true - // true - // true - // true - // false - // false -} -``` - -### GreaterOrEqual - -

验证参数`left`的值是否大于或参数`right`的值。

- -函数签名: - -```go -func GreaterOrEqual(left, right any) bool -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/compare" -) - -func main() { - result1 := compare.GreaterOrEqual(1, 1) - result2 := compare.GreaterOrEqual(2.2, 1.1) - result3 := compare.GreaterOrEqual("b", "b") - - time1 := time.Now() - time2 := time1.Add(time.Second) - result4 := compare.GreaterOrEqual(time2, time1) - - result5 := compare.GreaterOrEqual(1, 2) - result6 := compare.GreaterOrEqual(int64(2), 1) - result7 := compare.GreaterOrEqual("b", "c") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - fmt.Println(result7) - - // Output: - // true - // true - // true - // true - // false - // false - // false -} -``` - -### InDelta - -

检查增量内两个值是否相等。

- -函数签名: - -```go -func InDelta[T constraints.Integer | constraints.Float](left, right T, delta float64) bool -``` - -示例: - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/v2/compare" -) - -func main() { - result1 := InDelta(1, 1, 0) - result2 := InDelta(1, 2, 0) - - result3 := InDelta(2.0/3.0, 0.66667, 0.001) - result4 := InDelta(2.0/3.0, 0.0, 0.001) - - result5 := InDelta(float64(74.96)-float64(20.48), 54.48, 0) - result6 := InDelta(float64(74.96)-float64(20.48), 54.48, 1e-14) - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - fmt.Println(result5) - fmt.Println(result6) - - // Output: - // true - // false - // true - // false - // false - // true -} -```