pmark

package
v0.0.0-...-2d29fd2 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2024 License: MIT Imports: 4 Imported by: 0

README

preset/pmark

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInsufficientStackLen = errors.New("insufficient stack length")
	ErrMarkAreNotRegistered = errors.New("mark are not registered")
)

Functions

func IsPair

func IsPair[T ~string](begin, end T) bool

IsPair returns true if the two marks are a pair

Types

type Cell

type Cell[TPayload constraints.Ordered] struct {
	Val  TPayload `json:"val"`
	Mark `json:"mark"`
}

Cell - a cell with mark and payload

type Mark

type Mark string

Mark is a string type that represents a mark it is used to pair marks

const (
	NIL            Mark = ""
	BucketsAngleL  Mark = "<"
	BucketsAngleR  Mark = ">"
	BucketsRoundL  Mark = "("
	BucketsRoundR  Mark = ")"
	BucketsSquareL Mark = "["
	BucketsSquareR Mark = "]"
	BucketsCurlyL  Mark = "{"
	BucketsCurlyR  Mark = "}"
)

func (Mark) IsLeft

func (p Mark) IsLeft() (isLeft bool)

IsLeft returns true if the mark is a left mark

func (Mark) Name

func (p Mark) Name() string

Name returns the name of the mark

func (Mark) PairedWith

func (p Mark) PairedWith(other Mark) bool

PairedWith returns true if the mark is paired with the given mark

func (Mark) Pairing

func (p Mark) Pairing() Mark

Pairing returns the pairing mark - if the mark is not registered, it returns NIL - if the mark is a left mark, it returns the right mark - if the mark is a right mark, it returns the left mark

func (Mark) Registered

func (p Mark) Registered() (registered bool)

Registered returns true if the mark is registered

type Pair

type Pair[TPayload constraints.Ordered] struct {
	L Cell[TPayload] `json:"l"`
	R Cell[TPayload] `json:"r"`
}

Pair - a pair of cells, generally used for pairing results

func (Pair[TPayload]) Between

func (p Pair[TPayload]) Between(val TPayload, leftInclusive, rightInclusive bool) bool

Between - check if the pair is between the given value

func (Pair[TPayload]) Inside

func (p Pair[TPayload]) Inside(val TPayload) bool

Inside - check if the pair is inside the given value

type Stack

type Stack[TPayload constraints.Ordered] struct {
	Results []Pair[TPayload]
	// contains filtered or unexported fields
}

Stack - a stack of cells, supporting mark pairings

func NewStack

func NewStack[TPayload constraints.Ordered](cap int) *Stack[TPayload]

NewStack - create a new stack

func (*Stack[TPayload]) Consume

func (s *Stack[TPayload]) Consume(mark Mark, payload TPayload,
	actionPairing delegate.Action2[bool, Pair[TPayload]],
) error

Consume - try consumes the top cell of the stack if the top cell's mark is pair with the given mark, then the top cell will be popped out and paired with the given cell and the paired cell will be pushed into the result list otherwise, the given cell will be pushed into the stack

actionPairing - action to be called when each input mark and payload are inputted, when the top cell's mark is pair with the given mark, the action will be called with true and the pair, and the given cell will be the right cell; otherwise, the action will be called with false and the pair, the given cell will be the left cell of the pair

func (*Stack[TPayload]) Len

func (s *Stack[TPayload]) Len() int

Len returns the length of the stack

func (*Stack[TPayload]) Peek

func (s *Stack[TPayload]) Peek() (cell Cell[TPayload], err error)

Peek returns the top cell of the stack

func (*Stack[TPayload]) PeekMark

func (s *Stack[TPayload]) PeekMark() (mark Mark, err error)

PeekMark returns the mark of top cell of the stack

func (*Stack[TPayload]) Pop

func (s *Stack[TPayload]) Pop() (cell Cell[TPayload], err error)

Pop returns the top cell of the stack

func (*Stack[TPayload]) Push

func (s *Stack[TPayload]) Push(mark Mark, val TPayload) error

Push only registered mark can be pushed into the stack

Jump to

Keyboard shortcuts

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