-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.go
98 lines (85 loc) · 2.02 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
package datarithms
import (
"fmt"
"sync"
)
// Thread Safe circular queue implmentation using a slice for byte slices
type CircularQueue[T any] struct {
lock sync.RWMutex
queue []T
capacity int
start int
end int
length int
}
// Creates a new circular queue of given capacity
func CircularQueueInit[T any](capacity int) *CircularQueue[T] {
q := new(CircularQueue[T])
q.queue = make([]T, capacity)
q.capacity = capacity
q.start = 0
q.end = 0
q.length = 0
q.lock = sync.RWMutex{}
return q
}
// Adds a new element to the queue
func (q *CircularQueue[T]) Push(element T) {
q.lock.Lock()
q.queue[q.end] = element
q.end = (q.end + 1) % q.capacity
// If the queue is full, start overwriting from the beginning
if q.length == q.capacity {
q.start = (q.start + 1) % q.capacity
} else {
q.length++
}
q.lock.Unlock()
}
// Pops the element at the front of the queue
// If the queue is empty, returns the zero value followed by an error
func (q *CircularQueue[T]) Pop() (element T, err error) {
q.lock.Lock()
// If the queue is empty, return nil
if q.length == 0 {
q.lock.Unlock()
return element, fmt.Errorf("CircularQueue is empty")
}
element = q.queue[q.start]
q.start = (q.start + 1) % q.capacity
q.length--
q.lock.Unlock()
return element, nil
}
// Returns the element at the front of the queue
func (q *CircularQueue[T]) Front() T {
q.lock.RLock()
result := q.queue[q.start]
q.lock.RUnlock()
return result
}
// Returns the number of elements in the queue
func (q *CircularQueue[T]) Len() int {
q.lock.RLock()
result := q.length
q.lock.RUnlock()
return result
}
// Returns the capacity of the queue
func (q *CircularQueue[T]) Capacity() int {
q.lock.RLock()
result := q.capacity
q.lock.RUnlock()
return result
}
// Returns all the elements of the queue
func (q *CircularQueue[T]) All() []T {
q.lock.RLock()
result := make([]T, 0, q.length)
// From start to end
for i := q.start; i != q.end; i = (i + 1) % q.capacity {
result = append(result, q.queue[i])
}
q.lock.RUnlock()
return result
}