1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 08:32:40 +08:00
This commit is contained in:
2022-05-05 00:24:28 +08:00
parent d59666d9e6
commit d0e055016b
3 changed files with 57 additions and 6 deletions

View File

@ -2,6 +2,8 @@ package collections
import "sync"
var _ Queue[string] = &ArrayQueue[string]{}
// 数组队列,先进先出
type ArrayQueue[T any] struct {
array []T // 底层切片
@ -14,7 +16,7 @@ func NewArrayQueue[T any]() *ArrayQueue[T] {
}
// 入队
func (q *ArrayQueue[T]) Add(v T) {
func (q *ArrayQueue[T]) Put(v T) {
q.lock.Lock()
defer q.lock.Unlock()
@ -26,7 +28,7 @@ func (q *ArrayQueue[T]) Add(v T) {
}
// 出队
func (q *ArrayQueue[T]) Remove() any {
func (q *ArrayQueue[T]) Poll() T {
q.lock.Lock()
defer q.lock.Unlock()
@ -60,6 +62,10 @@ func (q *ArrayQueue[T]) Remove() any {
return v
}
func (q *ArrayQueue[T]) Peek() T {
return q.array[0]
}
// 栈大小
func (q *ArrayQueue[T]) Size() int {
return q.size