heap

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Example of using github.com/dengliu/gocommon/heap

import (

"fmt"

"github.com/dengliu/gocommon/heap"

)

func main() {
	fmt.Println("hello")

	hp := heap.NewHeap[myItem]()
	hp.Insert(myItem{1.2})
	hp.Insert(myItem{2.3})
	hp.Insert(myItem{3.3})
	hp.Insert(myItem{4.3})
	hp.Insert(myItem{4.3})
	hp.Insert(myItem{3.3})
	hp.Insert(myItem{4.3})

	for hp.Len() > 0 {
		fmt.Println(hp.Peek())
		i, _ := hp.Pop()
		fmt.Println(i)
	}
}
type myItem struct {
	rank float64
	data string
}
func (h myItem) Less(other heap.Item) bool {
	return h.data < other.(myItem).data
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Heap

type Heap[T Item] struct {
	// contains filtered or unexported fields
}

func NewHeap

func NewHeap[T Item]() Heap[T]

func (*Heap[T]) Insert

func (p *Heap[T]) Insert(v T)

Insert inserts a new element into the queue. Duplicate Item is allowed

func (*Heap[T]) Len

func (p *Heap[T]) Len() int

func (*Heap[T]) Peek

func (p *Heap[T]) Peek() (T, error)

Peek returns the element with the highest priority from the queue. In case of an empty queue, an error is ret

func (*Heap[T]) Pop

func (p *Heap[T]) Pop() (T, error)

Pop removes the element with the highest priority from the queue and returns it. In case of an empty queue, an error is returned.

type Item

type Item interface {
	// Less returns a bool that can be used to determine
	// ordering in the priority queue.
	Less(other Item) bool
}

Item is an item that can be added to the priority queue.

Jump to

Keyboard shortcuts

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