binaryheap

package
v0.22.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2022 License: BSD-2-Clause, ISC Imports: 6 Imported by: 0

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

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]) Clear

func (heap *Heap[T]) Clear()

Clear removes all elements from the heap.

func (*Heap[T]) FromJSON

func (heap *Heap[T]) FromJSON(data []byte) error

FromJSON populates the heap from the input JSON representation.

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]) IsEmpty added in v0.3.0

func (heap *Heap[T]) IsEmpty() bool

Empty returns true if heap does not contain any elements.

func (*Heap[T]) MarshalJSON

func (heap *Heap[T]) MarshalJSON() ([]byte, error)

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

func (heap *Heap[T]) Peek() (value T, ok bool)

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

func (heap *Heap[T]) Pop() (value T, ok bool)

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]) Size

func (heap *Heap[T]) Size() int

Size returns number of elements within the heap.

func (*Heap[T]) ToJSON

func (heap *Heap[T]) ToJSON() ([]byte, error)

ToJSON outputs the JSON representation of the heap.

func (*Heap[T]) ToString added in v0.3.0

func (heap *Heap[T]) ToString() string

String returns a string representation of container

func (*Heap[T]) UnmarshalJSON

func (heap *Heap[T]) UnmarshalJSON(bytes []byte) error

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL