Queue

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// QueueHead is a string constant that represents the start of the queue. It is used to
	// indicate where elements are removed from the queue.
	// The value of QueueHead is "← | ", which visually indicates the direction of element
	// removal.
	QueueHead string = "← | "

	// QueueSep is a string constant that is used as a separator between elements in the string
	// representation of the queue.
	// The value of QueueSep is " | ", which provides a clear visual separation between individual
	// elements in the queue.
	QueueSep string = " | "
)

QueueHead and QueueSep are constants used in the String() method of the Queuer interface to format the string representation of a queue.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayQueue added in v0.2.1

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

ArrayQueue is a generic type in Go that represents a queue data structure implemented using an array. It has a single field, values, which is a slice of type T. This slice stores the elements in the queue.

func NewArrayQueue added in v0.2.1

func NewArrayQueue[T any](values ...T) *ArrayQueue[T]

NewArrayQueue is a function that creates and returns a new instance of an ArrayQueue. It takes a variadic parameter of type T, which represents the initial values to be stored in the queue. The function creates a new ArrayQueue, initializes its values field with a slice of the same length as the input values, and then copies the input values into the new slice. The new ArrayQueue is then returned.

func (*ArrayQueue[T]) Clear added in v0.2.1

func (queue *ArrayQueue[T]) Clear()

func (*ArrayQueue[T]) Dequeue added in v0.2.1

func (queue *ArrayQueue[T]) Dequeue() (T, error)

func (*ArrayQueue[T]) Enqueue added in v0.2.1

func (queue *ArrayQueue[T]) Enqueue(value T)

func (*ArrayQueue[T]) IsEmpty added in v0.2.1

func (queue *ArrayQueue[T]) IsEmpty() bool

func (*ArrayQueue[T]) IsFull added in v0.2.1

func (queue *ArrayQueue[T]) IsFull() bool

IsFull is a method of the ArrayQueue type. It checks if the queue is full.

In this implementation, the method always returns false. This is because an ArrayQueue, implemented with a slice, can dynamically grow and shrink in size as elements are added or removed. Therefore, it is never considered full, and elements can always be added to it.

func (*ArrayQueue[T]) MustDequeue added in v0.2.1

func (queue *ArrayQueue[T]) MustDequeue() T

func (*ArrayQueue[T]) MustPeek added in v0.2.1

func (queue *ArrayQueue[T]) MustPeek() T

func (*ArrayQueue[T]) Peek added in v0.2.1

func (queue *ArrayQueue[T]) Peek() (T, error)

func (*ArrayQueue[T]) Size added in v0.2.1

func (queue *ArrayQueue[T]) Size() int

func (*ArrayQueue[T]) String added in v0.2.1

func (queue *ArrayQueue[T]) String() string

func (*ArrayQueue[T]) ToSlice added in v0.2.1

func (queue *ArrayQueue[T]) ToSlice() []T

type ErrEmptyQueue

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

ErrEmptyQueue is a struct that represents an error when attempting to perform a queue operation on an empty queue. It has a single field, operation, of type QueueOperationType, which indicates the type of operation that caused the error.

func (ErrEmptyQueue) Error

func (e ErrEmptyQueue) Error() string

Error is a method of the ErrEmptyQueue type that implements the error interface. It returns a string representation of the error. The method constructs the error message by concatenating the string "could not ", the string representation of the operation that caused the error, and the string ": queue is empty". This provides a clear and descriptive error message when attempting to perform a queue operation on an empty queue.

type ErrFullQueue

type ErrFullQueue struct{}

ErrFullQueue is a struct that represents an error when attempting to enqueue an element into a full queue. It does not have any fields as the error condition is solely based on the state of the queue being full.

func (ErrFullQueue) Error

func (e ErrFullQueue) Error() string

Error is a method of the ErrFullQueue type that implements the error interface. It returns a string representation of the error. The method returns the string "could not enqueue: queue is full", providing a clear and descriptive error message when attempting to enqueue an element into a full queue.

type ErrNegativeCapacity added in v0.2.1

type ErrNegativeCapacity struct{}

