aliyunpan/library/collection/queue.go

102 lines
1.8 KiB
Go
Raw Normal View History

2022-05-22 16:42:57 +08:00
package collection
2022-06-01 21:55:03 +08:00
import (
2022-06-04 13:20:33 +08:00
"strings"
2022-06-01 21:55:03 +08:00
"sync"
)
type QueueItem interface {
HashCode() string
}
2022-05-22 16:42:57 +08:00
type Queue struct {
queueList []interface{}
mutex sync.Mutex
}
func NewFifoQueue() *Queue {
return &Queue{}
}
func (q *Queue) Push(item interface{}) {
q.mutex.Lock()
defer q.mutex.Unlock()
if q.queueList == nil {
q.queueList = []interface{}{}
}
q.queueList = append(q.queueList, item)
}
2022-06-01 21:55:03 +08:00
func (q *Queue) PushUnique(item interface{}) {
q.mutex.Lock()
defer q.mutex.Unlock()
if q.queueList == nil {
q.queueList = []interface{}{}
} else {
for _, qItem := range q.queueList {
2022-06-04 13:20:33 +08:00
if strings.Compare(item.(QueueItem).HashCode(), qItem.(QueueItem).HashCode()) == 0 {
2022-06-01 21:55:03 +08:00
return
}
}
}
q.queueList = append(q.queueList, item)
}
2022-05-22 16:42:57 +08:00
func (q *Queue) Pop() interface{} {
q.mutex.Lock()
defer q.mutex.Unlock()
if q.queueList == nil {
q.queueList = []interface{}{}
}
if len(q.queueList) == 0 {
return nil
}
item := q.queueList[0]
q.queueList = q.queueList[1:]
return item
}
func (q *Queue) Length() int {
q.mutex.Lock()
defer q.mutex.Unlock()
return len(q.queueList)
}
2022-06-04 13:20:33 +08:00
func (q *Queue) Remove(item interface{}) {
q.mutex.Lock()
defer q.mutex.Unlock()
if q.queueList == nil {
q.queueList = []interface{}{}
}
if len(q.queueList) == 0 {
return
}
j := 0
for _, qItem := range q.queueList {
if strings.Compare(item.(QueueItem).HashCode(), qItem.(QueueItem).HashCode()) != 0 {
q.queueList[j] = qItem
j++
}
}
q.queueList = q.queueList[:j]
return
}
func (q *Queue) Contains(item interface{}) bool {
q.mutex.Lock()
defer q.mutex.Unlock()
if q.queueList == nil {
q.queueList = []interface{}{}
}
if len(q.queueList) == 0 {
return false
}
for _, qItem := range q.queueList {
if strings.Compare(item.(QueueItem).HashCode(), qItem.(QueueItem).HashCode()) == 0 {
return true
}
}
return false
}