pqueue

package
v0.0.0-...-91aa13a Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2023 License: MIT Imports: 1 Imported by: 0

README

非线程安全的优先队列(golang标准库中的heap是小根堆)

package main

import (
	"fmt"
	"github.com/gansidui/priority_queue"
)

type Node struct {
	priority int
	value    int
}

func (this *Node) Less(other interface{}) bool {
	return this.priority < other.(*Node).priority
}

func main() {
	q := priority_queue.New()

	q.Push(&Node{priority: 8, value: 1})
	q.Push(&Node{priority: 7, value: 2})
	q.Push(&Node{priority: 9, value: 3})

	x := q.Top().(*Node)
	fmt.Println(x.priority, x.value)

	for q.Len() > 0 {
		x = q.Pop().(*Node)
		fmt.Println(x.priority, x.value)
	}

	// output:
	// 7 2

	// 7 2
	// 8 1
	// 9 3
}

##LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Interface

type Interface interface {
	Less(other interface{}) bool
}

type PriorityQueue

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

Define priority queue struct

func New

func New() *PriorityQueue

func (*PriorityQueue) Fix

func (q *PriorityQueue) Fix(x Interface, i int)

func (*PriorityQueue) Len

func (q *PriorityQueue) Len() int

func (*PriorityQueue) Pop

func (q *PriorityQueue) Pop() Interface

func (*PriorityQueue) Push

func (q *PriorityQueue) Push(x Interface)

func (*PriorityQueue) Remove

func (q *PriorityQueue) Remove(i int) Interface

func (*PriorityQueue) Top

func (q *PriorityQueue) Top() Interface

Jump to

Keyboard shortcuts

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