Documentation
¶
Overview ¶
Package arraystack is a generic last-in, first-out stack backed by a slice. WithSafe makes a stack safe for concurrent use.
Index ¶
- type Option
- type Stack
- func New[E any](ops ...Option[E]) (*Stack[E], error)
- func NewFromMapKeys[K comparable, V any](m map[K]V, ops ...Option[K]) (*Stack[K], error)
- func NewFromMapValues[K comparable, V any](m map[K]V, ops ...Option[V]) (*Stack[V], error)
- func NewFromSlice[E any](slice []E, ops ...Option[E]) (*Stack[E], error)
- func (s *Stack[E]) Clear()
- func (s *Stack[E]) Clone() *Stack[E]
- func (s *Stack[E]) IsEmpty() bool
- func (s *Stack[E]) Len() int
- func (s *Stack[E]) MarshalJSON() ([]byte, error)
- func (s *Stack[E]) Peek() (E, bool)
- func (s *Stack[E]) Pop() (E, bool)
- func (s *Stack[E]) Push(e E)
- func (s *Stack[E]) String() string
- func (s *Stack[E]) UnmarshalJSON(data []byte) (err error)
- func (s *Stack[E]) Values() []E
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Stack ¶
type Stack[E any] struct { // contains filtered or unexported fields }
Stack represents a stack backed by a slice. The stack provides typical LIFO(last-in, first-out) behavior.
func New ¶
New creates and initializes a empty stack. Options can be provided to customize the stack's properties (e.g., thread safety).
func NewFromMapKeys ¶
func NewFromMapKeys[K comparable, V any](m map[K]V, ops ...Option[K]) (*Stack[K], error)
NewFromMapKeys creates and initializes a stack from the provided map keys. Options can be provided to customize the stack's properties (e.g., thread safety). Returns an empty stack if the provided map is nil or empty.
func NewFromMapValues ¶
func NewFromMapValues[K comparable, V any](m map[K]V, ops ...Option[V]) (*Stack[V], error)
NewFromMapValues creates a stack from the provided map values. Options can be provided to customize the stack's properties (e.g., thread safety). Returns an empty stack if the provided map is nil or empty.
func NewFromSlice ¶
NewFromSlice creates and initializes a stack from the provided slice, the last element of the slice on top. Options can be provided to customize the stack's properties (e.g., thread safety).
func (*Stack[E]) MarshalJSON ¶
MarshalJSON marshals the stack into a JSON array, the top of the stack first, the order Values lists it in.
func (*Stack[E]) Peek ¶
Peek returns the top element of the stack without removing it. Returns the zero value of E and false if the stack is empty.
func (*Stack[E]) Pop ¶
Pop removes and returns the top element of the stack. Returns the zero value of E and false if the stack is empty.
func (*Stack[E]) Push ¶
func (s *Stack[E]) Push(e E)
Push adds an element to the top of the stack. The stack's size increases by one.
func (*Stack[E]) UnmarshalJSON ¶
UnmarshalJSON replaces the stack with the elements of a JSON array read in the order MarshalJSON writes: the first element ends up on top.