ErrNegativeCapacity is a struct that represents an error when a negative capacity is provided for a queue. It does not have any fields as the error condition is solely based on the provided capacity being negative.

func (ErrNegativeCapacity) Error added in v0.2.1

func (e ErrNegativeCapacity) Error() string

Error is a method of the ErrNegativeCapacity type that implements the error interface. It returns a string representation of the error. The method returns the string "capacity of a queue cannot be negative", providing a clear and descriptive error message when a negative capacity is provided for a queue.

type ErrOutOfBoundsIterator added in v0.2.3

type ErrOutOfBoundsIterator struct{}

ErrOutOfBoundsIterator is a struct that represents an error when an iterator goes out of bounds. It does not have any fields as the error condition is solely based on the iterator exceeding the bounds of the data structure it is iterating over.

func (ErrOutOfBoundsIterator) Error added in v0.2.3

func (e ErrOutOfBoundsIterator) Error() string

Error is a method of the ErrOutOfBoundsIterator type that implements the error interface. It returns a string representation of the error. The method returns the string "iterator out of bounds", providing a clear and descriptive error message when an iterator goes out of bounds.

type ErrTooManyValues added in v0.2.1

type ErrTooManyValues struct{}

ErrTooManyValues is a struct that represents an error when too many values are provided for initializing a queue. It does not have any fields as the error condition is solely based on the number of provided values exceeding the capacity of the queue.

func (ErrTooManyValues) Error added in v0.2.1

func (e ErrTooManyValues) Error() string

Error is a method of the ErrTooManyValues type that implements the error interface. It returns a string representation of the error. The method returns the string "could not initialize queue: too many values", providing a clear and descriptive error message when too many values are provided for initializing a queue.

type LimitedArrayQueue added in v0.2.1

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

LimitedArrayQueue is a generic type in Go that represents a queue data structure with a limited capacity. It has a single field, values, which is a slice of type T. This slice stores the elements in the queue.

func NewLimitedArrayQueue added in v0.2.1

func NewLimitedArrayQueue[T any](capacity int, values ...T) (*LimitedArrayQueue[T], error)

NewLimitedArrayQueue is a function that creates and returns a new instance of a LimitedArrayQueue. It takes an integer capacity, which represents the maximum number of elements the queue can hold, and a variadic parameter of type T, which represents the initial values to be stored in the queue.

The function first checks if the provided capacity is negative. If it is, it returns an error of type ErrNegativeCapacity. It then checks if the number of initial values exceeds the provided capacity. If it does, it returns an error of type ErrTooManyValues.

If the provided capacity and initial values are valid, the function creates a new LimitedArrayQueue, initializes its values field with a slice of the same length as the input values and the provided capacity, and then copies the input values into the new slice. The new LimitedArrayQueue is then returned.

func (*LimitedArrayQueue[T]) Clear added in v0.2.1

func (queue *LimitedArrayQueue[T]) Clear()

func (*LimitedArrayQueue[T]) Dequeue added in v0.2.1

func (queue *LimitedArrayQueue[T]) Dequeue() (T, error)

func (*LimitedArrayQueue[T]) Enqueue added in v0.2.1

func (queue *LimitedArrayQueue[T]) Enqueue(value T)

Enqueue is a method of the LimitedArrayQueue type. It is used to add an element to the end of the queue.

The method takes a parameter, value, of a generic type T, which is the element to be added to the queue.

Before adding the element, the method checks if the current length of the values slice is equal to the capacity of the queue. If it is, it means the queue is full, and the method panics by throwing an ErrFullQueue error.

If the queue is not full, the method appends the value to the end of the values slice, effectively adding the element to the end of the queue.

func (*LimitedArrayQueue[T]) IsEmpty added in v0.2.1

func (queue *LimitedArrayQueue[T]) IsEmpty() bool

func (*LimitedArrayQueue[T]) IsFull added in v0.2.1

func (queue *LimitedArrayQueue[T]) IsFull() bool

func (*LimitedArrayQueue[T]) MustDequeue added in v0.2.1

func (queue *LimitedArrayQueue[T]) MustDequeue() T

