-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.go
92 lines (79 loc) · 1.5 KB
/
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package stack
import "container/list"
// Stack is describe as a list with lock
type Stack struct {
sem chan int
list *list.List
}
// CallbackFunc type describe any operation on each element in stack.
type CallbackFunc func(val interface{}) bool
// New create a new stack.
func New() *Stack {
sem := make(chan int, 1)
list := list.New()
return &Stack{sem, list}
}
// Push item to stack.
func (s *Stack) Push(value interface{}) {
s.sem <- 1
s.list.PushBack(value)
<-s.sem
}
// Pop item from stack.
func (s *Stack) Pop() interface{} {
s.sem <- 1
e := s.list.Back()
if e != nil {
s.list.Remove(e)
}
<-s.sem
if e != nil {
return e.Value
} else {
return nil
}
}
// Peak get the top item of the stack.
func (s *Stack) Peak() interface{} {
e := s.list.Back()
if e != nil {
return e.Value
}
return nil
}
// Len get the length of the stack.
func (s *Stack) Len() int {
return s.list.Len()
}
// Empty tests if the stack is empty.
func (s *Stack) Empty() bool {
return s.list.Len() == 0
}
// Map returns the first element in the stack causing mapFunc returns true.
func (s *Stack) Map(mapFunc CallbackFunc) interface{} {
s.sem <- 1
e := s.list.Front()
for e != nil {
if mapFunc(e.Value) {
<-s.sem
return e.Value
}
e = e.Next()
}
<-s.sem
return nil
}
// Contain tests if this item in the stack.
func (s *Stack) Contain(val interface{}) bool {
s.sem <- 1
e := s.list.Front()
for e != nil {
if e.Value == val {
<-s.sem
return true
}
e = e.Next()
}
<-s.sem
return false
}