-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqueue.go
More file actions
40 lines (33 loc) · 759 Bytes
/
queue.go
File metadata and controls
40 lines (33 loc) · 759 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
package queue
// Simpler is the represent of your items, You just make you type with 2 methods
// Len() return the number of your items and Pop return each item by index
type Simpler interface {
Pop(i int) interface{}
Len() int
}
func NewQueue(s Simpler) *Queue {
q := &Queue{
pop: make(chan interface{}),
emptyNotify: make(chan struct{}),
s: s,
}
go q.background()
return q
}
type Queue struct {
pop chan interface{}
emptyNotify chan struct{}
s Simpler
}
func (q *Queue) background() {
defer close(q.emptyNotify)
for i := 0; i < q.s.Len(); i++ {
q.pop <- q.s.Pop(i)
}
}
func (q *Queue) Pop() <-chan interface{} {
return q.pop
}
func (q *Queue) Empty() <-chan struct{} {
return q.emptyNotify
}