Stack

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

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

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

StackHead and StackSep are constants used in the String() method of the Stacker interface to format the string representation of a stack.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayStack added in v0.2.1

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

ArrayStack is a generic type in Go that represents a stack 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 stack.

func NewArrayStack added in v0.2.7

func NewArrayStack[T any](values ...T) *ArrayStack[T]

NewArrayStack is a function that creates and returns a new instance of an ArrayStack. It takes a variadic parameter of type T, which represents the initial values to be stored in the stack. The function creates a new ArrayStack, 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 ArrayStack is then returned.

func (*ArrayStack[T]) Clear added in v0.2.7

func (stack *ArrayStack[T]) Clear()

func (*ArrayStack[T]) IsEmpty added in v0.2.7

func (stack *ArrayStack[T]) IsEmpty() bool

func (*ArrayStack[T]) IsFull added in v0.2.7

func (stack *ArrayStack[T]) IsFull() bool

IsFull is a method of the ArrayStack type. It checks if the stack is full.

In this implementation, the method always returns false. This is because an ArrayStack, 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 (*ArrayStack[T]) Peek added in v0.2.7

func (stack *ArrayStack[T]) Peek() T

func (*ArrayStack[T]) Pop added in v0.2.7

func (stack *ArrayStack[T]) Pop() T

func (*ArrayStack[T]) Push added in v0.2.7

func (stack *ArrayStack[T]) Push(value T)

func (*ArrayStack[T]) Size added in v0.2.7

func (stack *ArrayStack[T]) Size() int

func (*ArrayStack[T]) String added in v0.2.7

func (stack *ArrayStack[T]) String() string

func (*ArrayStack[T]) ToSlice added in v0.2.7

func (stack *ArrayStack[T]) ToSlice() []T

type ErrEmptyStack

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

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

func NewErrEmptyStack added in v0.2.7

func NewErrEmptyStack(operation StackOperationType) *ErrEmptyStack

NewErrEmptyStack creates a new ErrEmptyStack. It takes the following parameter:

  • operation is the type of operation that caused the error.

The function returns the following:

  • A pointer to the new ErrEmptyStack.

func (*ErrEmptyStack) Error

func (e *ErrEmptyStack) Error() string

Error is a method of the ErrEmptyStack 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 ": stack is empty". This provides a clear and descriptive error message when attempting to perform a stack operation on an empty stack.

type ErrFullStack

type ErrFullStack struct{}

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

func (*ErrFullStack) Error

func (e *ErrFullStack) Error() string

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

type ErrNegativeCapacity added in v0.2.7

type ErrNegativeCapacity struct{}

ErrNegativeCapacity is a struct that represents an error when a negative capacity is provided for a stack. 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.7

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 stack cannot be negative", providing a clear and descriptive error message when a negative capacity is provided for a stack.

type ErrOutOfBoundsIterator added in v0.2.7

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.7

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.7

type ErrTooManyValues struct{}

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

func (*ErrTooManyValues) Error added in v0.2.7

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 stack: too many values", providing a clear and descriptive error message when too many values are provided for initializing a stack.

type LimitedArrayStack added in v0.2.7

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

LimitedArrayStack is a generic type in Go that represents a stack 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 stack.

func NewLimitedArrayStack added in v0.2.7

func NewLimitedArrayStack[T any](capacity int, values ...T) (*LimitedArrayStack[T], error)

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

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 LimitedArrayStack, 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 LimitedArrayStack is then returned.

func (*LimitedArrayStack[T]) Clear added in v0.2.7

func (stack *LimitedArrayStack[T]) Clear()

func (*LimitedArrayStack[T]) IsEmpty added in v0.2.7

func (stack *LimitedArrayStack[T]) IsEmpty() bool

func (*LimitedArrayStack[T]) IsFull added in v0.2.7

func (stack *LimitedArrayStack[T]) IsFull() bool

func (*LimitedArrayStack[T]) Peek added in v0.2.7

func (stack *LimitedArrayStack[T]) Peek() T

func (*LimitedArrayStack[T]) Pop added in v0.2.7

func (stack *LimitedArrayStack[T]) Pop() T

func (*LimitedArrayStack[T]) Push added in v0.2.7

func (stack *LimitedArrayStack[T]) Push(value T)

Push is a method of the LimitedArrayStack type. It is used to add an element to the end of the stack.

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

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

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

func (*LimitedArrayStack[T]) Size added in v0.2.7

func (stack *LimitedArrayStack[T]) Size() int

func (*LimitedArrayStack[T]) String added in v0.2.7

func (stack *LimitedArrayStack[T]) String() string

func (*LimitedArrayStack[T]) ToSlice added in v0.2.7

