-
Notifications
You must be signed in to change notification settings - Fork 1
/
Heap.mon
218 lines (204 loc) · 5.68 KB
/
Heap.mon
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package com.apamax.containers;
/** A comparator for a heap containing integers.
*/
event HeapIntegerComparator
{
/** True for a min-heap, false for a max-heap. */
boolean increasing;
/** Compare two integers.
* @returns Positive if increasing and a > b, or not increasing and a < b, 0 if equal and negative otherwise.
* @param a The first integer to compare.
* @param b The second integer to compare.
*/
action compare(any a, any b) returns integer
{
if (increasing) {
return <integer>a-<integer>b;
} else {
return <integer>b-<integer>a;
}
}
}
/** A comparator for a heap containing strings.
*/
event HeapStringComparator
{
/** True for a min-heap, false for a max-heap. */
boolean increasing;
/** Compare two strings.
* @returns Positive if increasing and a > b, or not increasing and a < b, 0 if equal and negative otherwise.
* @param a The first string to compare.
* @param b The second string to compare.
*/
action compare(any a, any b) returns integer
{
integer positive := 1;
if not increasing {
positive := -1;
}
if <string>a < <string>b {
return -positive;
} else if <string>a = <string>b {
return 0;
} else {
return positive;
}
}
}
/** A comparator for a heap containing events, given a field name which will be an integer to order the heap.
*/
event HeapIntegerFieldComparator
{
/** The field of the event to use for the comparison. Must be an integer. */
string fieldName;
/** True for a min-heap, false for a max-heap. */
boolean increasing;
/** Compare two events.
* @returns Positive if increasing and a > b, or not increasing and a < b, 0 if equal and negative otherwise.
* @param a The first event to compare.
* @param b The second event to compare.
*/
action compare(any a, any b) returns integer
{
if (increasing) {
return <integer>a.getField(fieldName)-<integer>b.getField(fieldName);
} else {
return <integer>b.getField(fieldName)-<integer>a.getField(fieldName);
}
}
}
/**
* A Max/Min-value Heap object with configurable type comparators.
*
* Create with a comparator object determining the Heap order and comparison function.
*
* Several built-in comparators are available.
*
* @see HeapIntegerFieldComparator
* @see HeapIntegerComparator
* @see HeapStringComparator
*/
event Heap
{
/** Create an empty heap with the given comparator.
* The comparator determines if this is a min-heap or a max-heap and how to compare the values.
* @param comparator A Comparator object with a <tt>compare</tt> action with the signature <tt>action<any, any> returns integer</tt>.
*/
static action create(any comparator) returns Heap
{
Heap h := new Heap;
h.data := new sequence<any>;
h.comparator := <action<any, any> returns integer> comparator.getAction("compare");
return h;
}
/** Create an in-place heap on the given sequence, with the given comparator.
* The comparator determines if this is a min-heap or a max-heap and how to compare the values.
* @param data A sequence which will be heapified in-place and further heap operations will be made on the array.
* @param comparator A Comparator object with a <tt>compare</tt> action with the signature <tt>action<any, any> returns integer</tt>.
*/
static action heapify(sequence<any> data, any comparator) returns Heap
{
Heap h := new Heap;
h.data := data;
h.comparator := <action<any, any> returns integer> comparator.getAction("compare");
h.makeHeap();
return h;
}
/** Return the top value from the heap, according to its comparator. */
action peekTop() returns any
{
return data[0];
}
/** Remove and return the top value from the heap, according to its comparator, maintaining the heap. */
action pop() returns any
{
any t := data[0];
data[0] := data[data.size()-1];
data.remove(data.size()-1);
heapifyDown(0);
return t;
}
/** Push a new value into the heap, maintaining the heap.
* @param val The value to add to the heap.
*/
action push(any val)
{
data.append(val);
heapifyUp(data.size()-1);
}
/** Returns true if the heap is empty. */
action empty() returns boolean
{
return data.size() = 0;
}
/** Returns the number of values in the heap. */
action size() returns integer
{
return data.size();
}
/** @private */
action swap(integer a, integer b)
{
any tmp := data[a];
data[a] := data[b];
data[b] := tmp;
}
/** @private */
action parentOf(integer index) returns integer
{
return (index-1)/2;
}
/** @private */
action leftChildOf(integer index) returns integer
{
integer child := (index*2)+1;
if child >= data.size() { return -1; }
else { return child; }
}
/** @private */
action rightChildOf(integer index) returns integer
{
integer child := (index+1)*2;
if child >= data.size() { return -1; }
else { return child; }
}
/** @private */
action heapifyDown(integer index)
{
integer leftChild := leftChildOf(index);
integer rightChild := rightChildOf(index);
integer largestNode := index;
if leftChild > 0 and comparator(data[leftChild], data[largestNode]) < 0 {
largestNode := leftChild;
}
if rightChild > 0 and comparator(data[rightChild], data[largestNode]) < 0 {
largestNode := rightChild;
}
if largestNode != index {
swap(index, largestNode);
heapifyDown(largestNode);
}
}
/** @private */
action heapifyUp(integer index)
{
integer parent := parentOf(index);
if index > 0 and comparator(data[index], data[parent]) < 0 {
swap(index, parent);
heapifyUp(parent);
}
}
/** @private */
action makeHeap()
{
integer lastNonLeaf := (data.size()/2)-1;
while lastNonLeaf >= 0 {
heapifyDown(lastNonLeaf);
lastNonLeaf := lastNonLeaf - 1;
}
}
/** @private */
sequence<any> data;
/** @private */
action<any, any> returns integer comparator;
}