-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.go
59 lines (46 loc) · 910 Bytes
/
stack.go
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
//
// The stack provides a way of switching views/levels/etc
// You push on to the stack the state you want to execute
// and once done you can remove it. Useful for things like
// pause screens or popup menus.
//
import (
"errors"
"sync"
)
type stack struct {
lock sync.Mutex
s []interface{}
}
func NewStack() *stack {
return &stack{sync.Mutex{}, make([]interface{}, 0)}
}
func (s stack) Peek() interface{} {
if s.Len() == 0 {
return nil
}
return s.s[s.Len()-1]
}
func (s stack) Len() int {
return len(s.s)
}
func (s stack) Empty() bool {
return s.Len() == 0
}
func (s *stack) Push(v interface{}) {
s.lock.Lock()
defer s.lock.Unlock()
s.s = append(s.s, v)
}
func (s *stack) Pop() (interface{}, error) {
s.lock.Lock()
defer s.lock.Unlock()
l := len(s.s)
if l == 0 {
return nil, errors.New("empty stack")
}
res := s.s[l-1]
s.s = s.s[:l-1]
return res, nil
}