heap

package
v0.0.0-...-8cfa9df Latest Latest
Warning

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

Go to latest
Published: May 30, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Example (Custom)
h := NewWith(func(a, b Item) int {
	return b.Priority - a.Priority
})
items := map[string]int{
	"banana": 3, "apple": 2, "pear": 4,
}
for name, priority := range items {
	h.Push(Item{
		Name:     name,
		Priority: priority,
	})
}
item := Item{
	Name:     "orange",
	Priority: 1,
}
h.Push(item)
item.Priority = 5
h.Push(item)
for h.Len() > 0 {
	item, _ := h.Pop()
	fmt.Printf("%.2d:%s ", item.Priority, item.Name)
}
Output:

05:orange 04:pear 03:banana 02:apple 01:orange
Example (Ints)
h := New[int]()
h.Push(2)
h.Push(1)
h.Push(5)
h.Push(3)
peek, _ := h.Peek()
fmt.Printf("minimum: %d\n", peek)
for h.Len() > 0 {
	cur, _ := h.Pop()
	fmt.Printf("%d ", cur)
}
Output:

minimum: 1
1 2 3 5
Example (WithComparator)
h := New(WithComparator(dsgo.Reverse(cmp.Compare[int])))
h.Push(2)
h.Push(1)
h.Push(5)
h.Push(3)
peek, _ := h.Peek()
fmt.Printf("maximum: %d\n", peek)
for h.Len() > 0 {
	cur, _ := h.Pop()
	fmt.Printf("%d ", cur)
}
Output:

maximum: 5
5 3 2 1
Example (WithData)
nums := []int{6, 8, 5, 9, 3}
h := New(WithData(nums), WithCapacity[int](len(nums)+1))
h.Push(1)
for h.Len() > 0 {
	cur, _ := h.Pop()
	fmt.Print(cur)
	fmt.Print(",")
}
Output:

1,3,5,6,8,9,

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Heap

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

func New

func New[T cmp.Ordered](ops ...Option[T]) *Heap[T]

func NewWith

func NewWith[T any](cmp dsgo.Comparator[T], ops ...Option[T]) *Heap[T]

func (*Heap[T]) Clear

func (h *Heap[T]) Clear()

Clear clears and init the heap

func (*Heap[T]) Empty

func (h *Heap[T]) Empty() bool

Empty returns if the heap is empty. The complexity is O(1)

func (*Heap[T]) Len

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

Len returns the size of the heap. The complexity is O(1)

func (*Heap[T]) LenX

func (h *Heap[T]) LenX() int

implements internal/heap.Interface

func (*Heap[T]) LessX

func (h *Heap[T]) LessX(i, j int) bool

func (*Heap[T]) Peek

func (h *Heap[T]) Peek() (value T, ok bool)

Peek returns the peek value of the heap The complexity is O(1)

func (*Heap[T]) Pop

func (h *Heap[T]) Pop() (value T, ok bool)

Pop removes and returns the peek element from the heap. The complexity is O(log n) where n is the size of the heap. Pop is equivalent to Remove(h.data[0]).

func (*Heap[T]) PopX

func (h *Heap[T]) PopX() T

func (*Heap[T]) Push

func (h *Heap[T]) Push(value T)

Push pushes the element value onto the heap. The complexity is O(log n) where n is the size of the heap.

func (*Heap[T]) PushX

func (h *Heap[T]) PushX(x T)

func (*Heap[T]) SwapX

func (h *Heap[T]) SwapX(i, j int)

func (*Heap[T]) Values

func (h *Heap[T]) Values() []T

Values returns the sorted values in the heap

type Option

type Option[T any] func(h *Heap[T])

func WithCapacity

func WithCapacity[T any](capacity int) Option[T]

func WithComparator

func WithComparator[T any](cmp dsgo.Comparator[T]) Option[T]

func WithData

func WithData[T any, S ~[]T](data S) Option[T]

Jump to

Keyboard shortcuts

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