forked from remind101/empire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.go
47 lines (39 loc) · 969 Bytes
/
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
package newrelic
import "sync"
// rootSegment is used as the parentID for root segments.
const rootSegment int64 = 0
type SegmentStack struct {
sync.Mutex
s []int64
}
func NewSegmentStack() *SegmentStack {
return &SegmentStack{s: []int64{}}
}
// Push pushes a segment id onto the segment stack.
func (s *SegmentStack) Push(id int64) {
s.Lock()
defer s.Unlock()
s.s = append(s.s, id)
}
// Pop pops a segment id off of the segment stack. It returns false if the stack is empty.
func (s *SegmentStack) Pop() (int64, bool) {
s.Lock()
defer s.Unlock()
if s.Len() == 0 {
return rootSegment, false
}
id := s.s[s.Len()-1]
s.s = s.s[:s.Len()-1]
return id, true
}
// Peek returns id from the top of the stack. It returns rootSegment if the stack is empty.
func (s *SegmentStack) Peek() int64 {
if s.Len() == 0 {
return rootSegment
}
return s.s[s.Len()-1]
}
// Len returns the length of the stack.
func (s *SegmentStack) Len() int {
return len(s.s)
}