-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.go
More file actions
154 lines (135 loc) · 4 KB
/
Copy pathqueue.go
File metadata and controls
154 lines (135 loc) · 4 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package linkedlistqueue
import (
"github.com/golanglibs/gocollections/generic"
"github.com/golanglibs/gocollections/list/doublylinkedlist"
)
/*
Linked list based queue. First element to be enqueued will be dequeued first (FIFO).
It uses gocollections/list/doublylinkedlist to perform queue operations.
Implements Queuer and Collectioner.
Queue is not thread safe
*/
type Queue[T any] struct {
container doublylinkedlist.DoublyLinkedList[T]
}
/*
Creates a new instance of Queue with the given elements with a default equality comparer
and returns pointer to the instance.
If no elements are given, then an empty queue is created. Elements must be comparable
*/
func New[K comparable](elements ...K) Queue[K] {
return Queue[K]{
container: doublylinkedlist.New(elements...),
}
}
/*
Creates a new instance of Queue with the given elements with nil equality comparer
and returns pointer to the instance.
If no elements are given, then an empty queue is created. Elements can be of any type
*/
func NewOfAny[T any](elements ...T) Queue[T] {
return Queue[T]{
container: doublylinkedlist.NewOfAny(elements...),
}
}
/*
Creates a new instance of Queue from the given collection with a default equality comparer
and returns pointer to the new instance.
Elements of the given collection must be comparable
*/
func NewFromCollection[K comparable](c generic.Collectioner[K]) Queue[K] {
return Queue[K]{
container: doublylinkedlist.NewFromCollection(c),
}
}
/*
Creates a new instance of Queue from the given collection with nil equality comparer
and returns pointer to the instance.
Elements of the given collection can be of any type
*/
func NewOfAnyFromCollection[T any](c generic.Collectioner[T]) Queue[T] {
return Queue[T]{
container: doublylinkedlist.NewOfAnyFromCollection(c),
}
}
/*
Sets the equality comparer with the given equals function. Implements Queuer.SetEqualityComparer
*/
func (q *Queue[T]) SetEqualityComparer(equals func(a *T, b *T) bool) {
q.container.SetEqualityComparer(equals)
}
/*
Returns the length of the Queue. Implements Queuer.Size and Collectioner.Size
*/
func (q *Queue[T]) Size() int {
return q.container.Size()
}
/*
Returns true if the Queue is empty. Implements Queuer.Empty and Collectioner.Empty
*/
func (q *Queue[T]) Empty() bool {
return q.container.Empty()
}
/*
Pushes the given value to the back of the queue. Implements Queuer.Enqueue
*/
func (q *Queue[T]) Enqueue(element T) {
q.container.Add(element)
}
/*
Removes the element at the front of the queue. Panics if Queue is empty.
Implements Queuer.Dequeue
*/
func (q *Queue[T]) Dequeue() {
if q.container.Empty() {
panic("Queue.Dequeue failed because queue is empty")
}
q.container.RemoveFront()
}
/*
Returns a reference to the element at the front of the queue without removing it. Panics if Queue is empty.
Implements Queuer.Peek
*/
func (q *Queue[T]) Peek() *T {
if q.container.Empty() {
panic("Queue.Peek failed because queue is empty")
}
return q.container.Front()
}
/*
Adds the given element to the back of the queue. Always returns true.
Queue.Add functions exactly the same as Queue.Enqueue except that it returns bool.
Implements Queuer.Add and Collectioner.Add
*/
func (q *Queue[T]) Add(element T) bool {
return q.container.Add(element)
}
/*
Removes the the given element and returns true if present in the Queue.
Returns false if the given element does not exist.
Implements Queuer.Remove and Collectioner.Remove
*/
func (q *Queue[T]) Remove(element T) bool {
return q.container.Remove(element)
}
/*
Returns true if the given element exists in the Queue. Returns false otherwise.
Implements Queuer.Contains and Collectioner.Contains
*/
func (q *Queue[T]) Contains(element T) bool {
return q.container.Contains(element)
}
/*
Empties the Queue.
Implements Queuer.Clear and Collectioner.Clear
*/
func (q *Queue[T]) Clear() {
q.container.Clear()
}
/*
Iterates through the Queue and executes the given "do" function on each element.
Implements Queuer.ForEach and Collectioner.ForEach
*/
func (q *Queue[T]) ForEach(do func(*T)) {
q.container.ForEach(do)
}