algo

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2026 License: MIT Imports: 2 Imported by: 0

README

Algo 工具包

提供常用的数据结构和算法实现,支持泛型。

📊 数据结构

Stack 栈
  • NewStack[T any]() *Stack[T] - 创建新栈
  • Push(item T) - 入栈
  • Pop() (T, bool) - 出栈
  • Peek() (T, bool) - 查看栈顶元素
  • Size() int - 返回栈大小
  • IsEmpty() bool - 检查栈是否为空
  • Clear() - 清空栈
Queue 队列
  • NewQueue[T any]() *Queue[T] - 创建新队列
  • Enqueue(item T) - 入队
  • Dequeue() (T, bool) - 出队
  • Peek() (T, bool) - 查看队首元素
  • Size() int - 返回队列大小
  • IsEmpty() bool - 检查队列是否为空
  • Clear() - 清空队列
Set 集合
  • NewSet[T comparable]() *Set[T] - 创建新集合
  • Add(item T) - 添加元素
  • Remove(item T) - 删除元素
  • Contains(item T) bool - 检查是否包含元素
  • Size() int - 返回集合大小
  • IsEmpty() bool - 检查集合是否为空
  • Clear() - 清空集合
  • ToSlice() []T - 转换为切片
  • Union(other *Set[T]) *Set[T] - 求并集
  • Intersect(other *Set[T]) *Set[T] - 求交集
  • Diff(other *Set[T]) *Set[T] - 求差集
LRU Cache
  • NewLRU[K comparable, V any](capacity int) *LRU[K, V] - 创建LRU缓存
  • Get(key K) (V, bool) - 获取值
  • Set(key K, value V) - 设置值
  • Remove(key K) bool - 删除键值对
  • Clear() - 清空缓存
  • Size() int - 返回当前大小
  • Cap() int - 返回容量

🔢 算法

排序算法
  • Sort[T cmp.Ordered](slice []T) - 对切片进行排序(快速排序)
  • SortDesc[T cmp.Ordered](slice []T) - 对切片进行降序排序
  • SortFunc[T any](slice []T, less func(a, b T) bool) - 使用自定义比较函数排序
  • IsSorted[T cmp.Ordered](slice []T) bool - 检查切片是否已排序
  • IsSortedFunc[T any](slice []T, less func(a, b T) bool) bool - 使用自定义比较函数检查是否已排序
搜索算法
  • BinarySearch[T cmp.Ordered](slice []T, target T) int - 二分查找,返回索引,不存在返回 -1
  • BinarySearchFunc[T any](slice []T, target T, cmp func(a, b T) int) int - 使用自定义比较函数进行二分查找
  • LinearSearch[T comparable](slice []T, target T) int - 线性查找,返回索引,不存在返回 -1
  • FindFirst[T any](slice []T, fn func(T) bool) int - 查找第一个满足条件的元素索引
  • FindLast[T any](slice []T, fn func(T) bool) int - 查找最后一个满足条件的元素索引
  • LowerBound[T cmp.Ordered](slice []T, target T) int - 查找第一个大于等于目标值的元素索引
  • UpperBound[T cmp.Ordered](slice []T, target T) int - 查找第一个大于目标值的元素索引

使用示例

package main

import (
	"fmt"
	"github.com/zzhuang94/go-kit/algo"
)

func main() {
	// Stack
	stack := algo.NewStack[int]()
	stack.Push(1)
	stack.Push(2)
	value, _ := stack.Pop()
	fmt.Println(value) // 2
	
	// Queue
	queue := algo.NewQueue[string]()
	queue.Enqueue("first")
	queue.Enqueue("second")
	value, _ := queue.Dequeue()
	fmt.Println(value) // "first"
	
	// Set
	set := algo.NewSet[int]()
	set.Add(1)
	set.Add(2)
	set.Add(2)
	fmt.Println(set.Size()) // 2
	fmt.Println(set.Contains(1)) // true
	
	// LRU Cache
	lru := algo.NewLRU[string, int](3)
	lru.Set("a", 1)
	lru.Set("b", 2)
	value, _ = lru.Get("a")
	fmt.Println(value) // 1
	
	// 排序
	numbers := []int{3, 1, 4, 1, 5, 9, 2, 6}
	algo.Sort(numbers)
	fmt.Println(numbers) // [1, 1, 2, 3, 4, 5, 6, 9]
	
	// 二分查找
	sorted := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
	index := algo.BinarySearch(sorted, 5)
	fmt.Println(index) // 4
	
	// 线性查找
	slice := []int{3, 1, 4, 1, 5, 9, 2, 6}
	index = algo.LinearSearch(slice, 5)
	fmt.Println(index) // 4
}

运行测试

go test ./algo -v

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BinarySearch

func BinarySearch[T cmp.Ordered](slice []T, target T) int

BinarySearch 二分查找,返回目标值的索引,如果不存在返回 -1 / Binary search, return index of target, -1 if not found

func BinarySearchFunc

func BinarySearchFunc[T any](slice []T, target T, cmp func(a, b T) int) int

BinarySearchFunc 使用自定义比较函数进行二分查找 / Binary search with custom comparison function

func FindFirst

func FindFirst[T any](slice []T, fn func(T) bool) int

FindFirst 查找第一个满足条件的元素索引,不存在返回 -1 / Find first element index matching condition, return -1 if not found

func FindLast

func FindLast[T any](slice []T, fn func(T) bool) int

FindLast 查找最后一个满足条件的元素索引,不存在返回 -1 / Find last element index matching condition, return -1 if not found

func IsSorted

func IsSorted[T cmp.Ordered](slice []T) bool

IsSorted 检查切片是否已排序 / Check if slice is sorted

func IsSortedFunc

