Documentation ¶
Overview ¶
Package queue implements a priority queue (min or max) in slice representation that utilizes insertion sort for keeping the order of elements. As items are added one-by-one, the queue is kept sorted at all times, and items are easily accessible using basic indexing
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Item ¶
type Item[T any] interface { // Less returns a flag indicating if the element // has a lower value than another element. // For max-priority queue implementations, Less should return true if A > B Less(b T) bool }
Item represents a single queue item
type Queue ¶
type Queue[T Item[T]] []T
Queue is the priority queue based on insertion sort
func (*Queue[T]) Fix ¶
func (q *Queue[T]) Fix()
Fix makes sure the priority queue is properly sorted
Example ¶
q := NewQueue[number]() q.Push(number{1}) // 1 q.Push(number{2}) // 2 q.Push(number{3}) // 3 q[0] = number{4} // 1 -> 4 q.Fix() fmt.Println(q[0].value)
Output: 2
func (*Queue[T]) Index ¶
Index returns the element at the specified index, if any. NOTE: panics if out of bounds
func (*Queue[T]) PopBack ¶
func (q *Queue[T]) PopBack() *T
PopBack removes the last element in the queue, if any
Example ¶
q := NewQueue[number]() q.Push(number{1}) // 1 q.Push(number{2}) // 2 q.Push(number{3}) // 3 popped := q.PopBack() fmt.Println(popped.value)
Output: 3
Click to show internal directories.
Click to hide internal directories.