File: stack.go

package info (click to toggle)
micro 2.0.15-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,128 kB
  • sloc: sh: 265; makefile: 77; xml: 53
file content (43 lines) | stat: -rw-r--r-- 849 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package buffer

// TEStack is a simple implementation of a LIFO stack for text events
type TEStack struct {
	Top  *Element
	Size int
}

// An Element which is stored in the Stack
type Element struct {
	Value *TextEvent
	Next  *Element
}

// Len returns the stack's length
func (s *TEStack) Len() int {
	return s.Size
}

// Push a new element onto the stack
func (s *TEStack) Push(value *TextEvent) {
	s.Top = &Element{value, s.Top}
	s.Size++
}

// Pop removes the top element from the stack and returns its value
// If the stack is empty, return nil
func (s *TEStack) Pop() (value *TextEvent) {
	if s.Size > 0 {
		value, s.Top = s.Top.Value, s.Top.Next
		s.Size--
		return
	}
	return nil
}

// Peek returns the top element of the stack without removing it
func (s *TEStack) Peek() *TextEvent {
	if s.Size > 0 {
		return s.Top.Value
	}
	return nil
}