From 2bc87d8a6b4c726300ddbafa879947d1f137e20b Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 16 Mar 2022 18:00:38 +0800 Subject: [PATCH] docs: add docs/algorithm.md --- docs/algorithm.md | 593 ++++++++++++++++++++++++++++++++++++++++ docs/algorithm_zh-CN.md | 593 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1186 insertions(+) create mode 100644 docs/algorithm.md create mode 100644 docs/algorithm_zh-CN.md diff --git a/docs/algorithm.md b/docs/algorithm.md new file mode 100644 index 0000000..e71b0c8 --- /dev/null +++ b/docs/algorithm.md @@ -0,0 +1,593 @@ +# Algorithm +Package algorithm implements some basic algorithm. eg. sort, search. + +
+ +## Source + +- [https://github.com/duke-git/lancet/blob/main/algorithm/sorter.go](https://github.com/duke-git/lancet/blob/main/algorithm/sorter.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/algorithm" +) +``` + +
+ +## Index +- [BubbleSort](#BubbleSort) +- [CountSort](#CountSort) +- [HeapSort](#HeapSort) +- [InsertionSort](#InsertionSort) +- [MergeSort](#MergeSort) +- [QuickSort](#QuickSort) +- [SelectionSort](#SelectionSort) +- [ShellSort](#ShellSort) +- [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) []T +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/algorithm" +) + +func main() { + type intComparator struct{} + + func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + val1, _ := v1.(int) + val2, _ := v2.(int) + + //ascending order + if val1 < val2 { + return -1 + } else if val1 > val2 { + return 1 + } + return 0 + } + + intSlice := []int{2, 1, 5, 3, 6, 4} + comparator := &intComparator{} + sortedSlice := algorithm.BubbleSort(intSlice, comparator) + + fmt.Println(sortedSlice) //[]int{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) []T +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/algorithm" +) + +func main() { + 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 interface{}, v2 interface{}) 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 + + //decending order + // if p1.Age > p2.Age { + // return -1 + // } else if p1.Age < p2.Age { + // return 1 + // } + } + + var peoples = []people{ + {Name: "a", Age: 20}, + {Name: "b", Age: 10}, + {Name: "c", Age: 17}, + {Name: "d", Age: 8}, + {Name: "e", Age: 28}, + } + comparator := &peopleAgeComparator{} + sortedPeople := algorithm.InsertionSort(peoples, comparator) + + fmt.Println(sortedSlice) //[{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) []T +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/algorithm" +) + +func main() { + type intComparator struct{} + + func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + val1, _ := v1.(int) + val2, _ := v2.(int) + + //ascending order + if val1 < val2 { + return -1 + } else if val1 > val2 { + return 1 + } + return 0 + } + + intSlice := []int{2, 1, 5, 3, 6, 4} + comparator := &intComparator{} + sortedSlice := algorithm.SelectionSort(intSlice, comparator) + + fmt.Println(sortedSlice) //[]int{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) []T +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/algorithm" +) + +func main() { + type intComparator struct{} + + func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + val1, _ := v1.(int) + val2, _ := v2.(int) + + //ascending order + if val1 < val2 { + return -1 + } else if val1 > val2 { + return 1 + } + return 0 + } + + intSlice := []int{2, 1, 5, 3, 6, 4} + comparator := &intComparator{} + sortedSlice := algorithm.ShellSort(intSlice, comparator) + + fmt.Println(sortedSlice) //[]int{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, lowIndex, highIndex int, comparator lancetconstraints.Comparator) []T +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/algorithm" +) + +func main() { + type intComparator struct{} + + func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + val1, _ := v1.(int) + val2, _ := v2.(int) + + //ascending order + if val1 < val2 { + return -1 + } else if val1 > val2 { + return 1 + } + return 0 + } + + intSlice := []int{2, 1, 5, 3, 6, 4} + comparator := &intComparator{} + sortedSlice := algorithm.QuickSort(intSlice, 0, len(intSlice)-1, comparator) + + fmt.Println(sortedSlice) //[]int{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) []T +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/algorithm" +) + +func main() { + type intComparator struct{} + + func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + val1, _ := v1.(int) + val2, _ := v2.(int) + + //ascending order + if val1 < val2 { + return -1 + } else if val1 > val2 { + return 1 + } + return 0 + } + + intSlice := []int{2, 1, 5, 3, 6, 4} + comparator := &intComparator{} + sortedSlice := algorithm.HeapSort(intSlice, comparator) + + fmt.Println(sortedSlice) //[]int{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, lowIndex, highIndex int, comparator lancetconstraints.Comparator) []T +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/algorithm" +) + +func main() { + type intComparator struct{} + + func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + val1, _ := v1.(int) + val2, _ := v2.(int) + + //ascending order + if val1 < val2 { + return -1 + } else if val1 > val2 { + return 1 + } + return 0 + } + + intSlice := []int{2, 1, 5, 3, 6, 4} + comparator := &intComparator{} + sortedSlice := algorithm.MergeSort(intSlice, 0, len(intSlice)-1, comparator) + + fmt.Println(sortedSlice) //[]int{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/algorithm" +) + +func main() { + type intComparator struct{} + + func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + val1, _ := v1.(int) + val2, _ := v2.(int) + + //ascending order + if val1 < val2 { + return -1 + } else if val1 > val2 { + return 1 + } + return 0 + } + + intSlice := []int{2, 1, 5, 3, 6, 4} + comparator := &intComparator{} + sortedSlice := algorithm.CountSort(intSlice, comparator) + + fmt.Println(sortedSlice) //[]int{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/algorithm" +) + +func main() { + type intComparator struct{} + + func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + val1, _ := v1.(int) + val2, _ := v2.(int) + + //ascending order + if val1 < val2 { + return -1 + } else if val1 > val2 { + return 1 + } + return 0 + } + + var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8} + comparator := &intComparator{} + foundIndex := algorithm.BinarySearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator) + fmt.Println(foundIndex) //4 + + notFoundIndex := algorithm.BinarySearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator) + fmt.Println(notFoundIndex) //-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/algorithm" +) + +func main() { + type intComparator struct{} + + func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + val1, _ := v1.(int) + val2, _ := v2.(int) + + //ascending order + if val1 < val2 { + return -1 + } else if val1 > val2 { + return 1 + } + return 0 + } + + var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8} + comparator := &intComparator{} + foundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator) + fmt.Println(foundIndex) //4 + + notFoundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator) + fmt.Println(notFoundIndex) //-1 +} +``` + + + + +### LinearSearch +

