Documentation
¶
Index ¶
- Variables
- func CALL[O any](aut Aut[O], arg any) O
- func EACH[O any](aut Aut[O], arg any) iter.Seq[O]
- func ERROR[O any](aut Aut[O]) error
- func IsEmpty(ll any) bool
- func Reset(ll any)
- func TRY[O any](aut Aut[O], arg any, handle func(err error) O) O
- type ArrayStack
- type Aut
- type Lister
- type Queue
- type RefusableStack
- func (s *RefusableStack[T]) Accept()
- func (s RefusableStack[T]) IsEmpty() bool
- func (s RefusableStack[T]) Peek() (T, error)
- func (s *RefusableStack[T]) Pop() (T, error)
- func (s RefusableStack[T]) Popped() []T
- func (s *RefusableStack[T]) Push(elem T) error
- func (s *RefusableStack[T]) PushMany(elems []T) error
- func (s *RefusableStack[T]) Refuse()
- func (s *RefusableStack[T]) RefuseOne()
- func (s *RefusableStack[T]) Reset()
- func (s RefusableStack[T]) Size() int
- func (s RefusableStack[T]) Validate() error
- type SizeFunc
- type Stacker
Constants ¶
This section is empty.
Variables ¶
var ( // ErrEmptyStack occurs when a pop or peek operation is called on an empty stack. // This error can be checked with the == operator. // // Format: // "empty stack" ErrEmptyStack error // ErrEmptyQueue occurs when a pop or peek operation is called on an empty queue. // This error can be checked with the == operator. // // Format: // "empty queue" ErrEmptyQueue error // ErrCannotPush occurs when a push operation is called on a refusable stack that // was not accepted nor refused yet. This error can be checked with the == operator. // // Format: // "cannot push elements: stack not accepted nor refused" ErrCannotPush error )
var ( ErrNoAutomaton error ErrBadlyImplemented error )
var SD listlikeT
SD is the namespace for SD-like functions.
var Stack stackT
Functions ¶
func CALL ¶
CALL executes the given automaton with the provided argument and returns its result. If the automaton reaches a terminal state, it resets and tries again. If the automaton is implemented incorrectly, it panics.
Parameters:
- aut: The automaton to execute.
- arg: The argument to pass to the automaton.
Returns:
- O: The result of the automaton execution.
Panics:
- ErrNoAutomaton: If the provided automaton is nil.
- ErrBadlyImplemented: If the automaton reaches a terminal state on the second call.
- error: If the automaton has an error.
func ERROR ¶
ERROR checks if the given automaton has an error and returns it.
Parameters:
- aut: The automaton to check for an error.
Returns:
- error: The error associated with the automaton if it has one, or nil if there is no error.
Panics:
- ErrNoAutomaton: If the provided automaton is nil.
func TRY ¶
TRY calls the given automaton with the given argument and returns its result if there is no error. If there is an error, it calls the given error handler with the error and returns its result. If the error handler is nil, it will panic with the error.
Parameters:
- aut: The automaton to call.
- arg: The argument to pass to the automaton.
- handle: The error handler to call if there is an error.
Returns:
- O: The result of either the automaton or the error handler.
Panics:
- ErrNoAutomaton: If the provided automaton is nil.
- error: If there is an error and the error handler is nil.
Types ¶
type ArrayStack ¶
type ArrayStack[T any] struct { // contains filtered or unexported fields }
ArrayStack is a simple implementation of a stack. An empty stack can either be created with the `var stack ArrayStack[T]` syntax or with the `new(ArrayStack[T])` constructor.
func NewStack ¶
func NewStack[T any](elems []T) *ArrayStack[T]
NewStack creates a new stack from a slice.
Parameters:
- elems: The elements to add to the stack.
Returns:
- *Stack[T]: The new stack. Never returns nil.
WARNING: As a side-effect, the original list will be reversed.
func (ArrayStack[T]) IsEmpty ¶
func (s ArrayStack[T]) IsEmpty() bool
IsEmpty implements the Lister interface.
func (ArrayStack[T]) Peek ¶
func (s ArrayStack[T]) Peek() (T, error)
Peek returns the element at the top of the stack.
Returns:
- T: The element at the top of the stack.
- error: An error if the stack is empty.
Errors:
- ErrEmptyStack: If the stack is empty.
func (*ArrayStack[T]) Pop ¶
func (s *ArrayStack[T]) Pop() (T, error)
Pop removes an element from the stack.
Returns:
- T: The element that was removed.
- error: An error if the element could not be removed from the stack.
Errors:
- ErrEmptyStack: If the stack is empty.
func (*ArrayStack[T]) Push ¶
func (s *ArrayStack[T]) Push(elem T) error
Push adds an element to the stack.
Parameters:
- elem: The element to add.
Returns:
- error: An error if the receiver is nil.
func (*ArrayStack[T]) PushMany ¶
func (s *ArrayStack[T]) PushMany(elems []T) error
PushMany adds multiple elements to the stack. If it has at least one element but the receiver is nil, an error is returned.
Parameters:
- elems: The elements to add.
Returns:
- error: An error if the receiver is nil.
WARNING: As a side-effect, the original list will be reversed.
func (*ArrayStack[T]) Reset ¶
func (s *ArrayStack[T]) Reset()
Reset implements the Lister interface.
type Lister ¶
type Lister interface {
// Size returns the number of elements in the list-like data structure.
//
// Returns:
// - int: The number of elements in the list-like data structure. Never negative.
Size() int
// IsEmpty checks whether the list-like data structure is empty.
//
// Returns:
// - bool: True if the list-like data structure is empty, false otherwise.
IsEmpty() bool
// Reset resets the list-like data structure for reuse.
Reset()
}
Lister is an interface that can be used by list-like data structures.
type Queue ¶
type Queue[T any] struct { // contains filtered or unexported fields }
Queue is a simple implementation of a queue. An empty queue can either be created with the `var queue Queue[T]` syntax or with the `new(Queue[T])`
func NewQueue ¶
NewQueue creates a new queue from a slice.
Parameters:
- elems: The elements to add to the queue.
Returns:
- *Queue[T]: The new queue. Never returns nil.
func (*Queue[T]) Dequeue ¶
Dequeue removes the first element from the queue.
Returns:
- T: The element that was removed.
- error: An error if the element could not be removed from the queue.
Errors:
- ErrEmptyQueue: If the queue is empty.
func (*Queue[T]) Enqueue ¶
Enqueue adds an element to the queue.
Parameters:
- elem: The element to add.
Returns:
- error: An error if the receiver is nil.
func (*Queue[T]) EnqueueMany ¶
EnqueueMany adds multiple elements to the queue. If it has at least one element but the receiver is nil, an error is returned.
Parameters:
- elems: The elements to add.
Returns:
- error: An error if the receiver is nil.
type RefusableStack ¶
type RefusableStack[T any] struct { // contains filtered or unexported fields }
RefusableStack is a simple implementation of a stack. An empty stack can either be created with the `var stack Stack[T]` syntax or with the `new(Stack[T])` constructor.
func NewRefusableStack ¶
func NewRefusableStack[T any](elems []T) *RefusableStack[T]
NewStack creates a new stack from a slice.
Parameters:
- elems: The elements to add to the stack.
Returns:
- *Stack[T]: The new stack. Never returns nil.
WARNING: As a side-effect, the original list will be reversed.
func (*RefusableStack[T]) Accept ¶
func (s *RefusableStack[T]) Accept()
Accept accepts all the elements that were popped. Does nothing if no element was popped.
func (RefusableStack[T]) IsEmpty ¶
func (s RefusableStack[T]) IsEmpty() bool
IsEmpty implements the Lister interface.
func (RefusableStack[T]) Peek ¶
func (s RefusableStack[T]) Peek() (T, error)
Peek returns the element at the top of the stack.
Returns:
- T: The element at the top of the stack.
- error: An error if the stack is empty.
Errors:
- ErrEmptyStack: If the stack is empty.
func (*RefusableStack[T]) Pop ¶
func (s *RefusableStack[T]) Pop() (T, error)
Pop removes an element from the stack.
Returns:
- T: The element that was removed.
- error: An error if the element could not be removed from the stack.
Errors:
- ErrEmptyStack: If the stack is empty.
func (RefusableStack[T]) Popped ¶
func (s RefusableStack[T]) Popped() []T
Popped returns the elements that were popped from the stack since the last Accept or Refuse operation. The returned slice contains the elements in the order they were popped, with the most recently popped element at the first position.
Returns:
- []T: The elements that were popped. Nil if no elements were popped.
func (*RefusableStack[T]) Push ¶
func (s *RefusableStack[T]) Push(elem T) error
Push adds an element to the stack.
Parameters:
- elem: The element to add.
Returns:
- error: An error if the receiver is nil.
func (*RefusableStack[T]) PushMany ¶
func (s *RefusableStack[T]) PushMany(elems []T) error
PushMany adds multiple elements to the stack. If it has at least one element but the receiver is nil, an error is returned.
Parameters:
- elems: The elements to add.
Returns:
- error: An error if the receiver is nil.
WARNING: As a side-effect, the original list will be reversed.
func (*RefusableStack[T]) Refuse ¶
func (s *RefusableStack[T]) Refuse()
Refuse refuses any element that was popped since the last time Accept was called. Does nothing if no element was popped.
func (*RefusableStack[T]) RefuseOne ¶
func (s *RefusableStack[T]) RefuseOne()
RefuseOne refuses the last popped element. Does nothing if no element was popped.
func (*RefusableStack[T]) Reset ¶
func (s *RefusableStack[T]) Reset()
Reset implements the Lister interface.
func (RefusableStack[T]) Size ¶
func (s RefusableStack[T]) Size() int
Size implements the Lister interface.
func (RefusableStack[T]) Validate ¶
func (s RefusableStack[T]) Validate() error
Validate implements the assert.Validater interface.
An instance of RefusableStack is valid if and only if the following conditions are met: - top >= 0 - top <= len(slice)