Documentation ¶
Overview ¶
Package heap provides a generic min or max heap that can be used as a priority queue. The backing data structure is a slice denoting an implicit binary heap. The default value of Heap is a valid empty heap.
The heap grows in the usual way by appending elements to the backing slice and then making adjustments to preserve the heap property. As the heap shrinks, smaller backing slices are periodically allocated and elements copied over. Empty heaps are guaranteed to be backed by nil slices.
If your type can be compared using the < operator then you can use the Push and Pop functions to manipulate the heap, e.g.:
var myMaxIntHeap heap.Heap[int, heap.Max] heap.Push(&myMaxIntHeap, 17) heap.Pop(&myMaxIntHeap)
If your type can't be compared using < then you can implement the Cmp method of the Orderable interface for your type (with a pointer receiver) and use the PushOrderable and PopOrderable functions:
var heap heap.Heap[myCustomType, heap.Min] heap.PushOrderable(&heap, myCustomType{Key: 1, Foo: "foo"}) heap.PopOrderable(&heap) type myCustomType struct { Key int Foo string } func (a *myCustomType) Cmp(b *myCustomType) int { return x.Key - y.Key }
Index ¶
- func Clear[T any, MOM MinOrMax](heap *Heap[T, MOM])
- func Filter[T c.Ordered, MOM MinOrMax](heap *Heap[T, MOM], ...)
- func FilterOrderable[T any, MOM MinOrMax, PT Orderable[T]](heap *Heap[T, MOM], ...)
- func FromSlice[T c.Ordered, MOM MinOrMax](heap *Heap[T, MOM], slice []T)
- func FromSliceOrderable[T any, MOM MinOrMax, PT Orderable[T]](heap *Heap[T, MOM], slice []T)
- func Len[T any, MOM MinOrMax](heap *Heap[T, MOM]) int
- func Peek[T any, MOM MinOrMax](heap *Heap[T, MOM]) (val T, ok bool)
- func Pop[T c.Ordered, MOM MinOrMax](heap *Heap[T, MOM]) (T, bool)
- func PopOrderable[T any, MOM MinOrMax, PT Orderable[T]](heap *Heap[T, MOM]) (T, bool)
- func Push[T c.Ordered, MOM MinOrMax](heap *Heap[T, MOM], elem T)
- func PushOrderable[T any, MOM MinOrMax, PT Orderable[T]](heap *Heap[T, MOM], elem T)
- type BreakOrContinue
- type Heap
- type Max
- type Min
- type MinOrMax
- type Orderable
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Filter ¶
func Filter[T c.Ordered, MOM MinOrMax](heap *Heap[T, MOM], f func(*T) (keepElement bool, breakOrContinue BreakOrContinue))
Filter iterates through the elements of the heap in the order given by the underlying slice. If the first return value of f is false then the relevant element is removed from the heap. If the second return value of f is Break then the iteration stops without visiting any subsequent items.
func FilterOrderable ¶
func FilterOrderable[T any, MOM MinOrMax, PT Orderable[T]](heap *Heap[T, MOM], f func(*T) (keepElement bool, breakOrContinue BreakOrContinue))
As for Filter, but for the case where T cannot be compared using < and there is an implementation of Orderable[T].
func FromSlice ¶ added in v1.3.0
FromSlice initializes the a heap from the elements of a slice using Floyd's heap-building algorithm. The previous contents of the heap (if any) are discarded. The slice is 'moved' into the Heap and should not be accessed or modified following a call to this function.
func FromSliceOrderable ¶ added in v1.3.0
As for FromSlice, but for the case where T cannot be compared using < and there is an implementation of Orderable[T].
func Pop ¶
Pop removes the min/max element from the heap for a T that satisfies constraints.Ordered.
func PopOrderable ¶
Pop removes the min/max element from the heap for a T that implements Orderable.
Types ¶
type BreakOrContinue ¶
type BreakOrContinue int
A BreakOrContinue value can be returned by an iteration callback to indicate whether or not iteration should continue.
const ( Break BreakOrContinue = iota Continue BreakOrContinue = iota )
type Heap ¶
Heap is a min or max heap backed by a slice denoting an implicit binary heap. Heap is marked as noCopy because the built-in copying operation creates a shallow copy of the underlying slice, which is likely to give rise to confusing and undesired behavior.
type MinOrMax ¶
type MinOrMax interface {
// contains filtered or unexported methods
}
The MinOrMax interface has two implementations (Min and Max) that can be passed as type parameters to Heap to choose between a min and max heap.
type Orderable ¶
If your type doesn't satisfy constraints.Ordered, define a Cmp method with a pointer receiver for your type. This method should return 0 if the two values compare equal, an int < 0 if the first value is less than the second, and an int > 0 otherwise.
Example:
type Date struct { Year int Month int Day int } func (m1 *Month) Cmp(m2 *Month) int { if m1.Year != m2.Year { return m1.Year - m2.Year } if m1.Month != m2.Month { return m1.Month - m2.Month } return m1.Day - m2.Day }