LinearSearch Simple linear search algorithm that iterates over all elements of an slice. 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, comparator lancetconstraints.Comparator) int +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/algorithm" +) + +func main() { + type intComparator struct{} + + func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + val1, _ := v1.(int) + val2, _ := v2.(int) + + //ascending order + if val1 < val2 { + return -1 + } else if val1 > val2 { + return 1 + } + return 0 + } + + intSlice := []int{2, 1, 5, 3, 6, 4} + comparator := &intComparator{} + foundIndex := algorithm.LinearSearch(intSlice, 5, comparator) + fmt.Println(foundIndex) //2 + + notFoundIndex := algorithm.LinearSearch(sortedNumbers, 0, comparator) + fmt.Println(notFoundIndex) //-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) +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/algorithm" +) + +func main() { + cache := algorithm.NewLRUCache[int, int](2) + + cache.Put(1, 1) + cache.Put(2, 2) + + _, ok := cache.Get(0) // ok -> false + + v, ok := cache.Get(1) // v->1, ok->true + +} +``` \ No newline at end of file diff --git a/docs/algorithm_zh-CN.md b/docs/algorithm_zh-CN.md new file mode 100644 index 0000000..8283391 --- /dev/null +++ b/docs/algorithm_zh-CN.md @@ -0,0 +1,593 @@ +# Algorithm +algorithm算法包实现一些基本算法,sort,search,lrucache。 + +
+ +## 源码 + +- [https://github.com/duke-git/lancet/blob/main/algorithm/sorter.go](https://github.com/duke-git/lancet/blob/main/algorithm/sorter.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/algorithm" +) +``` + +
+ +## 目录 +- [BubbleSort](#BubbleSort) +- [CountSort](#CountSort) +- [HeapSort](#HeapSort) +- [InsertionSort](#InsertionSort) +- [MergeSort](#MergeSort) +- [QuickSort](#QuickSort) +- [SelectionSort](#SelectionSort) +- [ShellSort](#ShellSort) +- [BinarySearch](#BinarySearch) +- [BinaryIterativeSearch](#BinaryIterativeSearch) +- [LinearSearch](#LinearSearch) +- [LRUCache](#LRUCache) + +
+ +## 文档 + + + +### BubbleSort +

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

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

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

+ +函数签名: + +```go +func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) []T +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/algorithm" +) + +func main() { + 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 interface{}, v2 interface{}) 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 + + //decending order + // if p1.Age > p2.Age { + // return -1 + // } else if p1.Age < p2.Age { + // return 1 + // } + } + + var peoples = []people{ + {Name: "a", Age: 20}, + {Name: "b", Age: 10}, + {Name: "c", Age: 17}, + {Name: "d", Age: 8}, + {Name: "e", Age: 28}, + } + comparator := &peopleAgeComparator{} + sortedPeople := algorithm.InsertionSort(peoples, comparator) + + fmt.Println(sortedSlice) //[{d 8} {b 10} {c 17} {a 20} {e 28}] +} +``` + + + + +### SelectionSort +

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

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

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

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

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

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

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

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

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

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

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

+ +函数签名: + +```go +func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/algorithm" +) + +func main() { + type intComparator struct{} + + func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + val1, _ := v1.(int) + val2, _ := v2.(int) + + //ascending order + if val1 < val2 { + return -1 + } else if val1 > val2 { + return 1 + } + return 0 + } + + intSlice := []int{2, 1, 5, 3, 6, 4} + comparator := &intComparator{} + sortedSlice := algorithm.CountSort(intSlice, comparator) + + fmt.Println(sortedSlice) //[]int{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 +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/algorithm" +) + +func main() { + type intComparator struct{} + + func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + val1, _ := v1.(int) + val2, _ := v2.(int) + + //ascending order + if val1 < val2 { + return -1 + } else if val1 > val2 { + return 1 + } + return 0 + } + + var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8} + comparator := &intComparator{} + foundIndex := algorithm.BinarySearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator) + fmt.Println(foundIndex) //4 + + notFoundIndex := algorithm.BinarySearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator) + fmt.Println(notFoundIndex) //-1 +} +``` + + + +### BinaryIterativeSearch +

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

+ +函数签名: + +```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/algorithm" +) + +func main() { + type intComparator struct{} + + func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + val1, _ := v1.(int) + val2, _ := v2.(int) + + //ascending order + if val1 < val2 { + return -1 + } else if val1 > val2 { + return 1 + } + return 0 + } + + var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8} + comparator := &intComparator{} + foundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator) + fmt.Println(foundIndex) //4 + + notFoundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator) + fmt.Println(notFoundIndex) //-1 +} +``` + + + + +### LinearSearch +

线性查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator

+ +函数签名: + +```go +func LinearSearch[T any](slice []T, target T, comparator lancetconstraints.Comparator) int +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/algorithm" +) + +func main() { + type intComparator struct{} + + func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + val1, _ := v1.(int) + val2, _ := v2.(int) + + //ascending order + if val1 < val2 { + return -1 + } else if val1 > val2 { + return 1 + } + return 0 + } + + intSlice := []int{2, 1, 5, 3, 6, 4} + comparator := &intComparator{} + foundIndex := algorithm.LinearSearch(intSlice, 5, comparator) + fmt.Println(foundIndex) //2 + + notFoundIndex := algorithm.LinearSearch(sortedNumbers, 0, comparator) + fmt.Println(notFoundIndex) //-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) +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/algorithm" +) + +func main() { + cache := algorithm.NewLRUCache[int, int](2) + + cache.Put(1, 1) + cache.Put(2, 2) + + _, ok := cache.Get(0) // ok -> false + + v, ok := cache.Get(1) // v->1, ok->true + +} +``` \ No newline at end of file