Documentation
¶
Overview ¶
* @Author: kamalyes 501893067@qq.com * @Date: 2025-01-20 10:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-01-20 10:00:00 * @FilePath: \go-toolbox\pkg\queue\bounded_queue.go * @Description: * BoundedQueue 是一个有界队列实现,支持最小/最大容量限制、自动扩缩容、阻塞/非阻塞操作 * 采用智能增长策略:小容量快速增长(2x),中容量渐进增长(1.5x),大容量固定增量 * 适用于需要流量控制、内存保护的高并发场景 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-08-21 16:01:08 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-08-22 09:59:16 * @FilePath: \go-toolbox\pkg\queue\deque.go * @Description: * Deque 是一个双端队列(double-ended queue)实现,支持从两端插入和删除元素。 * 该实现使用环形缓冲区来优化存储和访问效率。队列支持动态扩容和缩容, * 以适应不同的使用场景,提供了多种操作方法,包括 Push、Pop、Iterate 等 * 适合需要高效插入和删除的场景,如任务调度、缓存等 * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-10 21:51:58 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-11-11 11:26:09 * @FilePath: \go-toolbox\pkg\queue\priority_queue.go * @Description: 优先队列实现 * * Copyright (c) 2024 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-10 21:51:58 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-11-10 22:57:16 * @FilePath: \go-toolbox\pkg\queue\queue.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.
Index ¶
- Variables
- type BoundedQueue
- func (q *BoundedQueue[T]) Cap() int
- func (q *BoundedQueue[T]) Close()
- func (q *BoundedQueue[T]) Dequeue(ctx context.Context) (T, error)
- func (q *BoundedQueue[T]) DequeueTimeout(timeout time.Duration) (T, error)
- func (q *BoundedQueue[T]) Enqueue(ctx context.Context, item T) error
- func (q *BoundedQueue[T]) IsClosed() bool
- func (q *BoundedQueue[T]) IsEmpty() bool
- func (q *BoundedQueue[T]) SetAutoResize(enabled bool)
- func (q *BoundedQueue[T]) Size() int
- func (q *BoundedQueue[T]) Stats() map[string]interface{}
- func (q *BoundedQueue[T]) TryDequeue() (T, bool)
- type Deque
- func (q *Deque) At(i int) interface{}
- func (q *Deque) Back() (interface{}, error)
- func (q *Deque) Cap() int
- func (q *Deque) Clear()
- func (q *Deque) Front() (interface{}, error)
- func (q *Deque) Grow(n int)
- func (q *Deque) Index(f func(interface{}) bool) int
- func (q *Deque) Insert(i int, item interface{})
- func (q *Deque) Iter() func(func(interface{}) bool)
- func (q *Deque) IterPopBack() func(func(interface{}) bool)
- func (q *Deque) IterPopFront() func(func(interface{}) bool)
- func (q *Deque) Len() int
- func (q *Deque) PopBack() interface{}
- func (q *Deque) PopFront() interface{}
- func (q *Deque) PushBack(elem interface{})
- func (q *Deque) PushFront(elem interface{})
- func (q *Deque) RIndex(f func(interface{}) bool) int
- func (q *Deque) RIter() func(func(interface{}) bool)
- func (q *Deque) Rotate(n int)
- func (q *Deque) Set(i int, item interface{})
- type FIFOQueue
- func (q *FIFOQueue) Capacity() int
- func (q *FIFOQueue) Dequeue(ctx context.Context) (interface{}, error)
- func (q *FIFOQueue) Enqueue(ctx context.Context, item interface{}) error
- func (q *FIFOQueue) GrowthFactor() float64
- func (q *FIFOQueue) IsEmpty() bool
- func (q *FIFOQueue) MinCapacity() int
- func (q *FIFOQueue) SetGrowthFactor(value float64) *FIFOQueue
- func (q *FIFOQueue) SetShrinkFactor(value float64) *FIFOQueue
- func (q *FIFOQueue) ShrinkFactor() float64
- func (q *FIFOQueue) Size() int
- type Item
- type LIFOQueue
- type PriorityQueue
- func (pq *PriorityQueue) Dequeue(ctx context.Context) (interface{}, error)
- func (pq *PriorityQueue) Enqueue(ctx context.Context, item interface{}, priority int) error
- func (pq *PriorityQueue) IsEmpty() bool
- func (pq *PriorityQueue) Len() int
- func (pq *PriorityQueue) Less(i, j int) bool
- func (pq *PriorityQueue) Pop() interface{}
- func (pq *PriorityQueue) Push(x interface{})
- func (pq *PriorityQueue) Size() int
- func (pq *PriorityQueue) Swap(i, j int)
- type Queue
Constants ¶
This section is empty.
Variables ¶
var ( // ErrQueueClosed 队列已关闭错误 ErrQueueClosed = errors.New("队列已关闭") // ErrQueueFull 队列已满错误 ErrQueueFull = errors.New("队列已满") )
var ErrQueueEmpty = errors.New("队列为空")
定义队列为空的错误
Functions ¶
This section is empty.
Types ¶
type BoundedQueue ¶
type BoundedQueue[T any] struct { // contains filtered or unexported fields }
BoundedQueue 有界队列,支持泛型
func NewBoundedQueue ¶
func NewBoundedQueue[T any](minCap, maxCap int) *BoundedQueue[T]
NewBoundedQueue 创建有界队列 minCap: 最小容量, maxCap: 最大容量
func (*BoundedQueue[T]) Dequeue ¶
func (q *BoundedQueue[T]) Dequeue(ctx context.Context) (T, error)
Dequeue 从队列取出元素,阻塞直到有元素或上下文取消(实现Queue接口)
func (*BoundedQueue[T]) DequeueTimeout ¶
func (q *BoundedQueue[T]) DequeueTimeout(timeout time.Duration) (T, error)
DequeueTimeout 带超时的取出操作
func (*BoundedQueue[T]) Enqueue ¶
func (q *BoundedQueue[T]) Enqueue(ctx context.Context, item T) error
Enqueue 将元素加入队列(实现Queue接口)
func (*BoundedQueue[T]) IsEmpty ¶
func (q *BoundedQueue[T]) IsEmpty() bool
IsEmpty 检查队列是否为空(实现Queue接口)
func (*BoundedQueue[T]) SetAutoResize ¶
func (q *BoundedQueue[T]) SetAutoResize(enabled bool)
SetAutoResize 设置是否自动调整容量
func (*BoundedQueue[T]) Stats ¶
func (q *BoundedQueue[T]) Stats() map[string]interface{}
Stats 返回队列统计信息
func (*BoundedQueue[T]) TryDequeue ¶
func (q *BoundedQueue[T]) TryDequeue() (T, bool)
TryDequeue 非阻塞取出,如果队列为空立即返回
type Deque ¶
type Deque struct {
// contains filtered or unexported fields
}
Deque 表示双端队列数据结构的单个实例Deque 实例包含 指定类型的项
func NewDeque ¶
func NewDeque() *Deque
NewDeque 创建并返回一个新的 Deque 实例 该函数初始化一个双端队列,分配最小容量的缓冲区,并设置头、尾索引和元素计数
func (*Deque) At ¶
At 返回队列中索引 i 处的元素,而不移除该元素 此方法仅接受非负索引值At(0) 指的是第一个元素, 与 Front() 相同At(Len()-1) 指的是最后一个元素, 与 Back() 相同如果索引无效,调用会 panic
func (*Deque) Clear ¶
func (q *Deque) Clear()
Clear 移除队列中的所有元素,但保留当前容量 这在高频率重复使用队列时非常有用,以避免垃圾回收 只要仅添加项目,队列就不会被调整为更小的尺寸 只有在移除项目时,队列才会被调整为更小的尺寸
func (*Deque) Grow ¶
Grow 如果需要,增加双端队列的容量,以保证可以容纳另 n 个项目在 Grow(n) 之后,至少可以向队列中写入 n 个项目, 而无需再次分配如果 n 为负数,Grow 会 panic
func (*Deque) Index ¶
Index 返回满足 f(item) 的第一个项在 Deque 中的索引, 如果没有满足条件的项,则返回 -1如果 q 为 nil,则总是返回 -1 搜索是线性的,从索引 0 开始
func (*Deque) Iter ¶
Iter 返回一个迭代器,用于遍历 Deque 中的所有项目, 从前(索引 0)到后(索引 Len()-1)依次返回每个项目 在迭代过程中修改 Deque 会导致 panic
func (*Deque) IterPopBack ¶
IterPopBack 返回一个迭代器,该迭代器从双端队列的末尾迭代移除项目 这比一次移除一个项目更有效,因为它避免了中间的调整大小 如果需要调整大小,则仅在迭代结束时进行一次
func (*Deque) IterPopFront ¶
IterPopFront 返回一个迭代器,该迭代器从双端队列的前面迭代移除项目 这比一次移除一个项目更有效,因为它避免了中间的调整大小 如果需要调整大小,则仅在迭代结束时进行一次
func (*Deque) PopBack ¶
func (q *Deque) PopBack() interface{}
PopBack 从队列的末尾移除并返回元素 当与 PushBack 一起使用时实现 LIFO如果队列为空,则调用会 panic
func (*Deque) PopFront ¶
func (q *Deque) PopFront() interface{}
PopFront 从队列的前面移除并返回元素 当与 PushBack 一起使用时实现 FIFO如果队列为空,则调用会 panic
func (*Deque) PushBack ¶
func (q *Deque) PushBack(elem interface{})
PushBack 将元素追加到队列的末尾当使用 PopFront 删除元素时实现 FIFO, 当使用 PopBack 删除元素时实现 LIFO
func (*Deque) RIter ¶
RIter 返回一个反向迭代器,用于遍历 Deque 中的所有项目, 从后(索引 Len()-1)到前(索引 0)依次返回每个项目 在迭代过程中修改 Deque 会导致 panic
type FIFOQueue ¶
type FIFOQueue struct {
// contains filtered or unexported fields
}
FIFOQueue 实现了先进先出(FIFO)的队列
func NewFIFOQueue ¶
NewFIFOQueue 创建并返回一个新的 FIFO 队列
func (*FIFOQueue) SetGrowthFactor ¶
SetGrowthFactor 设置扩容因子
func (*FIFOQueue) SetShrinkFactor ¶
SetShrinkFactor 设置缩容因子
type LIFOQueue ¶
type LIFOQueue struct {
// contains filtered or unexported fields
}
LIFOQueue 实现了 LIFO 队列(栈)
type PriorityQueue ¶
type PriorityQueue struct {
// contains filtered or unexported fields
}
PriorityQueue 实现了优先队列,实际上是一个堆
func (*PriorityQueue) Dequeue ¶
func (pq *PriorityQueue) Dequeue(ctx context.Context) (interface{}, error)
Dequeue 从优先队列中取出最优先的元素,支持上下文取消
func (*PriorityQueue) Enqueue ¶
func (pq *PriorityQueue) Enqueue(ctx context.Context, item interface{}, priority int) error
Enqueue 将一个元素添加到优先队列中,支持上下文取消
func (*PriorityQueue) Less ¶
func (pq *PriorityQueue) Less(i, j int) bool
Less 判断队列中第 i 和第 j 个元素的优先级