-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
stack.go
54 lines (42 loc) · 912 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
// stack.go holds a simple stack which can hold ints.
//
// (This is used solely for the GOSUB/RETURN handler.)
package eval
import (
"errors"
"sync"
)
// Stack holds the stack-data, protected by a mutex
type Stack struct {
lock sync.Mutex
s []int
}
// NewStack returns a new stack (for holding integers)
func NewStack() *Stack {
return &Stack{sync.Mutex{}, make([]int, 0)}
}
// Push adds a new item to our stack.
func (s *Stack) Push(v int) {
s.lock.Lock()
defer s.lock.Unlock()
s.s = append(s.s, v)
}
// Pop returns an item from our stack.
func (s *Stack) Pop() (int, error) {
s.lock.Lock()
defer s.lock.Unlock()
l := len(s.s)
if l == 0 {
return 0, errors.New("Empty Stack")
}
res := s.s[l-1]
s.s = s.s[:l-1]
return res, nil
}
// Empty returns `true` if our stack is empty.
func (s *Stack) Empty() bool {
s.lock.Lock()
defer s.lock.Unlock()
l := len(s.s)
return (l == 0)
}