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 ¶
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]) 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).
Click to show internal directories.
Click to hide internal directories.