Documentation
¶
Overview ¶
Package priorityqueue implements a priority queue backed by binary queue.
An unbounded priority queue based on a priority queue. The elements of the priority queue are ordered by a comparator provided at queue construction time.
The heap of this queue is the least/smallest element with respect to the specified ordering. If multiple elements are tied for least value, the heap is one of those elements arbitrarily.
Structure is not thread safe.
References: https://en.wikipedia.org/wiki/Priority_queue
Index ¶
- type Iterator
- func (iterator *Iterator) Begin()
- func (iterator *Iterator) End()
- func (iterator *Iterator) First() bool
- func (iterator *Iterator) Index() int
- func (iterator *Iterator) Last() bool
- func (iterator *Iterator) Next() bool
- func (iterator *Iterator) NextTo(f func(index int, value interface{}) bool) bool
- func (iterator *Iterator) Prev() bool
- func (iterator *Iterator) PrevTo(f func(index int, value interface{}) bool) bool
- func (iterator *Iterator) Value() interface{}
- type Queue
- func NewFromIteratorWith[T any](comparator utils.Comparator[T], it ds.ReadCompForIterator[T]) *Queue[T]
- func NewFromIteratorsWith[T any](comparator utils.Comparator[T], first ds.ReadCompForIterator[T], ...) *Queue[T]
- func NewFromSliceWith[T any](comparator utils.Comparator[T], slice []T) *Queue[T]
- func NewWith[T any](comparator utils.Comparator[T]) *Queue[T]
- func (stack *Queue[T]) Begin() ds.ReadWriteOrdCompBidRandCollIterator[int, T]
- func (queue *Queue[T]) Clear()
- func (queue *Queue[T]) Dequeue() (value T, ok bool)
- func (stack *Queue[T]) End() ds.ReadWriteOrdCompBidRandCollIterator[int, T]
- func (queue *Queue[T]) Enqueue(value T)
- func (stack *Queue[T]) First() ds.ReadWriteOrdCompBidRandCollIterator[int, T]
- func (queue *Queue[T]) FromJSON(data []byte) error
- func (queue *Queue[T]) GetValues() []T
- func (queue *Queue[T]) IsEmpty() bool
- func (queue *Queue) Iterator() Iterator
- func (stack *Queue[T]) Last() ds.ReadWriteOrdCompBidRandCollIterator[int, T]
- func (queue *Queue[T]) MarshalJSON() ([]byte, error)
- func (queue *Queue[T]) Peek() (value T, ok bool)
- func (queue *Queue[T]) Size() int
- func (queue *Queue[T]) ToJSON() ([]byte, error)
- func (queue *Queue[T]) ToString() string
- func (queue *Queue[T]) UnmarshalJSON(bytes []byte) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Iterator ¶
type Iterator struct {
// contains filtered or unexported fields
}
Iterator returns a stateful iterator whose values can be fetched by an index.
func (*Iterator) Begin ¶
func (iterator *Iterator) Begin()
Begin resets the iterator to its initial state (one-before-first) Call Next() to fetch the first element if any.
func (*Iterator) End ¶
func (iterator *Iterator) End()
End moves the iterator past the last element (one-past-the-end). Call Prev() to fetch the last element if any.
func (*Iterator) First ¶
First moves the iterator to the first element and returns true if there was a first element in the container. If First() returns true, then first element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.
func (*Iterator) Index ¶
Index returns the current element's index. Does not modify the state of the iterator.
func (*Iterator) Last ¶
Last moves the iterator to the last element and returns true if there was a last element in the container. If Last() returns true, then last element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.
func (*Iterator) Next ¶
Next moves the iterator to the next element and returns true if there was a next element in the container. If Next() returns true, then next element's index and value can be retrieved by Index() and Value(). If Next() was called for the first time, then it will point the iterator to the first element if it exists. Modifies the state of the iterator.
func (*Iterator) NextTo ¶
NextTo moves the iterator to the next element from current position that satisfies the condition given by the passed function, and returns true if there was a next element in the container. If NextTo() returns true, then next element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.
func (*Iterator) Prev ¶
Prev moves the iterator to the previous element and returns true if there was a previous element in the container. If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.
func (*Iterator) PrevTo ¶
PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the passed function, and returns true if there was a next element in the container. If PrevTo() returns true, then next element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.
type Queue ¶
type Queue[T any] struct { Comparator utils.Comparator[T] // contains filtered or unexported fields }
Queue holds elements in an array-list
func NewFromIteratorWith ¶ added in v0.16.1
func NewFromIteratorWith[T any](comparator utils.Comparator[T], it ds.ReadCompForIterator[T]) *Queue[T]
NewFromIterator instantiates a new queue containing the elements provided by the passed iterator.
func NewFromIteratorsWith ¶ added in v0.16.1
func NewFromIteratorsWith[T any](comparator utils.Comparator[T], first ds.ReadCompForIterator[T], end ds.ComparableIterator) *Queue[T]
NewFromIterators instantiates a new queue containing the elements provided by first, until it is equal to end. end is a sentinel and not included.
func NewFromSliceWith ¶ added in v0.16.1
func NewFromSliceWith[T any](comparator utils.Comparator[T], slice []T) *Queue[T]
NewFromSlice instantiates a new queue containing the provided slice.
func NewWith ¶
func NewWith[T any](comparator utils.Comparator[T]) *Queue[T]
NewWith instantiates a new empty queue with the custom comparator.
func (*Queue[T]) Begin ¶ added in v0.16.1
func (stack *Queue[T]) Begin() 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 (*Queue[T]) Dequeue ¶
Dequeue removes first element of the queue and returns it, or nil if queue is empty. Second return parameter is true, unless the queue was empty and there was nothing to dequeue.
func (*Queue[T]) End ¶ added in v0.16.1
func (stack *Queue[T]) End() 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 (*Queue[T]) Enqueue ¶
func (queue *Queue[T]) Enqueue(value T)
Enqueue adds a value to the end of the queue
func (*Queue[T]) First ¶ added in v0.16.1
func (stack *Queue[T]) First() ds.ReadWriteOrdCompBidRandCollIterator[int, T]
First returns an initialized iterator, which points to it's first element.
func (*Queue[T]) GetValues ¶ added in v0.3.0
func (queue *Queue[T]) GetValues() []T
Values returns all elements in the queue.
func (*Queue[T]) IsEmpty ¶ added in v0.3.0
Empty returns true if queue does not contain any elements.
func (*Queue) Iterator ¶
Iterator returns a stateful iterator whose values can be fetched by an index.
func (*Queue[T]) Last ¶ added in v0.16.1
func (stack *Queue[T]) Last() ds.ReadWriteOrdCompBidRandCollIterator[int, T]
Last returns an initialized iterator, which points to it's last element.
func (*Queue[T]) MarshalJSON ¶
MarshalJSON @implements json.Marshaler
func (*Queue[T]) Peek ¶
Peek returns top element on the queue without removing it, or nil if queue is empty. Second return parameter is true, unless the queue was empty and there was nothing to peek.
func (*Queue[T]) UnmarshalJSON ¶
UnmarshalJSON @implements json.Unmarshaler