Documentation
¶
Index ¶
- func Fix[T Value[T]](h []T, i int)
- type Heap
- type MinMax
- func (h *MinMax[K, V]) Len() int
- func (h *MinMax[K, V]) PopMax() (K, V)
- func (h *MinMax[K, V]) PopMin() (K, V)
- func (h *MinMax[K, V]) Push(k K, v V)
- func (h *MinMax[K, V]) PushMaxN(k K, v V, n int)
- func (h *MinMax[K, V]) PushMinN(k K, v V, n int)
- func (h *MinMax[K, V]) Remove(i int) (k K, v V)
- func (h *MinMax[K, V]) Update(i int, k K, v V)
- type Option
- type Ordered
- type T
- type Value
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Heap ¶
type Heap[T Value[T]] []T
Heap is a generic heap implementation that can be used with any type that implements the Value interface. It provides methods to push, pop, remove, and fix elements in the heap. The heap is implemented as a slice in the same manner as the standard library's heap package, but it is generic and can work with any type that satisfies the Value interface.
Example ¶
package main import ( "fmt" "cloudeng.io/algo/container/heap" ) type Example struct { idx int val string } func (e Example) Less(b Example) bool { return e.idx < b.idx } func main() { h := heap.Heap[Example]{ {idx: 3, val: "three"}, {idx: 1, val: "one"}, {idx: 2, val: "two"}, {idx: 0, val: "zero"}, } h.Init() for h.Len() > 0 { v := h.Pop() fmt.Printf("%v %v ", v.idx, v.val) } fmt.Println() }
Output: 0 zero 1 one 2 two 3 three
type MinMax ¶
type MinMax[K Ordered, V any] struct { Keys []K Vals []V // contains filtered or unexported fields }
MinMax represents a min-max heap as described in: https://liacs.leidenuniv.nl/~stefanovtp/courses/StudentenSeminarium/Papers/AL/SMMH.pdf. Note that this requires the use of a dummy root node in the key and value slices, ie. Keys[0] and Values[0] is always empty.
func NewMinMax ¶
NewMinMax creates a new instance of MinMax.
Example ¶
package main import ( "fmt" "strconv" "cloudeng.io/algo/container/heap" ) func main() { h := heap.NewMinMax[int, string]() for _, i := range []int{12, 32, 25, 36, 13, 23, 26, 42, 49, 7, 15, 63, 92, 5} { h.Push(i, strconv.Itoa(i)) } for h.Len() > 0 { k, _ := h.PopMin() fmt.Printf("%v ", k) k, _ = h.PopMax() fmt.Printf("%v ", k) } fmt.Println() }
Output: 5 92 7 63 12 49 13 42 15 36 23 32 25 26
func (*MinMax[K, V]) Len ¶
Len returns the number of items stored in the heap, excluding the dummy root node.
func (*MinMax[K, V]) PopMax ¶
func (h *MinMax[K, V]) PopMax() (K, V)
PopMax removes and returns the largest key/value pair from the heap.
func (*MinMax[K, V]) PopMin ¶
func (h *MinMax[K, V]) PopMin() (K, V)
PopMin removes and returns the smallest key/value pair from the heap.
func (*MinMax[K, V]) Push ¶
func (h *MinMax[K, V]) Push(k K, v V)
Push pushes the key/value pair onto the heap.
func (*MinMax[K, V]) PushMaxN ¶
PushMaxN pushes the key/value pair onto the heap if the key is greater than than the current maximum whilst ensuring that the heap is no larger than n.
func (*MinMax[K, V]) PushMinN ¶
PushMinN pushes the key/value pair onto the heap if the key is less than the current minimum whilst ensuring that the heap is no larger than n.
type Option ¶
Option represents the options that can be passed to NewMin and NewMax.
func WithCallback ¶
WithCallback provides a callback function that is called after every operation with the values and indices of the elements that have changed location. Note that is not sufficient to track removal of items and hence any applications that requires such tracking should do so explicitly by wrapping the Pop operations and deleting the retried item from their data structures.
type Ordered ¶
type Ordered interface { ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | string }
Orderded represents the set of types that can be used as keys in a heap.
type T ¶
T represents a heap of keys and values.
func NewMax ¶
NewMax creates a new heap with descending order.
Example ¶
package main import ( "fmt" "strconv" "cloudeng.io/algo/container/heap" ) func main() { h := heap.NewMax[int, string]() for _, i := range []int{100, 19, 36, 17, 3, 25, 1, 2, 7} { h.Push(i, strconv.Itoa(i)) } for h.Len() > 0 { k, _ := h.Pop() fmt.Printf("%v ", k) } fmt.Println() }
Output: 100 36 25 19 17 7 3 2 1
func NewMin ¶
NewMin creates a new heap with ascending order.
Example ¶
package main import ( "fmt" "strconv" "cloudeng.io/algo/container/heap" ) func main() { h := heap.NewMin[int, string]() for _, i := range []int{100, 19, 36, 17, 3, 25, 1, 2, 7} { h.Push(i, strconv.Itoa(i)) } for h.Len() > 0 { k, _ := h.Pop() fmt.Printf("%v ", k) } fmt.Println() }
Output: 1 2 3 7 17 19 25 36 100
func (*T[K, V]) Pop ¶
func (h *T[K, V]) Pop() (K, V)
Pop removes and returns the top element from the heap.
type Value ¶
Value represents the interface that must be implemented by types that can be used as values in the generic Heap implementation. It requires a Less method that compares the current instance with another instance of the same type and returns true if the current instance is less than the other instance.