Stacker

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayStack

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

ArrayStack is a generic type that represents a stack data structure with or without a limited capacity. It is implemented using an array.

func NewArrayStack

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

NewArrayStack is a function that creates and returns a new instance of a ArrayStack.

Parameters:

  • values: A variadic parameter of type T, which represents the initial values to be stored in the stack.

Returns:

  • *ArrayStack[T]: A pointer to the newly created ArrayStack.

func (*ArrayStack[T]) Capacity

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

Capacity is a method of the ArrayStack type. It is used to return the capacity of the stack.

Returns:

  • int: -1

func (*ArrayStack[T]) Clear

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

Clear is a method of the ArrayStack type. It is used to remove aCommon elements from the stack, making it empty.

func (*ArrayStack[T]) Copy

func (stack *ArrayStack[T]) Copy() uc.Copier

Copy is a method of the ArrayStack type. It is used to create a shaCommonow copy of the stack.

Returns:

  • itf.Copier: A copy of the stack.

func (*ArrayStack[T]) CutNilValues

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

CutNilValues is a method of the ArrayStack type. It is used to remove aCommon nil values from the stack.

func (*ArrayStack[T]) IsEmpty

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

IsEmpty is a method of the ArrayStack type. It is used to check if the stack is empty.

Returns:

  • bool: A boolean value that is true if the stack is empty, and false otherwise.

func (*ArrayStack[T]) IsFull

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

IsFull is a method of the ArrayStack type. It is used to check if the stack is full.

Returns:

  • bool: false

func (*ArrayStack[T]) Iterator

func (stack *ArrayStack[T]) Iterator() itf.Iterater[T]

Iterator is a method of the ArrayStack type. It is used to return an iterator that iterates over the elements in the stack.

Returns:

  • itf.Iterater[T]: An iterator that iterates over the elements in the stack.

func (*ArrayStack[T]) Peek

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

Peek is a method of the ArrayStack type. It is used to return the element at the end of the stack without removing it.

Panics with an error of type *ErrEmptyList if the stack is empty.

Returns:

  • T: The element at the end of the stack.

func (*ArrayStack[T]) Pop

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

Pop is a method of the ArrayStack type. It is used to remove and return the element at the end of the stack.

Panics with an error of type *ErrEmptyList if the stack is empty.

Returns:

  • T: The element at the end of the stack.

func (*ArrayStack[T]) Push

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

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

Panics with an error of type *ErrCaCommonFailed if the stack is fuCommon.

Parameters:

  • value: The value of type T to be added to the stack.

func (*ArrayStack[T]) Size

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

Size is a method of the ArrayStack type. It is used to return the number of elements in the stack.

Returns:

  • int: An integer that represents the number of elements in the stack.

func (*ArrayStack[T]) Slice

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

Slice is a method of the ArrayStack type. It is used to return a slice of the elements in the stack.

Returns:

  • []T: A slice of the elements in the stack.

func (*ArrayStack[T]) String

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

String is a method of the ArrayStack type. It is used to return a string representation of the stack, including its capacity and the elements it contains.

Returns:

  • string: A string representation of the stack.

type ErrFullStack

type ErrFullStack[T any] struct {
	// Stack is the stack that is full.
	Stack Stacker[T]
}

ErrFullStack is an error type for a full stack.

func NewErrFullStack added in v0.2.43

func NewErrFullStack[T any](stack Stacker[T]) *ErrFullStack[T]

NewErrFullStack is a constructor for ErrFullStack.

Parameters:

  • stack: The stack that is full.

Returns:

  • *ErrFullStack: The error.

func (*ErrFullStack[T]) Error

func (e *ErrFullStack[T]) Error() string

Error returns the error message: "stack (%T) is full".

Returns:

  • string: The error message.

Behaviors:

  • If the stack is nil, the error message is "stack is full".

type LimitedArrayStack

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

