stack

package
v0.0.0-...-ca9eab0 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2024 License: MIT Imports: 3 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ReturnableStack

type ReturnableStack[T any] interface {
	StackType[T]
	GetFullCount() uint
	Save()
	Return() StackStatus
}

stacks that can create stack frames

type SaveStack

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

func NewSaveStack

func NewSaveStack[T any](cap uint, returnCap ...uint) *SaveStack[T]

only up to 2 arguments are used, one argument required; cap is the starting capacity for the stack. returnCap[0] is the starting capacity for the number of returns that can be saved. returnCap has a default value of 8

func (*SaveStack[T]) ElemStringSep

func (s *SaveStack[T]) ElemStringSep(encloseL, encloseR, frameSep, sep string) string

func (*SaveStack[T]) Empty

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

true iff current frame is empty

func (*SaveStack[T]) FullEmpty

func (stack *SaveStack[T]) FullEmpty() bool

true iff all frames are empty

func (*SaveStack[T]) GetCount

func (stack *SaveStack[T]) GetCount() uint

number of elements in current frame

func (*SaveStack[T]) GetFrames

func (stack *SaveStack[T]) GetFrames() uint

number of stack frames

func (*SaveStack[T]) GetFullCount

func (stack *SaveStack[T]) GetFullCount() uint

number of elems in all frames

func (*SaveStack[T]) Peek

func (stack *SaveStack[T]) Peek() (elem T, stat StackStatus)

func (*SaveStack[T]) Pop

func (stack *SaveStack[T]) Pop() (elem T, stat StackStatus)

func (*SaveStack[T]) Push

func (stack *SaveStack[T]) Push(elem T)

func (*SaveStack[T]) Rebase

func (stack *SaveStack[T]) Rebase() (stat StackStatus)

Returns base counter to saved base counter but does NOT change the stack counter. This function effectively "merges" the top two stack frames into a single frame

func (*SaveStack[T]) Return

func (stack *SaveStack[T]) Return() (stat StackStatus)

Return base counter to saved base counter and return stack counter to previous stack counter. Return fails when nothing is saved.

func (*SaveStack[T]) Save

func (stack *SaveStack[T]) Save()

create return point point to return to later

func (*SaveStack[T]) Search

func (s *SaveStack[T]) Search(predicate func(T) bool) (found bool, elem T)

search from top to bottom for first occurrence of element that predicate occurs for

func (*SaveStack[T]) Status

func (stack *SaveStack[T]) Status() StackStatus

type Stack

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

func NewStack

func NewStack[T any](cap uint) *Stack[T]

Return a pointer to a new, initialized stack with room for at least `cap` elements.

func (*Stack[T]) Clear

func (s *Stack[T]) Clear(n uint)

removes min(n, s.GetCount()) elements from the stack

func (*Stack[T]) ElemString

func (s *Stack[T]) ElemString() string

wraps fmt.Sprint(s.elems)

func (*Stack[T]) ElemStringSep

func (s *Stack[T]) ElemStringSep(encloseL, encloseR string, sep string) string

func (*Stack[T]) Empty

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

returns true iff stack has no elements

func (*Stack[T]) GetCapacity

func (s *Stack[T]) GetCapacity() uint

returns capicty of stack

func (*Stack[T]) GetCount

func (s *Stack[T]) GetCount() uint

returns number of elements in stack

func (*Stack[T]) MultiCheck

func (s *Stack[T]) MultiCheck(n int) (elems []T, stat StackStatus)

MultiCheck returns the top `n` elements of the stack (or an IllegalOperation if there are fewer than `n` elements). Elements are returned in reverse-popped order (see "IMPORTANT" below), e.g.,

[w, x, y, z].MultiCheck(3) == [x, y, z]

IMPORTANT: note that MultiCheck does NOT return a slice in the order (left-to-right) that elemnts would be popped! In fact, it does the opposite.

NOTE: elements are not removed from the stack

func (*Stack[T]) MultiPop

func (s *Stack[T]) MultiPop(n uint) (elems []T, stat StackStatus)

MultiPop pops and returns the top `n` elements of the stack (or an IllegalOperation if there are fewer than `n` elements). Elements are returned in they order (left-to-right in return value) that they are popped, e.g.,

[w, x, y, z].MultiPop(3) == [z, y, x]

To return the elements in the order they appear on the stack, use

(*Stack[T]) MultiCheck(int)

func (*Stack[T]) MultiPush

func (s *Stack[T]) MultiPush(elems ...T)

func (*Stack[T]) Peek

func (s *Stack[T]) Peek() (elem T, stat StackStatus)

returns the top element of the stack if it exists, else the Empty status

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() (T, StackStatus)

removes the top element of the stack and returns it if stack is not empty, else Empty status is returned

func (*Stack[T]) Push

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

Puts elem onto top of stack

func (*Stack[T]) Search

func (s *Stack[T]) Search(predicate func(T) bool) (found bool, elem T)

searches from top to bottom for first thing predicate returns true for

func (*Stack[T]) Status

func (s *Stack[T]) Status() StackStatus

returns condition of stack (Ok or Empty)

type StackPlus

type StackPlus[T any] interface {
	StackType[T]
	Clear(uint)
	MultiPush(...T)
	MultiPop(uint) ([]T, StackStatus)
}

stacks with the ability to pop and push multiple elements

type StackStatus

type StackStatus uint
const (
	Ok StackStatus = iota
	Empty
	Overflow
	IllegalOperation
	IllegalReturn
)

func (StackStatus) Is

func (stat StackStatus) Is(stat2 StackStatus) bool

func (StackStatus) IsEmpty

func (stat StackStatus) IsEmpty() bool

func (StackStatus) IsIllegalOperation

func (stat StackStatus) IsIllegalOperation() bool

func (StackStatus) IsOk

func (stat StackStatus) IsOk() bool

func (StackStatus) IsOverflow

func (stat StackStatus) IsOverflow() bool

func (StackStatus) NotOk

func (stat StackStatus) NotOk() bool

func (StackStatus) String

func (stat StackStatus) String() string

type StackType

type StackType[T any] interface {
	Push(T)
	Pop() (T, StackStatus)
	Peek() (T, StackStatus)
	GetCount() uint
	Status() StackStatus
}

interface for usual stack operations (i.e., push, pop, peek, and number of elements) and an additional operation to query various states

type StaticStack

type StaticStack[T any] Stack[T]

stack with fixed size (i.e., stack cannot request more capacity)

Jump to

Keyboard shortcuts

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