cyqueue

package
v0.0.0-...-ce1098a Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package cyqueue implements a circular queue.

Example
q := New[string](3)

seq := q.Push("foo")
q.Push("bar")
q.Push("baz")
elem, ok := q.Get(seq)
fmt.Println(seq, elem, ok)

q.Push("qux")
elem, ok = q.Get(seq)
fmt.Println(seq, elem, ok)

for seq, elem := range q.All() {
	fmt.Println(seq, elem)
}
Output:

1 foo true
1  false
4 qux
3 baz
2 bar

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 circular queue with elements of type T. Methods on Queue are safe for concurrent use.

func New

func New[T any](capacity int) *Queue[T]

New returns an empty Queue of the given capacity, which must be positive.

func (*Queue[T]) All

func (q *Queue[T]) All() iter.Seq2[int, T]

All iterates over the queue's elements with their sequential numbers, tail to head (newest elements first).

func (*Queue[T]) Cap

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

Cap returns the queue's capacity.

func (*Queue[T]) Copy

func (q *Queue[T]) Copy() (int, []T)

Copy returns a shallow copy of the queue, head to tail (oldest elements first), and the first element's sequential number (each subsequent element's being 1 greater than the preceding one).

func (*Queue[T]) Get

func (q *Queue[T]) Get(seq int) (T, bool)

Get returns the element with the given sequential number as returned by Queue.Push. If the element with this number has not yet been pushed or has already been discarded, Get returns zero, false.

func (*Queue[T]) Len

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

Len returns the number of elements in the queue.

func (*Queue[T]) Push

func (q *Queue[T]) Push(elem T) int

Push adds elem to the tail of the queue, discarding the head if the queue has already reached its capacity, and returns the 1-based sequential number of the added element, suitable for Queue.Get.

func (*Queue[T]) Set

func (q *Queue[T]) Set(seq int, elem T) bool

Set replaces the element with the given sequential number as returned by Queue.Push. If the element with this number has not yet been pushed or has already been discarded, Set returns false.

Jump to

Keyboard shortcuts

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