func (*LimitedArrayQueue[T]) MustPeek added in v0.2.1

func (queue *LimitedArrayQueue[T]) MustPeek() T

func (*LimitedArrayQueue[T]) Peek added in v0.2.1

func (queue *LimitedArrayQueue[T]) Peek() (T, error)

func (*LimitedArrayQueue[T]) Size added in v0.2.1

func (queue *LimitedArrayQueue[T]) Size() int

func (*LimitedArrayQueue[T]) String added in v0.2.1

func (queue *LimitedArrayQueue[T]) String() string

func (*LimitedArrayQueue[T]) ToSlice added in v0.2.1

func (queue *LimitedArrayQueue[T]) ToSlice() []T

type LimitedLinkedQueue added in v0.2.1

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

LimitedLinkedQueue is a generic type in Go that represents a queue data structure with a limited capacity, implemented using a linked list.

func NewLimitedLinkedQueue added in v0.2.1

func NewLimitedLinkedQueue[T any](capacity int, values ...T) (*LimitedLinkedQueue[T], error)

NewLimitedLinkedQueue is a function that creates and returns a new instance of a LimitedLinkedQueue. It takes an integer capacity, which represents the maximum number of elements the queue can hold, and a variadic parameter of type T, which represents the initial values to be stored in the queue.

The function first checks if the provided capacity is negative. If it is, it returns an error of type ErrNegativeCapacity. It then checks if the number of initial values exceeds the provided capacity. If it does, it returns an error of type ErrTooManyValues.

If the provided capacity and initial values are valid, the function creates a new LimitedLinkedQueue and initializes its size and capacity. It then creates a linked list of nodes from the initial values, with each node holding one value, and sets the front and back pointers of the queue. The new LimitedLinkedQueue is then returned.

func (*LimitedLinkedQueue[T]) Clear added in v0.2.1

func (queue *LimitedLinkedQueue[T]) Clear()

func (*LimitedLinkedQueue[T]) Dequeue added in v0.2.1

func (queue *LimitedLinkedQueue[T]) Dequeue() (T, error)

func (*LimitedLinkedQueue[T]) Enqueue added in v0.2.1

func (queue *LimitedLinkedQueue[T]) Enqueue(value T)

Enqueue is a method of the LimitedLinkedQueue type. It is used to add an element to the end of the queue.

The method takes a parameter, value, of a generic type T, which is the element to be added to the queue.

Before adding the element, the method checks if the current size of the queue is equal to or greater than its capacity. If it is, it means the queue is full, and the method panics by throwing an ErrFullQueue error.

If the queue is not full, the method creates a new linkedNode with the provided value. If the queue is currently empty (i.e., queue.back is nil), the new node is set as both the front and back of the queue. If the queue is not empty, the new node is added to the end of the queue by setting it as the next node of the current back node, and then updating the back pointer of the queue to the new node.

Finally, the size of the queue is incremented by 1 to reflect the addition of the new element.

func (*LimitedLinkedQueue[T]) IsEmpty added in v0.2.1

func (queue *LimitedLinkedQueue[T]) IsEmpty() bool

func (*LimitedLinkedQueue[T]) IsFull added in v0.2.1

func (queue *LimitedLinkedQueue[T]) IsFull() bool

func (*LimitedLinkedQueue[T]) MustDequeue added in v0.2.1

func (queue *LimitedLinkedQueue[T]) MustDequeue() T

func (*LimitedLinkedQueue[T]) MustPeek added in v0.2.1

func (queue *LimitedLinkedQueue[T]) MustPeek() T

func (*LimitedLinkedQueue[T]) Peek added in v0.2.1

func (queue *LimitedLinkedQueue[T]) Peek() (T, error)

func (*LimitedLinkedQueue[T]) Size added in v0.2.1

func (queue *LimitedLinkedQueue[T]) Size() int

func (*LimitedLinkedQueue[T]) String added in v0.2.1

func (queue *LimitedLinkedQueue[T]) String() string

func (*LimitedLinkedQueue[T]) ToSlice added in v0.2.1

