arraystack

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: BSD-2-Clause, ISC Imports: 5 Imported by: 0

Documentation

Overview

Package arraystack implements a stack backed by array list.

Structure is not thread safe.

Reference: https://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29#Array

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Iterator

type Iterator[T comparable] struct {
	// contains filtered or unexported fields
}

Iterator returns a stateful iterator whose values can be fetched by an index.

func (*Iterator[T]) Begin

func (iterator *Iterator[T]) Begin()

Begin resets the iterator to its initial state (one-before-first) Call Next() to fetch the first element if any.

func (*Iterator[T]) End

func (iterator *Iterator[T]) End()

End moves the iterator past the last element (one-past-the-end). Call Prev() to fetch the last element if any.

func (*Iterator[T]) First

func (iterator *Iterator[T]) First() bool

First moves the iterator to the first element and returns true if there was a first element in the container. If First() returns true, then first element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.

func (*Iterator[T]) Index

func (iterator *Iterator[T]) Index() int

Index returns the current element's index. Does not modify the state of the iterator.

func (*Iterator[T]) Last

func (iterator *Iterator[T]) Last() bool

Last moves the iterator to the last element and returns true if there was a last element in the container. If Last() returns true, then last element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.

func (*Iterator[T]) Next

func (iterator *Iterator[T]) Next() bool

Next moves the iterator to the next element and returns true if there was a next element in the container. If Next() returns true, then next element's index and value can be retrieved by Index() and Value(). If Next() was called for the first time, then it will point the iterator to the first element if it exists. Modifies the state of the iterator.

func (*Iterator[T]) NextTo

func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool

NextTo moves the iterator to the next element from current position that satisfies the condition given by the passed function, and returns true if there was a next element in the container. If NextTo() returns true, then next element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.

func (*Iterator[T]) Prev

func (iterator *Iterator[T]) Prev() bool

Prev moves the iterator to the previous element and returns true if there was a previous element in the container. If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.

func (*Iterator[T]) PrevTo

func (iterator *Iterator[T]) PrevTo(f func(index int, value T) bool) bool

PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the passed function, and returns true if there was a next element in the container. If PrevTo() returns true, then next element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.

func (*Iterator[T]) Value

func (iterator *Iterator[T]) Value() T

Value returns the current element's value. Does not modify the state of the iterator.

type Stack

type Stack[T comparable] struct {
	// contains filtered or unexported fields
}

Stack holds elements in an array-list

func New

func New[T comparable]() *Stack[T]

New instantiates a new empty stack

func (*Stack[T]) Clear

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

Clear removes all elements from the stack.

func (*Stack[T]) Empty

func (stack *Stack[T]) Empty() bool

Empty returns true if stack does not contain any elements.

func (*Stack[T]) FromJSON

func (stack *Stack[T]) FromJSON(data []byte) error

FromJSON populates the stack from the input JSON representation.

func (*Stack[T]) Iterator

func (stack *Stack[T]) Iterator() *Iterator[T]

Iterator returns a stateful iterator whose values can be fetched by an index.

func (*Stack[T]) MarshalJSON

func (stack *Stack[T]) MarshalJSON() ([]byte, error)

MarshalJSON @implements json.Marshaler

func (*Stack[T]) Peek

func (stack *Stack[T]) Peek() (value T, ok bool)

Peek returns top element on the stack without removing it, or nil if stack is empty. Second return parameter is true, unless the stack was empty and there was nothing to peek.

func (*Stack[T]) Pop

func (stack *Stack[T]) Pop() (value T, ok bool)

Pop removes top element on stack and returns it, or nil if stack is empty. Second return parameter is true, unless the stack was empty and there was nothing to pop.

func (*Stack[T]) Push

func (stack *Stack[T]) Push(value T)

Push adds a value onto the top of the stack

func (*Stack[T]) Size

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

Size returns number of elements within the stack.

func (*Stack[T]) String

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

String returns a string representation of container

func (*Stack[T]) ToJSON

func (stack *Stack[T]) ToJSON() ([]byte, error)

ToJSON outputs the JSON representation of the stack.

func (*Stack[T]) UnmarshalJSON

func (stack *Stack[T]) UnmarshalJSON(bytes []byte) error

UnmarshalJSON @implements json.Unmarshaler

func (*Stack[T]) Values

func (stack *Stack[T]) Values() []T

Values returns all elements in the stack (LIFO order).

Jump to

Keyboard shortcuts

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