2018-08-27 22:53:10 +03:00
|
|
|
package buffer
|
2016-03-20 01:16:10 +03:00
|
|
|
|
2018-08-26 06:06:44 +03:00
|
|
|
// TEStack is a simple implementation of a LIFO stack for text events
|
|
|
|
type TEStack struct {
|
2016-05-29 18:02:56 +03:00
|
|
|
Top *Element
|
|
|
|
Size int
|
2016-03-20 01:16:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// An Element which is stored in the Stack
|
|
|
|
type Element struct {
|
2016-05-29 18:02:56 +03:00
|
|
|
Value *TextEvent
|
|
|
|
Next *Element
|
2016-03-20 01:16:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the stack's length
|
2018-08-26 06:06:44 +03:00
|
|
|
func (s *TEStack) Len() int {
|
2016-05-29 18:02:56 +03:00
|
|
|
return s.Size
|
2016-03-20 01:16:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push a new element onto the stack
|
2018-08-26 06:06:44 +03:00
|
|
|
func (s *TEStack) Push(value *TextEvent) {
|
2016-05-29 18:02:56 +03:00
|
|
|
s.Top = &Element{value, s.Top}
|
|
|
|
s.Size++
|
2016-03-20 01:16:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pop removes the top element from the stack and returns its value
|
|
|
|
// If the stack is empty, return nil
|
2018-08-26 06:06:44 +03:00
|
|
|
func (s *TEStack) Pop() (value *TextEvent) {
|
2016-05-29 18:02:56 +03:00
|
|
|
if s.Size > 0 {
|
|
|
|
value, s.Top = s.Top.Value, s.Top.Next
|
|
|
|
s.Size--
|
2016-03-20 01:16:10 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-04-01 16:54:10 +03:00
|
|
|
|
|
|
|
// Peek returns the top element of the stack without removing it
|
2018-08-26 06:06:44 +03:00
|
|
|
func (s *TEStack) Peek() *TextEvent {
|
2016-05-29 18:02:56 +03:00
|
|
|
if s.Size > 0 {
|
|
|
|
return s.Top.Value
|
2016-04-01 16:54:10 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|