-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroundQueue.go
executable file
·103 lines (89 loc) · 2.02 KB
/
roundQueue.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package tools
import "fmt"
type RQueueDataItfc interface {
Equal(d RQueueDataItfc) bool
}
type RoundQueue struct {
slc []RQueueDataItfc
front, tail int // index
}
func (l *RoundQueue) Init(size int) bool {
if size <= 0 {
return false
}
l.slc = make([]RQueueDataItfc, size+1)
l.front = 0
l.tail = 0
return true
}
func (l *RoundQueue) Reset() {
l.front = 0
l.tail = 0
}
func (l *RoundQueue) Len() int { return (l.tail + len(l.slc) - l.front) % len(l.slc) }
func (l *RoundQueue) Full() bool { return (l.tail+1)%len(l.slc) == l.front }
func (l *RoundQueue) Empty() bool { return l.front == l.tail }
func (l *RoundQueue) Out() (RQueueDataItfc, bool) {
if l.Empty() {
return nil, false
}
v := l.slc[l.front]
l.front = (l.front + 1) % len(l.slc)
return v, true
}
func (l *RoundQueue) Push(v RQueueDataItfc) {
if l.Full() {
l.Out()
}
l.slc[l.tail] = v
l.tail = (l.tail + 1) % len(l.slc)
}
func (l *RoundQueue) PushUnique(v RQueueDataItfc) {
if d, ok := l.Top(); ok && d == v {
return
}
l.Del(v)
l.Push(v)
}
func (l *RoundQueue) Pop() (RQueueDataItfc, bool) {
if l.Empty() {
return nil, false
}
v := l.slc[l.tail]
l.tail = (l.tail + len(l.slc) - 1) % len(l.slc)
return v, true
}
func (l *RoundQueue) Top() (RQueueDataItfc, bool) {
if l.Empty() {
return nil, false
}
return l.slc[(l.tail+len(l.slc)-1)%len(l.slc)], true
}
func (l *RoundQueue) Del(v RQueueDataItfc) {
if l.Empty() {
return
}
var del bool
for i := l.front; i != l.tail; i = (i + 1) % len(l.slc) {
if del || l.slc[i].Equal(v) {
l.slc[i] = l.slc[(i+1)%len(l.slc)]
del = true
}
}
if del {
l.tail = (l.tail + len(l.slc) - 1) % len(l.slc)
}
}
// 取第0,1,2,....值
func (l *RoundQueue) Get(i int) (RQueueDataItfc, bool) {
if i < 0 || i >= l.Len() {
return nil, false
}
return l.slc[(l.front+i)%len(l.slc)], true
}
func (l *RoundQueue) Print() {
for i := l.front; i != l.tail; i = (i + 1) % len(l.slc) {
fmt.Printf("-%v", l.slc[i])
}
fmt.Println("\n", l.front, ",", l.tail, ",len:", l.Len(), ",size:", len(l.slc))
}