-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.go
67 lines (54 loc) · 1.24 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
// Package stack provides thread and type safe stack implementation.
package stack
import "sync"
// Stack struct stores stack elements in a slice of T
type Stack[T comparable] struct {
mu sync.Mutex
values []T
}
// New returns an empty stack of type T
func New[T comparable]() *Stack[T] {
return &Stack[T]{
values: nil,
}
}
// Push inserts v of type T to the top of the stack.
func (s *Stack[T]) Push(v T) {
s.mu.Lock()
defer s.mu.Unlock()
s.values = append(s.values, v)
}
// Pop removes and return the ast inserted element from the stack.
func (s *Stack[T]) Pop() (T, bool) {
s.mu.Lock()
defer s.mu.Unlock()
if s.IsEmpty() {
var zeroValue T
return zeroValue, false
}
i := s.Size() - 1
top := s.values[i]
s.values = s.values[:i]
return top, true
}
// Top returns the last inserted element from the stack.
func (s *Stack[T]) Top() (T, bool) {
s.mu.Lock()
defer s.mu.Unlock()
if s.IsEmpty() {
var zeroValue T
return zeroValue, false
}
return s.values[s.Size()-1], true
}
// Size returns the number of elements in the stack
func (s *Stack[T]) Size() int {
return len(s.values)
}
// IsEmpty checks if there are elements stored in the stack
func (s *Stack[T]) IsEmpty() bool {
if s.Size() > 0 {
return false
}
return true
}