mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-23 13:52:26 +08:00
doc: add new site for doc
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -9,4 +9,6 @@ fileutil/unzip/*
|
|||||||
fileutil/tempdir/*
|
fileutil/tempdir/*
|
||||||
slice/testdata/*
|
slice/testdata/*
|
||||||
cryptor/*.pem
|
cryptor/*.pem
|
||||||
test
|
test
|
||||||
|
docs/node_modules
|
||||||
|
docs/.vitepress/cache
|
||||||
89
docs/.vitepress/common.ts
Normal file
89
docs/.vitepress/common.ts
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
import { defineConfig, HeadConfig } from 'vitepress'
|
||||||
|
|
||||||
|
export const META_IMAGE = '/lancet_logo.png'
|
||||||
|
export const isProduction = process.env.NETLIFY && process.env.CONTEXT === 'production'
|
||||||
|
|
||||||
|
if (process.env.NETLIFY) {
|
||||||
|
console.log('Netlify build', process.env.CONTEXT)
|
||||||
|
}
|
||||||
|
|
||||||
|
const productionHead: HeadConfig[] = [
|
||||||
|
[
|
||||||
|
'script',
|
||||||
|
{
|
||||||
|
src: 'https://unpkg.com/thesemetrics@latest',
|
||||||
|
async: '',
|
||||||
|
type: 'text/javascript',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
]
|
||||||
|
|
||||||
|
const rControl = /[\u0000-\u001f]/g
|
||||||
|
const rSpecial = /[\s~`!@#$%^&*()\-_+=[\]{}|\\;:"'“”‘’<>,.?/]+/g
|
||||||
|
const rCombining = /[\u0300-\u036F]/g
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default slugification function
|
||||||
|
*/
|
||||||
|
export const slugify = (str: string): string =>
|
||||||
|
str
|
||||||
|
.normalize('NFKD')
|
||||||
|
// Remove accents
|
||||||
|
.replace(rCombining, '')
|
||||||
|
// Remove control characters
|
||||||
|
.replace(rControl, '')
|
||||||
|
// Replace special characters
|
||||||
|
.replace(rSpecial, '-')
|
||||||
|
// ensure it doesn't start with a number
|
||||||
|
.replace(/^(\d)/, '_$1')
|
||||||
|
|
||||||
|
export const commonConfig = defineConfig({
|
||||||
|
title: 'Lancet',
|
||||||
|
appearance: 'dark',
|
||||||
|
|
||||||
|
markdown: {
|
||||||
|
theme: {
|
||||||
|
dark: 'dracula-soft',
|
||||||
|
light: 'vitesse-light',
|
||||||
|
},
|
||||||
|
|
||||||
|
attrs: {
|
||||||
|
leftDelimiter: '%{',
|
||||||
|
rightDelimiter: '}%',
|
||||||
|
},
|
||||||
|
|
||||||
|
anchor: {
|
||||||
|
slugify,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
head: [
|
||||||
|
// ['link', { rel: 'icon', type: 'image/svg+xml', href: '/logo.svg' }],
|
||||||
|
['link', { rel: 'icon', type: 'image/png', href: '/lancet_logo_mini.png' }],
|
||||||
|
['meta', { name: 'theme-color', content: '#5f67ee' }],
|
||||||
|
['meta', { name: 'og:type', content: 'website' }],
|
||||||
|
['meta', { name: 'og:locale', content: 'zh' }],
|
||||||
|
|
||||||
|
...(isProduction ? productionHead : []),
|
||||||
|
],
|
||||||
|
|
||||||
|
themeConfig: {
|
||||||
|
logo: { src: '/lancet_logo_mini.png', width: 24, height: 24 },
|
||||||
|
outline: [2, 3],
|
||||||
|
|
||||||
|
search: {
|
||||||
|
provider: 'local',
|
||||||
|
},
|
||||||
|
socialLinks: [
|
||||||
|
{
|
||||||
|
icon: 'github',
|
||||||
|
link: 'https://github.com/duke-git/lancet',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
|
||||||
|
footer: {
|
||||||
|
copyright: 'Copyright © 2023-present Duke Du',
|
||||||
|
message: 'Released under the MIT License.',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
14
docs/.vitepress/config.mts
Normal file
14
docs/.vitepress/config.mts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import { defineConfig } from 'vitepress'
|
||||||
|
import { commonConfig } from './common'
|
||||||
|
import { zhConfig } from './zh'
|
||||||
|
import { enConfig } from './en'
|
||||||
|
|
||||||
|
// https://vitepress.dev/reference/site-config
|
||||||
|
export default defineConfig({
|
||||||
|
...commonConfig,
|
||||||
|
|
||||||
|
locales: {
|
||||||
|
root: { label: '简体中文', lang: 'zh-CN', link: '/', ...zhConfig },
|
||||||
|
en: { label: 'English', lang: 'en-US', link: '/en/', ...enConfig },
|
||||||
|
},
|
||||||
|
})
|
||||||
73
docs/.vitepress/en.ts
Normal file
73
docs/.vitepress/en.ts
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
import type { DefaultTheme, LocaleSpecificConfig } from 'vitepress'
|
||||||
|
|
||||||
|
export const META_URL = 'https://lancet.go.dev'
|
||||||
|
export const META_TITLE = 'Lancet'
|
||||||
|
export const META_DESCRIPTION = 'A powerful util function library of Go'
|
||||||
|
|
||||||
|
export const enConfig: LocaleSpecificConfig<DefaultTheme.Config> = {
|
||||||
|
description: META_DESCRIPTION,
|
||||||
|
|
||||||
|
head: [
|
||||||
|
['meta', { property: 'og:url', content: META_URL }],
|
||||||
|
['meta', { property: 'og:description', content: META_DESCRIPTION }],
|
||||||
|
['meta', { property: 'twitter:url', content: META_URL }],
|
||||||
|
['meta', { property: 'twitter:title', content: META_TITLE }],
|
||||||
|
['meta', { property: 'twitter:description', content: META_DESCRIPTION }],
|
||||||
|
],
|
||||||
|
|
||||||
|
themeConfig: {
|
||||||
|
nav: [
|
||||||
|
{
|
||||||
|
text: 'Home',
|
||||||
|
link: '/en/',
|
||||||
|
activeMatch: '^/en/',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: 'Guide',
|
||||||
|
link: '/en/guide/introduction',
|
||||||
|
activeMatch: '^/en/guide/',
|
||||||
|
},
|
||||||
|
{ text: 'API', link: '/en/api/overview', activeMatch: '^/en/api/' },
|
||||||
|
{
|
||||||
|
text: 'Links',
|
||||||
|
items: [
|
||||||
|
{
|
||||||
|
text: 'Releaselog',
|
||||||
|
link: 'https://github.com/duke-git/lancet/releases',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
|
||||||
|
sidebar: {
|
||||||
|
'/en/': [
|
||||||
|
{
|
||||||
|
text: 'Introduction',
|
||||||
|
items: [
|
||||||
|
{
|
||||||
|
text: 'What is Lancet?',
|
||||||
|
link: '/en/guide/introduction',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: 'getting started',
|
||||||
|
link: '/en/guide/getting_started',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
'/en/api/': [
|
||||||
|
{
|
||||||
|
text: 'overview',
|
||||||
|
items: [{ text: 'overview of API', link: '/en/api/overview' }],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: 'packages',
|
||||||
|
items: [
|
||||||
|
{ text: 'algorithm', link: '/en/api/packages/algorithm' },
|
||||||
|
{ text: 'compare', link: '/en/api/packages/compare' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
83
docs/.vitepress/zh.ts
Normal file
83
docs/.vitepress/zh.ts
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
import type { DefaultTheme, LocaleSpecificConfig } from 'vitepress'
|
||||||
|
|
||||||
|
export const META_URL = 'https://lancet.go.dev'
|
||||||
|
export const META_TITLE = 'Lancet'
|
||||||
|
export const META_DESCRIPTION = '一个强大的Go语言工具函数库'
|
||||||
|
|
||||||
|
export const zhConfig: LocaleSpecificConfig<DefaultTheme.Config> = {
|
||||||
|
description: META_DESCRIPTION,
|
||||||
|
|
||||||
|
head: [
|
||||||
|
['meta', { property: 'og:url', content: META_URL }],
|
||||||
|
['meta', { property: 'og:description', content: META_DESCRIPTION }],
|
||||||
|
['meta', { property: 'twitter:url', content: META_URL }],
|
||||||
|
['meta', { property: 'twitter:title', content: META_TITLE }],
|
||||||
|
['meta', { property: 'twitter:description', content: META_DESCRIPTION }],
|
||||||
|
],
|
||||||
|
|
||||||
|
themeConfig: {
|
||||||
|
outline: {
|
||||||
|
label: '本页内容',
|
||||||
|
},
|
||||||
|
|
||||||
|
docFooter: {
|
||||||
|
prev: '上一页',
|
||||||
|
next: '下一页',
|
||||||
|
},
|
||||||
|
|
||||||
|
nav: [
|
||||||
|
{
|
||||||
|
text: '首页',
|
||||||
|
link: '/',
|
||||||
|
activeMatch: '^/',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: '指南',
|
||||||
|
link: '/guide/introduction',
|
||||||
|
activeMatch: '^/guide/',
|
||||||
|
},
|
||||||
|
{ text: 'API', link: '/api/overview', activeMatch: '^/api/' },
|
||||||
|
{
|
||||||
|
text: '相关链接',
|
||||||
|
items: [
|
||||||
|
{
|
||||||
|
text: '更新日志',
|
||||||
|
link: 'https://github.com/duke-git/lancet/releases',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
|
||||||
|
sidebar: {
|
||||||
|
'/': [
|
||||||
|
{
|
||||||
|
text: '介绍',
|
||||||
|
items: [
|
||||||
|
{
|
||||||
|
text: 'Lancet是什么?',
|
||||||
|
link: '/guide/introduction',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: '开始',
|
||||||
|
link: '/guide/getting_started',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
|
||||||
|
'/api/': [
|
||||||
|
{
|
||||||
|
text: '概览',
|
||||||
|
items: [{ text: 'API概述', link: '/api/overview' }],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: 'API文档',
|
||||||
|
items: [
|
||||||
|
{ text: '算法', link: '/api/packages/algorithm' },
|
||||||
|
{ text: '比较器', link: '/api/packages/compare' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
69
docs/api/overview.md
Normal file
69
docs/api/overview.md
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
---
|
||||||
|
outline: deep
|
||||||
|
---
|
||||||
|
|
||||||
|
# API概述
|
||||||
|
|
||||||
|
<b>lancet(柳叶刀)是一个强大、全面、高效、可复用的go语言工具函数库。包含25个包,超过600个工具函数。功能涵盖字符串处理、切片处理、网络、并发、加解密、文件处理、时间/日期、流处理、迭代器等等。</b>
|
||||||
|
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.package-title {
|
||||||
|
color: black;
|
||||||
|
font-size: 18px;
|
||||||
|
text-align: center;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
.package-container {
|
||||||
|
font-size: 16px;
|
||||||
|
border: 1px dashed;
|
||||||
|
padding: 10px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.package-cell {
|
||||||
|
height: 40px;
|
||||||
|
width: 140px;
|
||||||
|
display: inline-block;
|
||||||
|
vertical-align: middle;
|
||||||
|
line-height: 40px;
|
||||||
|
background: #ecefff;
|
||||||
|
border: 1px solid;
|
||||||
|
margin-right: 10px;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
border-radius: 6px;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<p class="package-title">lancet功能模块</p>
|
||||||
|
<div class="package-container">
|
||||||
|
<div class="package-cell">algorithm</div>
|
||||||
|
<div class="package-cell">compare</div>
|
||||||
|
<div class="package-cell">concurrency</div>
|
||||||
|
<div class="package-cell">condition</div>
|
||||||
|
<div class="package-cell">convertor</div>
|
||||||
|
<div class="package-cell">cryptor</div>
|
||||||
|
<div class="package-cell">datastructure</div>
|
||||||
|
<div class="package-cell">datetime</div>
|
||||||
|
<div class="package-cell">fileutil</div>
|
||||||
|
<div class="package-cell">formatter</div>
|
||||||
|
<div class="package-cell">function</div>
|
||||||
|
<div class="package-cell">iterator</div>
|
||||||
|
<div class="package-cell">maputil</div>
|
||||||
|
<div class="package-cell">mathutil</div>
|
||||||
|
<div class="package-cell">netutil</div>
|
||||||
|
<div class="package-cell">pointer</div>
|
||||||
|
<div class="package-cell">random</div>
|
||||||
|
<div class="package-cell">retry</div>
|
||||||
|
<div class="package-cell">slice</div>
|
||||||
|
<div class="package-cell">stream</div>
|
||||||
|
<div class="package-cell">structs</div>
|
||||||
|
<div class="package-cell">strutil</div>
|
||||||
|
<div class="package-cell">system</div>
|
||||||
|
<div class="package-cell">tuple</div>
|
||||||
|
<div class="package-cell">validator</div>
|
||||||
|
<div class="package-cell">xerror</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
636
docs/api/packages/algorithm.md
Normal file
636
docs/api/packages/algorithm.md
Normal file
@@ -0,0 +1,636 @@
|
|||||||
|
# Algorithm
|
||||||
|
|
||||||
|
algorithm 算法包实现一些基本算法,sort,search,lrucache。
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 源码
|
||||||
|
|
||||||
|
- [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)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 用法
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 目录
|
||||||
|
|
||||||
|
- [BubbleSort](#BubbleSort)
|
||||||
|
- [InsertionSort](#InsertionSort)
|
||||||
|
- [SelectionSort](#SelectionSort)
|
||||||
|
- [ShellSort](#ShellSort)
|
||||||
|
- [QuickSort](#QuickSort)
|
||||||
|
- [HeapSort](#HeapSort)
|
||||||
|
- [MergeSort](#MergeSort)
|
||||||
|
- [CountSort](#CountSort)
|
||||||
|
- [BinarySearch](#BinarySearch)
|
||||||
|
- [BinaryIterativeSearch](#BinaryIterativeSearch)
|
||||||
|
- [LinearSearch](#LinearSearch)
|
||||||
|
- [LRUCache](#LRUCache)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 文档
|
||||||
|
|
||||||
|
### <span id="BubbleSort">BubbleSort</span>
|
||||||
|
|
||||||
|
<p>冒泡排序,参数comparator需要实现包lancetconstraints.Comparator。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="display:inline-block;float:right;">[运行](https://go.dev/play/p/GNdv7Jg2Taj)</span></b>
|
||||||
|
|
||||||
|
```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]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="InsertionSort">InsertionSort</span>
|
||||||
|
|
||||||
|
<p>插入排序,参数comparator需要实现包lancetconstraints.Comparator。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="display:inline-block;float:right;">[运行](https://go.dev/play/p/G5LJiWgJJW6)</span></b>
|
||||||
|
|
||||||
|
```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}]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="SelectionSort">SelectionSort</span>
|
||||||
|
|
||||||
|
<p>选择排序,参数comparator需要实现包lancetconstraints.Comparator。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="display:inline-block;float:right;">[运行](https://go.dev/play/p/oXovbkekayS)</span></b>
|
||||||
|
|
||||||
|
```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]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ShellSort">ShellSort</span>
|
||||||
|
|
||||||
|
<p>希尔排序,参数comparator需要实现包lancetconstraints.Comparator。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="display:inline-block;float:right;">[运行](https://go.dev/play/p/3ibkszpJEu3)</span></b>
|
||||||
|
|
||||||
|
```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]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="QuickSort">QuickSort</span>
|
||||||
|
|
||||||
|
<p>快速排序,参数comparator需要实现包lancetconstraints.Comparator。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func QuickSort[T any](slice []T comparator lancetconstraints.Comparator)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="display:inline-block;float:right;">[运行](https://go.dev/play/p/7Y7c1Elk3ax)</span></b>
|
||||||
|
|
||||||
|
```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]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="HeapSort">HeapSort</span>
|
||||||
|
|
||||||
|
<p>堆排序,参数comparator需要实现包lancetconstraints.Comparator。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="display:inline-block;float:right;">[运行](https://go.dev/play/p/u6Iwa1VZS_f)</span></b>
|
||||||
|
|
||||||
|
```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]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="MergeSort">MergeSort</span>
|
||||||
|
|
||||||
|
<p>归并排序,参数comparator需要实现包lancetconstraints.Comparator。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="display:inline-block;float:right;">[运行](https://go.dev/play/p/ydinn9YzUJn)</span></b>
|
||||||
|
|
||||||
|
```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]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="CountSort">CountSort</span>
|
||||||
|
|
||||||
|
<p>计数排序,参数comparator需要实现包lancetconstraints.Comparator。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="display:inline-block;float:right;">[运行](https://go.dev/play/p/tB-Umgm0DrP)</span></b>
|
||||||
|
|
||||||
|
```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]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="BinarySearch">BinarySearch</span>
|
||||||
|
|
||||||
|
<p>二分递归查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例: <span style="display:inline-block;float:right;">[运行](https://go.dev/play/p/t6MeGiUSN47)</span></b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="BinaryIterativeSearch">BinaryIterativeSearch</span>
|
||||||
|
|
||||||
|
<p>二分迭代查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例: <span style="display:inline-block;float:right;">[运行](https://go.dev/play/p/Anozfr8ZLH3)</span></b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="LinearSearch">LinearSearch</span>
|
||||||
|
|
||||||
|
<p>基于传入的相等函数线性查找元素,返回元素索引,未找到元素返回-1。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例: <span style="display:inline-block;float:right;">[运行](https://go.dev/play/p/IsS7rgn5s3x)</span></b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="LRUCache">LRUCache</span>
|
||||||
|
|
||||||
|
<p>lru算法实现缓存。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="display:inline-block;float:right;">[运行](https://go.dev/play/p/-EZjgOURufP)</span></b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
375
docs/api/packages/compare.md
Normal file
375
docs/api/packages/compare.md
Normal file
@@ -0,0 +1,375 @@
|
|||||||
|
# Compare
|
||||||
|
|
||||||
|
compare包提供几个轻量级的类型比较函数。
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 源码:
|
||||||
|
|
||||||
|
- [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)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 用法:
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"github.com/duke-git/lancet/v2/condition"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 目录
|
||||||
|
|
||||||
|
- [Equal](#Equal)
|
||||||
|
- [EqualValue](#EqualValue)
|
||||||
|
- [LessThan](#LessThan)
|
||||||
|
- [GreaterThan](#GreaterThan)
|
||||||
|
- [LessOrEqual](#LessOrEqual)
|
||||||
|
- [GreaterOrEqual](#GreaterOrEqual)
|
||||||
|
- [InDelta](#InDelta)
|
||||||
|
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 文档
|
||||||
|
|
||||||
|
### <span id="Equal">Equal</span>
|
||||||
|
|
||||||
|
<p>检查两个值是否相等(检查类型和值)</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Equal(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例: <span style="display:inline-block;float:right;">[运行](https://go.dev/play/p/wmVxR-to4lz)</span></b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="EqualValue">EqualValue</span>
|
||||||
|
|
||||||
|
<p>检查两个值是否相等(只检查值)</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func EqualValue(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例: <span style="display:inline-block;float:right;">[运行](https://go.dev/play/p/fxnna_LLD9u)</span></b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="LessThan">LessThan</span>
|
||||||
|
|
||||||
|
<p>验证参数`left`的值是否小于参数`right`的值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func LessThan(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例: <span style="display:inline-block;float:right;">[运行](https://go.dev/play/p/cYh7FQQj0ne)</span></b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="GreaterThan">GreaterThan</span>
|
||||||
|
|
||||||
|
<p>验证参数`left`的值是否大于参数`right`的值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GreaterThan(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例: <span style="display:inline-block;float:right;">[运行](https://go.dev/play/p/9-NYDFZmIMp)</span></b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="LessOrEqual">LessOrEqual</span>
|
||||||
|
|
||||||
|
<p>验证参数`left`的值是否小于或等于参数`right`的值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func LessOrEqual(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例: <span style="display:inline-block;float:right;">[运行](https://go.dev/play/p/e4T_scwoQzp)</span></b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="GreaterOrEqual">GreaterOrEqual</span>
|
||||||
|
|
||||||
|
<p>验证参数`left`的值是否大于或参数`right`的值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GreaterOrEqual(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例: <span style="display:inline-block;float:right;">[运行](https://go.dev/play/p/vx8mP0U8DFk)</span></b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="InDelta">InDelta</span>
|
||||||
|
|
||||||
|
<p>检查增量内两个值是否相等。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func InDelta[T constraints.Integer | constraints.Float](left, right T, delta float64) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例: <span style="display:inline-block;float:right;">[运行](https://go.dev/play/p/TuDdcNtMkjo)</span></b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
70
docs/en/api/overview.md
Normal file
70
docs/en/api/overview.md
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
---
|
||||||
|
outline: deep
|
||||||
|
---
|
||||||
|
|
||||||
|
# API Overview
|
||||||
|
|
||||||
|
<b>Lancet (Lancet) is a powerful, comprehensive, efficient and reusable go language tool function library. Contains 25 packages, more than 600 utility functions. Functions cover string processing, slice processing, network, concurrency, encryption and decryption, file processing, time/date, stream processing, iterators, and more.</b>
|
||||||
|
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.package-title {
|
||||||
|
color: black;
|
||||||
|
font-size: 18px;
|
||||||
|
text-align: center;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
.package-container {
|
||||||
|
font-size: 16px;
|
||||||
|
border: 1px dashed;
|
||||||
|
padding: 10px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.package-cell {
|
||||||
|
height: 40px;
|
||||||
|
width: 140px;
|
||||||
|
display: inline-block;
|
||||||
|
vertical-align: middle;
|
||||||
|
line-height: 40px;
|
||||||
|
background: #ecefff;
|
||||||
|
border: 1px solid;
|
||||||
|
margin-right: 10px;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
border-radius: 6px;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<p class="package-title">lancet function module</p>
|
||||||
|
<div class="package-container">
|
||||||
|
<div class="package-cell">algorithm</div>
|
||||||
|
<div class="package-cell">compare</div>
|
||||||
|
<div class="package-cell">concurrency</div>
|
||||||
|
<div class="package-cell">condition</div>
|
||||||
|
<div class="package-cell">convertor</div>
|
||||||
|
<div class="package-cell">cryptor</div>
|
||||||
|
<div class="package-cell">datastructure</div>
|
||||||
|
<div class="package-cell">datetime</div>
|
||||||
|
<div class="package-cell">fileutil</div>
|
||||||
|
<div class="package-cell">formatter</div>
|
||||||
|
<div class="package-cell">function</div>
|
||||||
|
<div class="package-cell">iterator</div>
|
||||||
|
<div class="package-cell">maputil</div>
|
||||||
|
<div class="package-cell">mathutil</div>
|
||||||
|
<div class="package-cell">netutil</div>
|
||||||
|
<div class="package-cell">pointer</div>
|
||||||
|
<div class="package-cell">random</div>
|
||||||
|
<div class="package-cell">retry</div>
|
||||||
|
<div class="package-cell">slice</div>
|
||||||
|
<div class="package-cell">stream</div>
|
||||||
|
<div class="package-cell">structs</div>
|
||||||
|
<div class="package-cell">strutil</div>
|
||||||
|
<div class="package-cell">system</div>
|
||||||
|
<div class="package-cell">tuple</div>
|
||||||
|
<div class="package-cell">validator</div>
|
||||||
|
<div class="package-cell">xerror</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
636
docs/en/api/packages/algorithm.md
Normal file
636
docs/en/api/packages/algorithm.md
Normal file
@@ -0,0 +1,636 @@
|
|||||||
|
# Algorithm
|
||||||
|
|
||||||
|
Package algorithm implements some basic algorithm. eg. sort, search.
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 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)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 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)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
### <span id="BubbleSort">BubbleSort</span>
|
||||||
|
|
||||||
|
<p>Sort slice with bubble sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/GNdv7Jg2Taj)</span></b>
|
||||||
|
|
||||||
|
```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]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="InsertionSort">InsertionSort</span>
|
||||||
|
|
||||||
|
<p>Sort slice with insertion sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/G5LJiWgJJW6)</span></b>
|
||||||
|
|
||||||
|
```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}]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="SelectionSort">SelectionSort</span>
|
||||||
|
|
||||||
|
<p>Sort slice with selection sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/oXovbkekayS)</span></b>
|
||||||
|
|
||||||
|
```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]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="ShellSort">ShellSort</span>
|
||||||
|
|
||||||
|
<p>Sort slice with shell sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/3ibkszpJEu3)</span></b>
|
||||||
|
|
||||||
|
```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]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="QuickSort">QuickSort</span>
|
||||||
|
|
||||||
|
<p>Sort slice with quick sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func QuickSort[T any](slice []T comparator lancetconstraints.Comparator)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:<span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/7Y7c1Elk3ax)</span></b>
|
||||||
|
|
||||||
|
```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]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="HeapSort">HeapSort</span>
|
||||||
|
|
||||||
|
<p>Sort slice with heap sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/u6Iwa1VZS_f)</span></b>
|
||||||
|
|
||||||
|
```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]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="MergeSort">MergeSort</span>
|
||||||
|
|
||||||
|
<p>Sort slice with merge sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/ydinn9YzUJn)</span></b>
|
||||||
|
|
||||||
|
```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]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="CountSort">CountSort</span>
|
||||||
|
|
||||||
|
<p>Sort slice with count sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/tB-Umgm0DrP)</span></b>
|
||||||
|
|
||||||
|
```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]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="BinarySearch">BinarySearch</span>
|
||||||
|
|
||||||
|
<p>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.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/t6MeGiUSN47)</span></b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="BinaryIterativeSearch">BinaryIterativeSearch</span>
|
||||||
|
|
||||||
|
<p>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.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/Anozfr8ZLH3)</span></b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="LinearSearch">LinearSearch</span>
|
||||||
|
|
||||||
|
<p>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.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/IsS7rgn5s3x)</span></b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="LRUCache">LRUCache</span>
|
||||||
|
|
||||||
|
<p>LRUCache implements mem cache with lru.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/IsS7rgn5s3x)</span></b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
374
docs/en/api/packages/compare.md
Normal file
374
docs/en/api/packages/compare.md
Normal file
@@ -0,0 +1,374 @@
|
|||||||
|
# Compare
|
||||||
|
|
||||||
|
Package compare provides a lightweight comparison function on any type.
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 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)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Usage:
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"github.com/duke-git/lancet/v2/condition"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Index
|
||||||
|
|
||||||
|
- [Equal](#Equal)
|
||||||
|
- [EqualValue](#EqualValue)
|
||||||
|
- [LessThan](#LessThan)
|
||||||
|
- [GreaterThan](#GreaterThan)
|
||||||
|
- [LessOrEqual](#LessOrEqual)
|
||||||
|
- [GreaterOrEqual](#GreaterOrEqual)
|
||||||
|
- [InDelta](#InDelta)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
### <span id="Equal">Equal</span>
|
||||||
|
|
||||||
|
<p>Checks if two values are equal or not. (check both type and value)</p>
|
||||||
|
|
||||||
|
<b>Signature: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/wmVxR-to4lz)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Equal(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="EqualValue">EqualValue</span>
|
||||||
|
|
||||||
|
<p>Checks if two values are equal or not. (check value only)</p>
|
||||||
|
|
||||||
|
<b>Signature: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/fxnna_LLD9u)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func EqualValue(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="LessThan">LessThan</span>
|
||||||
|
|
||||||
|
<p>Checks if value `left` less than value `right`.</p>
|
||||||
|
|
||||||
|
<b>Signature: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/cYh7FQQj0ne)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func LessThan(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="GreaterThan">GreaterThan</span>
|
||||||
|
|
||||||
|
<p>Checks if value `left` greater than value `right`.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GreaterThan(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/9-NYDFZmIMp)</span></b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="LessOrEqual">LessOrEqual</span>
|
||||||
|
|
||||||
|
<p>Checks if value `left` less than or equal than value `right`.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func LessOrEqual(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/e4T_scwoQzp)</span></b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="GreaterOrEqual">GreaterOrEqual</span>
|
||||||
|
|
||||||
|
<p>Checks if value `left` less greater or equal than value `right`.</p>
|
||||||
|
|
||||||
|
<b>Signature: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/vx8mP0U8DFk)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GreaterOrEqual(left, right any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="InDelta">InDelta</span>
|
||||||
|
|
||||||
|
<p>Checks if two values are equal or not within a delta.</p>
|
||||||
|
|
||||||
|
<b>Signature: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/TuDdcNtMkjo)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func InDelta[T constraints.Integer | constraints.Float](left, right T, delta float64) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
```
|
||||||
50
docs/en/guide/getting_started.md
Normal file
50
docs/en/guide/getting_started.md
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
---
|
||||||
|
outline: deep
|
||||||
|
---
|
||||||
|
|
||||||
|
# Installation
|
||||||
|
|
||||||
|
1. <b>For users who use go1.18 and above, it is recommended to install lancet v2.x.x. Cause in v2.x.x all functions was rewriten with generics of go1.18.</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
|
||||||
|
```
|
||||||
|
|
||||||
|
2. <b>For users who use version below go1.18, you should install v1.x.x. The latest of v1.x.x is v1.4.1. </b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
go get github.com/duke-git/lancet // below go1.18, install latest version of v1.x.x
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Lancet organizes the code into package structure, and you need to import the corresponding package name when use it. For example, if you use string-related functions, just import the strutil package like below:
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "github.com/duke-git/lancet/v2/strutil"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
Here takes the string function `Reverse` (reverse order string) as an example, and the strutil package needs to be imported.
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
s := "hello"
|
||||||
|
rs := strutil.Reverse(s)
|
||||||
|
fmt.Println(rs) //olleh
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## More
|
||||||
|
|
||||||
|
Check out the [APIs]([API](https://lancet.go.dev/api/overview.html)) for details.
|
||||||
18
docs/en/guide/introduction.md
Normal file
18
docs/en/guide/introduction.md
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
---
|
||||||
|
outline: deep
|
||||||
|
---
|
||||||
|
|
||||||
|
# What is lancet?
|
||||||
|
|
||||||
|
<b>Lancet is a powerful, efficient, and reusable util function library of go. Inspired by the java apache common package and lodash.js. </b>
|
||||||
|
|
||||||
|
|
||||||
|
## Why lancet?
|
||||||
|
|
||||||
|
Lancet makes Go dev easier by taking the hassle out of working with concurrency, net, math, slice, string, etc.
|
||||||
|
Lancet's utility methods are great for:
|
||||||
|
|
||||||
|
- Iterating slice and array.
|
||||||
|
- Manipulating strings.
|
||||||
|
- Work with net and http.
|
||||||
|
- Other tools, eg. random, crypto, stream, retry, etc.
|
||||||
36
docs/en/index.md
Normal file
36
docs/en/index.md
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
---
|
||||||
|
# https://vitepress.dev/reference/default-theme-home-page
|
||||||
|
layout: home
|
||||||
|
|
||||||
|
hero:
|
||||||
|
name: "Lancet"
|
||||||
|
text: "A powerful util function library of Go"
|
||||||
|
tagline: Simple, powerful, and efficient.
|
||||||
|
actions:
|
||||||
|
- theme: brand
|
||||||
|
text: Get Started
|
||||||
|
link: /en/guide/getting_started
|
||||||
|
- theme: alt
|
||||||
|
text: View on GitHub
|
||||||
|
link: https://github.com/duke-git/lancet
|
||||||
|
# - theme: alt
|
||||||
|
# text: API Examples
|
||||||
|
# link: /api-examples
|
||||||
|
image:
|
||||||
|
src: /lancet_logo.png
|
||||||
|
alt: lancet
|
||||||
|
|
||||||
|
features:
|
||||||
|
- title: Powerful
|
||||||
|
icon: 💪
|
||||||
|
details: support 600+ go util functions. eg. string, slice, datetime, net, crypto, concurrency...
|
||||||
|
- title: Modular by design
|
||||||
|
icon: 🏗
|
||||||
|
details: Each module is designed as a package with no coupling between modules.
|
||||||
|
- title: Pure
|
||||||
|
icon: 💅
|
||||||
|
details: Only depends on two kinds of libraries, go standard library and golang.org/x.
|
||||||
|
- title: Simple
|
||||||
|
icon: 👏
|
||||||
|
details: Well structure, test for every exported function.
|
||||||
|
---
|
||||||
48
docs/guide/getting_started.md
Normal file
48
docs/guide/getting_started.md
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
---
|
||||||
|
outline: deep
|
||||||
|
---
|
||||||
|
|
||||||
|
# 安装
|
||||||
|
|
||||||
|
1. <b>使用 go1.18 及以上版本的用户,建议安装 v2.x.x。 因为 v2.x.x 应用 go1.18 的泛型重写了大部分函数。</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
|
||||||
|
```
|
||||||
|
|
||||||
|
2. <b>使用 go1.18 以下版本的用户,必须安装 v1.x.x。目前最新的 v1 版本是 v1.4.1。</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
go get github.com/duke-git/lancet // below go1.18, install latest version of v1.x.x
|
||||||
|
```
|
||||||
|
|
||||||
|
## 用法
|
||||||
|
|
||||||
|
lancet 是以包的结构组织代码的,使用时需要导入相应的包名。例如:如果使用字符串相关函数,需要导入 strutil 包:
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "github.com/duke-git/lancet/v2/strutil"
|
||||||
|
```
|
||||||
|
|
||||||
|
## 示例
|
||||||
|
|
||||||
|
此处以字符串工具函数 Reverse(逆序字符串)为例,需要导入 strutil 包:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
s := "hello"
|
||||||
|
rs := strutil.Reverse(s)
|
||||||
|
fmt.Println(rs) //olleh
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## More
|
||||||
|
|
||||||
|
其他特性请参考[API](https://lancet.go.dev/api/overview.html).
|
||||||
18
docs/guide/introduction.md
Normal file
18
docs/guide/introduction.md
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
---
|
||||||
|
outline: deep
|
||||||
|
---
|
||||||
|
|
||||||
|
# lancet是什么?
|
||||||
|
|
||||||
|
<b>lancet(柳叶刀)是一个强大、全面、高效、可复用的go语言工具函数库。lancet受到了java apache common包和lodash.js的启发。 </b>
|
||||||
|
|
||||||
|
|
||||||
|
## 为什么选择lancet?
|
||||||
|
|
||||||
|
Lancet 消除了处理并发、网络、数学、切片、字符串等的麻烦,使 Go 开发变得更容易。
|
||||||
|
Lancet 的实用方法非常适合:
|
||||||
|
|
||||||
|
- 迭代切片和数组。
|
||||||
|
- 操作字符串。
|
||||||
|
- 处理网络和http请求。
|
||||||
|
- 其他工具,例如。 随机、加密、流、重试等。
|
||||||
45
docs/index.md
Normal file
45
docs/index.md
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
---
|
||||||
|
# https://vitepress.dev/reference/default-theme-home-page
|
||||||
|
layout: home
|
||||||
|
|
||||||
|
hero:
|
||||||
|
name: 'Lancet'
|
||||||
|
text: '一个强大的Go语言工具函数库'
|
||||||
|
tagline: '简洁, 强大, 高效'
|
||||||
|
actions:
|
||||||
|
- theme: brand
|
||||||
|
text: 开始使用
|
||||||
|
link: /guide/getting_started
|
||||||
|
- theme: alt
|
||||||
|
text: 在GitHub中查看
|
||||||
|
link: https://github.com/duke-git/lancet
|
||||||
|
|
||||||
|
image:
|
||||||
|
src: /lancet_logo.png
|
||||||
|
alt: lancet
|
||||||
|
|
||||||
|
features:
|
||||||
|
- title: 全面
|
||||||
|
icon: 💪
|
||||||
|
details: 特性丰富,支持600+ go util函数。字符串、切片、日期时间、网络、加密、并发...
|
||||||
|
- title: 模块化设计
|
||||||
|
icon: 🏗
|
||||||
|
details: 每个模块设计成一个包,模块之间无耦合。
|
||||||
|
- title: 纯净
|
||||||
|
icon: 💅
|
||||||
|
details: 只依赖go标准库和golang.org/x。
|
||||||
|
- title: 简洁
|
||||||
|
icon: 👏
|
||||||
|
details: 结构良好,测试每个导出的函数。
|
||||||
|
---
|
||||||
|
|
||||||
|
<p style="position:relative; top: -316px;left: 540px;">
|
||||||
|
<img style="display: inline-block;margin-right:10px;" src="https://img.shields.io/github/stars/duke-git/lancet?style=social" alt="">
|
||||||
|
|
||||||
|
<img style="display: inline-block" src="https://img.shields.io/github/forks/duke-git/lancet?style=social" alt="">
|
||||||
|
|
||||||
|
<!-- [](https://github.com/duke-git/lancet)
|
||||||
|
|
||||||
|
[](https://github.com/duke-git/lancet) -->
|
||||||
|
|
||||||
|
</p>
|
||||||
BIN
docs/lancet_logo.png
Normal file
BIN
docs/lancet_logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 18 KiB |
BIN
docs/lancet_logo_mini.png
Normal file
BIN
docs/lancet_logo_mini.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 9.3 KiB |
1277
docs/package-lock.json
generated
Normal file
1277
docs/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
13
docs/package.json
Normal file
13
docs/package.json
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"name": "lancet-docs",
|
||||||
|
"private": true,
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"docs:dev": "vitepress dev",
|
||||||
|
"docs:build": "vitepress build",
|
||||||
|
"docs:preview": "vitepress preview"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"vitepress": "^1.0.0-rc.4"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user