-
Notifications
You must be signed in to change notification settings - Fork 3
/
bounded_queue.go
53 lines (42 loc) · 972 Bytes
/
bounded_queue.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
50
51
52
53
package queue
import "fmt"
type BoundedQueue[T any] struct {
capacity int
queue *Queue[T]
}
func NewBoundedQueue[T any](capacity int) *BoundedQueue[T] {
q := make(Queue[T], 0, capacity)
bq := &BoundedQueue[T]{
capacity: capacity,
queue: &q,
}
return bq
}
func (bq *BoundedQueue[T]) IsFull() bool {
return bq.Len() == bq.Cap()
}
func (bq *BoundedQueue[T]) IsEmpty() bool {
return bq.queue.IsEmpty()
}
func (bq *BoundedQueue[T]) Cap() int {
return bq.capacity
}
func (bq *BoundedQueue[T]) Len() int {
return bq.queue.Len()
}
func (bq *BoundedQueue[T]) Enqueue(values ...T) error {
if len(values)+bq.queue.Len() > (*bq).capacity {
return fmt.Errorf("queue would overflow")
}
bq.queue.Enqueue(values...)
return nil
}
func (bq *BoundedQueue[T]) Dequeue() (T, error) {
return bq.queue.Dequeue()
}
func (bq *BoundedQueue[T]) Peek() (T, error) {
return bq.queue.Peek()
}
func (bq *BoundedQueue[T]) Values() []T {
return bq.queue.Values()
}