-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.go
56 lines (47 loc) · 989 Bytes
/
array.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
// Copyright © 2020 ichenq@outlook.com All rights reserved.
// Distributed under the terms and conditions of the BSD License.
// See accompanying files LICENSE.
package go_queue_benchmark
import (
"fmt"
"strings"
)
type ArrayQueue struct {
buf []interface{}
}
func NewArrayQueue(capacity int) Queue {
if capacity <= 0 {
capacity = MinQueueBufferCapacity
}
return &ArrayQueue{
buf: make([]interface{}, 0, capacity),
}
}
func (q ArrayQueue) String() string {
var sb = &strings.Builder{}
for i := 0; i < len(q.buf); i++ {
sb.WriteString(fmt.Sprintf("%v ", q.buf[i]))
}
return sb.String()
}
func (q *ArrayQueue) Len() int {
return len(q.buf)
}
func (q *ArrayQueue) Front() interface{} {
if len(q.buf) > 0 {
return q.buf[0]
}
return nil
}
func (q *ArrayQueue) Enqueue(v interface{}) bool {
q.buf = append(q.buf, v)
return true
}
func (q *ArrayQueue) Dequeue() interface{} {
if len(q.buf) > 0 {
v := q.buf[0]
q.buf = q.buf[1:]
return v
}
return nil
}