-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint.go
51 lines (41 loc) · 785 Bytes
/
int.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
package filo
import (
"sync"
)
// IntStack integer stack first in last out
// safe for concurrent usage
type IntStack struct {
items []int
mu *sync.RWMutex
}
// Push pushes new item to the stack
func (i *IntStack) Push(j int) {
i.mu.Lock()
i.items = append(i.items, j)
i.mu.Unlock()
}
// Pop pops the last string from the stack
func (i *IntStack) Pop() int {
i.mu.Lock()
defer i.mu.Unlock()
ln := len(i.items)
if ln == 0 {
return 0
}
tail := i.items[ln-1]
i.items = i.items[:ln-1]
return tail
}
// Len gets the number of items pushed
// into the stack
func (i *IntStack) Len() int {
i.mu.RLock()
defer i.mu.RUnlock()
return len(i.items)
}
// NewIntStack creates new IntStack
func NewIntStack() *IntStack {
return &IntStack{
mu: &sync.RWMutex{},
}
}