LimitedArrayStack is a generic type that represents a stack data structure with or without a limited capacity. It is implemented using an array.

func NewLimitedArrayStack

func NewLimitedArrayStack[T any](values ...T) *LimitedArrayStack[T]

NewLimitedArrayStack is a function that creates and returns a new instance of a LimitedArrayStack.

Parameters:

  • values: A variadic parameter of type T, which represents the initial values to be stored in the stack.

Returns:

  • *LimitedArrayStack[T]: A pointer to the newly created LimitedArrayStack.

func (*LimitedArrayStack[T]) Capacity

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

Capacity is a method of the LimitedArrayStack type. It is used to return the maximum number of elements the stack can hold.

Returns:

  • optional.Int: An optional integer that represents the maximum number of elements the stack can hold.

func (*LimitedArrayStack[T]) Clear

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

Clear is a method of the LimitedArrayStack type. It is used to remove all elements from the stack, making it empty.

func (*LimitedArrayStack[T]) Copy

func (stack *LimitedArrayStack[T]) Copy() uc.Copier

Copy is a method of the LimitedArrayStack type. It is used to create a shallow copy of the stack.

Returns:

  • itf.Copier: A copy of the stack.

func (*LimitedArrayStack[T]) CutNilValues

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

CutNilValues is a method of the LimitedArrayStack type. It is used to remove all nil values from the stack.

func (*LimitedArrayStack[T]) IsEmpty

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

IsEmpty is a method of the LimitedArrayStack type. It is used to check if the stack is empty.

Returns:

  • bool: A boolean value that is true if the stack is empty, and false otherwise.

func (*LimitedArrayStack[T]) IsFull

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

IsFull is a method of the LimitedArrayStack type. It is used to check if the stack is full, i.e., if it has reached its maximum capacity.

Returns:

  • isFull: A boolean value that is true if the stack is full, and false otherwise.

func (*LimitedArrayStack[T]) Iterator

func (stack *LimitedArrayStack[T]) Iterator() itf.Iterater[T]

Iterator is a method of the LimitedArrayStack type. It is used to return an iterator that iterates over the elements in the stack.

Returns:

  • itf.Iterater[T]: An iterator that iterates over the elements in the stack.

func (*LimitedArrayStack[T]) Peek

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

Peek is a method of the LimitedArrayStack type. It is used to return the element at the end of the stack without removing it.

Panics with an error of type *ErrCallFailed if the stack is empty.

Returns:

  • T: The element at the end of the stack.

func (*LimitedArrayStack[T]) Pop

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

Pop is a method of the LimitedArrayStack type. It is used to remove and return the element at the end of the stack.

Panics with an error of type *ErrCallFailed if the stack is empty.

Returns:

  • T: The element at the end of the stack.

func (*LimitedArrayStack[T]) Push

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

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

Panics with an error of type *ErrCallFailed if the stack is full.

Parameters:

  • value: The value of type T to be added to the stack.

func (*LimitedArrayStack[T]) Size

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

Size is a method of the LimitedArrayStack type. It is used to return the number of elements in the stack.

Returns:

  • int: An integer that represents the number of elements in the stack.

func (*LimitedArrayStack[T]) Slice

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

Slice is a method of the LimitedArrayStack type. It is used to return a slice of the elements in the stack.

Returns:

  • []T: A slice of the elements in the stack.

func (*LimitedArrayStack[T]) String

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

String is a method of the LimitedArrayStack type. It is used to return a string representation of the stack, including its capacity and the elements it contains.

Returns:

  • string: A string representation of the stack.

type LimitedLinkedStack

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

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

func NewLimitedLinkedStack

func NewLimitedLinkedStack[T any](values ...T) *LimitedLinkedStack[T]

NewLimitedLinkedStack is a function that creates and returns a new instance of a LimitedLinkedStack.

Parameters:

  • values: A variadic parameter of type T, which represents the initial values to be stored in the stack.

