-
Notifications
You must be signed in to change notification settings - Fork 2
/
stack.go
103 lines (88 loc) · 1.88 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
93
94
95
96
97
98
99
100
101
102
103
package threadsafe
import (
"sync"
"github.com/golang-collections/collections/stack"
)
// Stack is a thread-safe stack.
type Stack struct {
s *stack.Stack
mu sync.Mutex
}
// NewStack creates a new thread-safe stack.
func NewStack() *Stack {
return &Stack{
s: stack.New(),
}
}
// Push adds an element to the stack.
func (s *Stack) Push(value interface{}) {
s.mu.Lock()
defer s.mu.Unlock()
s.s.Push(value)
}
// Pop removes and returns an element from the stack.
func (s *Stack) Pop() (interface{}, bool) {
s.mu.Lock()
defer s.mu.Unlock()
if s.s.Len() == 0 {
return nil, false
}
return s.s.Pop(), true
}
// Len returns the number of elements in the stack.
func (s *Stack) Len() int {
s.mu.Lock()
defer s.mu.Unlock()
return s.s.Len()
}
// Peek returns the element at the top of the stack without removing it.
// Example:
//
// value, ok := s.Peek()
func (s *Stack) Peek() (interface{}, bool) {
s.mu.Lock()
defer s.mu.Unlock()
if s.s.Len() == 0 {
return nil, false
}
return s.s.Peek(), true
}
// IsEmpty checks if the stack is empty.
// Example:
//
// isEmpty := s.IsEmpty()
func (s *Stack) IsEmpty() bool {
s.mu.Lock()
defer s.mu.Unlock()
return s.s.Len() == 0
}
// Clear removes all elements from the stack.
// Example:
//
// s.Clear()
func (s *Stack) Clear() {
s.mu.Lock()
defer s.mu.Unlock()
s.s = stack.New()
}
// Values returns a slice of all elements in the stack.
// Example:
//
// values := s.Values()
func (s *Stack) Values() []interface{} {
s.mu.Lock()
defer s.mu.Unlock()
// Create a temporary slice to hold the values
values := make([]interface{}, 0, s.s.Len())
// Temporarily pop all elements to capture them
length := s.s.Len()
for i := 0; i < length; i++ {
value := s.s.Pop()
values = append(values, value)
}
// Push the elements back to restore the original state
for i := len(values) - 1; i >= 0; i-- {
s.s.Push(values[i])
}
return values
}