-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.go
181 lines (144 loc) · 4.09 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package collections
import (
"fmt"
"sync"
)
// Queue implements a FIFO data structure. It is not thread-safe.
type Queue[T any] struct {
items []T
}
// NewQueue returns a new queue with the given initial items.
func NewQueue[T any](values ...T) *Queue[T] {
return &Queue[T]{items: values}
}
// Enqueue adds an item to the end of the queue.
func (q *Queue[T]) Enqueue(item T) {
q.items = append(q.items, item)
}
// Dequeue removes and returns the item at the front of the queue. If the queue
// is empty, an error is returned.
func (q *Queue[T]) Dequeue() (T, error) {
var zero T
if len(q.items) == 0 {
return zero, ErrEmptyQueue
}
item := q.items[0]
q.items = q.items[1:]
return item, nil
}
// Peek returns the item at the front of the queue without removing it. If the
// queue is empty, an error is returned.
func (q *Queue[T]) Peek() (T, error) {
var zero T
if len(q.items) == 0 {
return zero, ErrEmptyQueue
}
return q.items[0], nil
}
// IsEmpty returns true if the queue is empty.
func (q *Queue[T]) IsEmpty() bool {
return len(q.items) == 0
}
// Size returns the number of items in the queue.
func (q *Queue[T]) Size() int {
return len(q.items)
}
// String returns a string representation of the queue.
func (q *Queue[T]) String() string {
return fmt.Sprintf("%v", q.items)
}
// Clear removes all items from the queue.
func (q *Queue[T]) Clear() {
q.items = []T{}
}
// CopyTo copies the items in the queue to the given slice, starting at the
// given index. If the index is out of range, an error is returned. If the
// slice is not large enough to hold all the items, an error is returned.
func (q *Queue[T]) CopyTo(items []T, index int) error {
if index < 0 || index > len(items) {
return ErrIndexOutOfRange
}
if len(items)-index < len(q.items) {
return ErrIndexOutOfRange
}
copy(items[index:], q.items)
return nil
}
// ConcurrentQueue implements a FIFO data structure. It is thread-safe.
type ConcurrentQueue[T any] struct {
items []T
mutex sync.RWMutex
}
// NewConcurrentQueue returns a new queue with the given initial items.
func NewConcurrentQueue[T any](values ...T) *ConcurrentQueue[T] {
return &ConcurrentQueue[T]{items: values}
}
// Enqueue adds an item to the end of the queue.
func (q *ConcurrentQueue[T]) Enqueue(item T) {
q.mutex.Lock()
defer q.mutex.Unlock()
q.items = append(q.items, item)
}
// Dequeue removes and returns the item at the front of the queue. If the queue
// is empty, an error is returned.
func (q *ConcurrentQueue[T]) Dequeue() (T, error) {
q.mutex.Lock()
defer q.mutex.Unlock()
var zero T
if len(q.items) == 0 {
return zero, ErrEmptyQueue
}
item := q.items[0]
q.items = q.items[1:]
return item, nil
}
// Peek returns the item at the front of the queue without removing it. If the
// queue is empty, an error is returned.
func (q *ConcurrentQueue[T]) Peek() (T, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()
var zero T
if len(q.items) == 0 {
return zero, ErrEmptyQueue
}
return q.items[0], nil
}
// IsEmpty returns true if the queue is empty.
func (q *ConcurrentQueue[T]) IsEmpty() bool {
q.mutex.RLock()
defer q.mutex.RUnlock()
return len(q.items) == 0
}
// Size returns the number of items in the queue.
func (q *ConcurrentQueue[T]) Size() int {
q.mutex.RLock()
defer q.mutex.RUnlock()
return len(q.items)
}
// String returns a string representation of the queue.
func (q *ConcurrentQueue[T]) String() string {
q.mutex.RLock()
defer q.mutex.RUnlock()
return fmt.Sprintf("%v", q.items)
}
// Clear removes all items from the queue.
func (q *ConcurrentQueue[T]) Clear() {
q.mutex.Lock()
defer q.mutex.Unlock()
q.items = []T{}
}
// CopyTo copies the items in the queue to the given slice, starting at the
// given index. If the index is out of range, an error is returned. If the
// slice is not large enough to hold all the items, an error is returned.
func (q *ConcurrentQueue[T]) CopyTo(items []T, index int) error {
q.mutex.RLock()
defer q.mutex.RUnlock()
if index < 0 || index > len(items) {
return ErrIndexOutOfRange
}
if len(items)-index < len(q.items) {
return ErrIndexOutOfRange
}
copy(items[index:], q.items)
return nil
}