func (queue *LimitedLinkedQueue[T]) ToSlice() []T

type LinkedQueue added in v0.2.1

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

LinkedQueue is a generic type in Go that represents a queue data structure implemented using a linked list.

func NewLinkedQueue added in v0.2.1

func NewLinkedQueue[T any](values ...T) *LinkedQueue[T]

NewLinkedQueue is a function that creates and returns a new instance of a LinkedQueue. It takes a variadic parameter of type T, which represents the initial values to be stored in the queue.

If no initial values are provided, the function simply returns a new LinkedQueue with all its fields set to their zero values.

If initial values are provided, the function creates a new LinkedQueue and initializes its size. It then creates a linked list of nodes from the initial values, with each node holding one value, and sets the front and back pointers of the queue. The new LinkedQueue is then returned.

func (*LinkedQueue[T]) Clear added in v0.2.1

func (queue *LinkedQueue[T]) Clear()

func (*LinkedQueue[T]) Dequeue added in v0.2.1

func (queue *LinkedQueue[T]) Dequeue() (T, error)

func (*LinkedQueue[T]) Enqueue added in v0.2.1

func (queue *LinkedQueue[T]) Enqueue(value T)

func (*LinkedQueue[T]) IsEmpty added in v0.2.1

func (queue *LinkedQueue[T]) IsEmpty() bool

func (*LinkedQueue[T]) IsFull added in v0.2.1

func (queue *LinkedQueue[T]) IsFull() bool

func (*LinkedQueue[T]) MustDequeue added in v0.2.1

func (queue *LinkedQueue[T]) MustDequeue() T

func (*LinkedQueue[T]) MustPeek added in v0.2.1

func (queue *LinkedQueue[T]) MustPeek() T

func (*LinkedQueue[T]) Peek added in v0.2.1

func (queue *LinkedQueue[T]) Peek() (T, error)

func (*LinkedQueue[T]) Size added in v0.2.1

func (queue *LinkedQueue[T]) Size() int

func (*LinkedQueue[T]) String added in v0.2.1

func (queue *LinkedQueue[T]) String() string

func (*LinkedQueue[T]) ToSlice added in v0.2.1

func (queue *LinkedQueue[T]) ToSlice() []T

type QueueIterator added in v0.2.1

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

QueueIterator is a generic type in Go that represents an iterator for a queue.

The values field is a slice of type T, which represents the elements stored in the queue.

The currentIndex field is an integer that keeps track of the current index position of the iterator in the queue. It is used to iterate over the elements in the queue.

func NewQueueIterator added in v0.2.1

func NewQueueIterator[T any](queue Queuer[T]) *QueueIterator[T]

NewQueueIterator is a function that creates and returns a new QueueIterator object for a given queue. It takes a queue of type Queuer[T] as an argument, where T can be any type.

The function uses the ToSlice method of the queue to get a slice of its values, and initializes the currentIndex to -1, indicating that the iterator is at the start of the queue.

The returned QueueIterator can be used to iterate over the elements in the queue.

func (*QueueIterator[T]) GetNext added in v0.2.3

func (iterator *QueueIterator[T]) GetNext() T

GetNext is a method of the QueueIterator type. It is used to move the iterator to the next element in the queue and return the value of that element. If the iterator is at the end of the queue, the method panics by throwing an ErrOutOfBoundsIterator error.

This method is typically used in a loop to iterate over all the elements in a queue.

func (*QueueIterator[T]) HasNext added in v0.2.3

func (iterator *QueueIterator[T]) HasNext() bool

HasNext is a method of the QueueIterator type. It returns true if there are more elements that the iterator is pointing to, and false otherwise.

This method is typically used in conjunction with the GetNext method to iterate over and access all the elements in a queue.

type QueueOperationType added in v0.2.1

type QueueOperationType int

QueueOperationType is an integer type that represents the type of operation performed on a queue. It is used in error handling to specify the operation that caused an error.

const (
	// Dequeue represents a dequeue operation, which removes an element from the queue.
	Dequeue QueueOperationType = iota

	// Peek represents a peek operation, which retrieves the element at the front of the
	// queue without removing it.
	Peek
)

