Documentation ¶
Index ¶
- Variables
- func IsPair[T ~string](begin, end T) bool
- type Cell
- type Mark
- type Pair
- type Stack
- func (s *Stack[TPayload]) Consume(mark Mark, payload TPayload, ...) error
- func (s *Stack[TPayload]) Len() int
- func (s *Stack[TPayload]) Peek() (cell Cell[TPayload], err error)
- func (s *Stack[TPayload]) PeekMark() (mark Mark, err error)
- func (s *Stack[TPayload]) Pop() (cell Cell[TPayload], err error)
- func (s *Stack[TPayload]) Push(mark Mark, val TPayload) error
Constants ¶
This section is empty.
Variables ¶
var ( ErrInsufficientStackLen = errors.New("insufficient stack length") ErrMarkAreNotRegistered = errors.New("mark are not registered") )
Functions ¶
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
func (Mark) PairedWith ¶
PairedWith returns true if the mark is paired with the given mark
func (Mark) Pairing ¶
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 ¶
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
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