queue

package
v0.19.4 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var (
	// ErrQueueClosed 队列已关闭错误
	ErrQueueClosed = errors.New("队列已关闭")
	// ErrQueueFull 队列已满错误
	ErrQueueFull = errors.New("队列已满")
)
View Source
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]) Cap

func (q *BoundedQueue[T]) Cap() int

Cap 返回当前队列容量

func (*BoundedQueue[T]) Close

func (q *BoundedQueue[T]) Close()

Close 关闭队列

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]) IsClosed

func (q *BoundedQueue[T]) IsClosed() bool

IsClosed 检查队列是否已关闭

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]) Size

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

Size 返回队列元素数量(实现Queue接口)

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

func (q *Deque) At(i int) interface{}

At 返回队列中索引 i 处的元素,而不移除该元素 此方法仅接受非负索引值At(0) 指的是第一个元素, 与 Front() 相同At(Len()-1) 指的是最后一个元素, 与 Back() 相同如果索引无效,调用会 panic

func (*Deque) Back

func (q *Deque) Back() (interface{}, error)

Back 返回队列末尾的元素这是 PopBack 返回的元素 如果队列为空,则调用会返回错误信息

func (*Deque) Cap

func (q *Deque) Cap() int

Cap 返回 Deque 的当前容量如果 q 为 nil,q.Cap() 返回零

func (*Deque) Clear

func (q *Deque) Clear()

Clear 移除队列中的所有元素,但保留当前容量 这在高频率重复使用队列时非常有用,以避免垃圾回收 只要仅添加项目,队列就不会被调整为更小的尺寸 只有在移除项目时,队列才会被调整为更小的尺寸

func (*Deque) Front

func (q *Deque) Front() (interface{}, error)

Front 返回队列前面的元素这是 PopFront 返回的元素 如果队列为空,则调用会返回错误信息

func (*Deque) Grow

func (q *Deque) Grow(n int)

Grow 如果需要,增加双端队列的容量,以保证可以容纳另 n 个项目在 Grow(n) 之后,至少可以向队列中写入 n 个项目, 而无需再次分配如果 n 为负数,Grow 会 panic

func (*Deque) Index

func (q *Deque) Index(f func(interface{}) bool) int

Index 返回满足 f(item) 的第一个项在 Deque 中的索引, 如果没有满足条件的项,则返回 -1如果 q 为 nil,则总是返回 -1 搜索是线性的,从索引 0 开始

func (*Deque) Insert

func (q *Deque) Insert(i int, item interface{})

Insert 用于将元素插入队列中的指定索引位置如果索引无效, 调用会 panic

func (*Deque) Iter

func (q *Deque) Iter() func(func(interface{}) bool)

Iter 返回一个迭代器,用于遍历 Deque 中的所有项目, 从前(索引 0)到后(索引 Len()-1)依次返回每个项目 在迭代过程中修改 Deque 会导致 panic

func (*Deque) IterPopBack

func (q *Deque) IterPopBack() func(func(interface{}) bool)

IterPopBack 返回一个迭代器,该迭代器从双端队列的末尾迭代移除项目 这比一次移除一个项目更有效,因为它避免了中间的调整大小 如果需要调整大小,则仅在迭代结束时进行一次

func (*Deque) IterPopFront

func (q *Deque) IterPopFront() func(func(interface{}) bool)

IterPopFront 返回一个迭代器,该迭代器从双端队列的前面迭代移除项目 这比一次移除一个项目更有效,因为它避免了中间的调整大小 如果需要调整大小,则仅在迭代结束时进行一次

func (*Deque) Len

func (q *Deque) Len() int

Len 返回当前存储在队列中的元素数量如果 q 为 nil, q.Len() 返回零

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) PushFront

func (q *Deque) PushFront(elem interface{})

PushFront 在队列的前面插入元素

func (*Deque) RIndex

func (q *Deque) RIndex(f func(interface{}) bool) int

RIndex 与 Index 相同,但从后向前搜索返回的索引 从前向后,其中索引 0 是 Front() 返回的项目的索引

func (*Deque) RIter

func (q *Deque) RIter() func(func(interface{}) bool)

RIter 返回一个反向迭代器,用于遍历 Deque 中的所有项目, 从后(索引 Len()-1)到前(索引 0)依次返回每个项目 在迭代过程中修改 Deque 会导致 panic

func (*Deque) Rotate

func (q *Deque) Rotate(n int)

Rotate 将双端队列旋转 n 步,从前到后如果 n 为负,则从后到前旋转 让 Deque 提供 Rotate 可以避免使用仅 Pop 和 Push 方法实现旋转时可能发生的调整大小 如果 q.Len() 为 1 或更少,或 q 为 nil,则 Rotate 不执行任何操作