func (QueueOperationType) String added in v0.2.1

func (qot QueueOperationType) String() string

String is a method of the QueueOperationType type. It returns a string representation of the queue operation type.

The method uses an array of strings where the index corresponds to the integer value of the QueueOperationType. The string at the corresponding index is returned as the string representation of the QueueOperationType.

This method is typically used for error messages and logging.

type Queuer added in v0.2.1

type Queuer[T any] interface {
	// The Enqueue method adds a value of type T to the end of the queue.
	Enqueue(value T)

	// The Dequeue method removes and returns the element at the front of the queue.
	// If the queue is empty, it returns an error.
	Dequeue() (T, error)

	// The MustDequeue method is a convenience method that dequeues an element from the queue
	// and returns it.
	// If the queue is empty, it will panic.
	MustDequeue() T

	// The Peek method returns the element at the front of the queue without removing it.
	Peek() (T, error)

	// MustPeek is a method that returns the value at the front of the queue without removing
	// it.
	// If the queue is empty, it will panic.
	MustPeek() T

	// The IsEmpty method checks if the queue is empty and returns a boolean value indicating
	// whether it is empty or not.
	IsEmpty() bool

	// The Size method returns the number of elements currently in the queue.
	Size() int

	// The ToSlice method returns a slice containing all the elements in the queue.
	ToSlice() []T

	// The Clear method is used to remove all elements from the queue, making it empty.
	Clear()

	// The IsFull method checks if the queue is full, meaning it has reached its maximum
	// capacity and cannot accept any more elements.
	IsFull() bool

	// The String method returns a string representation of the Queuer.
	String() string
}

Package queue provides a Queuer interface that defines methods for a queue data structure.

Queuer is an interface that defines methods for a queue data structure. It includes methods to add and remove elements, check if the queue is empty or full, get the size of the queue, convert the queue to a slice, clear the queue, and get a string representation of the queue.

type SafeQueue added in v0.2.3

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

SafeQueue is a generic type in Go that represents a thread-safe queue data structure implemented using a linked list.

func NewSafeQueue added in v0.2.3

func NewSafeQueue[T any](values ...T) *SafeQueue[T]

NewSafeQueue is a function that creates and returns a new instance of a SafeQueue. It takes a variadic parameter of type T, which represents the initial values to be stored in the queue.

If no initial values are provided, the function simply returns a new SafeQueue with all its fields set to their zero values.

If initial values are provided, the function creates a new SafeQueue and initializes its size. It then creates a linked list of safeNodes from the initial values, with each node holding one value, and sets the front and back pointers of the queue. The new SafeQueue is then returned.

func (*SafeQueue[T]) Clear added in v0.2.3

func (queue *SafeQueue[T]) Clear()

func (*SafeQueue[T]) Dequeue added in v0.2.3

func (queue *SafeQueue[T]) Dequeue() (T, error)

func (*SafeQueue[T]) Enqueue added in v0.2.3

func (queue *SafeQueue[T]) Enqueue(value T)

func (*SafeQueue[T]) IsEmpty added in v0.2.3

func (queue *SafeQueue[T]) IsEmpty() bool

func (*SafeQueue[T]) IsFull added in v0.2.3

func (queue *SafeQueue[T]) IsFull() bool

func (*SafeQueue[T]) MustDequeue added in v0.2.3

func (queue *SafeQueue[T]) MustDequeue() T

func (*SafeQueue[T]) MustPeek added in v0.2.3

func (queue *SafeQueue[T]) MustPeek() T

func (*SafeQueue[T]) Peek added in v0.2.3

func (queue *SafeQueue[T]) Peek() (T, error)

func (*SafeQueue[T]) Size added in v0.2.3

func (queue *SafeQueue[T]) Size() int

func (*SafeQueue[T]) String added in v0.2.3

func (queue *SafeQueue[T]) String() string

func (*SafeQueue[T]) ToSlice added in v0.2.3

func (queue *SafeQueue[T]) ToSlice() []T

Jump to

Keyboard shortcuts

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