-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.go
49 lines (41 loc) · 1023 Bytes
/
array.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package queue
type ArrayQueue[T any] struct {
queue []T
current int32
}
// NewArrayQueue returns a new queue with the provided elements.
func NewArrayQueue[T any](elems []T) *ArrayQueue[T] {
current := int32(0)
if len(elems) > 0 {
current = int32(len(elems))
}
return &ArrayQueue[T]{queue: elems, current: current}
}
func (a *ArrayQueue[T]) Push(item T) error {
a.queue = append(a.queue, item)
a.current++
return nil
}
func (a *ArrayQueue[T]) Pop() (T, error) {
if a.current == 0 {
var zeroValue T
return zeroValue, nil
}
a.current--
return a.queue[a.current], nil
}
func (a *ArrayQueue[T]) Len() int32 {
return a.current
}
func (a *ArrayQueue[T]) Enqueue(elems []T) error {
copy(a.queue[a.current:], elems)
return nil
}
func (a *ArrayQueue[T]) Dequeue(count int32) ([]T, error) {
available := min(count, a.Len())
elems := a.queue[a.current-available : a.current]
a.current -= available
return elems, nil
}
var _ Queue[any] = &ArrayQueue[any]{}
var _ WorkQueue[any] = &ArrayQueue[any]{}