Returns:

  • *LimitedLinkedStack[T]: A pointer to the newly created LimitedLinkedStack.

func (*LimitedLinkedStack[T]) Capacity

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

Capacity is a method of the LimitedLinkedStack type. It is used to return the maximum number of elements the stack can hold.

Returns:

  • optional.Int: The maximum number of elements the stack can hold.

func (*LimitedLinkedStack[T]) Clear

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

Clear is a method of the LimitedLinkedStack type. It is used to remove all elements from the stack.

func (*LimitedLinkedStack[T]) Copy

func (stack *LimitedLinkedStack[T]) Copy() uc.Copier

Copy is a method of the LimitedLinkedStack type. It is used to create a shallow copy of the stack.

Returns:

  • itf.Copier: A copy of the stack.

func (*LimitedLinkedStack[T]) CutNilValues

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

CutNilValues is a method of the LimitedLinkedStack type. It is used to remove all nil values from the stack.

func (*LimitedLinkedStack[T]) IsEmpty

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

IsEmpty is a method of the LimitedLinkedStack type. It is used to check if the stack is empty.

Returns:

  • bool: true if the stack is empty, and false otherwise.

func (*LimitedLinkedStack[T]) IsFull

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

IsFull is a method of the LimitedLinkedStack type. It is used to check if the stack is full.

Returns:

  • isFull: true if the stack is full, and false otherwise.

func (*LimitedLinkedStack[T]) Iterator

func (stack *LimitedLinkedStack[T]) Iterator() itf.Iterater[T]

Iterator is a method of the LimitedLinkedStack type. It is used to return an iterator for the elements in the stack.

Returns:

  • itf.Iterater[T]: An iterator for the elements in the stack.

func (*LimitedLinkedStack[T]) Peek

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

Peek is a method of the LimitedLinkedStack type. It is used to return the last element in the stack without removing it.

Panics with an error of type *ErrCallFailed if the stack is empty.

Returns:

  • T: The value of the last element in the stack.

func (*LimitedLinkedStack[T]) Pop

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

Pop is a method of the LimitedLinkedStack type. It is used to remove and return the last element in the stack.

Panics with an error of type *ErrCallFailed if the stack is empty.

Returns:

  • T: The value of the last element in the stack.

func (*LimitedLinkedStack[T]) Push

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

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

Panics with an error of type *ErrCallFailed if the stack is full.

Parameters:

  • value: The value to be added to the stack.

func (*LimitedLinkedStack[T]) Size

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

Size is a method of the LimitedLinkedStack type. It is used to return the number of elements in the stack.

Returns:

  • int: The number of elements in the stack.

func (*LimitedLinkedStack[T]) Slice

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

Slice is a method of the LimitedLinkedStack type. It is used to return a slice of the elements in the stack.

Returns:

  • []T: A slice of the elements in the stack.

func (*LimitedLinkedStack[T]) String

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

String is a method of the LimitedLinkedStack type. It is used to return a string representation of the stack, which includes the size, capacity, and elements in the stack.

Returns:

  • string: A string representation of the stack.

type LinkedStack

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

LinkedStack is a generic type that represents a stack data structure with or without a limited capacity, implemented using a linked list.

func NewLinkedStack

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

NewLinkedStack is a function that creates and returns a new instance of a LinkedStack.

Parameters:

  • values: A variadic parameter of type T, which represents the initial values to be stored in the stack.

Returns:

  • *LinkedStack[T]: A pointer to the newly created LinkedStack.

func (*LinkedStack[T]) Capacity

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

Capacity is a method of the LinkedStack type. It is used to return the maximum number of elements that the stack can store.

Returns:

  • int: -1

func (*LinkedStack[T]) Clear

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

Clear is a method of the LinkedStack type. It is used to remove aCommon elements from the stack.

func (*LinkedStack[T]) Copy

func (stack *LinkedStack[T]) Copy() uc.Copier

