代码拉取完成,页面将自动刷新
package tools
import "errors"
var (
NoElement = errors.New("no element in queue")
QueueFull = errors.New("queue full")
)
type (
// Queue 工具库,循环队列
// TODO 使用模板重构
Queue struct {
maxsize int
head int
tail int
container []uint32
}
)
func (q *Queue) Init(capacity int) {
q.head = 0
q.tail = 0
q.maxsize = capacity + 1
q.container = make([]uint32, q.maxsize)
}
func (q *Queue) Capacity() int {
return q.maxsize - 1
}
func (q *Queue) Size() int {
return (q.tail - q.head + q.maxsize) % q.maxsize
}
func (q *Queue) Full() bool {
return (q.tail+1)%q.maxsize == q.head
}
func (q *Queue) Empty() bool {
return q.head == q.tail
}
func (q *Queue) Top() (uint32, error) {
if q.Empty() {
return 0, NoElement
}
return q.container[q.head], nil
}
func (q *Queue) Pop() (uint32, error) {
if q.Empty() {
return 0, NoElement
}
elem := q.container[q.head]
q.head = (q.head + 1) % q.maxsize
return elem, nil
}
func (q *Queue) Push(element uint32) error {
if q.Full() {
return QueueFull
}
q.container[q.tail] = element
q.tail = (q.tail + 1) % q.maxsize
return nil
}
func (q *Queue) UnInit() {
q.container = nil
q.tail = 0
q.tail = 0
q.maxsize = 0
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。