-
Notifications
You must be signed in to change notification settings - Fork 522
/
queue.go
165 lines (140 loc) · 3.3 KB
/
queue.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package helper
import (
"sync"
"sync/atomic"
)
// QueueNode represents a node in the queue
type QueueNode interface {
Next() interface{}
SetNext(next interface{})
Data() interface{}
}
// Queue represetns a queue
type Queue struct {
count int64
head QueueNode
tail QueueNode
mux sync.Mutex
}
// Push add a node to queue
func (q *Queue) Push(node QueueNode) {
q.mux.Lock()
defer q.mux.Unlock()
q.PushWithoutLock(node)
}
// Pull get a node from queue
func (q *Queue) Pull(add *int64) QueueNode {
q.mux.Lock()
defer q.mux.Unlock()
node := q.PullWithOutLock()
if node == nil {
return nil
}
if add != nil {
atomic.AddInt64(add, 1)
}
return node
}
// PushWithoutLock is no lock mode of Push
func (q *Queue) PushWithoutLock(node QueueNode) {
if q.tail == nil {
q.head = node
q.tail = node
q.count = 1
} else {
q.tail.SetNext(node)
q.tail = node
q.count++
}
}
// PullWithOutLock is no lock mode of Pull
func (q *Queue) PullWithOutLock() QueueNode {
var node QueueNode
if q.head != nil {
node = q.head
q.head, _ = node.Next().(QueueNode)
if q.head == nil {
q.tail = nil
}
node.SetNext(nil)
q.count--
} else {
q.count = 0
}
return node
}
// CleanWithOutLock is no lock mode of Clean
func (q *Queue) CleanWithOutLock() {
q.count = 0
q.head = nil
q.tail = nil
}
// Clean remove all node on queue
func (q *Queue) Clean() {
q.mux.Lock()
defer q.mux.Unlock()
q.CleanWithOutLock()
}
// GetCountWithOutLock is no lock mode of GetCount
func (q *Queue) GetCountWithOutLock() int64 {
return q.count
}
// GetCount get the node count
func (q *Queue) GetCount() int64 {
q.mux.Lock()
defer q.mux.Unlock()
return q.count
}
// NewQueue create and init a new Queue
func NewQueue() *Queue {
return &Queue{
count: int64(0),
head: nil,
tail: nil,
mux: sync.Mutex{},
}
}
// QueueIteratorNode implements the helper.Iterator interface with ability to accept new item when being iterated
type QueueIteratorNode struct {
next QueueNode
data interface{}
}
// Next return the next node
func (q *QueueIteratorNode) Next() interface{} {
if q.next == nil {
return nil
}
return q.next
}
// SetNext updates the next pointer of the node
func (q *QueueIteratorNode) SetNext(next interface{}) {
if next == nil {
q.next = nil
} else {
q.next, _ = next.(QueueNode)
}
}
// Data returns data of the node
func (q *QueueIteratorNode) Data() interface{} {
return q.data
}
// NewQueueIteratorNode creates a new QueueIteratorNode
func NewQueueIteratorNode(data interface{}) *QueueIteratorNode {
return &QueueIteratorNode{
data: data,
}
}