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 ¶
- type Heap
- func (h *Heap[T]) Clear()
- func (h *Heap[T]) Empty() bool
- func (h *Heap[T]) Len() int
- func (h *Heap[T]) LenX() int
- func (h *Heap[T]) LessX(i, j int) bool
- func (h *Heap[T]) Peek() (value T, ok bool)
- func (h *Heap[T]) Pop() (value T, ok bool)
- func (h *Heap[T]) PopX() T
- func (h *Heap[T]) Push(value T)
- func (h *Heap[T]) PushX(x T)
- func (h *Heap[T]) SwapX(i, j int)
- func (h *Heap[T]) Values() []T
- type Option
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 (*Heap[T]) Pop ¶
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]).
Click to show internal directories.
Click to hide internal directories.