-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_array_stack.go
77 lines (63 loc) · 1.36 KB
/
sync_array_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
package stacks
import "sync"
type SyncArrayStack[T any] struct {
stack *ArrayStack[T]
lock sync.RWMutex
}
var _ Stack[any] = &SyncArrayStack[any]{}
func NewSyncArrayStack[T any]() *SyncArrayStack[T] {
return &SyncArrayStack[T]{
stack: NewArrayStack[T](),
lock: sync.RWMutex{},
}
}
func (x *SyncArrayStack[T]) Push(values ...T) {
x.lock.Lock()
defer x.lock.Unlock()
x.stack.Push(values...)
}
func (x *SyncArrayStack[T]) Pop() T {
x.lock.Lock()
defer x.lock.Unlock()
return x.stack.Pop()
}
func (x *SyncArrayStack[T]) PopE() (T, error) {
x.lock.Lock()
defer x.lock.Unlock()
return x.stack.PopE()
}
func (x *SyncArrayStack[T]) Peek() T {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.Peek()
}
func (x *SyncArrayStack[T]) PeekE() (T, error) {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.PeekE()
}
func (x *SyncArrayStack[T]) IsEmpty() bool {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.IsEmpty()
}
func (x *SyncArrayStack[T]) IsNotEmpty() bool {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.IsNotEmpty()
}
func (x *SyncArrayStack[T]) Size() int {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.Size()
}
func (x *SyncArrayStack[T]) Clear() {
x.lock.Lock()
defer x.lock.Unlock()
x.stack.Clear()
}
func (x *SyncArrayStack[T]) String() string {
x.lock.RLock()
defer x.lock.RUnlock()
return x.stack.String()
}