cqueue

package
v1.13.1 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PriorityItem added in v1.11.1

type PriorityItem[T any] struct {
	Value    T
	Priority int
}

PriorityItem is an item with a priority. It is used to store items in the priority queue. Each item has a value of type T and an integer priority.

type PriorityQueue added in v1.11.1

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

A PriorityQueue implements heap.Interface and holds Items. The zero value for PriorityQueue is an empty queue ready to use. PriorityQueue is a thread-safe priority queue that can hold elements of any type. It uses a mutex to ensure that only one goroutine can access the queue at a time.

func NewPriorityQueue added in v1.11.1

func NewPriorityQueue[T any](MaxCapacity int) *PriorityQueue[T]

func (*PriorityQueue[T]) Clear added in v1.11.1

func (pq *PriorityQueue[T]) Clear()

Clear removes all elements from the priority queue.

func (*PriorityQueue[T]) Close added in v1.11.2

func (pq *PriorityQueue[T]) Close()

Close marks the priority queue as closed and wakes up all waiting goroutines.

func (*PriorityQueue[T]) Len added in v1.11.1

func (pq *PriorityQueue[T]) Len() int

Len returns the number of elements in the priority queue.

func (*PriorityQueue[T]) Peek added in v1.11.1

func (pq *PriorityQueue[T]) Peek() (value T, ok bool)

Peek returns the highest priority element without removing it from the queue. If the queue is empty, it returns the zero value of T and false.

func (*PriorityQueue[T]) Pop added in v1.11.1

func (pq *PriorityQueue[T]) Pop() (value T, ok bool)

Pop removes and returns the highest priority element from the priority queue. If the queue is empty, it returns the zero value of T and false.

func (*PriorityQueue[T]) Push added in v1.11.1

func (pq *PriorityQueue[T]) Push(value PriorityItem[T]) bool

Push adds an element to the priority queue with the given priority. Returns false if the queue is at max capacity.

func (*PriorityQueue[T]) SearchFunc added in v1.11.1

func (pq *PriorityQueue[T]) SearchFunc(fn func(T) bool) (T, bool)

SearchFunc searches for an element in the priority queue that satisfies the given function. It returns the element and true if found, otherwise it returns the zero value of T and false.

func (*PriorityQueue[T]) Shrink added in v1.11.1

func (pq *PriorityQueue[T]) Shrink()

Shrink reduces the capacity of the priority queue's underlying slice to fit its length.

func (*PriorityQueue[T]) WaitPop added in v1.11.1

func (pq *PriorityQueue[T]) WaitPop() (T, bool)

WaitPop blocks until an element is available and returns it.

func (*PriorityQueue[T]) WithRecycler added in v1.11.1

func (pq *PriorityQueue[T]) WithRecycler(r recycler.Recycler) *PriorityQueue[T]

type Queue

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

Generic queue type Queue is a thread-safe queue that can hold elements of any type. It uses a mutex to ensure that only one goroutine can access the queue at a time.

func NewQueue

func NewQueue[T any](maxcap int) *Queue[T]

NewQueue creates a new instance of Queue for the specified type T.

func (*Queue[T]) Clear

func (q *Queue[T]) Clear()

Clear removes all elements from the queue. It locks the queue to ensure thread safety while clearing.

func (*Queue[T]) Close added in v1.10.4

func (q *Queue[T]) Close()

Close marks the queue as closed and wakes up all waiting goroutines.

func (*Queue[T]) DeleteFunc

func (q *Queue[T]) DeleteFunc(fn func(T) bool) bool

Delete removes the first occurrence of an element from the queue.

func (*Queue[T]) IsEmpty

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

IsEmpty checks if the queue is empty. It locks the queue to ensure thread safety while checking.

func (*Queue[T]) Len

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

Len returns the current length of the queue. It locks the queue to ensure thread safety while accessing the length.

func (*Queue[T]) Peek

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

Peek returns the first element of the queue without removing it. It locks the queue to ensure thread safety while accessing the element. If the queue is empty, it returns a zero value of type T and false.

func (*Queue[T]) Pop

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

Pop removes and returns the first element from the queue. It locks the queue to ensure thread safety while removing the element. If the queue is empty, it returns a zero value of type T and false.

func (*Queue[T]) PopBatch

func (q *Queue[T]) PopBatch(n int) ([]T, bool)

PopBatch removes and returns up to `n` elements from the queue. It locks the queue to ensure thread safety while removing the elements. If the queue has fewer than `n` elements, it returns all available elements.

func (*Queue[T]) Push

func (q *Queue[T]) Push(data ...T) error

Push adds one or more elements to the end of the queue. It locks the queue to ensure thread safety while adding elements. If the queue has a maximum capacity and is full, it returns an error. If the queue is not full, it appends the elements to the end of the queue. It returns nil if the operation is successful.

func (*Queue[T]) SearchFunc

func (q *Queue[T]) SearchFunc(fn func(T) bool) (T, bool)

Search searches for an element in the queue using a custom function.

func (*Queue[T]) Shrink added in v1.9.6

func (q *Queue[T]) Shrink()

Shrink reduces the capacity of the queue to fit its current length. It locks the queue to ensure thread safety while shrinking.

func (*Queue[T]) WaitPop added in v1.10.1

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

WaitPop removes and returns the first element from the queue.

func (*Queue[T]) WithRecycler added in v1.10.1

func (q *Queue[T]) WithRecycler(r recycler.Recycler) *Queue[T]

WithRecycler sets a recycler for the queue.

type Xchan added in v1.8.1

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

Xchan is a concurrent channel that allows writing to an input channel and reading from an output channel. It uses a ring buffer to manage the flow of data between the input and output channels. It supports burst writes and ensures that the output channel is not blocked by full buffers. The buffer size is configurable, and it can handle concurrent writes and reads efficiently.

func NewXchan added in v1.8.1

func NewXchan[T any](ctx context.Context, conf XchanConf) *Xchan[T]

func (*Xchan[T]) BufferLen added in v1.8.1

func (x *Xchan[T]) BufferLen() int

BufferLen returns the number of elements in the buffer. It uses atomic operations to ensure thread safety when accessing the size.

func (*Xchan[T]) In added in v1.8.1

func (x *Xchan[T]) In() chan<- T

In returns the input channel for writing elements.

func (*Xchan[T]) Len added in v1.8.1

func (x *Xchan[T]) Len() int

Len returns the total number of elements in the input channel, buffer, and output channel. It calculates the length of the input channel, the size of the buffer, and the length

func (*Xchan[T]) Out added in v1.8.1

func (x *Xchan[T]) Out() <-chan T

Out returns the output channel for reading elements.

type XchanConf added in v1.8.1

type XchanConf struct {
	Bufsize int // Size of the buffer
	Insize  int // Size of the input channel
	Outsize int // Size of the output channel
}

Jump to

Keyboard shortcuts

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