forked from Altiscale/burstingsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpqueue.rb
367 lines (321 loc) · 7.11 KB
/
pqueue.rb
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# Extended to allow import of ordered entries without the need to reheap
# and not duplicating queue on push
# Initial implementation found here: https://github.com/rubyworks/pqueue/blob/master/lib/pqueue.rb
# Priority queue with array based heap.
#
# A priority queue is like a standard queue, except that each inserted
# elements is given a certain priority, based on the result of the
# comparison block given at instantiation time. Also, retrieving an element
# from the queue will always return the one with the highest priority
# (see #pop and #top).
#
# The default is to compare the elements in respect to their #<=> method.
# For example, Numeric elements with higher values will have higher
# priorities.
#
# Note that as of version 2.0 the internal queue is kept in the reverse order
# from how it was kept in previous version. If you had used #to_a in the
# past then be sure to adjust for the priorities to be ordered back-to-front
# instead of the other way around.
#
class PQueue
#
VERSION = "2.1.0" #:erb: VERSION = "<%= version %>"
#
# Returns a new priority queue.
#
# If elements are given, build the priority queue with these initial
# values. The elements object must respond to #to_a.
#
# If a block is given, it will be used to determine the priority between
# the elements. The block must must take two arguments and return `1`, `0`,
# or `-1` or `true`, `nil` or `false. It should return `0` or `nil` if the
# two arguments are considered equal, return `1` or `true` if the first
# argument is considered greater than the later, and `-1` or `false` if
# the later is considred to be greater than the first.
#
# By default, the priority queue retrieves maximum elements first
# using the #<=> method.
#
def initialize(elements=nil, &block) # :yields: a, b
@que = []
@cmp = block || lambda{ |a,b| a <=> b }
replace(elements) if elements
end
protected
#
# The underlying heap.
#
attr_reader :que #:nodoc:
public
#
# Priority comparison procedure.
#
attr_reader :cmp
#
# Returns the size of the queue.
#
def size
@que.size
end
#
# Alias of size.
#
alias length size
#
# Add an element in the priority queue.
#
def push(v)
@que << v
reheap(@que.size-1)
self
end
def push_without_reheap(v)
@que << v
#reheap(@que.size-1)
self
end
#
# Traditional alias for #push.
#
alias enq push
#
# Alias of #push.
#
alias :<< :push
#
# Get the element with the highest priority and remove it from
# the queue.
#
# The highest priority is determined by the block given at instantiation
# time.
#
# The deletion time is O(log n), with n is the size of the queue.
#
# Return nil if the queue is empty.
#
def pop
return nil if empty?
@que.pop
end
#
# Traditional alias for #pop.
#
alias deq pop
# Get the element with the lowest priority and remove it from
# the queue.
#
# The lowest priority is determined by the block given at instantiation
# time.
#
# The deletion time is O(log n), with n is the size of the queue.
#
# Return nil if the queue is empty.
#
def shift
return nil if empty?
@que.shift
end
#
# Returns the element with the highest priority, but
# does not remove it from the queue.
#
def top
return nil if empty?
return @que.last
end
#
# Traditional alias for #top.
#
alias peek top
#
# Returns the element with the lowest priority, but
# does not remove it from the queue.
#
def bottom
return nil if empty?
return @que.first
end
def init_with_sorted_array(elements)
if empty?
if elements.kind_of?(Array)
initialize_copy_from_sorted_array(elements)
else
raise ArguementError, 'Only supporting sorted arrays'
end
else
raise ArguementError, 'Only supporting sorted arrays when empty'
end
return self
end
#
# Add more than one element at the same time. See #push.
#
# The elements object must respond to #to_a, or be a PQueue itself.
#
def concat(elements)
if empty?
if elements.kind_of?(PQueue)
initialize_copy(elements)
else
replace(elements)
end
else
if elements.kind_of?(PQueue)
@que.concat(elements.que)
sort!
else
@que.concat(elements.to_a)
sort!
end
end
return self
end
#
# Alias for #concat.
#
alias :merge! :concat
#
# Return top n-element as a sorted array.
#
def take(n=@size)
a = []
n.times{a.push(pop)}
a
end
#
# Returns true if there is no more elements left in the queue.
#
def empty?
@que.empty?
end
#
# Remove all elements from the priority queue.
#
def clear
@que.clear
self
end
#
# Replace the content of the heap by the new elements.
#
# The elements object must respond to #to_a, or to be
# a PQueue itself.
#
def replace(elements)
if elements.kind_of?(PQueue)
initialize_copy(elements)
else
@que.replace(elements.to_a)
sort!
end
self
end
#
# Return a sorted array, with highest priority first.
#
def to_a
@que.dup
end
#
# Return true if the given object is present in the queue.
#
def include?(element)
@que.include?(element)
end
#
# Push element onto queue while popping off and returning the next element.
# This is qquivalent to successively calling #pop and #push(v).
#
def swap(v)
r = pop
push(v)
r
end
#
# Iterate over the ordered elements, destructively.
#
def each_pop #:yields: popped
until empty?
yield pop
end
nil
end
#
# Pretty inspection string.
#
def inspect
"<#{self.class}: size=#{size}, top=#{top || "nil"}>"
end
#
# Return true if the queues contain equal elements.
#
def ==(other)
size == other.size && to_a == other.to_a
end
private
#
#
#
def initialize_copy(other)
@cmp = other.cmp
@que = other.que.dup
sort!
end
def initialize_copy_from_sorted_array(other)
@que = other
end
#
# The element at index k will be repositioned to its proper place.
#
# This, of course, assumes the queue is already sorted.
#
def reheap(k)
return self if size <= 1
#que = @que.dup
v = @que.delete_at(k)
i = binary_index(que, v)
@que.insert(i, v)
#@que = que
return self
end
#
# Sort the queue in accordance to the given comparison procedure.
#
def sort!
@que.sort! do |a,b|
case @cmp.call(a,b)
when 0, nil then 0
when 1, true then 1
when -1, false then -1
else
warn "bad comparison procedure in #{self.inspect}"
0
end
end
self
end
#
# Alias of #sort!
#
alias heapify sort!
#
def binary_index(que, target)
upper = que.size - 1
lower = 0
while(upper >= lower) do
idx = lower + (upper - lower) / 2
comp = @cmp.call(target, que[idx])
case comp
when 0, nil
return idx
when 1, true
lower = idx + 1
when -1, false
upper = idx - 1
else
end
end
lower
end
end # class PQueue