Documentation
¶
Overview ¶
Package binaryheap implements a binary heap backed by array list.
Comparator defines this heap as either min or max heap.
Structure is not thread safe.
References: http://en.wikipedia.org/wiki/Binary_heap
Index ¶
- type Heap
- func New[T any](comparator utils.Comparator[T], values ...T) *Heap[T]
- func NewFromIterator[T any](comparator utils.Comparator[T], begin ds.ReadForIterator[T]) *Heap[T]
- func NewFromIterators[T any](comparator utils.Comparator[T], begin ds.ReadCompForIterator[T], ...) *Heap[T]
- func NewFromSlice[T any](comparator utils.Comparator[T], slice []T) *Heap[T]
- func (heap *Heap[T]) Clear()
- func (heap *Heap[T]) FromJSON(data []byte) error
- func (heap *Heap[T]) GetValues() []T
- func (heap *Heap[T]) IsEmpty() bool
- func (heap *Heap[T]) MarshalJSON() ([]byte, error)
- func (list *Heap[T]) NewOrderedIterator(index int, size int) *OrderedIterator[T]
- func (heap *Heap[T]) OrderedBegin() ds.ReadWriteOrdCompBidRandCollIterator[int, T]
- func (heap *Heap[T]) OrderedEnd() ds.ReadWriteOrdCompBidRandCollIterator[int, T]
- func (heap *Heap[T]) OrderedFirst() ds.ReadWriteOrdCompBidRandCollIterator[int, T]
- func (heap *Heap[T]) OrderedLast() ds.ReadWriteOrdCompBidRandCollIterator[int, T]
- func (heap *Heap[T]) Peek() (value T, ok bool)
- func (heap *Heap[T]) Pop() (value T, ok bool)
- func (heap *Heap[T]) Push(values ...T)
- func (heap *Heap[T]) Size() int
- func (heap *Heap[T]) ToJSON() ([]byte, error)
- func (heap *Heap[T]) ToString() string
- func (heap *Heap[T]) UnmarshalJSON(bytes []byte) error
- type OrderedIterator
- func (it *OrderedIterator[T]) DistanceTo(other ds.OrderedIterator) int
- func (it *OrderedIterator[T]) Get() (value T, found bool)
- func (it *OrderedIterator[T]) GetAt(i int) (value T, found bool)
- func (it *OrderedIterator[T]) GetAtKey(i int) (value T, found bool)
- func (it *OrderedIterator[T]) GetKey() (int, bool)
- func (it *OrderedIterator[T]) Index() (int, bool)
- func (it *OrderedIterator[T]) IsAfter(other ds.OrderedIterator) bool
- func (it *OrderedIterator[T]) IsBefore(other ds.OrderedIterator) bool
- func (it *OrderedIterator[T]) IsBegin() bool
- func (it *OrderedIterator[T]) IsEnd() bool
- func (it *OrderedIterator[T]) IsEqual(other ds.ComparableIterator) bool
- func (it *OrderedIterator[T]) IsFirst() bool
- func (it *OrderedIterator[T]) IsLast() bool
- func (it *OrderedIterator[T]) IsValid() bool
- func (it *OrderedIterator[T]) MoveBy(n int) bool
- func (it *OrderedIterator[T]) MoveTo(i int) bool
- func (it *OrderedIterator[T]) MoveToKey(i int) bool
- func (it *OrderedIterator[T]) Next() bool
- func (it *OrderedIterator[T]) NextN(n int) bool
- func (it *OrderedIterator[T]) Previous() bool
- func (it *OrderedIterator[T]) PreviousN(n int) bool
- func (it *OrderedIterator[T]) Set(value T) bool
- func (it *OrderedIterator[T]) SetAt(i int, value T) bool
- func (it *OrderedIterator[T]) SetAtKey(i int, value T) bool
- func (it *OrderedIterator[T]) Size() int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Heap ¶
type Heap[T any] struct { Comparator utils.Comparator[T] // contains filtered or unexported fields }
Heap holds elements in an array-list
func New ¶ added in v0.16.1
func New[T any](comparator utils.Comparator[T], values ...T) *Heap[T]
NewWith instantiates a new empty heap tree with the custom comparator.
func NewFromIterator ¶ added in v0.16.1
func NewFromIterator[T any](comparator utils.Comparator[T], begin ds.ReadForIterator[T]) *Heap[T]
NewFromIterator instantiates a new stack containing the elements provided by the passed iterator.
func NewFromIterators ¶ added in v0.16.1
func NewFromIterators[T any](comparator utils.Comparator[T], begin ds.ReadCompForIterator[T], end ds.ComparableIterator) *Heap[T]
NewFromIterators instantiates a new stack containing the elements provided by first, until it is equal to end. end is a sentinel and not included.
func NewFromSlice ¶ added in v0.16.1
func NewFromSlice[T any](comparator utils.Comparator[T], slice []T) *Heap[T]
NewFromSlice instantiates a new stack containing the provided slice.
func (*Heap[T]) GetValues ¶ added in v0.3.0
func (heap *Heap[T]) GetValues() []T
Values returns all elements in the heap.
func (*Heap[T]) MarshalJSON ¶
MarshalJSON @implements json.Marshaler
func (*Heap[T]) NewOrderedIterator ¶ added in v0.16.1
func (list *Heap[T]) NewOrderedIterator(index int, size int) *OrderedIterator[T]
NewOrderedIterator returns a stateful iterator whose values can be fetched by an index.
func (*Heap[T]) OrderedBegin ¶ added in v0.16.1
func (heap *Heap[T]) OrderedBegin() ds.ReadWriteOrdCompBidRandCollIterator[int, T]
Begin returns an initialized iterator, which points to one element before it's first. Unless Next() is called, the iterator is in an invalid state.
func (*Heap[T]) OrderedEnd ¶ added in v0.16.1
func (heap *Heap[T]) OrderedEnd() ds.ReadWriteOrdCompBidRandCollIterator[int, T]
End returns an initialized iterator, which points to one element afrer it's last. Unless Previous() is called, the iterator is in an invalid state.
func (*Heap[T]) OrderedFirst ¶ added in v0.16.1
func (heap *Heap[T]) OrderedFirst() ds.ReadWriteOrdCompBidRandCollIterator[int, T]
First returns an initialized iterator, which points to it's first element.
func (*Heap[T]) OrderedLast ¶ added in v0.16.1
func (heap *Heap[T]) OrderedLast() ds.ReadWriteOrdCompBidRandCollIterator[int, T]
Last returns an initialized iterator, which points to it's last element.
func (*Heap[T]) Peek ¶
Peek returns top element on the heap without removing it, or nil if heap is empty. Second return parameter is true, unless the heap was empty and there was nothing to peek.
func (*Heap[T]) Pop ¶
Pop removes top element on heap and returns it, or nil if heap is empty. Second return parameter is true, unless the heap was empty and there was nothing to pop.
func (*Heap[T]) Push ¶
func (heap *Heap[T]) Push(values ...T)
Push adds a value onto the heap and bubbles it up accordingly.
func (*Heap[T]) UnmarshalJSON ¶
UnmarshalJSON @implements json.Unmarshaler
type OrderedIterator ¶ added in v0.16.1
type OrderedIterator[T any] struct { // contains filtered or unexported fields }
OrderedIterator holding the iterator's state
func (*OrderedIterator[T]) DistanceTo ¶ added in v0.16.1
func (it *OrderedIterator[T]) DistanceTo(other ds.OrderedIterator) int
If other is of type IndexedOrderedIterator, IndexedOrderedIterator.Index() will be used, possibly executing in O(1)
func (*OrderedIterator[T]) Get ¶ added in v0.16.1
func (it *OrderedIterator[T]) Get() (value T, found bool)
func (*OrderedIterator[T]) GetAt ¶ added in v0.16.1
func (it *OrderedIterator[T]) GetAt(i int) (value T, found bool)
func (*OrderedIterator[T]) GetAtKey ¶ added in v0.21.0
func (it *OrderedIterator[T]) GetAtKey(i int) (value T, found bool)
func (*OrderedIterator[T]) GetKey ¶ added in v0.21.0
func (it *OrderedIterator[T]) GetKey() (int, bool)
func (*OrderedIterator[T]) Index ¶ added in v0.16.1
func (it *OrderedIterator[T]) Index() (int, bool)
func (*OrderedIterator[T]) IsAfter ¶ added in v0.16.1
func (it *OrderedIterator[T]) IsAfter(other ds.OrderedIterator) bool
func (*OrderedIterator[T]) IsBefore ¶ added in v0.16.1
func (it *OrderedIterator[T]) IsBefore(other ds.OrderedIterator) bool
func (*OrderedIterator[T]) IsBegin ¶ added in v0.16.1
func (it *OrderedIterator[T]) IsBegin() bool
func (*OrderedIterator[T]) IsEnd ¶ added in v0.16.1
func (it *OrderedIterator[T]) IsEnd() bool
func (*OrderedIterator[T]) IsEqual ¶ added in v0.16.1
func (it *OrderedIterator[T]) IsEqual(other ds.ComparableIterator) bool
func (*OrderedIterator[T]) IsFirst ¶ added in v0.16.1
func (it *OrderedIterator[T]) IsFirst() bool
func (*OrderedIterator[T]) IsLast ¶ added in v0.16.1
func (it *OrderedIterator[T]) IsLast() bool
func (*OrderedIterator[T]) IsValid ¶ added in v0.16.1
func (it *OrderedIterator[T]) IsValid() bool
func (*OrderedIterator[T]) MoveBy ¶ added in v0.16.1
func (it *OrderedIterator[T]) MoveBy(n int) bool
func (*OrderedIterator[T]) MoveTo ¶ added in v0.16.1
func (it *OrderedIterator[T]) MoveTo(i int) bool
func (*OrderedIterator[T]) MoveToKey ¶ added in v0.21.0
func (it *OrderedIterator[T]) MoveToKey(i int) bool
func (*OrderedIterator[T]) Next ¶ added in v0.16.1
func (it *OrderedIterator[T]) Next() bool
func (*OrderedIterator[T]) NextN ¶ added in v0.16.1
func (it *OrderedIterator[T]) NextN(n int) bool
func (*OrderedIterator[T]) Previous ¶ added in v0.16.1
func (it *OrderedIterator[T]) Previous() bool
func (*OrderedIterator[T]) PreviousN ¶ added in v0.16.1
func (it *OrderedIterator[T]) PreviousN(n int) bool
func (*OrderedIterator[T]) Set ¶ added in v0.16.1
func (it *OrderedIterator[T]) Set(value T) bool
func (*OrderedIterator[T]) SetAt ¶ added in v0.16.1
func (it *OrderedIterator[T]) SetAt(i int, value T) bool
func (*OrderedIterator[T]) SetAtKey ¶ added in v0.21.0
func (it *OrderedIterator[T]) SetAtKey(i int, value T) bool
func (*OrderedIterator[T]) Size ¶ added in v0.16.1
func (it *OrderedIterator[T]) Size() int