mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-12-20 09:00:04 +08:00
chore: cleanup queue code
This commit is contained in:
parent
26335f8ecc
commit
d6f1af5372
@ -2,8 +2,6 @@ package queue
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/samber/lo"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Queue is a simple concurrent safe queue
|
// Queue is a simple concurrent safe queue
|
||||||
@ -24,33 +22,32 @@ func (q *Queue[T]) Put(items ...T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Pop returns the head of items.
|
// Pop returns the head of items.
|
||||||
func (q *Queue[T]) Pop() T {
|
func (q *Queue[T]) Pop() (head T) {
|
||||||
if len(q.items) == 0 {
|
if len(q.items) == 0 {
|
||||||
return lo.Empty[T]()
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
q.lock.Lock()
|
q.lock.Lock()
|
||||||
head := q.items[0]
|
head = q.items[0]
|
||||||
q.items = q.items[1:]
|
q.items = q.items[1:]
|
||||||
q.lock.Unlock()
|
q.lock.Unlock()
|
||||||
return head
|
return head
|
||||||
}
|
}
|
||||||
|
|
||||||
// Last returns the last of item.
|
// Last returns the last of item.
|
||||||
func (q *Queue[T]) Last() T {
|
func (q *Queue[T]) Last() (last T) {
|
||||||
if len(q.items) == 0 {
|
if len(q.items) == 0 {
|
||||||
return lo.Empty[T]()
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
q.lock.RLock()
|
q.lock.RLock()
|
||||||
last := q.items[len(q.items)-1]
|
last = q.items[len(q.items)-1]
|
||||||
q.lock.RUnlock()
|
q.lock.RUnlock()
|
||||||
return last
|
return last
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy get the copy of queue.
|
// Copy get the copy of queue.
|
||||||
func (q *Queue[T]) Copy() []T {
|
func (q *Queue[T]) Copy() (items []T) {
|
||||||
items := []T{}
|
|
||||||
q.lock.RLock()
|
q.lock.RLock()
|
||||||
items = append(items, q.items...)
|
items = append(items, q.items...)
|
||||||
q.lock.RUnlock()
|
q.lock.RUnlock()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user