func (*Deque) Set

func (q *Deque) Set(i int, item interface{})

Set 将项目分配给队列中索引 i 的位置 Set 的索引与 At 相同,但执行相反的操作 如果索引无效,调用会 panic

type FIFOQueue

type FIFOQueue struct {
	// contains filtered or unexported fields
}

FIFOQueue 实现了先进先出(FIFO)的队列

func NewFIFOQueue

func NewFIFOQueue(capacity int, autoResize bool) *FIFOQueue

NewFIFOQueue 创建并返回一个新的 FIFO 队列

func (*FIFOQueue) Capacity

func (q *FIFOQueue) Capacity() int

Capacity 返回队列的容量

func (*FIFOQueue) Dequeue

func (q *FIFOQueue) Dequeue(ctx context.Context) (interface{}, error)

Dequeue 从队列头部移除并返回一个元素

func (*FIFOQueue) Enqueue

func (q *FIFOQueue) Enqueue(ctx context.Context, item interface{}) error

Enqueue 向队列尾部添加一个元素

func (*FIFOQueue) GrowthFactor

func (q *FIFOQueue) GrowthFactor() float64

GrowthFactor 返回当前的扩容因子

func (*FIFOQueue) IsEmpty

func (q *FIFOQueue) IsEmpty() bool

IsEmpty 检查队列是否为空

func (*FIFOQueue) MinCapacity

func (q *FIFOQueue) MinCapacity() int

MinCapacity 返回队列的最小容量限制

func (*FIFOQueue) SetGrowthFactor

func (q *FIFOQueue) SetGrowthFactor(value float64) *FIFOQueue

SetGrowthFactor 设置扩容因子

func (*FIFOQueue) SetShrinkFactor

func (q *FIFOQueue) SetShrinkFactor(value float64) *FIFOQueue

SetShrinkFactor 设置缩容因子

func (*FIFOQueue) ShrinkFactor

func (q *FIFOQueue) ShrinkFactor() float64

ShrinkFactor 返回当前的缩容因子

func (*FIFOQueue) Size

func (q *FIFOQueue) Size() int

Size 返回队列中的元素数量

type Item

type Item struct {
	// contains filtered or unexported fields
}

Item 定义了优先队列中的元素

type LIFOQueue

type LIFOQueue struct {
	// contains filtered or unexported fields
}

LIFOQueue 实现了 LIFO 队列(栈)

func NewLIFOQueue

func NewLIFOQueue() *LIFOQueue

NewLIFOQueue 创建一个新的 LIFO 队列(栈)

func (*LIFOQueue) Dequeue

func (l *LIFOQueue) Dequeue(ctx context.Context) (interface{}, error)

Dequeue 从队列中取出元素(栈的弹栈操作)

func (*LIFOQueue) Enqueue

func (l *LIFOQueue) Enqueue(ctx context.Context, item interface{}) error

Enqueue 将元素添加到队列中(栈的压栈操作)

func (*LIFOQueue) IsEmpty

func (l *LIFOQueue) IsEmpty() bool

IsEmpty 判断队列是否为空

func (*LIFOQueue) Size

func (l *LIFOQueue) Size() int

Size 返回队列的大小

type PriorityQueue

type PriorityQueue struct {
	// contains filtered or unexported fields
}

PriorityQueue 实现了优先队列,实际上是一个堆

func NewPriorityQueue

func NewPriorityQueue() *PriorityQueue

NewPriorityQueue 创建并返回一个新的优先队列

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) IsEmpty

func (pq *PriorityQueue) IsEmpty() bool

IsEmpty 判断优先队列是否为空

func (*PriorityQueue) Len

func (pq *PriorityQueue) Len() int

Len 返回队列的长度

func (*PriorityQueue) Less

func (pq *PriorityQueue) Less(i, j int) bool

Less 判断队列中第 i 和第 j 个元素的优先级

func (*PriorityQueue) Pop

func (pq *PriorityQueue) Pop() interface{}

Pop 从队列中移除并返回最优先的元素(堆顶元素)

func (*PriorityQueue) Push

func (pq *PriorityQueue) Push(x interface{})

Push 将一个元素添加到队列中(堆)

func (*PriorityQueue) Size

func (pq *PriorityQueue) Size() int

Size 返回优先队列的大小

func (*PriorityQueue) Swap

func (pq *PriorityQueue) Swap(i, j int)

Swap 交换队列中第 i 和第 j 个元素的位置

type Queue

type Queue interface {
	Enqueue(ctx context.Context, item interface{}) error
	Dequeue(ctx context.Context) (interface{}, error)
	IsEmpty() bool
	Size() int
}

Queue 接口定义了队列的基本操作

Jump to

Keyboard shortcuts

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