Documentation
¶
Overview ¶
Package heapx is an advanced heap, which can remove or update any item in O(nlogn) time. It uses hash maps to memory inner index and count for any item.
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 = 3 h.Update(item) h.Remove(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
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) h.Remove(3) h.Remove(100) for h.Len() > 0 { cur, _ := h.Pop() fmt.Print(cur) fmt.Print(",") }
Output: 1,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(values ...T)
- func (h *Heap[T]) PushX(x T)
- func (h *Heap[T]) Remove(value T) bool
- func (h *Heap[T]) SwapX(i, j int)
- func (h *Heap[T]) Update(value T) bool
- 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 comparable] struct { // contains filtered or unexported fields }
func NewWith ¶
func NewWith[T comparable](cmp dsgo.Comparator[T], ops ...Option[T]) *Heap[T]
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]).
func (*Heap[T]) Push ¶
func (h *Heap[T]) Push(values ...T)
Push pushes the elements values onto the heap. The complexity is O(mlog n) where m is the size of values and n is the size of the heap.
func (*Heap[T]) Remove ¶
Remove removes value (any element) from the heap. This method is only supported for advanced Heap, it will panics if called on a base heap. The complexity is O(log n) where n = h.Len().
func (*Heap[T]) Update ¶
Update re-establishes the heap ordering after the element has changed its value. This method is only supported for advanced Heap, it will panics if called on a base heap. Changing the value of the element value and then calling Update is equivalent to, but less expensive than, calling Remove(value) followed by a Push of the new value. The complexity is O(log n) where n = h.Len().
type Option ¶
type Option[T comparable] func(h *Heap[T])
func WithCapacity ¶
func WithCapacity[T comparable](capacity int) Option[T]
func WithComparator ¶
func WithComparator[T comparable](cmp dsgo.Comparator[T]) Option[T]
func WithData ¶
func WithData[T comparable, S ~[]T](data S) Option[T]