queue

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2024 License: MIT Imports: 3 Imported by: 0

README

queue-go

GoDoc Build Status

queue provides a FIFO queue backed by a circular buffer.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Queue

type Queue[T any] struct {
	// contains filtered or unexported fields
}

Queue is a FIFO queue backed by a circular buffer. The zero value for Queue is an empty queue ready to use. Queue is NOT safe for concurrent use.

Example
var q queue.Queue[int]
q.Push(3)
q.Push(1)
q.Push(4)

for !q.IsEmpty() {
	x, ok := q.Pop()
	if !ok {
		panic("queue should not be empty here")
	}
	fmt.Println(x)
}
Output:

3
1
4

func (*Queue[T]) All

func (q *Queue[T]) All() iter.Seq[T]

All returns an iterator over all elements in the queue. Do not modify the queue while iterating.

Example
var q queue.Queue[int]
q.Push(3)
q.Push(1)
q.Push(4)

for x := range q.All() {
	fmt.Println(x)
}
Output:

3
1
4

func (*Queue[T]) At added in v0.4.0

func (q *Queue[T]) At(i int) T

At returns the element at the specified index. If the index is out of range, it panics.

func (*Queue[T]) Backward added in v0.3.0

func (q *Queue[T]) Backward() iter.Seq[T]

Backward returns an iterator over all elements in the queue in reverse order (newest first). Do not modify the queue while iterating.

func (*Queue[T]) IsEmpty

func (q *Queue[T]) IsEmpty() bool

IsEmpty returns true if the queue is empty.

func (*Queue[T]) Len

func (q *Queue[T]) Len() int

Len returns the number of elements in the queue.

func (*Queue[T]) Peek

func (q *Queue[T]) Peek() (T, bool)

Peek returns the element at the front of the queue without removing it. If the queue is empty, Peek returns the zero value of T and false.

func (*Queue[T]) Pop

func (q *Queue[T]) Pop() (T, bool)

Pop removes and returns the element at the front of the queue. If the queue is empty, Pop returns the zero value of T and false.

func (*Queue[T]) Push

func (q *Queue[T]) Push(x T)

Push adds an element to the back of the queue.

func (*Queue[T]) PushMany added in v0.8.0

func (q *Queue[T]) PushMany(xs []T)

PushMany adds multiple elements to the back of the queue. PushMany is more efficient than calling Push multiple times.

Jump to

Keyboard shortcuts

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