-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.go
More file actions
47 lines (40 loc) · 687 Bytes
/
Copy pathqueue.go
File metadata and controls
47 lines (40 loc) · 687 Bytes
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
package queue
type Queue struct {
data []any
head int
len int
}
func New(cap int) *Queue {
return &Queue{data: make([]any, cap)}
}
func (q *Queue) Cap() int {
return cap(q.data)
}
func (q *Queue) Len() int {
return q.len
}
func (q *Queue) Peek() any {
if q.len <= 0 {
return nil
}
return q.data[q.head]
}
func (q *Queue) Pop() any {
if q.len <= 0 {
return nil
}
q.len--
q.head = (q.head + 1) % cap(q.data)
return q.data[q.head]
}
func (q *Queue) Push(d any) {
q.len++
if q.len >= len(q.data) {
tbuf := make([]any, q.len*2)
copy(tbuf, q.data[q.head:])
copy(tbuf, q.data[:q.head])
q.data = tbuf
q.head = 0
}
q.data[(q.head+q.len)%len(q.data)] = d
}