monotoneStack

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2025 License: BSD-2-Clause Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MonotoneStack

type MonotoneStack[T any] struct {
	// contains filtered or unexported fields
}

func NewMonotoneStack

func NewMonotoneStack[T constraints.Ordered](t MonotoneType, isStrict bool) *MonotoneStack[T]

NewMonotoneStack returns a new MonotoneStack.

t is the type of monotone stack: increase or decrease

isStrict controls whether the stack is strictly monotonic; if isStrict is false, the elem can be pushed onto the stack when top == elem; if isStrict is true, the elem can not be pushed onto the stack when top == elem.

func NewMonotoneStackWithCompare

func NewMonotoneStackWithCompare[T any](f compareFunc[T]) *MonotoneStack[T]

NewMonotoneStackWithCompare returns a new MonotoneStack.

f is Custom comparison function

compareFunc[T] func(top, elem T) bool

when push elem onto stack , if "f(top,elem)" returns true, elem is pushed into the stack;

func (*MonotoneStack[T]) Check

func (s *MonotoneStack[T]) Check(elem T) bool

Check whether the elem can be pushed onto the stack

func (*MonotoneStack[T]) Clear

func (s *MonotoneStack[T]) Clear()

Clear removes all items from the stack.

func (*MonotoneStack[T]) IsEmpty

func (s *MonotoneStack[T]) IsEmpty() bool

IsEmpty returns true if the stack is empty.

func (*MonotoneStack[T]) List

func (s *MonotoneStack[T]) List() []T

List return the copy of stack.

func (*MonotoneStack[T]) Pop

func (s *MonotoneStack[T]) Pop() (T, bool)

Pop removes the item on the top of the stack and returns it. If the stack is empty, Pop returns the zero value of the type T and false.

func (*MonotoneStack[T]) Push

func (s *MonotoneStack[T]) Push(elem T) []T

Push adds an elem to the top of the stack.

return the popList those popped from the stack due to elem being pushed onto stack

popList keeps the order of popping

func (*MonotoneStack[T]) Size

func (s *MonotoneStack[T]) Size() int

Size returns the number of items in the stack.

func (*MonotoneStack[T]) Top

func (s *MonotoneStack[T]) Top() (T, bool)

Top returns the item on the top of the stack without removing it. If the stack is empty, Top returns the zero value of the type T and false.

func (*MonotoneStack[T]) TryPush

func (s *MonotoneStack[T]) TryPush(elem T) bool

TryPush attempts to push elem onto the monotone stack.

return whether elem is pushed onto the stack successfully

type MonotoneType

type MonotoneType int
const (
	MonotoneTypeIncrease MonotoneType
	MonotoneTypeDecrease
)

Jump to

Keyboard shortcuts

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