-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueue.go
185 lines (150 loc) · 2.9 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
182
183
184
185
package jdeque
import "sync"
// Queue implements a deque and values can be pushed or popped from front
// and back.
type Queue[T any] struct {
head, tail *chunk[T]
chunkSize int
pool sync.Pool
}
// New creates a new Queue.
func New[T any](chunkSize int) *Queue[T] {
return &Queue[T]{
chunkSize: chunkSize,
pool: sync.Pool{
New: func() interface{} {
return newChunk[T](chunkSize)
},
},
}
}
func (q *Queue[T]) initialize() {
if q.head != nil {
return
}
q.head = q.pool.Get().(*chunk[T])
q.tail = q.head
}
// PushFront adds a new value to the right of the queue.
func (q *Queue[T]) PushFront(value T) {
q.initialize()
ok := q.head.PushFront(value)
if ok {
return
}
c := q.pool.Get().(*chunk[T])
ok = c.PushFront(value)
if !ok {
panic("could not PushFront to a new chunk")
}
c.left = q.head
q.head.right = c
q.head = c
}
// PopFront retrieves the rightmost value. Returns false if the queue is empty.
func (q *Queue[T]) PopFront() (T, bool) {
if q.head == nil {
var v T
return v, false
}
v, ok := q.head.PopFront()
if ok {
return v, ok
}
if q.head.left != nil {
q.head.left.right = nil
}
previous := q.head
q.head = q.head.left
previous.start = 0
previous.size = 0
previous.left = nil
previous.right = nil
q.pool.Put(previous)
if q.head != nil {
return q.head.PopFront()
}
return v, ok
}
// PushBack adds a new value to the left of the queue.
func (q *Queue[T]) PushBack(value T) {
q.initialize()
ok := q.tail.PushBack(value)
if ok {
return
}
c := q.pool.Get().(*chunk[T])
ok = c.PushBack(value)
if !ok {
panic("could not PushBack to a new chunk")
}
c.right = q.tail
q.tail.left = c
q.tail = c
}
// PopBack retrieves the leftmost value. Returns false if the queue is empty.
func (q *Queue[T]) PopBack() (T, bool) {
if q.tail == nil {
var v T
return v, false
}
v, ok := q.tail.PopBack()
if ok {
return v, ok
}
if q.tail.right != nil {
q.tail.right.left = nil
}
previous := q.tail
q.tail = q.tail.right
previous.start = 0
previous.size = 0
previous.left = nil
previous.right = nil
q.pool.Put(previous)
if q.tail != nil {
return q.tail.PopBack()
}
return v, ok
}
// Len returns the size of the queue.
func (q *Queue[T]) Len() int {
if q.tail == nil {
return 0
}
var size int
start := q.tail
for {
size += start.size
if start.right == nil {
break
}
start = start.right
}
return size
}
// PeekFront retrieves the rightmost value without consuming it. Returns false if the queue is empty.
func (q *Queue[T]) PeekFront() (T, bool) {
if q.head == nil {
var v T
return v, false
}
v, ok := q.head.PeekFront()
if ok {
return v, ok
}
if q.head.left != nil {
q.head.left.right = nil
}
previous := q.head
q.head = q.head.left
previous.start = 0
previous.size = 0
previous.left = nil
previous.right = nil
q.pool.Put(previous)
if q.head != nil {
return q.head.PeekFront()
}
return v, ok
}