Copy is a method of the LinkedStack type. It is used to create a shaCommonow copy of the stack.

Returns:

  • itf.Copier: A copy of the stack.

func (*LinkedStack[T]) CutNilValues

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

CutNilValues is a method of the LinkedStack type. It is used to remove aCommon nil values from the stack.

func (*LinkedStack[T]) IsEmpty

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

IsEmpty is a method of the LinkedStack type. It is used to check if the stack is empty.

Returns:

  • bool: true if the stack is empty, and false otherwise.

func (*LinkedStack[T]) IsFull

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

IsFull is a method of the LinkedStack type. It is used to check if the stack is full.

Returns:

  • bool: false

func (*LinkedStack[T]) Iterator

func (stack *LinkedStack[T]) Iterator() itf.Iterater[T]

Iterator is a method of the LinkedStack type. It is used to return an iterator for the elements in the stack.

Returns:

  • itf.Iterater[T]: An iterator for the elements in the stack.

func (*LinkedStack[T]) Peek

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

Peek is a method of the LinkedStack type. It is used to return the last element in the stack without removing it.

Panics with an error of type *ErrEmptyList if the stack is empty.

Returns:

  • T: The value of the last element in the stack.

func (*LinkedStack[T]) Pop

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

Pop is a method of the LinkedStack type. It is used to remove and return the last element in the stack.

Panics with an error of type *ErrEmptyList if the stack is empty.

Returns:

  • T: The value of the last element in the stack.

func (*LinkedStack[T]) Push

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

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

Panics with an error of type *ErrCaCommonFailed if the stack is fuCommon.

Parameters:

  • value: The value to be added to the stack.

func (*LinkedStack[T]) Size

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

Size is a method of the LinkedStack type. It is used to return the number of elements in the stack.

Returns:

  • int: The number of elements in the stack.

func (*LinkedStack[T]) Slice

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

Slice is a method of the LinkedStack type. It is used to return a slice of the elements in the stack.

Returns:

  • []T: A slice of the elements in the stack.

func (*LinkedStack[T]) String

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

String is a method of the LinkedStack type. It is used to return a string representation of the stack, which includes the size, capacity, and elements in the stack.

Returns:

  • string: A string representation of the stack.

type StackNode

type StackNode[T any] struct {
	// value is the value stored in the node.
	Value T
	// contains filtered or unexported fields
}

StackNode represents a node in a linked list.

func NewStackNode

func NewStackNode[T any](value T) *StackNode[T]

NewStackNode is a constructor function that creates a new linkedNode with the given value.

Parameters:

  • value: The value to store in the linkedNode.

Returns:

  • *linkedNode: A pointer to the newly created linkedNode.

func (*StackNode[T]) Next

func (node *StackNode[T]) Next() *StackNode[T]

Next is a method that returns the next linkedNode in the list.

Returns:

  • *linkedNode: A pointer to the next linkedNode in the list.

func (*StackNode[T]) SetNext

func (node *StackNode[T]) SetNext(next *StackNode[T])

SetNext is a method that sets the next linkedNode in the list.

Parameters:

  • next: A pointer to the next linkedNode in the list.

type Stacker

type Stacker[T any] interface {
	// Push is a method that adds a value of type T to the end of the stack.
	// If the stack is full, it will panic.
	//
	// Parameters:
	//
	//   - value: The value of type T to add to the stack.
	Push(value T) error

	// Pop is a method that pops an element from the stack and returns it.
	// If the stack is empty, it will panic.
	//
	// Returns:
	//
	//   - T: The value of type T that was popped.
	Pop() (T, error)

	// 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.
	//
	// Returns:
	//
	//   - T: The value of type T at the front of the stack.
	Peek() (T, error)

	ListLike.ListLike[T]
}

Stacker is an interface that defines methods for a stack data structure.

Jump to

Keyboard shortcuts

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