-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.go
49 lines (40 loc) · 874 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
package inducedates
import (
"errors"
"sync"
)
// NodeStack makes a node stack for pushing and pulling.
type NodeStack struct {
lock sync.Mutex // you don't have to do this if you don't want thread safety
s []*Node
}
// NewNodeStack returns a pointer to a node stack
func NewNodeStack() *NodeStack {
return &NodeStack{sync.Mutex{}, make([]*Node, 0)}
}
// Push push the node into the stack
func (s *NodeStack) Push(v *Node) {
s.lock.Lock()
defer s.lock.Unlock()
s.s = append(s.s, v)
}
// Empty is the node stack empty?
func (s *NodeStack) Empty() (ret bool) {
ret = true
if len(s.s) > 0 {
ret = false
}
return
}
// Pop a node off the stack
func (s *NodeStack) Pop() (*Node, error) {
s.lock.Lock()
defer s.lock.Unlock()
l := len(s.s)
if l == 0 {
return nil, errors.New("Empty NodeStack")
}
res := s.s[l-1]
s.s = s.s[:l-1]
return res, nil
}