forked from nytlabs/st-core
/
source_priority_queue.go
153 lines (140 loc) · 2.72 KB
/
source_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package core
import (
"sync"
"github.com/oleiade/lane"
)
type queue []*PQMessage
type PriorityQueue struct {
queue *lane.PQueue
sync.Mutex
}
type PQMessage struct {
val interface{}
t int
index int
}
func PriorityQueueStore() SourceSpec {
return SourceSpec{
Name: "priority-queue",
Type: PRIORITY,
New: NewPriorityQueue,
}
}
func NewPriorityQueue() Source {
var pq *lane.PQueue = lane.NewPQueue(lane.MINPQ)
return &PriorityQueue{
queue: pq,
}
}
func (pq PriorityQueue) GetType() SourceType {
return PRIORITY
}
func pqPush() Spec {
return Spec{
Name: "pqPush",
Inputs: []Pin{
Pin{"in", ANY},
Pin{"priority", NUMBER},
},
Outputs: []Pin{
Pin{"out", BOOLEAN},
},
Source: PRIORITY,
Kernel: func(in, out, internal MessageMap, s Source, i chan Interrupt) Interrupt {
pq := s.(*PriorityQueue)
priority, ok := in[1].(float64)
if !ok {
out[0] = NewError("pqPush needs a Number for a priority")
return nil
}
pq.queue.Push(in[0], int(priority))
out[0] = true
return nil
},
}
}
func pqPop() Spec {
return Spec{
Name: "pqPop",
Inputs: []Pin{
Pin{"trigger", ANY},
},
Outputs: []Pin{
Pin{"out", ANY},
Pin{"priority", NUMBER},
},
Source: PRIORITY,
Kernel: func(in, out, internal MessageMap, s Source, i chan Interrupt) Interrupt {
pq := s.(*PriorityQueue)
if pq.queue.Size() == 0 {
out[0] = NewError("empty PriorityQueue")
return nil
}
msg, priority := pq.queue.Pop()
out[0] = msg
out[1] = float64(priority)
return nil
},
}
}
func pqPeek() Spec {
return Spec{
Name: "pqPeek",
Inputs: []Pin{
Pin{"trigger", ANY},
},
Outputs: []Pin{
Pin{"out", ANY},
Pin{"priority", NUMBER},
},
Source: PRIORITY,
Kernel: func(in, out, internal MessageMap, s Source, i chan Interrupt) Interrupt {
pq := s.(*PriorityQueue)
if pq.queue.Size() == 0 {
out[0] = NewError("empty PriorityQueue")
return nil
}
msg, priority := pq.queue.Head()
out[0] = msg
out[1] = float64(priority)
return nil
},
}
}
func pqLen() Spec {
return Spec{
Name: "pqLen",
Inputs: []Pin{
Pin{"trigger", ANY},
},
Outputs: []Pin{
Pin{"length", NUMBER},
},
Source: PRIORITY,
Kernel: func(in, out, internal MessageMap, s Source, i chan Interrupt) Interrupt {
pq := s.(*PriorityQueue)
out[0] = float64(pq.queue.Size())
return nil
},
}
}
func pqClear() Spec {
return Spec{
Name: "pqClear",
Inputs: []Pin{
Pin{"clear", ANY},
},
Outputs: []Pin{
Pin{"cleared", BOOLEAN},
},
Source: PRIORITY,
Kernel: func(in, out, internal MessageMap, s Source, i chan Interrupt) Interrupt {
pq := s.(*PriorityQueue)
for pq.queue.Size() != 0 {
_, _ = pq.queue.Pop()
}
out[0] = true
return nil
},
}
}