]> git.lizzy.rs Git - micro.git/blob - cmd/micro/stack.go
Code optimisation (#1117)
[micro.git] / cmd / micro / stack.go
1 package main
2
3 // Stack is a simple implementation of a LIFO stack for text events
4 type Stack 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 *Stack) Len() int {
17         return s.Size
18 }
19
20 // Push a new element onto the stack
21 func (s *Stack) 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 *Stack) 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 *Stack) Peek() *TextEvent {
39         if s.Size > 0 {
40                 return s.Top.Value
41         }
42         return nil
43 }