queue

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2025 License: BSD-3-Clause Imports: 1 Imported by: 0

README


This utility package provides generic functions for queue operations.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fifo

type Fifo struct {
	// contains filtered or unexported fields
}

Fifo represents a dynamic, memory-efficient queue with a minimum fixed size.

func NewFifo

func NewFifo(minSize int) *Fifo

NewFifo creates a new fifo data queue with a minimum fixed size.

func (*Fifo) IsEmpty

func (q *Fifo) IsEmpty() bool

IsEmpty returns true if the queue is empty.

Example

ExampleFifo_IsEmpty demonstrates the usage of the IsEmpty method.

package main

import (
	"fmt"

	"github.com/exonlabs/go-utils/pkg/queue"
)

func main() {
	q := queue.NewFifo(2)
	fmt.Println(q.IsEmpty())
	q.Push(1)
	fmt.Println(q.IsEmpty())

}
Output:

true
false

func (*Fifo) Length

func (q *Fifo) Length() int

Length returns the current number of items in the queue.

Example

ExampleFifo_Length demonstrates the usage of the Length method.

package main

import (
	"fmt"

	"github.com/exonlabs/go-utils/pkg/queue"
)

func main() {
	q := queue.NewFifo(2)
	fmt.Println(q.Length())
	q.Push(1, 2)
	fmt.Println(q.Length())

}
Output:

0
2

func (*Fifo) Pop

func (q *Fifo) Pop() any

Pop removes and returns data from the start of the queue. Returns nil if the queue is empty. Shrinks the queue to minimum size if data length is less than minimum size.

Example

ExampleFifo_Pop demonstrates the usage of the Pop method.

package main

import (
	"fmt"

	"github.com/exonlabs/go-utils/pkg/queue"
)

func main() {
	q := queue.NewFifo(2)
	q.Push(1, 2, 3)
	fmt.Println(q.Pop())
	fmt.Println(q.Pop())

}
Output:

1
2

func (*Fifo) PopN

func (q *Fifo) PopN(n int) []any

PopN removes and returns data with length n from the start of the queue. May return data less than n when n > count. Shrinks the queue to minimum size if remaining data size is less than the minimum size.

Example

ExampleFifo_PopN demonstrates the usage of the PopN method.

package main

import (
	"fmt"

	"github.com/exonlabs/go-utils/pkg/queue"
)

func main() {
	q := queue.NewFifo(2)
	q.Push(1, 2, 3, 4)
	fmt.Println(q.PopN(3))

}
Output:

[1 2 3]

func (*Fifo) Push

func (q *Fifo) Push(data ...any) error

Push adds data to the end of the queue, nil data items are ignored. Automatically resizes to a larger size if the queue is full.

Example

ExampleFifo_Push demonstrates the usage of the Push method.

package main

import (
	"fmt"

	"github.com/exonlabs/go-utils/pkg/queue"
)

func main() {
	q := queue.NewFifo(2)
	q.Push(1, 2, 3)
	fmt.Println(q.Length())

}
Output:

3

func (*Fifo) Size

func (q *Fifo) Size() int

Size returns the current size of the queue.

Jump to

Keyboard shortcuts

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