queue

package
v0.9.10 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2023 License: MIT, MIT Imports: 1 Imported by: 0

README

queue

import "github.com/fufuok/utils/generic/queue"

Package queue provides an implementation of a First In First Out (FIFO) queue. The FIFO queue is implemented using the doubly-linked list from the 'list' package.

Example

{
	q := New[int]()
	q.Enqueue(1)
	q.Enqueue(2)

	q.Each(func(i int) {
		fmt.Println(i)
	})

}
Output
1
2

Index

type Queue

Queue is a simple First In First Out (FIFO) queue.

type Queue[T any] struct {
    // contains filtered or unexported fields
}
func New
func New[T any]() *Queue[T]

New returns an empty First In First Out (FIFO) queue.

func Of
func Of[S ~[]E, E any](slice S) *Queue[E]

Of returns a First In First Out (FIFO) queue that has been populated with values from an existing slice.

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

Clear empties the queue, resetting it to zero elements.

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

Copy returns a shallow copy of this queue.

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

Dequeue removes and returns the item at the front of the queue.

A panic occurs if the queue is Empty.

Example

{
	q := New[int]()
	q.Enqueue(1)

	fmt.Println(q.Dequeue())

}
Output
1

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

DequeueAll removes and returns all the items in the queue.

func (*Queue[T]) Each
func (q *Queue[T]) Each(fn func(t T))

Each calls 'fn' on every item in the queue, starting with the least recently pushed element.

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

Empty returns true if the queue is empty.

Example (Empty)

{
	q := New[int]()

	fmt.Println(q.Empty())

}
Output
true

Example (Nonempty)

{
	q := New[int]()
	q.Enqueue(1)

	fmt.Println(q.Empty())

}
Output
false

func (*Queue[T]) Enqueue
func (q *Queue[T]) Enqueue(value T)

Enqueue inserts 'value' to the end of the queue.

Example

{
	q := New[int]()
	q.Enqueue(1)
}

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

Len returns the number of items currently in the queue.

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

Peek returns the item at the front of the queue without removing it.

A panic occurs if the queue is Empty.

Example

{
	q := New[int]()
	q.Enqueue(1)

	fmt.Println(q.Peek())

}
Output
1

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

PeekAll returns all the items in the queue without removing them.

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

TryDequeue tries to remove and return the item at the front of the queue.

If the queue is empty, then false is returned as the second return value.

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

TryPeek tries to return the item at the front of the queue without removing it.

If the queue is empty, then false is returned as the second return value.

Generated by gomarkdoc

Documentation

Overview

Package queue provides an implementation of a First In First Out (FIFO) queue. The FIFO queue is implemented using the doubly-linked list from the 'list' package.

Example
q := New[int]()
q.Enqueue(1)
q.Enqueue(2)

q.Each(func(i int) {
	fmt.Println(i)
})
Output:

1
2

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 simple First In First Out (FIFO) queue.

func New

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

New returns an empty First In First Out (FIFO) queue.

func Of added in v0.9.9

func Of[S ~[]E, E any](slice S) *Queue[E]

Of returns a First In First Out (FIFO) queue that has been populated with values from an existing slice.

func (*Queue[T]) Clear added in v0.9.9

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

Clear empties the queue, resetting it to zero elements.

func (*Queue[T]) Copy added in v0.9.9

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

Copy returns a shallow copy of this queue.

func (*Queue[T]) Dequeue

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

Dequeue removes and returns the item at the front of the queue.

A panic occurs if the queue is Empty.

Example
q := New[int]()
q.Enqueue(1)

fmt.Println(q.Dequeue())
Output:

1

func (*Queue[T]) DequeueAll added in v0.9.9

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

DequeueAll removes and returns all the items in the queue.

func (*Queue[T]) Each

func (q *Queue[T]) Each(fn func(t T))

Each calls 'fn' on every item in the queue, starting with the least recently pushed element.

func (*Queue[T]) Empty

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

Empty returns true if the queue is empty.

Example (Empty)
q := New[int]()

fmt.Println(q.Empty())
Output:

true
Example (Nonempty)
q := New[int]()
q.Enqueue(1)

fmt.Println(q.Empty())
Output:

false

func (*Queue[T]) Enqueue

func (q *Queue[T]) Enqueue(value T)

Enqueue inserts 'value' to the end of the queue.

Example
q := New[int]()
q.Enqueue(1)

func (*Queue[T]) Len added in v0.9.9

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

Len returns the number of items currently in the queue.

func (*Queue[T]) Peek

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

Peek returns the item at the front of the queue without removing it.

A panic occurs if the queue is Empty.

Example
q := New[int]()
q.Enqueue(1)

fmt.Println(q.Peek())
Output:

1

func (*Queue[T]) PeekAll added in v0.9.9

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

PeekAll returns all the items in the queue without removing them.

func (*Queue[T]) TryDequeue added in v0.9.9

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

TryDequeue tries to remove and return the item at the front of the queue.

If the queue is empty, then false is returned as the second return value.

func (*Queue[T]) TryPeek added in v0.9.9

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

TryPeek tries to return the item at the front of the queue without removing it.

If the queue is empty, then false is returned as the second return value.

Jump to

Keyboard shortcuts

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