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