-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
65 lines (51 loc) · 1.1 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
package queue
import (
"github.com/zytekaron/structs"
"github.com/zytekaron/structs/list"
"golang.org/x/exp/constraints"
)
// Queue is an implementation of a double-ended queue
// backed by *list.List
type Queue[V any] struct {
data *list.List[V]
}
func New[V any](eq structs.EqualFunc[V]) *Queue[V] {
return &Queue[V]{
data: list.New[V](eq),
}
}
func NewOrdered[V constraints.Ordered]() *Queue[V] {
return &Queue[V]{
data: list.NewOrdered[V](),
}
}
func (q *Queue[V]) IsEmpty() bool {
return q.data.IsEmpty()
}
func (q *Queue[V]) Enqueue(value V) {
q.data.Push(value)
}
func (q *Queue[V]) Peek() V {
return q.data.Peek()
}
func (q *Queue[V]) Dequeue() V {
return q.data.Pop()
}
func (q *Queue[V]) Contains(value V) bool {
return q.data.Contains(value)
}
func (q *Queue[V]) Iterator() structs.Iterator[V] {
return q.data.Iterator()
}
func (q *Queue[V]) DescendingIterator() structs.Iterator[V] {
return q.data.DescendingIterator()
}
func (q *Queue[V]) Size() int {
return q.data.Size()
}
func (q *Queue[V]) Clear() {
q.data.Clear()
}
func (q *Queue[V]) Values() []V {
return q.data.Values()
}