ListLike

package
v0.2.14 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

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

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() optional.Int

Capacity is a method of the ArrayStack 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 (*ArrayStack[T]) Clear

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

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

func (*ArrayStack[T]) CutNilValues

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

CutNilValues is a method of the ArrayStack type. It is used to remove all 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() (isFull bool)

IsFull is a method of the ArrayStack 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 (*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 *ErrCallFailed 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 *ErrCallFailed 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 *ErrCallFailed if the stack is full.

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 added in v0.2.10

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.

func (*ArrayStack[T]) WithCapacity

func (stack *ArrayStack[T]) WithCapacity(capacity int) (Stacker[T], error)

WithCapacity is a method of the ArrayStack type. It is used to set the maximum number of elements the stack can hold.

Panics with an error of type *ErrCallFailed if the capacity is already set, or with an error of type *ErrInvalidParameter if the provided capacity is negative or less than the current number of elements in the stack.

Parameters:

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

Returns:

  • Stacker[T]: A pointer to the stack with the new capacity set.

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

func NewErrEmptyStack[T any](stack Stacker[T]) *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 {
	// contains filtered or unexported fields
}

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 NewErrFullStack

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

NewErrFullStack creates a new ErrFullStack. 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 ErrFullStack.

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 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() optional.Int

Capacity is a method of the LinkedStack 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 (*LinkedStack[T]) Clear

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

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

func (*LinkedStack[T]) CutNilValues

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

CutNilValues is a method of the LinkedStack type. It is used to remove all 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() (isFull bool)

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

Returns:

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

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 *ErrCallFailed 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 *ErrCallFailed 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 *ErrCallFailed if the stack is full.

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 added in v0.2.10

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.

func (*LinkedStack[T]) WithCapacity

func (stack *LinkedStack[T]) WithCapacity(capacity int) (Stacker[T], error)

WithCapacity is a method of the LinkedStack type. It is used to set the maximum number of elements the stack can hold.

Panics with an error of type *ErrCallFailed if the capacity is already set, or with an error of type *ErrInvalidParameter if the provided capacity is negative or less than the current number of elements in the stack.

Parameters:

  • capacity: An integer representing the maximum number of elements the stack can hold.

Returns:

  • Stacker[T]: A pointer to the current instance of the LinkedStack.

type Stacker

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

	// 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, 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.
	Peek() (T, error)

	// WithCapacity is a special function that modifies an existing stack data
	// structure to have a specific capacity. Panics if the list already has a capacity
	// set or if the new capacity is less than the current size of the list-like data
	// structure.
	//
	// As a result, it is recommended to use this function only when creating a new
	// list-like data structure.
	WithCapacity(int) (Stacker[T], error)

	// .ListLike[T] is an interface that defines methods for a stack data structure.
	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