-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.go
More file actions
50 lines (39 loc) · 751 Bytes
/
queue.go
File metadata and controls
50 lines (39 loc) · 751 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
48
49
50
package jobqueue
import "container/list"
type stack struct {
cap int
list *list.List
}
func newStack(cap int) *stack {
return &stack{
cap: cap,
list: list.New(),
}
}
func (s *stack) empty() bool {
return s.list.Len() == 0
}
func (s *stack) full() bool {
return s.cap > 0 && s.list.Len() == s.cap
}
func (s *stack) bottom() *job {
if s.list.Len() == 0 {
return nil
}
return s.list.Back().Value.(*job)
}
func (s *stack) push(j *job) {
j.entry = s.list.PushFront(j)
}
func (s *stack) removeEntry(e *list.Element) *job {
s.list.Remove(e)
j := e.Value.(*job)
j.entry = nil
return j
}
func (s *stack) pop() *job {
return s.removeEntry(s.list.Front())
}
func (s *stack) shift() *job {
return s.removeEntry(s.list.Back())
}