func IsSortedFunc[T any](slice []T, less func(a, b T) bool) bool

IsSortedFunc 使用自定义比较函数检查切片是否已排序 / Check if slice is sorted using custom comparison function

func LinearSearch

func LinearSearch[T comparable](slice []T, target T) int

LinearSearch 线性查找,返回目标值的索引,如果不存在返回 -1 / Linear search, return index of target, -1 if not found

func LowerBound

func LowerBound[T cmp.Ordered](slice []T, target T) int

LowerBound 查找第一个大于等于目标值的元素索引 / Find first element index >= target

func Sort

func Sort[T cmp.Ordered](slice []T)

Sort 对切片进行排序(使用快速排序)/ Sort slice using quicksort

func SortDesc

func SortDesc[T cmp.Ordered](slice []T)

SortDesc 对切片进行降序排序 / Sort slice in descending order

func SortFunc

func SortFunc[T any](slice []T, less func(a, b T) bool)

SortFunc 使用自定义比较函数对切片进行排序 / Sort slice using custom comparison function

func UpperBound

func UpperBound[T cmp.Ordered](slice []T, target T) int

UpperBound 查找第一个大于目标值的元素索引 / Find first element index > target

Types

type LRU

type LRU[K comparable, V any] struct {
	// contains filtered or unexported fields
}

LRU LRU缓存 / LRU cache

func NewLRU

func NewLRU[K comparable, V any](capacity int) *LRU[K, V]

NewLRU 创建LRU缓存 / Create LRU cache

func (*LRU[K, V]) Cap

func (l *LRU[K, V]) Cap() int

Cap 返回容量 / Return capacity

func (*LRU[K, V]) Clear

func (l *LRU[K, V]) Clear()

Clear 清空缓存 / Clear cache

func (*LRU[K, V]) Get

func (l *LRU[K, V]) Get(key K) (V, bool)

Get 获取值 / Get value

func (*LRU[K, V]) Remove

func (l *LRU[K, V]) Remove(key K) bool

Remove 删除键值对 / Remove key-value pair

func (*LRU[K, V]) Set

func (l *LRU[K, V]) Set(key K, value V)

Set 设置值 / Set value

func (*LRU[K, V]) Size

func (l *LRU[K, V]) Size() int

Size 返回当前大小 / Return current size

type Queue

type Queue[T any] struct {
	// contains filtered or unexported fields
}

Queue 队列数据结构 / Queue data structure

func NewQueue

func NewQueue[T any]() *Queue[T]

NewQueue 创建新队列 / Create new queue

func (*Queue[T]) Clear

func (q *Queue[T]) Clear()

Clear 清空队列 / Clear queue

func (*Queue[T]) Dequeue

func (q *Queue[T]) Dequeue() (T, bool)

Dequeue 出队 / Dequeue item

func (*Queue[T]) Enqueue

func (q *Queue[T]) Enqueue(item T)

Enqueue 入队 / Enqueue item

func (*Queue[T]) IsEmpty

func (q *Queue[T]) IsEmpty() bool

IsEmpty 检查队列是否为空 / Check if queue is empty

func (*Queue[T]) Peek

func (q *Queue[T]) Peek() (T, bool)

Peek 查看队首元素 / Peek at front element

func (*Queue[T]) Size

func (q *Queue[T]) Size() int

Size 返回队列大小 / Return queue size

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set 集合数据结构 / Set data structure

func NewSet

func NewSet[T comparable]() *Set[T]

NewSet 创建新集合 / Create new set

func (*Set[T]) Add

func (s *Set[T]) Add(item T)

Add 添加元素 / Add element

func (*Set[T]) Clear

func (s *Set[T]) Clear()

Clear 清空集合 / Clear set

func (*Set[T]) Contains

func (s *Set[T]) Contains(item T) bool

Contains 检查是否包含元素 / Check if contains element

func (*Set[T]) Diff

func (s *Set[T]) Diff(other *Set[T]) *Set[T]

Diff 求差集(s 中有但 other 中没有的元素)/ Get difference (elements in s but not in other)

func (*Set[T]) Intersect

func (s *Set[T]) Intersect(other *Set[T]) *Set[T]

Intersect 求交集 / Get intersection

func (*Set[T]) IsEmpty

func (s *Set[T]) IsEmpty() bool

IsEmpty 检查集合是否为空 / Check if set is empty

func (*Set[T]) Remove

func (s *Set[T]) Remove(item T)

Remove 删除元素 / Remove element

func (*Set[T]) Size

func (s *Set[T]) Size() int

Size 返回集合大小 / Return set size

func (*Set[T]) ToSlice

func (s *Set[T]) ToSlice() []T

ToSlice 转换为切片 / Convert to slice

func (*Set[T]) Union

func (s *Set[T]) Union(other *Set[T]) *Set[T]

Union 求并集 / Get union

type Stack

type Stack[T any] struct {
	// contains filtered or unexported fields
}

Stack 栈数据结构 / Stack data structure

func NewStack

func NewStack[T any]() *Stack[T]

NewStack 创建新栈 / Create new stack

func (*Stack[T]) Clear

func (s *Stack[T]) Clear()

Clear 清空栈 / Clear stack

func (*Stack[T]) IsEmpty

func (s *Stack[T]) IsEmpty() bool

IsEmpty 检查栈是否为空 / Check if stack is empty

func (*Stack[T]) Peek

func (s *Stack[T]) Peek() (T, bool)

Peek 查看栈顶元素 / Peek at top element

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() (T, bool)

Pop 出栈 / Pop item from stack

func (*Stack[T]) Push

func (s *Stack[T]) Push(item T)

Push 入栈 / Push item onto stack

func (*Stack[T]) Size

func (s *Stack[T]) Size() int

Size 返回栈大小 / Return stack size

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL