Documentation
¶
Index ¶
- func AscSort[T any](params []T)
- func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator)
- func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
- func DescSort[T any](params []T)
- func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator)
- func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator)
- func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator)
- func QuickSort[T any](slice []T, comparator lancetconstraints.Comparator)
- func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator)
- func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator)
- func Sort[T any](params []T, comparator lancetconstraints.Comparator)
- type AscComparator
- type DescComparator
- type LRUCache
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BubbleSort ¶
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator)
BubbleSort applys the bubble sort algorithm to sort the collection, will change the original collection data. Play: https://go.dev/play/p/GNdv7Jg2Taj
Example ¶
numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} BubbleSort(numbers, comparator) fmt.Println(numbers)
Output: [1 2 3 4 5 6]
func CountSort ¶
func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
CountSort applys the count sort algorithm to sort the collection, don't change the original collection data. Play: https://go.dev/play/p/tB-Umgm0DrP
Example ¶
numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} sortedNumber := CountSort(numbers, comparator) fmt.Println(numbers) fmt.Println(sortedNumber)
Output: [2 1 5 3 6 4] [1 2 3 4 5 6]
func HeapSort ¶
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator)
HeapSort applys the heap sort algorithm to sort the collection, will change the original collection data. Play: https://go.dev/play/p/u6Iwa1VZS_f
Example ¶
numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} HeapSort(numbers, comparator) fmt.Println(numbers)
Output: [1 2 3 4 5 6]
func InsertionSort ¶
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator)
InsertionSort applys the insertion sort algorithm to sort the collection, will change the original collection data. Play: https://go.dev/play/p/G5LJiWgJJW6
Example ¶
numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} InsertionSort(numbers, comparator) fmt.Println(numbers)
Output: [1 2 3 4 5 6]
func MergeSort ¶
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator)
MergeSort applys the merge sort algorithm to sort the collection, will change the original collection data. Play: https://go.dev/play/p/ydinn9YzUJn
Example ¶
numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} MergeSort(numbers, comparator) fmt.Println(numbers)
Output: [1 2 3 4 5 6]
func QuickSort ¶
func QuickSort[T any](slice []T, comparator lancetconstraints.Comparator)
QuickSort quick sorting for slice, lowIndex is 0 and highIndex is len(slice)-1. Play: https://go.dev/play/p/7Y7c1Elk3ax
Example ¶
numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} QuickSort(numbers, comparator) fmt.Println(numbers)
Output: [1 2 3 4 5 6]
func SelectionSort ¶
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator)
SelectionSort applys the selection sort algorithm to sort the collection, will change the original collection data. Play: https://go.dev/play/p/oXovbkekayS
Example ¶
numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} SelectionSort(numbers, comparator) fmt.Println(numbers)
Output: [1 2 3 4 5 6]
func ShellSort ¶
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator)
ShellSort applys the shell sort algorithm to sort the collection, will change the original collection data. Play: https://go.dev/play/p/3ibkszpJEu3
Example ¶
numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} ShellSort(numbers, comparator) fmt.Println(numbers)
Output: [1 2 3 4 5 6]
Types ¶
type AscComparator ¶
type AscComparator struct{}
type DescComparator ¶
type DescComparator struct{}
type LRUCache ¶
type LRUCache[K comparable, V any] struct { // contains filtered or unexported fields }
LRUCache lru cache (thread unsafe)
func NewLRUCache ¶
func NewLRUCache[K comparable, V any](capacity int) *LRUCache[K, V]
NewLRUCache creates a LRUCache pointer instance.
func (*LRUCache[K, V]) Delete ¶
Delete item from lru cache.
Example ¶
cache := NewLRUCache[int, int](2) cache.Put(1, 1) cache.Put(2, 2) result1, ok1 := cache.Get(1) ok2 := cache.Delete(2) _, ok3 := cache.Get(2) fmt.Println(result1, ok1) fmt.Println(ok2) fmt.Println(ok3)
Output: 1 true true false
func (*LRUCache[K, V]) Get ¶
Get value of key from lru cache. Play: https://go.dev/play/p/iUynEfOP8G0
Example ¶
cache := 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)
Output: 1 true 2 true 0 false
func (*LRUCache[K, V]) Len ¶
Len returns the number of items in the cache.
Example ¶
cache := NewLRUCache[int, int](2) cache.Put(1, 1) cache.Put(2, 2) result := cache.Len() fmt.Println(result)
Output: 2
func (*LRUCache[K, V]) Put ¶
func (l *LRUCache[K, V]) Put(key K, value V)
Put value of key into lru cache. Play: https://go.dev/play/p/iUynEfOP8G0
Example ¶
cache := 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)
Output: 1 true 2 true 0 false