pqueue 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/.ruby +1 -1
  2. data/HISTORY.rdoc +16 -0
  3. data/lib/pqueue.rb +24 -24
  4. data/test/test_pqueue.rb +24 -2
  5. metadata +7 -7
data/.ruby CHANGED
@@ -44,7 +44,7 @@ revision: 0
44
44
  created: '2001-03-10'
45
45
  summary: Queue of Prioritized Elements
46
46
  title: PQueue
47
- version: 2.0.1
47
+ version: 2.0.2
48
48
  name: pqueue
49
49
  description: ! 'A priority queue is like a standard queue, except that each inserted
50
50
  elements
@@ -1,5 +1,21 @@
1
1
  = CHANGE HISTORY
2
2
 
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, but I forgot to put flower in the batter, so they burnt
8
+ up in minutes and made a mess that took a half-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. To top it all off, it was at this moment I realized PQueue's 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 search algorithm.
17
+
18
+
3
19
  == 2.0.1 / 2011-10-29
4
20
 
5
21
  Quick fix to remove old legacy library that was supposed to be
@@ -261,31 +261,9 @@ class PQueue
261
261
  que = @que.dup
262
262
 
263
263
  v = que.delete_at(k)
264
+ i = binary_index(que, v)
264
265
 
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)
289
267
 
290
268
  @que = que
291
269
 
@@ -314,4 +292,26 @@ class PQueue
314
292
  #
315
293
  alias heapify sort!
316
294
 
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
+
317
317
  end # class PQueue
@@ -1,9 +1,10 @@
1
1
  require 'microtest'
2
+ require 'ae'
2
3
  require 'ae/legacy'
3
4
 
4
5
  require 'pqueue'
5
6
 
6
- class TC_PQueue < MicroTest::TestCase
7
+ class PQueueTest < MicroTest::TestCase
7
8
  include AE::Legacy::Assertions
8
9
 
9
10
  ARY_TEST = [2,6,1,3,8,15,0,-4,7,8,10]
@@ -152,5 +153,26 @@ class TC_PQueue < MicroTest::TestCase
152
153
  q.pop
153
154
  assert_not_equal(q, r)
154
155
  end
155
- end
156
156
 
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pqueue
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -17,7 +17,7 @@ date: 2011-10-29 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: detroit
20
- requirement: &22996340 !ruby/object:Gem::Requirement
20
+ requirement: &18261020 !ruby/object:Gem::Requirement
21
21
  none: false
22
22
  requirements:
23
23
  - - ! '>='
@@ -25,10 +25,10 @@ dependencies:
25
25
  version: '0'
26
26
  type: :development
27
27
  prerelease: false
28
- version_requirements: *22996340
28
+ version_requirements: *18261020
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: microtest
31
- requirement: &22995600 !ruby/object:Gem::Requirement
31
+ requirement: &18260240 !ruby/object:Gem::Requirement
32
32
  none: false
33
33
  requirements:
34
34
  - - ! '>='
@@ -36,10 +36,10 @@ dependencies:
36
36
  version: '0'
37
37
  type: :development
38
38
  prerelease: false
39
- version_requirements: *22995600
39
+ version_requirements: *18260240
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: ae
42
- requirement: &22994880 !ruby/object:Gem::Requirement
42
+ requirement: &18259560 !ruby/object:Gem::Requirement
43
43
  none: false
44
44
  requirements:
45
45
  - - ! '>='
@@ -47,7 +47,7 @@ dependencies:
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
- version_requirements: *22994880
50
+ version_requirements: *18259560
51
51
  description: ! 'A priority queue is like a standard queue, except that each inserted
52
52
  elements
53
53