forked from knownsec/ksubdomain
/
stack.go
58 lines (50 loc) · 951 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
48
49
50
51
52
53
54
55
56
57
58
package core
import (
"errors"
"sync"
)
type (
Stack struct {
top *node
length int
lock sync.RWMutex
}
node struct {
value uint32
prev *node
}
)
var LocalStack *Stack
// Create a new stack
func NewStack() *Stack {
return &Stack{nil, 0, sync.RWMutex{}}
}
// Return the number of items in the stack
func (this *Stack) Len() int {
return this.length
}
func (this *Stack) Empty() bool {
this.lock.Lock()
defer this.lock.Unlock()
return this.length > 0
}
// Pop the top item of the stack and return it
func (this *Stack) Pop() (uint32, error) {
this.lock.Lock()
defer this.lock.Unlock()
if this.length == 0 {
return 0, errors.New("Stack length is empty")
}
n := this.top
this.top = n.prev
this.length--
return n.value, nil
}
// Push a value onto the top of the stack
func (this *Stack) Push(value uint32) {
this.lock.Lock()
defer this.lock.Unlock()
n := &node{value, this.top}
this.top = n
this.length++
}