-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_min_stack.go
89 lines (73 loc) · 1.58 KB
/
sync_min_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
package stacks
import "sync"
type SyncMinStack[T any] struct {
stack *MinStack[T]
lock sync.RWMutex
}
var _ Stack[any] = &SyncMinStack[any]{}
func NewSyncMinStack[T any](comparator Comparator[T]) *SyncMinStack[T] {
return &SyncMinStack[T]{
stack: NewMinStack[T](comparator),
lock: sync.RWMutex{},
}
}
func (x *SyncMinStack[T]) Push(values ...T) {
x.lock.Lock()
defer x.lock.Unlock()
x.stack.Push(values...)
}
func (x *SyncMinStack[T]) Pop() T {
x.lock.Lock()
defer x.lock.Unlock()
return x.stack.Pop()
}
func (x *SyncMinStack[T]) PopE() (T, error) {
x.lock.Lock()
defer x.lock.Unlock()
return x.stack.PopE()
}
func (x *SyncMinStack[T]) Peek() T {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.Peek()
}
func (x *SyncMinStack[T]) PeekE() (T, error) {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.PeekE()
}
func (x *SyncMinStack[T]) IsEmpty() bool {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.IsEmpty()
}
func (x *SyncMinStack[T]) IsNotEmpty() bool {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.IsNotEmpty()
}
func (x *SyncMinStack[T]) Size() int {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.Size()
}
func (x *SyncMinStack[T]) Clear() {
x.lock.Lock()
defer x.lock.Unlock()
x.stack.Clear()
}
func (x *SyncMinStack[T]) String() string {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.String()
}
func (x *SyncMinStack[T]) GetMin() T {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.GetMin()
}
func (x *SyncMinStack[T]) GetMinE() (T, error) {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.GetMinE()
}