-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
priority_queue.go
108 lines (94 loc) · 2.19 KB
/
priority_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package priority_queue
import "errors"
//go:generate stringer -type=Priority
// Priority of signing request
type Priority int
const (
// UnknownPriority represents an Unknown Priority signing request
UnknownPriority Priority = iota
// LowPriority represents an Low Priority signing request
LowPriority
// MediumPriority represents an Medium Priority signing request
MediumPriority
// HighPriority represents an High Priority signing request
HighPriority
)
// Item is used to push to and pop value with priority
type Item struct {
// Value represents any value
Value interface{}
// Priority represents priority of the signing reqeust
Priority Priority
}
// PriorityQueue represents priority channels
type PriorityQueue struct {
high chan Item
medium chan Item
low chan Item
}
func New(size int) *PriorityQueue {
q := PriorityQueue{
high: make(chan Item, size),
medium: make(chan Item, size),
low: make(chan Item, size),
}
return &q
}
// Push adds an item to the priority queue
func (q *PriorityQueue) Push(i Item) {
switch i.Priority {
case HighPriority:
q.high <- i
case MediumPriority:
q.medium <- i
case LowPriority:
q.low <- i
}
}
// Pop returns appropriate item from the priority queue
func (q *PriorityQueue) Pop() Item {
for {
select {
case Item := <-q.high:
return Item
default:
select {
case Item := <-q.medium:
return Item
case Item := <-q.low:
return Item
default:
select {
case Item := <-q.high:
return Item
case Item := <-q.medium:
return Item
case Item := <-q.low:
return Item
}
}
}
}
}
// LenAll represents lengths of priority channels
type LenAll struct {
Low int `json:"low"`
Medium int `json:"medium"`
High int `json:"high"`
}
// Len returns length of the channel by priority
func (q *PriorityQueue) Len(p Priority) (int, error) {
switch p {
case LowPriority:
return len(q.low), nil
case MediumPriority:
return len(q.medium), nil
case HighPriority:
return len(q.high), nil
}
return -1, errors.New("wrong priority name")
}
// LenAll returns lengths of all priority channels
func (q *PriorityQueue) LenAll() LenAll {
return LenAll{len(q.low), len(q.medium), len(q.high)}
}