Skip to content

Commit cd07f0e

Browse files
committed
updates
1 parent 45cc261 commit cd07f0e

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

queue.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ const (
2121
// Queue implements a FIFO data structure that can support a few priorities.
2222
type Queue interface {
2323
// Append adds the data to the Queue at priority level PriorityNormal.
24-
Append(data interface{})
24+
Append(data any)
2525

2626
// AppendPriority adds the data to the Queue with respect to priority.
27-
AppendPriority(data interface{}, priority QueuePriority)
27+
AppendPriority(data any, priority QueuePriority)
2828

2929
// Signal returns the Queue signal channel.
3030
Signal() <-chan struct{}
3131

3232
// Next returns the data at the front of the Queue.
33-
Next() (interface{}, bool)
33+
Next() (any, bool)
3434

3535
// Process will execute the callback parameter for each element on the Queue.
36-
Process(callback func(interface{}))
36+
Process(callback func(any))
3737

3838
// Empty returns true if the Queue is empty.
3939
Empty() bool
@@ -45,10 +45,10 @@ type Queue interface {
4545
type queue struct {
4646
sync.Mutex
4747
signal chan struct{}
48-
low []interface{}
49-
norm []interface{}
50-
high []interface{}
51-
crit []interface{}
48+
low []any
49+
norm []any
50+
high []any
51+
crit []any
5252
}
5353

5454
// NewQueue returns an initialized Queue.
@@ -57,16 +57,16 @@ func NewQueue() Queue {
5757
}
5858

5959
// Append implements the Queue interface.
60-
func (q *queue) Append(data interface{}) {
60+
func (q *queue) Append(data any) {
6161
q.append(data, PriorityNormal)
6262
}
6363

6464
// AppendPriority implements the Queue interface.
65-
func (q *queue) AppendPriority(data interface{}, priority QueuePriority) {
65+
func (q *queue) AppendPriority(data any, priority QueuePriority) {
6666
q.append(data, priority)
6767
}
6868

69-
func (q *queue) append(data interface{}, priority QueuePriority) {
69+
func (q *queue) append(data any, priority QueuePriority) {
7070
q.Lock()
7171
defer q.Unlock()
7272

@@ -126,22 +126,26 @@ func (q *queue) drain() {
126126
}
127127

128128
// Next implements the Queue interface.
129-
func (q *queue) Next() (interface{}, bool) {
129+
func (q *queue) Next() (any, bool) {
130130
q.Lock()
131131
defer q.Unlock()
132132

133-
var data interface{}
133+
var data any
134134
if len(q.crit) > 0 {
135135
data = q.crit[0]
136+
q.crit[0] = nil // prevent memory leak
136137
q.crit = q.crit[1:]
137138
} else if len(q.high) > 0 {
138139
data = q.high[0]
140+
q.high[0] = nil
139141
q.high = q.high[1:]
140142
} else if len(q.norm) > 0 {
141143
data = q.norm[0]
144+
q.norm[0] = nil
142145
q.norm = q.norm[1:]
143146
} else if len(q.low) > 0 {
144147
data = q.low[0]
148+
q.low[0] = nil
145149
q.low = q.low[1:]
146150
} else {
147151
q.drain()
@@ -153,7 +157,7 @@ func (q *queue) Next() (interface{}, bool) {
153157
}
154158

155159
// Process implements the Queue interface.
156-
func (q *queue) Process(callback func(interface{})) {
160+
func (q *queue) Process(callback func(any)) {
157161
element, ok := q.Next()
158162

159163
for ok {

0 commit comments

Comments
 (0)