Skip to content

Commit ca71497

Browse files
committed
Fix reheap method. [bug]
1 parent 98d46c9 commit ca71497

File tree

5 files changed

+66
-28
lines changed

5 files changed

+66
-28
lines changed

.ruby

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ revision: 0
4444
created: '2001-03-10'
4545
summary: Queue of Prioritized Elements
4646
title: PQueue
47-
version: 2.0.1
47+
version: 2.0.2
4848
name: pqueue
4949
description: ! 'A priority queue is like a standard queue, except that each inserted
5050
elements

HISTORY.rdoc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
= CHANGE HISTORY
22

3+
== 2.0.2 / 2011-10-29
4+
5+
It's been one of those days. I went to to get a wash cloth for the shower,
6+
on my way through the kitchen realized the chilling cookie dough needed to
7+
be put in the oven, bit I forgot to put flower in the batter, so they burnt
8+
in minute and made a mess that took almost an hour to clean-up. Then I discover
9+
the shower still running, hot water was all but gone, and my bedroom felt like
10+
a suana. And to top it all off, it was at this point I realized my binary
11+
search algrithm didn't work. Sigh. And fuck if my grandmother won't stop making
12+
me food I don't want to eat. Yea, one of those days.
13+
14+
Changes:
15+
16+
* Fixed #reheap method.
17+
18+
319
== 2.0.1 / 2011-10-29
420

521
Quick fix to remove old legacy library that was supposed to be

lib/pqueue.rb

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -261,31 +261,9 @@ def reheap(k)
261261
que = @que.dup
262262

263263
v = que.delete_at(k)
264+
i = binary_index(que, v)
264265

265-
i = que.size.div(2)
266-
q = i
267-
r = nil
268-
269-
loop do
270-
case @cmp.call(v, que[i])
271-
when 0, nil
272-
r = i
273-
break
274-
when 1, true
275-
i = (que.size + i).div(2)
276-
i += 1 if i == q # don't repeat yourself
277-
when -1, false
278-
i = (i).div(2)
279-
i -= 1 if i == q # don't repeat yourself
280-
else
281-
warn "bad comparison procedure in #{self.inspect}"
282-
r = i
283-
break
284-
end
285-
q = i
286-
end
287-
288-
que.insert(r, v)
266+
que.insert(i, v)
289267

290268
@que = que
291269

@@ -314,4 +292,26 @@ def sort!
314292
#
315293
alias heapify sort!
316294

295+
#
296+
def binary_index(que, target)
297+
upper = que.size - 1
298+
lower = 0
299+
300+
while(upper >= lower) do
301+
idx = lower + (upper - lower) / 2
302+
comp = @cmp.call(target, que[idx])
303+
304+
case comp
305+
when 0, nil
306+
return idx
307+
when 1, true
308+
lower = idx + 1
309+
when -1, false
310+
upper = idx - 1
311+
else
312+
end
313+
end
314+
lower
315+
end
316+
317317
end # class PQueue

meta/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.0.1
1+
2.0.2

test/test_pqueue.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
require 'microtest'
2+
require 'ae'
23
require 'ae/legacy'
34

45
require 'pqueue'
56

6-
class TC_PQueue < MicroTest::TestCase
7+
class PQueueTest < MicroTest::TestCase
78
include AE::Legacy::Assertions
89

910
ARY_TEST = [2,6,1,3,8,15,0,-4,7,8,10]
@@ -152,5 +153,26 @@ def test_array_copied
152153
q.pop
153154
assert_not_equal(q, r)
154155
end
155-
end
156156

157+
def test_reheap
158+
q = PQueue.new([2,4,5])
159+
q << 6
160+
q.to_a.assert == [2,4,5,6]
161+
q << 1
162+
q.to_a.assert == [1,2,4,5,6]
163+
q << 3
164+
q.to_a.assert == [1,2,3,4,5,6]
165+
166+
q = PQueue.new([100,5,25])
167+
q.to_a.assert == [5,25,100]
168+
q << 17
169+
q.to_a.assert == [5,17,25,100]
170+
q << 0
171+
q.to_a.assert == [0,5,17,25,100]
172+
q << -5
173+
q.to_a.assert == [-5,0,5,17,25,100]
174+
q << 100
175+
q.to_a.assert == [-5,0,5,17,25,100,100]
176+
end
177+
178+
end

0 commit comments

Comments
 (0)