func (stack *LimitedArrayStack[T]) ToSlice() []T

type LimitedLinkedStack added in v0.2.7

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

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

func NewLimitedLinkedStack added in v0.2.7

func NewLimitedLinkedStack[T any](capacity int, values ...T) (*LimitedLinkedStack[T], error)

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

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 LimitedLinkedStack 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 stack. The new LimitedLinkedStack is then returned.

func (*LimitedLinkedStack[T]) Clear added in v0.2.7

func (stack *LimitedLinkedStack[T]) Clear()

func (*LimitedLinkedStack[T]) IsEmpty added in v0.2.7

func (stack *LimitedLinkedStack[T]) IsEmpty() bool

func (*LimitedLinkedStack[T]) IsFull added in v0.2.7

func (stack *LimitedLinkedStack[T]) IsFull() bool

func (*LimitedLinkedStack[T]) Peek added in v0.2.7

func (stack *LimitedLinkedStack[T]) Peek() T

func (*LimitedLinkedStack[T]) Pop added in v0.2.7

func (stack *LimitedLinkedStack[T]) Pop() T

func (*LimitedLinkedStack[T]) Push added in v0.2.7

func (stack *LimitedLinkedStack[T]) Push(value T)

Push is a method of the LimitedLinkedStack type. It is used to add an element to the end of the stack.

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

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

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

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

func (*LimitedLinkedStack[T]) Size added in v0.2.7

func (stack *LimitedLinkedStack[T]) Size() int

func (*LimitedLinkedStack[T]) String added in v0.2.7

func (stack *LimitedLinkedStack[T]) String() string

func (*LimitedLinkedStack[T]) ToSlice added in v0.2.7

func (stack *LimitedLinkedStack[T]) ToSlice() []T

type LinkedStack added in v0.2.1

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

LinkedStack is a generic type in Go that represents a stack data structure implemented using a linked list.

func NewLinkedStack added in v0.2.7

func NewLinkedStack[T any](values ...T) *LinkedStack[T]

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

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

If initial values are provided, the function creates a new LinkedStack 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 stack. The new LinkedStack is then returned.

func (*LinkedStack[T]) Clear added in v0.2.7

func (stack *LinkedStack[T]) Clear()

func (*LinkedStack[T]) IsEmpty added in v0.2.7

func (stack *LinkedStack[T]) IsEmpty() bool

func (*LinkedStack[T]) IsFull added in v0.2.7

func (stack *LinkedStack[T]) IsFull() bool

func (*LinkedStack[T]) Peek added in v0.2.7

func (stack *LinkedStack[T]) Peek() T

func (*LinkedStack[T]) Pop added in v0.2.7

func (stack *LinkedStack[T]) Pop() T

func (*LinkedStack[T]) Push added in v0.2.7

func (stack *LinkedStack[T]) Push(value T)

func (*LinkedStack[T]) Size added in v0.2.7

func (stack *LinkedStack[T]) Size() int

func (*LinkedStack[T]) String added in v0.2.7

func (stack *LinkedStack[T]) String() string

func (*LinkedStack[T]) ToSlice added in v0.2.7

func (stack *LinkedStack[T]) ToSlice() []T

type StackIterator added in v0.2.7

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

StackIterator is a generic type in Go that represents an iterator for a stack.

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

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

func NewStackIterator added in v0.2.7

func NewStackIterator[T any](stack Stacker[T]) *StackIterator[T]

NewStackIterator is a function that creates and returns a new StackIterator object for a given stack. It takes a stack of type Stacker[T] as an argument, where T can be any type.

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

The returned StackIterator can be used to iterate over the elements in the stack.

func (*StackIterator[T]) GetNext added in v0.2.7

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

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

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

func (*StackIterator[T]) HasNext added in v0.2.7

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

HasNext is a method of the StackIterator 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 stack.

type StackOperationType added in v0.2.7

type StackOperationType int

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

const (
	// Pop represents a pop operation, which removes an element from the stack.
	Pop StackOperationType = iota

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

func (StackOperationType) String added in v0.2.7

func (qot StackOperationType) String() string

String is a method of the StackOperationType type. It returns a string representation of the stack operation type.

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

This method is typically used for error messages and logging.

type Stacker added in v0.2.7

type Stacker[T any] interface {
	// The Push method adds a value of type T to the end of the stack.
	Push(value T)

	// The Pop method is a convenience method that pops an element from the stack
	// and returns it.
	// If the stack is empty, it will panic.
	Pop() T

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

	// The IsEmpty method checks if the stack 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 stack.
	Size() int

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

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

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

	fmt.Stringer
}

Package stack provides a Stacker interface that defines methods for a stack data structure.

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

Jump to

Keyboard shortcuts

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