diff --git a/README.md b/README.md index 5896e58..ff79165 100644 --- a/README.md +++ b/README.md @@ -81,19 +81,41 @@ import "github.com/duke-git/lancet/v2/algorithm" ``` #### Function list: - -- [BubbleSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#BubbleSort) -- [CountSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#CountSort) -- [HeapSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#HeapSort) -- [InsertionSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#InsertionSort) -- [MergeSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#MergeSort) -- [QuickSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#QuickSort) -- [SelectionSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#SelectionSort) -- [ShellSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#ShellSort) -- [BinarySearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#BinarySearch) -- [BinaryIterativeSearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#BinaryIterativeSearch) -- [LinearSearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LinearSearch) -- [LRUCache](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LRUCache) +- **BubbleSort** : sorts slice with bubble sort algorithm, will change the original slice. +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#BubbleSort)] +[[play](https://go.dev/play/p/GNdv7Jg2Taj)] +- **CountSort** : sorts slice with bubble sort algorithm, don't change original slice. +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#CountSort)] +[[play](https://go.dev/play/p/tB-Umgm0DrP)] +- **HeapSort** : sorts slice with heap sort algorithm, will change the original slice. +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#HeapSort)] +[[play](https://go.dev/play/p/u6Iwa1VZS_f)] +- **InsertionSort** : sorts slice with insertion sort algorithm, will change the original slice. +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#InsertionSort)] +[[play](https://go.dev/play/p/G5LJiWgJJW6)] +- **MergeSort** : sorts slice with merge sort algorithm, will change the original slice. +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#MergeSort)] +[[play](https://go.dev/play/p/ydinn9YzUJn)] +- **QuickSort** : sorts slice with quick sort algorithm, will change the original slice. +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#QuickSort)] +[[play](https://go.dev/play/p/7Y7c1Elk3ax)] +- **SelectionSort** : sorts slice with selection sort algorithm, will change the original slice. +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#SelectionSort)] +[[play](https://go.dev/play/p/oXovbkekayS)] +- **ShellSort** : sorts slice with shell sort algorithm, will change the original slice. +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#ShellSort)] +[[play](https://go.dev/play/p/3ibkszpJEu3)] +- **BinarySearch** : returns the index of target within a sorted slice, use binary search (recursive call itself). +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#BinarySearch)] +[[play](https://go.dev/play/p/t6MeGiUSN47)] +- **BinaryIterativeSearch** : returns the index of target within a sorted slice, use binary search (no recursive). +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#BinaryIterativeSearch)] +[[play](https://go.dev/play/p/Anozfr8ZLH3)] +- **LinearSearch** : returns the index of target in slice base on equal function. +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LinearSearch)] +- **LRUCache** : implements memory cache with lru algorithm. +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LRUCache)] +[[play](https://go.dev/play/p/-EZjgOURufP)] ### 2. Concurrency package contain some functions to support concurrent programming. eg, goroutine, channel, async. diff --git a/README_zh-CN.md b/README_zh-CN.md index 0898f30..85ee991 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -79,19 +79,41 @@ import "github.com/duke-git/lancet/v2/algorithm" ``` #### Function list: - -- [BubbleSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BubbleSort) -- [CountSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#CountSort) -- [HeapSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#HeapSort) -- [InsertionSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#InsertionSort) -- [MergeSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#MergeSort) -- [QuickSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#QuickSort) -- [SelectionSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#SelectionSort) -- [ShellSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#ShellSort) -- [BinarySearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BinarySearch) -- [BinaryIterativeSearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BinaryIterativeSearch) -- [LinearSearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LinearSearch) -- [LRUCache](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LRUCache) +- **BubbleSort** : 使用冒泡排序算法对切片进行排序。 +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BubbleSort)] +[[play](https://go.dev/play/p/GNdv7Jg2Taj)] +- **CountSort** : 使用计数排序算法对切片进行排序。不改变原数据。 +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#CountSort)] +[[play](https://go.dev/play/p/tB-Umgm0DrP)] +- **HeapSort** : 使用堆排序算法对切片进行排序。 +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#HeapSort)] +[[play](https://go.dev/play/p/u6Iwa1VZS_f)] +- **InsertionSort** : 使用插入排序算法对切片进行排序。 +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#InsertionSort)] +[[play](https://go.dev/play/p/G5LJiWgJJW6)] +- **MergeSort** : 使用合并排序算法对切片进行排序。 +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#MergeSort)] +[[play](https://go.dev/play/p/ydinn9YzUJn)] +- **QuickSort** : 使用快速排序算法对切片进行排序。 +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#QuickSort)] +[[play](https://go.dev/play/p/7Y7c1Elk3ax)] +- **SelectionSort** : 使用选择排序算法对切片进行排序。 +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#SelectionSort)] +[[play](https://go.dev/play/p/oXovbkekayS)] +- **ShellSort** : 使用希尔排序算法对切片进行排序。 +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#ShellSort)] +[[play](https://go.dev/play/p/3ibkszpJEu3)] +- **BinarySearch** : 返回排序切片中目标值的索引,使用二分搜索(递归调用)。 +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BinarySearch)] +[[play](https://go.dev/play/p/t6MeGiUSN47)] +- **BinaryIterativeSearch** :返回排序切片中目标值的索引,使用二分搜索(非递归)。 +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BinaryIterativeSearch)] +[[play](https://go.dev/play/p/Anozfr8ZLH3)] +- **LinearSearch** : 基于传入的相等函数返回切片中目标值的索引。(线性查找) +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LinearSearch)] +- **LRUCache** : 应用lru算法实现内存缓存. +[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LRUCache)] +[[play](https://go.dev/play/p/-EZjgOURufP)] ### 2. concurrency包含一些支持并发编程的功能。例如:goroutine, channel, async 等。 diff --git a/algorithm/search.go b/algorithm/search.go index 73a01b7..b44b1a6 100644 --- a/algorithm/search.go +++ b/algorithm/search.go @@ -1,7 +1,7 @@ // Copyright 2021 dudaodong@gmail.com. All rights reserved. // Use of this source code is governed by MIT license -// Package algorithm contain some basic algorithm functions. eg. sort, search, list, linklist, stack, queue, tree, graph. TODO +// Package algorithm contain some basic algorithm functions. eg. sort, search, list, linklist, stack, queue, tree, graph. package algorithm import "github.com/duke-git/lancet/v2/lancetconstraints" diff --git a/algorithm/sort.go b/algorithm/sort.go index 5dee7f2..aaf2f45 100644 --- a/algorithm/sort.go +++ b/algorithm/sort.go @@ -1,7 +1,6 @@ // Copyright 2021 dudaodong@gmail.com. All rights reserved. // Use of this source code is governed by MIT license -// Package algorithm contain some basic algorithm functions. eg. sort, search package algorithm import "github.com/duke-git/lancet/v2/lancetconstraints" @@ -35,7 +34,7 @@ func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) { } // SelectionSort applys the selection sort algorithm to sort the collection, will change the original collection data. -// Play: +// Play: https://go.dev/play/p/oXovbkekayS func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) { for i := 0; i < len(slice); i++ { min := i diff --git a/docs/algorithm.md b/docs/algorithm.md index 7ff2da6..82173b8 100644 --- a/docs/algorithm.md +++ b/docs/algorithm.md @@ -570,6 +570,8 @@ func main() { func NewLRUCache[K comparable, V any](capacity int) *LRUCache[K, V] func (l *LRUCache[K, V]) Get(key K) (V, bool) func (l *LRUCache[K, V]) Put(key K, value V) +func (l *LRUCache[K, V]) Delete(key K) bool +func (l *LRUCache[K, V]) Len() int ``` Example: @@ -586,10 +588,14 @@ func main() { cache.Put(1, 1) cache.Put(2, 2) + cache.Put(3, 3) - _, ok := cache.Get(0) // ok -> false + fmt.Println(cache.Len()) // 3 - v, ok := cache.Get(1) // v->1, ok->true + v, ok := cache.Get(1) + fmt.Println(v, ok) // 1 true + ok = cache.Delete(1) + fmt.Println(ok) // true } ``` \ No newline at end of file diff --git a/docs/algorithm_zh-CN.md b/docs/algorithm_zh-CN.md index d0dfc4e..771988a 100644 --- a/docs/algorithm_zh-CN.md +++ b/docs/algorithm_zh-CN.md @@ -570,6 +570,8 @@ func main() { func NewLRUCache[K comparable, V any](capacity int) *LRUCache[K, V] func (l *LRUCache[K, V]) Get(key K) (V, bool) func (l *LRUCache[K, V]) Put(key K, value V) +func (l *LRUCache[K, V]) Delete(key K) bool +func (l *LRUCache[K, V]) Len() int ``` Example: @@ -586,10 +588,14 @@ func main() { cache.Put(1, 1) cache.Put(2, 2) + cache.Put(3, 3) - _, ok := cache.Get(0) // ok -> false + fmt.Println(cache.Len()) // 3 - v, ok := cache.Get(1) // v->1, ok->true + v, ok := cache.Get(1) + fmt.Println(v, ok) // 1 true + ok = cache.Delete(1) + fmt.Println(ok) // true } ``` \ No newline at end of file