structs

package
v2.4.7 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2023 License: GPL-3.0 Imports: 1 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Heap

type Heap[E constraints.Ordered] []E

Heap is a priority queue which sorts strings lexicographically.

Example

How to create and use a Heap.

Just uses the usual standard heap package functions.

// Create a Heap.
stringHeap := make(Heap[string], 0)
heap.Init(&stringHeap)

// Push some strings. Make sure to use heap.Push rather than stringHeap.Push.
heap.Push(&stringHeap, "Crisps")
heap.Push(&stringHeap, "Egg")
heap.Push(&stringHeap, "Bananas")
heap.Push(&stringHeap, "Doughnut")
heap.Push(&stringHeap, "Apple")
heap.Push(&stringHeap, "Fried chicken")
heap.Push(&stringHeap, "Orange")
heap.Push(&stringHeap, "Grapefruit")

// We can get the length using stringHeap.Len.
fmt.Println("Length before:", stringHeap.Len())

// Pop them off.
for stringHeap.Len() > 0 {
	fmt.Println(heap.Pop(&stringHeap).(string))
}

fmt.Println("Length after:", stringHeap.Len())
Output:
Length before: 8
Apple
Bananas
Crisps
Doughnut
Egg
Fried chicken
Grapefruit
Orange
Length after: 0

func (Heap[E]) Len

func (h Heap[E]) Len() int

Len gives the length of the Heap.

func (Heap[E]) Less

func (h Heap[E]) Less(i, j int) bool

Less returns true if the element at the first index is less than the element at the second.

func (*Heap[E]) Pop

func (h *Heap[E]) Pop() any

Pop pops the tail of the queue.

func (*Heap[E]) Push

func (h *Heap[E]) Push(x any)

Push pushes the given element onto the heap.

func (Heap[E]) Swap

func (h Heap[E]) Swap(i, j int)

Swap swaps the two elements indicated via the given indices.

Jump to

Keyboard shortcuts

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