stack

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyStack occurs when the stack is empty. This error can be checked
	// with the == operator.
	//
	// Format:
	// 	"stack is empty"
	ErrEmptyStack error
)

Functions

This section is empty.

Types

type ArrayStack

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

ArrayStack is a generic stack implemented using an array.

func (ArrayStack[E]) IsEmpty

func (as ArrayStack[E]) IsEmpty() bool

IsEmpty implements CoreStack.

func (*ArrayStack[E]) Pop

func (as *ArrayStack[E]) Pop() (E, error)

Pop implements CoreStack.

func (*ArrayStack[E]) Push

func (as *ArrayStack[E]) Push(e E) error

Push implements CoreStack.

func (*ArrayStack[E]) Reset

func (as *ArrayStack[E]) Reset() error

Reset implements common.Resetter.

func (ArrayStack[E]) Slice

func (as ArrayStack[E]) Slice() []E

Slice implements Collection.

type Collection

type Collection[E any] interface {
	// Slice returns a slice of the elements in the collection.
	//
	// Returns:
	//   - []E: A slice of the elements in the collection.
	Slice() []E
}

Collection is an interface that represents a collection of elements.

type CoreStack

type CoreStack[E any] interface {
	// Push pushes an element onto the stack.
	//
	// Parameters:
	//   - e: The element to be pushed onto the stack.
	//
	// Returns:
	//   - error: An error if the element could not be pushed onto the stack.
	//
	// Errors:
	//   - common.ErrNilReceiver: If the receiver is nil.
	Push(e E) error

	// Pop pops an element from the stack.
	//
	// Returns:
	//   - E: The element that was popped from the stack.
	//   - error: An error if the element could not be popped from the stack.
	//
	// Errors:
	//   - common.ErrNilReceiver: If the receiver is nil.
	//   - ErrEmptyStack: If the stack is empty.
	Pop() (E, error)

	// IsEmpty checks if the stack is empty.
	//
	// Returns:
	//   - bool: True if the stack is empty, false otherwise.
	IsEmpty() bool
}

CoreStack is a generic stack interface.

type RefusableStack

type RefusableStack[E any] struct {
	// contains filtered or unexported fields
}

RefusableStack is a stack that can be reset.

func RefusableOf

func RefusableOf[E any](stack Stack[E]) (*RefusableStack[E], error)

RefusableOf creates a new RefusableStack from the provided stack.

Parameters:

  • stack: The stack to be wrapped in a RefusableStack.

Returns:

  • *RefusableStack[E]: A pointer to the newly created RefusableStack.
  • error: An error if the provided stack is nil.

Errors:

  • common.ErrBadParam: If the stack parameter is nil.

func (*RefusableStack[E]) Accept

func (s *RefusableStack[E]) Accept() error

Accept resets the popped stack, effectively "accepting" the popped elements.

Returns:

  • error: An error if the receiver is nil or if the popped stack could not be reset.

Errors:

  • common.ErrNilReceiver: If the receiver is nil.

func (*RefusableStack[E]) Pop

func (s *RefusableStack[E]) Pop() (E, error)

Pop implements CoreStack.

func (RefusableStack[E]) Popped

func (s RefusableStack[E]) Popped() []E

Popped returns the elements that were popped from the stack.

Returns:

  • []E: The elements that were popped from the stack.

func (*RefusableStack[E]) Push

func (s *RefusableStack[E]) Push(e E) error

Push implements CoreStack.

func (*RefusableStack[E]) Refuse

func (s *RefusableStack[E]) Refuse() error

Refuse transfers all elements from the stack to the popped stack, effectively "refusing" the stack.

Returns:

  • error: An error if the receiver is nil.

Errors:

  • common.ErrNilReceiver: If the receiver is nil.

func (*RefusableStack[E]) Reset

func (s *RefusableStack[E]) Reset() error

Reset implements common.Resetter.

func (RefusableStack[E]) Slice

func (s RefusableStack[E]) Slice() []E

Slice implements Collection.

type Stack

type Stack[E any] interface {
	CoreStack[E]
	Collection[E]
	common.Resetter
}

Stack is an interface that extends CoreStack and Collection.

Jump to

Keyboard shortcuts

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