queue

package
v0.11.27 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

* @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

This section is empty.

Functions

This section is empty.

Types

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

func (q *FIFOQueue) IsEmpty() bool

IsEmpty 检查队列是否为空

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 (s *LIFOQueue) Dequeue(ctx context.Context) (interface{}, error)

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

func (*LIFOQueue) Enqueue

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

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

func (*LIFOQueue) IsEmpty

func (s *LIFOQueue) IsEmpty() bool

IsEmpty 判断队列是否为空

func (*LIFOQueue) Size

func (s *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