Debian Codegraph @

internal/buffer/stack.go

raw
1 package buffer
2
3 // TEStack is a simple implementation of a LIFO stack for text events
4 type TEStack struct {
5 Top *Element
6 Size int
7 }
8
9 // An Element which is stored in the Stack
10 type Element struct {
11 Value *TextEvent
12 Next *Element
13 }
14
15 // Len returns the stack's length
16 func (s *TEStack) Len() int {
17 return s.Size
18 }
19
20 // Push a new element onto the stack
21 func (s *TEStack) Push(value *TextEvent) {
22 s.Top = &Element{value, s.Top}
23 s.Size++
24 }
25
26 // Pop removes the top element from the stack and returns its value
27 // If the stack is empty, return nil
28 func (s *TEStack) Pop() (value *TextEvent) {
29 if s.Size > 0 {
30 value, s.Top = s.Top.Value, s.Top.Next
31 s.Size--
32 return
33 }
34 return nil
35 }
36
37 // Peek returns the top element of the stack without removing it
38 func (s *TEStack) Peek() *TextEvent {
39 if s.Size > 0 {
40 return s.Top.Value
41 }
42 return nil
43 }