heapx

package
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

heapx package provides enhanced list heap apis. note not safety in concurrent operation.

Example (IntHeap)

This example inserts several ints into an IntHeap, checks the minimum, and removes them in order of priority.

h := heapx.NewHeap(IntHeap{2, 1, 5}, func(p1, p2 int) int {
	return p1 - p2
})

h.Push(3)

for h.Len() > 0 {
	fmt.Printf("%d ", h.Pop())
}
Output:

1 2 3 5

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Heap

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

Heap base on generics to build a heap tree for any type

func NewHeap

func NewHeap[E any](t []E, cmp base.CMP[E]) *Heap[E]

NewHeap return Heap pointer and init the heap tree

Example
h := heapx.NewHeap([]Player{}, func(p1, p2 Player) int {
	return p1.level - p2.level // level小的先出
})

// 初始化 100个数据
for i := 100; i > 0; i-- {
	h.Push(Player{i, "name" + strconv.Itoa(i)})
}

player := h.Pop()
fmt.Println(player.level)
player = h.Pop()
fmt.Println(player.level)
Output:

1
2

func (*Heap[E]) Copy

func (h *Heap[E]) Copy() *Heap[E]

Copy to copy heap

func (*Heap[E]) Element

func (h *Heap[E]) Element(index int) (e E, err error)

func (*Heap[E]) Len

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

func (*Heap[E]) Pop

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

Pop removes and returns the minimum element (according to Less) from the heap. The complexity is O(log n) where n = h.Len(). Pop is equivalent to Remove(h, 0).

func (*Heap[E]) Push

func (h *Heap[E]) Push(v E)

Push pushes the element x onto the heap. The complexity is O(log n) where n = h.Len().

func (*Heap[E]) Remove

func (h *Heap[E]) Remove(index int) E

Remove removes and returns the element at index i from the heap. The complexity is O(log n) where n = h.Len().

Jump to

Keyboard shortcuts

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