-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathqueue.go
46 lines (38 loc) · 1.13 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
package lane
// Queue is a First In First Out data structure implementation.
//
// Built upon a Deque container, its API focuses on the following core
// functionalities: Enqueue, Dequeue, Head, Size, Empty.
//
// Every operation has a time complexity of *O(1)*.
//
// Every operation over an instantiated Queue are goroutine-safe.
type Queue[T any] struct {
container *Deque[T]
}
// NewQueue produces a new Queue instance.
func NewQueue[T any](items ...T) *Queue[T] {
deque := NewDeque[T]()
for _, item := range items {
deque.container.PushFront(item)
}
return &Queue[T]{
container: deque,
}
}
// Enqueue adds an item at the back of the Queue in *O(1)* time complexity.
func (q *Queue[T]) Enqueue(item T) {
q.container.Prepend(item)
}
// Dequeue removes and returns the Queue's front item in *O(1)* time complexity.
func (q *Queue[T]) Dequeue() (item T, ok bool) {
return q.container.Pop()
}
// Head returns the Queue's front queue item in *O(1)* time complexity.
func (q *Queue[T]) Head() (item T, ok bool) {
return q.container.Last()
}
// Size returns the size of the Queue.
func (q *Queue[T]) Size() uint {
return q.container.Size()
}