1 Star 0 Fork 0

magicianlyx/GoLog

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
queue.go 963 Bytes
Copy Edit Raw Blame History
magicianlyx authored 2022-01-24 17:45 +08:00 . -
package structure
import (
"sync"
)
type Queue struct {
l sync.RWMutex
list []interface{}
size int
}
func NewQueue(size int) *Queue {
if size <= 0 {
size = 10
}
return &Queue{
l: sync.RWMutex{},
list: make([]interface{}, size),
size: size,
}
}
func (q *Queue) Push(v ...interface{}) {
q.l.Lock()
defer q.l.Unlock()
if len(q.list) >= q.size {
q.list = q.list[1:]
}
q.list = append(q.list, v)
}
func (q *Queue) Pop() interface{} {
q.l.Lock()
defer q.l.Unlock()
if len(q.list) >= 1 {
o := q.list[0]
q.list = q.list[1:]
return o
} else {
return nil
}
}
func (q *Queue) Peek() interface{} {
q.l.RLock()
defer q.l.RUnlock()
if len(q.list) >= 1 {
return q.list[0]
} else {
return nil
}
}
func (q *Queue) Clone() *Queue {
list := make([]interface{}, q.size)
q.l.RLock()
for i := range q.list {
list[i] = q.list[i]
}
q.l.RUnlock()
return &Queue{
l: sync.RWMutex{},
list: list,
size: q.size,
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/magicianlyx/GoLog.git
git@gitee.com:magicianlyx/GoLog.git
magicianlyx
GoLog
GoLog
20c45f9b998d

Search