-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_max_stack.go
89 lines (73 loc) · 1.58 KB
/
sync_max_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 SyncMaxStack[T any] struct {
stack *MaxStack[T]
lock sync.RWMutex
}
var _ Stack[any] = &SyncMaxStack[any]{}
func NewSyncMaxStack[T any](comparator Comparator[T]) *SyncMaxStack[T] {
return &SyncMaxStack[T]{
stack: NewMaxStack[T](comparator),
lock: sync.RWMutex{},
}
}
func (x *SyncMaxStack[T]) Push(values ...T) {
x.lock.Lock()
defer x.lock.Unlock()
x.stack.Push(values...)
}
func (x *SyncMaxStack[T]) Pop() T {
x.lock.Lock()
defer x.lock.Unlock()
return x.stack.Pop()
}
func (x *SyncMaxStack[T]) PopE() (T, error) {
x.lock.Lock()
defer x.lock.Unlock()
return x.stack.PopE()
}
func (x *SyncMaxStack[T]) Peek() T {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.Peek()
}
func (x *SyncMaxStack[T]) PeekE() (T, error) {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.PeekE()
}
func (x *SyncMaxStack[T]) IsEmpty() bool {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.IsEmpty()
}
func (x *SyncMaxStack[T]) IsNotEmpty() bool {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.IsNotEmpty()
}
func (x *SyncMaxStack[T]) Size() int {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.Size()
}
func (x *SyncMaxStack[T]) Clear() {
x.lock.Lock()
defer x.lock.Unlock()
x.stack.Clear()
}
func (x *SyncMaxStack[T]) String() string {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.String()
}
func (x *SyncMaxStack[T]) GetMax() T {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.GetMax()
}
func (x *SyncMaxStack[T]) GetMaxE() (T, error) {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.GetMaxE()
}