/
queue.go
58 lines (43 loc) · 802 Bytes
/
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
package queue
import "sync"
type Queue struct {
queue []interface{}
len int
lock *sync.Mutex
}
func New() *Queue {
queue := &Queue{}
queue.queue = make([]interface{}, 0)
queue.len = 0
queue.lock = new(sync.Mutex)
return queue
}
func (q *Queue) Len() int {
//q.lock.Lock()
//defer q.lock.Unlock()
return q.len
}
func (q *Queue) isEmpty() bool {
q.lock.Lock()
defer q.lock.Unlock()
return q.len == 0
}
func (q *Queue) Shift() (el interface{}) {
//q.lock.Lock()
//defer q.lock.Unlock()
el, q.queue = q.queue[0], q.queue[1:]
q.len--
return
}
func (q *Queue) Push(el interface{}) {
//q.lock.Lock()
//defer q.lock.Unlock()
q.queue = append(q.queue, el)
q.len++
return
}
func (q *Queue) Peek() interface{} {
q.lock.Lock()
defer q.lock.Unlock()
return q.queue[0]
}